2

私はスタンフォードの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){"と書くと、コンパイル時に互換性のないタイプのエラーメッセージが表示されます。

4

2 に答える 2

2

次のメソッドをString呼び出すことで、コンテンツにアクセスできます。例えばword()edu.stanford.nlp.ling.Word

import edu.stanford.nlp.ling.Word;

List<Word> words = ...
for (Word word : words) {
  if (word.word().equals(args(1))) {
    System.err.println("Yes!");
  }
}

また、互換性のないタイプのクラス(vsなど)を比較Listしようとすると、コンパイラまたはIDEが通常警告することを意味するため、ジェネリックスを定義するときにジェネリックスを使用することをお勧めします。WordString

編集

NLPAPIの古いバージョンを見ていました。最新のDocumentPreprocessorドキュメントを見ると、メソッドを定義するIterable<List<HasWord>>ために実装されていることがわかります。したがって、コードは次のようになります。HasWordword()

DocumentPreprocessor dp = ...
for (HasWord hw : dp) {
  if (hw.word().equals(args[1])) {
    System.err.println("Yes!");
  }
}
于 2011-10-13T13:46:35.200 に答える
2

言葉は優雅に印刷できるので、シンプルword.toString().equals(arg[1])で十分です。

于 2011-10-13T14:10:56.757 に答える