2017-04-17 4 views
2

名前付きエンティティに対して何らかの出力を与えるために、ついにTokensRegexコードを取得できました。しかし、出力は私が欲しいものではありません。私はルールには微調整が必​​要だと思う。TokensRegexは、名前付きエンティティの正しい出力を得るためのルールを提供します。

ここでは、コードです:

public static void main(String[] args) 
    { 
     String rulesFile = "D:\\Workspace\\resource\\NERRulesFile.rules.txt"; 
     String dataFile = "D:\\Workspace\\data\\GoldSetSentences.txt"; 

     Properties props = new Properties(); 
     props.put("annotators", "tokenize, ssplit, pos, lemma, ner"); 
     props.setProperty("ner.useSUTime", "0"); 
     StanfordCoreNLP pipeline = new StanfordCoreNLP(props); 
     pipeline.addAnnotator(new TokensRegexAnnotator(rulesFile)); 
     String inputText = "Bill Edelman, CEO and chairman of Paragonix Inc. announced that the company is expanding it's operations in China."; 

     Annotation document = new Annotation(inputText); 
     pipeline.annotate(document); 
     Env env = TokenSequencePattern.getNewEnv(); 
     env.setDefaultStringMatchFlags(NodePattern.CASE_INSENSITIVE); 
     env.setDefaultStringPatternFlags(Pattern.CASE_INSENSITIVE); 
     List<CoreMap> sentences = document.get(SentencesAnnotation.class); 
     CoreMapExpressionExtractor extractor = CoreMapExpressionExtractor.createExtractorFromFiles(env, rulesFile); 

     /* Next we can go over the annotated sentences and extract the annotated words, 
     Using the CoreLabel Object */ 
     for (CoreMap sentence : sentences) 
     { 

      List<MatchedExpression> matched = extractor.extractExpressions(sentence); 

      for(MatchedExpression phrase : matched){ 

       // Print out matched text and value 
       System.out.println("matched: " + phrase.getText() + " with value: " + phrase.getValue()); 
       // Print out token information 
       CoreMap cm = phrase.getAnnotation(); 
       for (CoreLabel token : cm.get(TokensAnnotation.class)) 
       { 
        if (token.tag().equals("NNP")){ 
         String leftContext = token.before(); 
         String rightContext = token.after(); 
         System.out.println(leftContext); 
         System.out.println(rightContext); 


         String word = token.get(TextAnnotation.class); 
         String lemma = token.get(LemmaAnnotation.class); 
         String pos = token.get(PartOfSpeechAnnotation.class); 
         String ne = token.get(NamedEntityTagAnnotation.class); 
         System.out.println("matched token: " + "word="+word + ", lemma="+lemma + ", pos=" + pos + "ne=" + ne); 
        } 

       } 
      } 
     } 
    } 
} 

そして、ここでは、ルール・ファイルがあります:

$TITLES_CORPORATE = (/chief/ /administrative/ /officer/|cao|ceo|/chief/ /executive/ /officer/|/chairman/|/vice/ /president/) 
$ORGANIZATION_TITLES = (/International/|/inc\./|/corp/|/llc/) 

# For detecting organization names like 'Paragonix Inc.' 

{ ruleType: "tokens", 
    pattern: ([{pos: NNP}]+ $ORGANIZATION_TITLES), 
    action: (Annotate($0, ner, "ORGANIZATION"),Annotate($1, ner, "ORGANIZATION")) 
} 

