2017-04-12 19 views
1

私が取り組んでいるプロジェクトでは、JavaでopenNLP APIを使用しています。事は、私のプログラムでは、ただ対応していない単語だけを処理するということです。 コード:openNLP java-multi termポルトガル語NER

String line = input.nextLine(); 


      InputStream inputStreamTokenizer = new FileInputStream("/home/bruno/openNLP/apache-opennlp-1.7.2-src/models/pt-token.bin"); 
      TokenizerModel tokenModel = new TokenizerModel(inputStreamTokenizer); 

      //Instantiating the TokenizerME class 
      TokenizerME tokenizer = new TokenizerME(tokenModel); 
      String tokens[] = tokenizer.tokenize(line); 


      InputStream inputStream = new FileInputStream("/home/bruno/openNLP/apache-opennlp-1.7.2-src/models/pt-sent.bin"); 
      SentenceModel model = new SentenceModel(inputStream); 

      //Instantiating the SentenceDetectorME class 
      SentenceDetectorME detector = new SentenceDetectorME(model); 

      //Detecting the sentence 
      String sentences[] = detector.sentDetect(line); 

      //Loading the NER-location model 
      //InputStream inputStreamLocFinder = new FileInputStream("/home/bruno/openNLP/apache-opennlp-1.7.2-src/models/en-ner-location.bin");  
      //TokenNameFinderModel model = new TokenNameFinderModel(inputStreamLocFinder); 

      //Loading the NER-person model 
      InputStream inputStreamNameFinder = new FileInputStream("/home/bruno/TryOllie/data/pt-ner-floresta.bin");  
      TokenNameFinderModel model2 = new TokenNameFinderModel(inputStreamNameFinder); 

      //Instantiating the NameFinderME class 
      NameFinderME nameFinder2 = new NameFinderME(model2); 

      //Finding the names of a location 
      Span nameSpans2[] = nameFinder2.find(tokens); 

      //Printing the spans of the locations in the sentence 
      //for(Span s: nameSpans)   
      //System.out.println(s.toString()+" "+tokens[s.getStart()]); 

      Set<String> x = new HashSet<String>(); 
      x.add("event"); 
      x.add("artprod"); 
      x.add("place"); 
      x.add("organization"); 
      x.add("person"); 
      x.add("numeric"); 

      SimpleTokenizer simpleTokenizer = SimpleTokenizer.INSTANCE; 
      Span[] tokenz = simpleTokenizer.tokenizePos(line); 
      Set<String> tk = new HashSet<String>(); 
      for(Span tok : tokenz){ 
       tk.add(line.substring(tok.getStart(), tok.getEnd())); 
      } 

      for(Span n: nameSpans2) 
      { 
       if(x.contains(n.getType())) 
        System.out.println(n.toString()+ " -> " + tokens[n.getStart()]); 

      } 

私が手に出力されている:アントニオ・カンポスは、人、ない人であるようにやろうとして何イム

Ficheiro com extensao: file.txt 
[1..2) event -> choque[3..4) event -> cadeia[6..7) artprod -> viaturas[13..14) event -> feira[16..18) place -> Avenida[20..21) place -> Porto[24..25) event -> incêndio[2..3) event -> acidente[5..6) artprod -> viaturas[44..45) organization -> JN[46..47) person -> António[47..48) place -> Campos[54..60) organization -> Batalhão[1..2) event -> acidente[6..8) numeric -> 9[11..12) place -> Porto-Matosinhos[21..22) event -> ocorrência[29..30) artprod -> .[4..5) organization -> Sapadores[7..10) organization -> Bombeiros[14..15) numeric -> 15 

は、マルチタームのNERある - >アントニオとPlace - > Campos、またはOrganization - > Universidade Nova de Lisboa

答えて

0

スタンフォード - NLPプロセスはシングルワードのみです。 coreNLPにSentenceを指定しても、それはトークンに分割され、それらを1つずつ処理します。私は多項にNERが適用されることは聞いたことがありません。

