2017-10-17 5 views
1

내 코드의 목적은 문서 (pdf 또는 doc 파일)를 제출하고 그 안에 모든 텍스트를 가져 오는 것입니다. stanford nlp가 분석 할 텍스트를 제공하십시오. 코드는 잘 작동합니다. 그러나 문서에 "Pardeep Kumar"와 같은 이름이 있다고 가정합니다. 다음 그것을 받았다 출력은이다java 용 스탠포드 nlp api : 전체가 아닌 부분으로 이름을 얻는 방법

Pardeep NNP PERSON

쿠마 NNP PERSON을

하지만 난 이렇게 될 원한다

Pardeep 쿠마 NNP 사람

어떻게해야합니까? 그게 실제로 어떻게 하나의 이름이나 비슷한 것을 만들어내는 두 단어가 인접 해 있는지 확인하는 것입니다. 어떻게 다른 단어로 나눌 수 있습니까? 당신은 entitymentions 주석 자를 사용하려면

public class readstuff { 

     public static void analyse(String data) { 

      // creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER, parsing, and coreference resolution 
      Properties props = new Properties(); 
      props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref"); 

      StanfordCoreNLP pipeline = new StanfordCoreNLP(props); 


      // create an empty Annotation just with the given text 
      Annotation document = new Annotation(data); 

      // run all Annotators on this text 
      pipeline.annotate(document); 

      List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class); 

      // System.out.println("word"+"\t"+"POS"+"\t"+"NER"); 
      for (CoreMap sentence : sentences) { 

       // traversing the words in the current sentence 
       // a CoreLabel is a CoreMap with additional token-specific methods 

       for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) { 
        // this is the text of the token 
        String word = token.get(CoreAnnotations.TextAnnotation.class); 
        // this is the POS tag of the token 
        String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class); 
        // this is the NER label of the token 
        String ne = token.get(CoreAnnotations.NamedEntityTagAnnotation.class); 

        if(ne.equals("PERSON") || ne.equals("LOCATION") || ne.equals("DATE")) 
        { 

         System.out.format("%32s%10s%16s",word,pos,ne); 
         System.out.println(); 
        //System.out.println(word +"  \t"+pos +"\t"+ne); 
        } 

       } 
      } 
     } 

    public static void main(String[] args) throws FileNotFoundException, IOException, TransformerConfigurationException{ 

     JFileChooser window=new JFileChooser(); 
     int a=window.showOpenDialog(null); 

     if(a==JFileChooser.APPROVE_OPTION){ 
      String name=window.getSelectedFile().getName(); 
      String extension = name.substring(name.lastIndexOf(".") + 1, name.length()); 
      String data = null; 

      if(extension.equals("docx")){ 
       XWPFDocument doc=new XWPFDocument(new FileInputStream(window.getSelectedFile())); 
       XWPFWordExtractor extract= new XWPFWordExtractor(doc); 
       //System.out.println("docx file reading..."); 
       data=extract.getText(); 
       //extract.getMetadataTextExtractor(); 
      } 
      else if(extension.equals("doc")){ 
       HWPFDocument doc=new HWPFDocument(new FileInputStream(window.getSelectedFile())); 
       WordExtractor extract= new WordExtractor(doc); 
       //System.out.println("doc file reading..."); 
       data=extract.getText(); 
      } 
      else if(extension.equals("pdf")){ 
       //System.out.println(window.getSelectedFile()); 
       PdfReader reader=new PdfReader(new FileInputStream(window.getSelectedFile())); 
       int n=reader.getNumberOfPages(); 
       for(int i=1;i<n;i++) 
       { 
        //System.out.println(data); 
       data=data+PdfTextExtractor.getTextFromPage(reader,i); 
       } 
      } 
      else{ 
       System.out.println("format not supported"); 
      } 

     analyse(data); 
     } 
    } 



} 
+0

내가 생각할 수있는 것은 사전에 인도어 이름이 사전에 없으므로 영어 단어인지 아닌지 여부를 알기 위해 사전을 사용할 수 있다는 것입니다. 사전에 없으면 요구 사항에 따라 인접하여 인쇄물을 변경할 수 있습니다. – Lokesh

+0

고마워요,하지만 그게 대답이 아니에요. –

답변

0

:

여기 내 코드입니다.

package edu.stanford.nlp.examples; 

import edu.stanford.nlp.pipeline.*; 
import edu.stanford.nlp.ling.*; 
import edu.stanford.nlp.util.*; 

import java.util.*; 

public class EntityMentionsExample { 

    public static void main(String[] args) { 
    Annotation document = 
     new Annotation("John Smith visited Los Angeles on Tuesday. He left Los Angeles on Wednesday."); 
    Properties props = new Properties(); 
    props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,entitymentions"); 
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props); 
    pipeline.annotate(document); 

    for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) { 
     for (CoreMap entityMention : sentence.get(CoreAnnotations.MentionsAnnotation.class)) { 
     System.out.println(entityMention); 
     System.out.println(entityMention.get(CoreAnnotations.EntityTypeAnnotation.class)); 
     } 
    } 
    } 
} 
+0

당신은 여러 문장을 위해 그것을 어떻게 수행 할 것을 제안 할 수 있습니까? 또는 귀하의 코드와 동일한 방식으로 작동하기 위해 내 코드에서 필요한 변경 사항은 무엇입니까? –

+0

2 개의 문장으로 작업하도록 예제를 변경했습니다. – StanfordNLPHelp