私はスタンフォードのNLPパーサー(http://nlp.stanford.edu/software/lex-parser.shtml)を使用して、テキストのブロックを文に分割し、どの文に特定の単語が含まれているかを確認しています。
これまでの私のコードは次のとおりです。
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.process.*;
public class TokenizerDemo {
public static void main(String[] args) throws IOException {
DocumentPreprocessor dp = new DocumentPreprocessor(args[0]);
for (List sentence : dp) {
for (Object word : sentence) {
System.out.println(word);
System.out.println(word.getClass().getName());
if (word.equals(args[1])) {
System.out.println("yes!\n");
}
}
}
}
}
「javaTokenizerDemotestfile.txtwall」を使用してコマンドラインからコードを実行します
testfile.txtの内容は次のとおりです。
Humpty Dumpty sat on a wall. Humpty Dumpty had a great fall.
したがって、プログラムで最初の文の「wall」を検出する必要があります(「wall」はコマンドラインの2番目の引数として入力されます)。しかし、プログラムは「yes!」を出力しないため、「wall」を検出しません。プログラムの出力は次のとおりです。
Humpty
edu.stanford.nlp.ling.Word
Dumpty
edu.stanford.nlp.ling.Word
sat
edu.stanford.nlp.ling.Word
on
edu.stanford.nlp.ling.Word
a
edu.stanford.nlp.ling.Word
wall
edu.stanford.nlp.ling.Word
.
edu.stanford.nlp.ling.Word
Humpty
edu.stanford.nlp.ling.Word
Dumpty
edu.stanford.nlp.ling.Word
had
edu.stanford.nlp.ling.Word
a
edu.stanford.nlp.ling.Word
great
edu.stanford.nlp.ling.Word
fall
edu.stanford.nlp.ling.Word
.
edu.stanford.nlp.ling.Word
スタンフォードパーサーのDocumentPreprocessorは、テキストを2つの文に正しく分割します。問題は、equalsメソッドの使用にあるようです。各単語のタイプは「edu.stanford.nlp.ling.Word」です。単語の基になる文字列にアクセスしようとしたので、文字列が「壁」に等しいかどうかを確認できますが、アクセス方法がわかりません。
2番目のforループを"for(Word word:sentence){"と書くと、コンパイル時に互換性のないタイプのエラーメッセージが表示されます。