+0

イムが、私は言葉を見て、たとえばアントニオ・カンポスを見つけて、アントニオを一人として、カンポスを場所として、あるいは他の人として見つけ出す必要があります... フェルナンド・ペソアのユニバティーのような組織にも同じことが言えます。組織としての大学ではなく、他の人としてのペルソナとしてのフェルナンド – Break

+0

申し訳ありませんが、私はあなたの問題を理解していますが、これはどのように**スタンフォードNLP **の仕事です。 NERはこのプロセスが完了した後にのみ動作します(tokenize、ssplit、pos、補題)。だから、それは明らかにNERプロセスは単語をスニールするだけです。この[リンク](https://stanfordnlp.github.io/CoreNLP/dependencies.html)があなたにアイデアを与えるかもしれません。 –

+0

だから、私は単一の言葉しか見ることができません。つまり、そのNERで私の問題を解決することはできません。 – Break

1

誤ったデータ構造を印刷しています。スパンgetSartとgetEndは、エンティティの一部であるトークンのシーケンスを指します。最初のトークンだけを印刷しています。

また、文章検出前にトークン化を行っています。

次のコードを試してみてください。私のプログラムのみを個別に各単語を見て、どこに問題があるということ、私はトークン化する必要がありますので、文章にテキストを壊し、およびトークン化各単語

// load the models outside your loop 
InputStream inputStream = 
    new FileInputStream("/home/bruno/openNLP/apache-opennlp-1.7.2-src/models/pt-sent.bin"); 
SentenceModel model = new SentenceModel(inputStream); 

//Instantiating the SentenceDetectorME class 
SentenceDetectorME detector = new SentenceDetectorME(model); 

InputStream inputStreamTokenizer = 
    new FileInputStream("/home/bruno/openNLP/apache-opennlp-1.7.2-src/models/pt-token.bin"); 
TokenizerModel tokenModel = new TokenizerModel(inputStreamTokenizer); 
//Instantiating the TokenizerME class 
TokenizerME tokenizer = new TokenizerME(tokenModel); 


//Loading the NER-person model 
InputStream inputStreamNameFinder = new FileInputStream("/home/bruno/TryOllie/data/pt-ner-floresta.bin"); 
TokenNameFinderModel model2 = new TokenNameFinderModel(inputStreamNameFinder); 

//Instantiating the NameFinderME class 
NameFinderME nameFinder2 = new NameFinderME(model2); 

String line = input.nextLine(); 

while(line != null) { 

    // first we find sentences 
    String sentences[] = detector.sentDetect(line); 

    for (String sentence : 
     sentences) { 
    // now we find the sentence tokens 
    String tokens[] = tokenizer.tokenize(sentence); 

    // now we are good to apply NER 
    Span[] nameSpans = nameFinder2.find(tokens); 

    // now we can print the spans 
    System.out.println(Arrays.toString(Span.spansToStrings(nameSpans, tokens))); 

    line = input.nextLine(); 
    } 
} 
+0

あなたがwcolen答えるために、私がしようと試みてきたありがとうその解決策が、出力された: Ficheiro COM extensao:file.txtは [Ljava.lang.String; 6f2b958e [Ljava.lang.String @; @ 1eb44e46 [ Ljava.lang.String; 6504e3b2 [Ljava.lang.String @; 515f550a [Ljava.lang.String @; 626b2d4a [Ljava.lang.String @; 5e91993f 例外@スレッドに "メイン" java.util.NoSuchElementException :いいえ行がner.Wiki.main(Wiki.java:76) でjava.util.Scanner.nextLine(Scanner.java:1540)で \t \tを発見した私はのためのtoStringのような他の方法で出力をしようと試みてきました。それと以前と同じだったのですが、ここで何か間違っていますか?ありがとうございます – Break

+0

'Span.spansToStrings(...)'は文字列の配列を返します。 'Arrays.toString(Span.spansToStrings(nameSpans、tokens))'を使ってやり直してください。 – wcolen

関連する問題