2014-02-14 55 views
-1

**我正在製作一個關於情感分析的項目。所以我用stanford POS tagger來標記句子。我想從句子中提取名詞短語,但它只是標記名詞。 我如何從中得到名詞短語。我用java編碼。 我搜索網站上,我發現這個製作一個名詞短語: 對於名詞短語,這種模式或正則表達式如下:使用stanford POS tagger在情感分析中找到名詞短語

(形容詞|名詞)*(名詞介詞)? (形容詞|名詞)*名詞 即零個或多個形容詞或名詞,後跟一個名詞和介詞的選項組,後跟零個或多個形容詞或名詞,後跟單個名詞。

我正在嘗試使用java的reguler表達式庫進行編碼。即正則表達式。但無法找到理想的結果。 有沒有人有它的代碼? **

回答

1

我已經編碼這個。並且解決方案是...... 它將從只包含名詞的句子中提取所有名詞短語。例如 。像NP是:白虎。它會提取「白虎」。

public static void maketree(String sent, int sno, Sentences sen) 
{ 
    try 
    { 
     LexicalizedParser parser = LexicalizedParser.loadModel("stanford-parser-full-2014-01-04\\stanford-parser-3.3.1-models\\edu\\stanford\\nlp\\models\\lexparser\\englishPCFG.ser.gz"); 
     String sent2 = "Picture Quality of this camera is very good"; 
     String sent1[] = sent2.split(" "); 
     List<CoreLabel> rawWords = Sentence.toCoreLabelList(sent1); 
     Tree x = parser.apply(rawWords); 
     x.indexLeaves(); 
     System.out.println(x); 
     findNP(x,sen); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 

public static void findNP(Tree t, Sentences sent) 
{ 
    if (t.label().value().equals("NP")) 
    { 
     noun(t,sent); 
    } 
    else 
    { 
     for (Tree child : t.children()) 
     {     
      findNP(child,sent); 
     } 
    } 

} 

    public static void noun(Tree t,Sentences sent) 
{  
    String noun=""; 
    for(Tree temp : t.children()) 
    { 
     String val = temp.label().value(); 
     if(val.equals("NN") || val.equals("NNS") || val.equals("NNP") || val.equals("NNPS")) 
     { 
      Tree nn[] = temp.children(); 
      String ss = Sentence.listToString(nn[0].yield()); 
      if(noun=="") 
      { 
       noun = ss; 
      } 
      else 
      { 
       noun = noun+" "+ss; 
      } 
     } 
     else 
     { 
      if(noun!="") 
      { 
       sent.nouns[i++] = noun; 
       noun = ""; 
      } 
      noun(temp,sent); 
     } 
    } 
    if(noun!="") 
    { 
     sent.nouns[i++] = noun; 
    } 
} 
0

您能否檢查鏈接並對此發表評論。你能否請我,如果 「白虎」會得到與你上面的代碼相同的結果。可能代碼不完整,這就是爲什麼我得到一些錯誤。

爲例如:

sent.nouns [I ++] =名詞; // sent.nouns ?????它似乎是不確定的。您能否請您獲取完整的代碼,或者您是否可以通過下面的鏈接進行溝通。

這裏是鏈接

Extract Noun phrase using stanford NLP

感謝您的幫助

相關問題