2017-04-12 8 views
1

저는 작업중인 프로젝트에서 java에서 openNLP API를 사용하고 있습니다. 것은 나의 프로그램으로 나는 오직 서신 만 처리한다. 코드 :openNLP 자바 - 멀티 용어 포르투갈어 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입니다 안토니오 캄포스는 사람이 아닌 사람처럼 -> 안토니오 및 장소 - > Campos 또는 Organisation Nova de Lisboa

답변

0

Stanford-NLP 과정은 한 단어로만 진행됩니다. coreNLP에 Sentence를 지정해도 토큰으로 분리되어 하나씩 처리됩니다. 그리고 다 학기에 NER이 적용되는 것을 들어 본 적이 없습니다.

+0

임 문장에 텍스트를 깨는을하고, 토큰 화 각 단어를, 그래서 내 프로그램은 개별적으로 각각의 단어에서보고, 내 문제가있다 어디, 내가 가지고 토큰 화하는 것이 아니라 나는 말을보고 예를 들어 사람 안토니오 캄포스를 찾을 때 안토니오가 아닌 한 사람으로, 캄포 스는 장소 또는 다른 사람으로 찾아야합니다 ... 페르난도 페소아의 유니버 시티와 같은 단체들도 마찬가지입니다. 그것을 하나의 조직으로, 조직으로서의 대학으로, 다른 사람과 마찬가지로 페르난도와 사람과 페소 아로 발견하십시오. – Break

+0

알았어, 네 문제는 이해하지만 이건 어떻게 ** 스탠포드 -NLP ** 일인지. NER은이 프로세스가 완료된 후에 만 ​​작동합니다 (tokenize, ssplit, pos, lemma). 따라서 NER 프로세스가 단어를 잘못 쓰는 것이 확실합니다. 이 [link] (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 답변을 위해, 내가 tryed 한 감사하는 솔루션되지만 출력했다 : Ficheiro 닷컴 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 : 없음 라인은 내가 대한 tostring 같은 다른 방법을 출력을 tryed했습니다 ner.Wiki.main (Wiki.java:76)에서 java.util.Scanner.nextLine (Scanner.java:1540) \t에서 \t하는 결과가 없습니다. . 그리고 그것 이전과 같았습니다. 내가 여기서 뭔가 잘못하고있는 것입니까? 고맙습니다. – Break

+0

Span.spansToStrings (...)는 문자열 배열을 반환합니다. 'Arrays.toString (Span.spansToStrings (nameSpans, tokens))'를 사용하여 다시 시도하십시오. – wcolen