2011-02-13 13 views
9

मैं स्टैनफोर्ड एनएलपी पार्सर के साथ वाक्यों की एक सूची को पार्स करना चाहता हूं। मेरी सूची एक ArrayList है, मैं LexicalizedParser के साथ सभी सूची कैसे पार्स कर सकता हूं? स्टैनफोर्ड NLP सेवाक्यों की सूची कैसे पार्स करें?

Tree parse = (Tree) lp1.apply(sentence); 

उत्तर

1

असल प्रलेखन कैसे वाक्य पार्स करने के लिए की नमूना प्रदान:

मैं प्रत्येक वाक्य से इस फार्म प्राप्त करना चाहते हैं।

आप हालांकि एक प्रलेखन में खुदाई कर सकते हैं प्रलेखन here

+2

पार्सर के साथ आने वाले पार्सर डेमो उदाहरणों को भी देखें। आप सीधे एक स्ट्रिंग पर लागू() को कॉल कर सकते हैं जो एक वाक्य है। –

20

पा सकते हैं, मैं विशेष रूप से लिंक के लिए कदम और/या मर जाते हैं इतने पर यहाँ कोड प्रदान करने के लिए, जा रहा हूँ। यह विशेष उत्तर पूरे पाइपलाइन का उपयोग करता है। यदि पूरे पाइपलाइन में दिलचस्पी नहीं है, तो मैं केवल एक सेकंड में वैकल्पिक उत्तर प्रदान करूंगा।

नीचे उदाहरण स्टैनफोर्ड पाइपलाइन का उपयोग करने का पूरा तरीका है। यदि कोररेंशन रिज़ॉल्यूशन में दिलचस्पी नहीं है, तो कोड की तीसरी पंक्ति से dcoref हटा दें। तो नीचे दिए गए उदाहरण में, पाइपलाइन आपके लिए वाक्य को विभाजित करती है (एसएसप्लिट एनोटेटर) यदि आप इसे टेक्स्ट के टेक्स्ट (टेक्स्ट वेरिएबल) में खिलाते हैं। सिर्फ एक वाक्य है? खैर, यह ठीक है, आप इसे पाठ चर के रूप में फ़ीड कर सकते हैं।

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

    // read some text in the text variable 
    String text = ... // Add your text here! 

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

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

    // these are all the sentences in this document 
    // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types 
    List<CoreMap> sentences = document.get(SentencesAnnotation.class); 

    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(TokensAnnotation.class)) { 
     // this is the text of the token 
     String word = token.get(TextAnnotation.class); 
     // this is the POS tag of the token 
     String pos = token.get(PartOfSpeechAnnotation.class); 
     // this is the NER label of the token 
     String ne = token.get(NamedEntityTagAnnotation.class);  
     } 

     // this is the parse tree of the current sentence 
     Tree tree = sentence.get(TreeAnnotation.class); 

     // this is the Stanford dependency graph of the current sentence 
     SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class); 
    } 

    // This is the coreference link graph 
    // Each chain stores a set of mentions that link to each other, 
    // along with a method for getting the most representative mention 
    // Both sentence and token offsets start at 1! 
    Map<Integer, CorefChain> graph = 
     document.get(CorefChainAnnotation.class); 
1

तो वादे के अनुसार, आप पूर्ण स्टैनफोर्ड पाइप लाइन तक पहुँचने के लिए (हालांकि मुझे विश्वास है कि दृष्टिकोण की सिफारिश की है) नहीं करना चाहते हैं, तो आप LexicalizedParser वर्ग के साथ सीधे काम कर सकते हैं। इस मामले में, आप स्टैनफोर्ड पार्सर का नवीनतम संस्करण डाउनलोड करेंगे (जबकि दूसरा कोरएनएलपी उपकरण का उपयोग करेगा)। सुनिश्चित करें कि पार्सर जार के अतिरिक्त, आपके पास उपयुक्त पार्सर के लिए मॉडल फ़ाइल है जिसके साथ आप काम करना चाहते हैं। उदाहरण कोड:

LexicalizedParser lp1 = new LexicalizedParser("englishPCFG.ser.gz", new Options()); 
String sentence = "It is a fine day today"; 
Tree parse = lp.parse(sentence); 

नोट यह पार्सर के संस्करण 3.3.1 के लिए काम करता है।

संबंधित मुद्दे