2013-02-04 24 views
1

我正在使用斯坦福CoreNLP分析器來運行某些文本,並且還有日期短語,例如'10月的第二個星期一'和'過去的一年'。該庫會將每個標記適當地標記爲一個DATE命名實體,但是有沒有一種方法來以編程方式獲取這個整個日期短語?這不僅僅是日期,ORGANIZATION命名的實體也會這樣做(例如,「國際奧委會」可以是在給定文本示例中標識的)。是否有可能獲得一組包含短語的特定命名實體標記

String content = "Thanksgiving, or Thanksgiving Day (Canadian French: Jour de" 
     + " l'Action de grâce), occurring on the second Monday in October, is" 
     + " an annual Canadian holiday which celebrates the harvest and other" 
     + " blessings of the past year."; 

Properties p = new Properties(); 
p.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse"); 
StanfordCoreNLP pipeline = new StanfordCoreNLP(p); 

Annotation document = new Annotation(content); 
pipeline.annotate(document); 

for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) { 
    for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) { 

     String word = token.get(CoreAnnotations.TextAnnotation.class); 
     String ne = token.get(CoreAnnotations.NamedEntityTagAnnotation.class); 

     if (ne.equals("DATE")) { 
      System.out.println("DATE: " + word); 
     } 

    } 
} 

其中,斯坦福註釋和分級加載後,會產生輸出:

DATE: Thanksgiving 
DATE: Thanksgiving 
DATE: the 
DATE: second 
DATE: Monday 
DATE: in 
DATE: October 
DATE: the 
DATE: past 
DATE: year 

我覺得圖書館必須要承認的短語和使用它們的命名實體標記,所以問題是數據會以某種方式保存並通過api獲取?

感謝, 凱文

回答

1

後在郵件列表上的討論,我發現,API不支持此功能。我的解決方案是隻保留最後一個NE的狀態,並在必要時建立一個字符串。 John B.從nlp郵件列表中回答我的問題很有幫助。

0

非常感謝,我也打算這麼做。然而,斯坦福NER API支持classifyToCharOffset(或類似的東西)來獲得整個短語。我不知道,也許這只是你想法的實現:D。

0

命名實體惡搞和部分的語音惡搞是CoreNLP管道內不同的算法和似乎API消費者的任務是整合它們。

請原諒我的C#,但這裏是一個簡單的類:

public class NamedNounPhrase 
    { 
     public NamedNounPhrase() 
     { 
      Phrase = string.Empty; 
      Tags = new List<string>(); 
     } 

     public string Phrase { get; set; } 

     public IList<string> Tags { get; set; } 

    } 

和一些代碼來發現所有的頂級名詞短語及其相關的命名實體標籤:

private void _monkey() 
    { 

     ... 

     var nounPhrases = new List<NamedNounPhrase>(); 

     foreach (CoreMap sentence in sentences.toArray()) 
     { 
      var tree = 
       (Tree)sentence.get(new TreeCoreAnnotations.TreeAnnotation().getClass()); 

      if (null != tree) 
       _walk(tree, nounPhrases); 
     } 

     foreach (var nounPhrase in nounPhrases) 
      Console.WriteLine(
       "{0} ({1})", 
       nounPhrase.Phrase, 
       string.Join(", ", nounPhrase.Tags) 
       ); 
    } 

    private void _walk(Tree tree, IList<NamedNounPhrase> nounPhrases) 
    { 
     if ("NP" == tree.value()) 
     { 
      var nounPhrase = new NamedNounPhrase(); 

      foreach (Tree leaf in tree.getLeaves().toArray()) 
      { 
       var label = (CoreLabel) leaf.label(); 
       nounPhrase.Phrase += (string) label.get(new CoreAnnotations.TextAnnotation().getClass()) + " "; 
       nounPhrase.Tags.Add((string) label.get(new CoreAnnotations.NamedEntityTagAnnotation().getClass())); 
      } 

      nounPhrases.Add(nounPhrase); 
     } 
     else 
     { 
      foreach (var child in tree.children()) 
      { 
       _walk(child, nounPhrases); 
      } 
     } 
    } 

。希望幫助!

相關問題