本文整理汇总了Java中org.languagetool.JLanguageTool.getAnalyzedSentence方法的典型用法代码示例。如果您正苦于以下问题:Java JLanguageTool.getAnalyzedSentence方法的具体用法?Java JLanguageTool.getAnalyzedSentence怎么用?Java JLanguageTool.getAnalyzedSentence使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.languagetool.JLanguageTool
的用法示例。
在下文中一共展示了JLanguageTool.getAnalyzedSentence方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testTokenizationWithPosTag
import org.languagetool.JLanguageTool; //导入方法依赖的package包/类
@Test
public void testTokenizationWithPosTag() throws IOException {
JLanguageTool lt = new JLanguageTool(new PosTaggingDemo());
AnalyzedSentence analyzedSentence = lt.getAnalyzedSentence("This, isn't a test.");
List<GoogleToken> tokens = GoogleToken.getGoogleTokens(analyzedSentence, false, new MyWordTokenizer());
assertThat(tokens.get(0).token, is("This"));
assertThat(tokens.get(0).posTags.toString(), is("[This/DT]"));
assertThat(tokens.get(1).token, is(","));
assertThat(tokens.get(1).posTags.toString(), is("[,/null]"));
assertThat(tokens.get(2).token, is("isn"));
assertThat(tokens.get(3).token, is("'t"));
assertThat(tokens.get(4).token, is("a"));
assertThat(tokens.get(5).token, is("test"));
assertThat(tokens.get(5).posTags.toString(), is("[test/NN]"));
assertThat(tokens.get(6).token, is("."));
}
示例2: testChunker
import org.languagetool.JLanguageTool; //导入方法依赖的package包/类
@Test
public void testChunker() throws Exception {
JLanguageTool lt = new JLanguageTool(new Ukrainian());
AnalyzedSentence analyzedSentence = lt.getAnalyzedSentence("Для годиться.");
AnalyzedSentence disambiguated = chunker.disambiguate(analyzedSentence);
AnalyzedTokenReadings[] tokens = disambiguated.getTokens();
assertTrue(tokens[1].getReadings().toString().contains("<adv>"));
assertTrue(tokens[4].getReadings().toString().contains("<adv>"));
analyzedSentence = lt.getAnalyzedSentence("на його думку");
disambiguated = chunker.disambiguate(analyzedSentence);
tokens = disambiguated.getTokens();
assertTrue(tokens[1].getReadings().toString().contains("<insert>"));
assertTrue(tokens[3].getReadings().toString().contains("<insert>"));
assertTrue(tokens[5].getReadings().toString().contains("<insert>"));
}
示例3: findMatchingSentences
import org.languagetool.JLanguageTool; //导入方法依赖的package包/类
private List<MatchingSentence> findMatchingSentences(IndexSearcher indexSearcher, TopDocs topDocs, JLanguageTool languageTool) throws IOException {
List<MatchingSentence> matchingSentences = new ArrayList<>();
for (ScoreDoc match : topDocs.scoreDocs) {
Document doc = indexSearcher.doc(match.doc);
String sentence = doc.get(FIELD_NAME);
List<RuleMatch> ruleMatches = languageTool.check(sentence);
if (ruleMatches.size() > 0) {
String source = doc.get(SOURCE_FIELD_NAME);
String title = doc.get(Indexer.TITLE_FIELD_NAME);
AnalyzedSentence analyzedSentence = languageTool.getAnalyzedSentence(sentence);
MatchingSentence matchingSentence = new MatchingSentence(sentence, source, title, analyzedSentence, ruleMatches);
matchingSentences.add(matchingSentence);
}
}
return matchingSentences;
}
示例4: tagTextAndDisplayResults
import org.languagetool.JLanguageTool; //导入方法依赖的package包/类
private void tagTextAndDisplayResults(final JLanguageTool langTool) {
if (StringTools.isEmpty(textArea.getText().trim())) {
textArea.setText(messages.getString("enterText2"));
} else {
// tag text
final List<String> sentences = langTool.sentenceTokenize(textArea
.getText());
final StringBuilder sb = new StringBuilder();
try {
for (String sent : sentences) {
final AnalyzedSentence analyzedText = langTool
.getAnalyzedSentence(sent);
// final String analyzedTextString =
// StringTools.escapeHTML(analyzedText.toString(", ")).
// replace("[", "<font color='#888888'>[").replace("]",
// "]</font>");
final String analyzedTextString = StringTools
.escapeHTML(analyzedText.toString())
.replace("[", "<font color='#888888'>[")
.replace("]", "]</font>");
sb.append(analyzedTextString);
sb.append("\n");
}
} catch (IOException e) {
sb.append("An error occurred while tagging the text: "
+ e.getMessage());
}
resultArea.setText(HTML_FONT_START + sb.toString() + HTML_FONT_END);
}
}
示例5: testCorrectSentences
import org.languagetool.JLanguageTool; //导入方法依赖的package包/类
private void testCorrectSentences(JLanguageTool languageTool, JLanguageTool allRulesLanguageTool,
Language lang, AbstractPatternRule rule) throws IOException {
List<CorrectExample> goodSentences = rule.getCorrectExamples();
// necessary for XML Pattern rules containing <or>
List<AbstractPatternRule> rules = allRulesLanguageTool.getPatternRulesByIdAndSubId(rule.getId(), rule.getSubId());
for (CorrectExample goodSentenceObj : goodSentences) {
// enable indentation use
String goodSentence = goodSentenceObj.getExample().replaceAll("[\\n\\t]+", "");
goodSentence = cleanXML(goodSentence);
assertTrue(lang + ": Empty correct example in rule " + rule.getFullId(), goodSentence.trim().length() > 0);
boolean isMatched = false;
// necessary for XML Pattern rules containing <or>
for (Rule auxRule : rules) {
isMatched = isMatched || match(auxRule, goodSentence, languageTool);
}
if (isMatched) {
AnalyzedSentence analyzedSentence = languageTool.getAnalyzedSentence(goodSentence);
StringBuilder sb = new StringBuilder("Analyzed token readings:");
for (AnalyzedTokenReadings atr : analyzedSentence.getTokens()) {
sb.append(" ").append(atr);
}
fail(lang + ": Did not expect error in:\n" +
" " + goodSentence + "\n" +
" " + sb + "\n" +
"Matching Rule: " + rule.getFullId());
}
// avoid matches with all the *other* rules:
/*
List<RuleMatch> matches = allRulesLanguageTool.check(goodSentence);
for (RuleMatch match : matches) {
System.err.println("WARN: " + lang.getShortCode() + ": '" + goodSentence + "' did not match "
+ rule.getId() + " but matched " + match.getRule().getId());
}
*/
}
}
示例6: testDisambiguate
import org.languagetool.JLanguageTool; //导入方法依赖的package包/类
@Test
public void testDisambiguate() throws Exception {
Disambiguator chunker = new MultiWordChunker("/pl/multiwords.txt");
JLanguageTool lt = new JLanguageTool(new English());
AnalyzedSentence analyzedSentence = lt.getAnalyzedSentence("A test... More.");
AnalyzedSentence disambiguated = chunker.disambiguate(analyzedSentence);
AnalyzedTokenReadings[] tokens = disambiguated.getTokens();
assertTrue(tokens[4].getReadings().toString().contains("<ELLIPSIS>"));
assertTrue(tokens[6].getReadings().toString().contains("</ELLIPSIS>"));
}
示例7: testDisambiguateMultiSpace
import org.languagetool.JLanguageTool; //导入方法依赖的package包/类
@Test
public void testDisambiguateMultiSpace() throws Exception {
Disambiguator chunker = new MultiWordChunker("/uk/multiwords.txt");
JLanguageTool lt = new JLanguageTool(new Ukrainian());
AnalyzedSentence analyzedSentence = lt.getAnalyzedSentence("для годиться.");
AnalyzedSentence disambiguated = chunker.disambiguate(analyzedSentence);
AnalyzedTokenReadings[] tokens = disambiguated.getTokens();
assertTrue(tokens[1].getReadings().toString().contains("<adv>"));
assertTrue(tokens[4].getReadings().toString().contains("</adv>"));
}
示例8: match
import org.languagetool.JLanguageTool; //导入方法依赖的package包/类
private RuleMatch[] match(BitextPatternRule rule, String src, String trg,
JLanguageTool srcLanguageTool,
JLanguageTool trgLanguageTool) throws IOException {
AnalyzedSentence srcText = srcLanguageTool.getAnalyzedSentence(src);
AnalyzedSentence trgText = trgLanguageTool.getAnalyzedSentence(trg);
return rule.match(srcText, trgText);
}
示例9: test
import org.languagetool.JLanguageTool; //导入方法依赖的package包/类
@Test
public void test() throws IOException {
Language german = Languages.getLanguageForShortCode("de");
OldSpellingRule rule = new OldSpellingRule(JLanguageTool.getMessageBundle());
JLanguageTool lt = new JLanguageTool(german);
AnalyzedSentence sentence1 = lt.getAnalyzedSentence("Ein Kuß");
assertThat(rule.match(sentence1).length, is(1));
assertThat(rule.match(sentence1)[0].getSuggestedReplacements().toString(), is("[Kuss]"));
AnalyzedSentence sentence2 = lt.getAnalyzedSentence("Das Corpus delicti");
assertThat(rule.match(sentence2).length, is(1));
assertThat(rule.match(sentence2)[0].getSuggestedReplacements().toString(), is("[Corpus Delicti]"));
// inflected forms should work, too (as long as the inflection database still contains the old variants):
AnalyzedSentence sentence3 = lt.getAnalyzedSentence("In Rußlands Weiten");
assertThat(rule.match(sentence3).length, is(1));
assertThat(rule.match(sentence3)[0].getSuggestedReplacements().toString(), is("[Russlands]"));
AnalyzedSentence sentence4 = lt.getAnalyzedSentence("Hot pants");
assertThat(rule.match(sentence4).length, is(1));
assertThat(rule.match(sentence4)[0].getSuggestedReplacements().toString(), is("[Hotpants]"));
AnalyzedSentence sentence5 = lt.getAnalyzedSentence("Ich muß los");
assertThat(rule.match(sentence5).length, is(1));
assertThat(rule.match(sentence5)[0].getSuggestedReplacements().toString(), is("[muss]"));
assertThat(rule.match(lt.getAnalyzedSentence("In Russland")).length, is(0));
assertThat(rule.match(lt.getAnalyzedSentence("In Russlands Weiten")).length, is(0));
assertThat(rule.match(lt.getAnalyzedSentence("Schlüsse")).length, is(0));
assertThat(rule.match(lt.getAnalyzedSentence("in Schloß Holte")).length, is(0));
assertThat(rule.match(lt.getAnalyzedSentence("Asse")).length, is(0));
}
示例10: testContractions
import org.languagetool.JLanguageTool; //导入方法依赖的package包/类
@Test
public void testContractions() throws Exception {
JLanguageTool langTool = new JLanguageTool(new English());
AnalyzedSentence analyzedSentence = langTool.getAnalyzedSentence("I'll be there");
AnalyzedTokenReadings[] tokens = analyzedSentence.getTokens();
assertThat(tokens[1].getChunkTags().get(0), is(new ChunkTag("B-NP-singular")));
assertThat(tokens[2].getChunkTags().size(), is(0)); // "'" cannot be mapped as we tokenize differently
assertThat(tokens[3].getChunkTags().size(), is(0)); // "ll" cannot be mapped as we tokenize differently
assertThat(tokens[5].getChunkTags().get(0), is(new ChunkTag("I-VP")));
}
示例11: tagText
import org.languagetool.JLanguageTool; //导入方法依赖的package包/类
/**
* Tags text using the LanguageTool tagger, printing results to System.out.
*
* @param contents Text to tag.
* @param lt LanguageTool instance
*/
public static void tagText(String contents, JLanguageTool lt) throws IOException {
AnalyzedSentence analyzedText;
List<String> sentences = lt.sentenceTokenize(contents);
for (String sentence : sentences) {
analyzedText = lt.getAnalyzedSentence(sentence);
System.out.println(analyzedText);
}
}
示例12: evaluate
import org.languagetool.JLanguageTool; //导入方法依赖的package包/类
@SuppressWarnings("ConstantConditions")
private void evaluate(List<Sentence> sentences, boolean isCorrect, String token, String homophoneToken, List<Long> evalFactors) throws IOException {
println("======================");
printf("Starting evaluation on " + sentences.size() + " sentences with %s/%s:\n", token, homophoneToken);
JLanguageTool lt = new JLanguageTool(language);
List<Rule> allActiveRules = lt.getAllActiveRules();
for (Rule activeRule : allActiveRules) {
lt.disableRule(activeRule.getId());
}
for (Sentence sentence : sentences) {
String textToken = isCorrect ? token : homophoneToken;
String plainText = sentence.getText();
String replacement = plainText.indexOf(textToken) == 0 ? StringTools.uppercaseFirstChar(token) : token;
String replacedTokenSentence = isCorrect ? plainText : plainText.replaceFirst("(?i)\\b" + textToken + "\\b", replacement);
AnalyzedSentence analyzedSentence = lt.getAnalyzedSentence(replacedTokenSentence);
for (Long factor : evalFactors) {
rule.setConfusionSet(new ConfusionSet(factor, homophoneToken, token));
RuleMatch[] matches = rule.match(analyzedSentence);
boolean consideredCorrect = matches.length == 0;
String displayStr = plainText.replaceFirst("(?i)\\b" + textToken + "\\b", "**" + replacement + "**");
if (consideredCorrect && isCorrect) {
evalValues.get(factor).trueNegatives++;
} else if (!consideredCorrect && isCorrect) {
evalValues.get(factor).falsePositives++;
println("false positive with factor " + factor + ": " + displayStr);
} else if (consideredCorrect && !isCorrect) {
//println("false negative: " + displayStr);
evalValues.get(factor).falseNegatives++;
} else {
evalValues.get(factor).truePositives++;
//System.out.println("true positive: " + displayStr);
}
}
}
}
示例13: evaluateSentence
import org.languagetool.JLanguageTool; //导入方法依赖的package包/类
private void evaluateSentence(NeuralNetworkRule rule, boolean token1IsCorrect, String token1, String token2, Map<Double, EvalValues> evalValues, List<Double> evalMinScores, JLanguageTool lt, Sentence sentence) {
String textToken = token1IsCorrect ? token1 : token2;
String plainText = sentence.getText();
String replacement = token1;
String replacedTokenSentence = token1IsCorrect ? plainText : plainText.replaceFirst("(?i)\\b" + textToken + "\\b", replacement);
try {
AnalyzedSentence analyzedSentence = lt.getAnalyzedSentence(replacedTokenSentence);
for (Double minScore : evalMinScores) {
evaluateSentenceWithMinScore(rule, token1IsCorrect, textToken, plainText, replacement, analyzedSentence, evalValues, minScore);
}
} catch (IOException e) {
throw new RuntimeException("Error while analyzing sentence", e);
}
}
示例14: tagTextAndDisplayResults
import org.languagetool.JLanguageTool; //导入方法依赖的package包/类
private void tagTextAndDisplayResults() {
final JLanguageTool langTool = ltSupport.getLanguageTool();
// tag text
final List<String> sentences = langTool.sentenceTokenize(textArea.getText());
final StringBuilder sb = new StringBuilder();
try {
for (String sent : sentences) {
final AnalyzedSentence analyzedText = langTool.getAnalyzedSentence(sent);
final String analyzedTextString = StringTools.escapeHTML(analyzedText.toString(",")).
replace("<S>", "<S><br>").
replace("[", "<font color='" + TAG_COLOR + "'>[").
replace("]", "]</font><br>");
sb.append(analyzedTextString).append('\n');
}
} catch (Exception e) {
sb.append(getStackTraceAsHtml(e));
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if (taggerDialog == null) {
taggerDialog = new JDialog(frame);
taggerDialog.setTitle(messages.getString("taggerWindowTitle"));
taggerDialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
taggerDialog.setResizable(true);
taggerDialog.setSize(640, 480);
taggerDialog.setLocationRelativeTo(frame);
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
taggerDialog.setVisible(false);
}
};
taggerDialog.getRootPane().registerKeyboardAction(actionListener,
stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
JPanel panel = new JPanel(new GridBagLayout());
taggerDialog.add(panel);
taggerArea = new JTextPane();
taggerArea.setContentType("text/html");
taggerArea.setEditable(false);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.weightx = 1.0;
c.weighty = 1.0;
c.insets = new Insets(8,8,4,8);
c.fill = GridBagConstraints.BOTH;
panel.add(new JScrollPane(taggerArea),c);
c.gridx = 0;
c.gridy = 1;
c.weightx = 0.0;
c.weighty = 0.0;
c.insets = new Insets(4,8,8,8);
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.EAST;
JButton closeButton = new JButton(
messages.getString("guiCloseButton"));
closeButton.addActionListener(actionListener);
panel.add(closeButton,c);
}
taggerDialog.setVisible(true);
taggerArea.setText(HTML_FONT_START + sb.toString() + HTML_FONT_END);
}
});
}
示例15: match
import org.languagetool.JLanguageTool; //导入方法依赖的package包/类
private boolean match(Rule rule, String sentence, JLanguageTool languageTool) throws IOException {
AnalyzedSentence analyzedSentence = languageTool.getAnalyzedSentence(sentence);
RuleMatch[] matches = rule.match(analyzedSentence);
return matches.length > 0;
}