# For extracting organization names from a pattern - 'Genome International is planning to expand its operations in China.' 
#(in the sentence given above the words planning and expand are part of the $OrgContextWords macros) 
{ 
    ruleType: "tokens", 
    pattern: (([{tag:/NNP.*/}]+) /,/*? /is|had|has|will|would/*? /has|had|have|will/*? /be|been|being/*? (?:[]{0,5}[{lemma:$OrgContextWords}]) /of|in|with|for|to|at|like|on/*?), 
    result: (Annotate($1, ner, "ORGANIZATION")) 
} 

# For sentence like - Bill Edelman, Chairman and CEO of Paragonix Inc./ Zuckerberg CEO Facebook said today.... 

ENV.defaults["stage"] = 1 
{ 
    pattern: ($TITLES_CORPORATE), 
    action: (Annotate($1, ner, "PERSON_TITLE")) 
} 

ENV.defaults["stage"] = 2 
{ 
    ruleType: "tokens", 
    pattern: (([ { pos:NNP} ]+) /,/*? (?:TITLES_CORPORATE)? /and|&/*? (?:TITLES_CORPORATE)? /,/*? /of|for/? /,/*? [ { pos:NNP } ]+), 
    result: (Annotate($1, ner, "PERSON"),Annotate($2, ner, "ORGANIZATION")) 
} 

私が手出力は次のようになります。

matched: Paragonix Inc. announced that the company is expanding with 
value: LIST([LIST([ORGANIZATION, ORGANIZATION])]) 
matched token: word=Paragonix, lemma=Paragonix, pos=NNPne=PERSON 
matched token: word=Inc., lemma=Inc., pos=NNP, ne=ORGANIZATION 

私は期待していた出力は次のようになります。

matched: Paragonix Inc. announced that the company is expanding with 
value: LIST([LIST([ORGANIZATION, ORGANIZATION])]) 
matched token: word=Paragonix, lemma=Paragonix, pos=NNPne=ORGANIZATION 
matched token: word=Inc., lemma=Inc., pos=NNP, ne=ORGANIZATION 

また、ビル・エデルマンは人物ではありません。ビル・エデルマンを含むフレーズは特定されていませんが、私はそのためのルールを持っています。どのフレーズを逃してしまわないように、フレーズ全体が各ルールにマッチするようにルールを準備する必要がありますか?

答えて

1
  1. メインのGitHubページ(4月14日現在)に最新のスタンフォードCoreNLPを表すjarファイルを作成しました。 (最新のコード付き)

  2. このコマンドは(Java APIを使用している場合、代わりtokensregex設定は、Propertiesオブジェクトに渡すことができます)TokensRegexAnnotatorを使用してのために働く必要があります。ここでは

    java -Xmx8g edu.stanford.nlp.pipeline.StanfordCoreNLP -annotators tokenize,ssplit,pos,lemma,ner,tokensregex -tokensregex.rules example.rules -tokensregex.caseInsensitive -file example.txt -outputFormat text 
    
  3. がルールです$0は、パターン全体を意味

    ner = { type: "CLASS", value: "edu.stanford.nlp.ling.CoreAnnotations$NamedEntityTagAnnotation" } 
    
    $ORGANIZATION_TITLES = "/inc\.|corp\./" 
    
    $COMPANY_INDICATOR_WORDS = "/company|corporation/" 
    
    { pattern: (([{pos: NNP}]+ $ORGANIZATION_TITLES) /is/ /a/ $COMPANY_INDICATOR_WORDS), action: (Annotate($1, ner, "RULE_FOUND_ORG")) } 
    
    { pattern: ($COMPANY_INDICATOR_WORDS /that/ ([{pos: NNP}]+) /works/ /for/), action: (Annotate($1, ner, "RULE_FOUND_PERSON")) } 
    

    注こと、及び$1は最初のキャプチャグループを意味します。ファイル私は、ショーは文型をもとにマッチングすることを書きました。この例では、私が一致させたいものを表すテキストの周りに余分なカッコを入れています。

    Iは、例えば上でこれを実行した:Paragonix Inc. is a company that Joe Smith works for.

    この例では、第二ラウンドの最初のラウンドからの抽出を用いて示す:この例では、文Joe Smith works for Paragonix Inc.で正しく動作するはず

    ner = { type: "CLASS", value: "edu.stanford.nlp.ling.CoreAnnotations$NamedEntityTagAnnotation" } 
    
    $ORGANIZATION_TITLES = "/inc\.|corp\./" 
    
    $COMPANY_INDICATOR_WORDS = "/company|corporation/" 
    
    ENV.defaults["stage"] = 1 
    
    { pattern: (/works/ /for/ ([{pos: NNP}]+ $ORGANIZATION_TITLES)), action: (Annotate($1, ner, "RULE_FOUND_ORG")) } 
    
    ENV.defaults["stage"] = 2 
    
    { pattern: (([{pos: NNP}]+) /works/ /for/ [{ner: "RULE_FOUND_ORG"}]), action: (Annotate($1, ner, "RULE_FOUND_PERSON")) } 
    

+0

ありがとうございます!私はこれを修正し、ルールファイルを動作させることができました。しかし、私はここに尋ねられた別の関連する問題に固執しています - http://stackoverflow.com/questions/43521697/getting-output-in-the-desired-format-using-tokenregexこれに答えるのを手伝ってもらえますか?再度、感謝します! – serendipity

関連する問題