【问题标题】:How to get POS tagging using Stanford Parser如何使用 Stanford Parser 获取 POS 标记
【发布时间】:2010-09-17 08:01:24
【问题描述】:

我正在使用 Stanford Parser 来解析单词对之间的依赖关系,但我还需要单词的标记。但是,在 ParseDemo.java 中,程序只输出了标记树。我需要这样的每个单词的标记:

My/PRP$ dog/NN also/RB likes/VBZ eating/VBG bananas/NNS ./.

不是这样的:

(ROOT
  (S
    (NP (PRP$ My) (NN dog))
    (ADVP (RB also))
    (VP (VBZ likes)
      (S
        (VP (VBG eating)
          (S
            (ADJP (NNS bananas))))))
    (. .)))

谁能帮帮我?非常感谢。

【问题讨论】:

    标签: nlp stanford-nlp


    【解决方案1】:

    如果您主要对在程序中操作标签感兴趣,并且不需要TreePrint 功能,您可以将标签词作为列表获取:

    LexicalizedParser lp =
      LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
    Tree parse = lp.apply(Arrays.asList(sent));
    List taggedWords = parse.taggedYield();    
    

    【讨论】:

    • @ChristopherManning 如何在 python 中实现这一点
    【解决方案2】:

    在命令行运行 edu.stanford.nlp.parser.lexparser.LexicalizedParser 时,你想使用:

    -outputFormat "wordsAndTags"
    

    以编程方式,使用由 formatString="wordsAndTags" 构造的 TreePrint 类并调用 printTree,如下所示:

    TreePrint posPrinter = new TreePrint("wordsAndTags", yourPrintWriter);
    posPrinter.printTree(yourLexParser.getBestParse());
    

    【讨论】:

      【解决方案3】:
      String[] sent = { "This", "is", "an", "easy", "sentence", "." };
      List<CoreLabel> rawWords = Sentence.toCoreLabelList(sent);
      Tree parse = lp.apply(rawWords);
      ArrayList ar=parse.taggedYield();
      System.out.println(ar.toString());
      

      【讨论】:

      • 这里的lp 是什么?你错过了解释它。
      • @talha06: 我猜这是一个 LexicalizedParser
      • 是的,它是一个 LexicalizedParser。
      【解决方案4】:

      这个答案有点过时,所以我决定添加自己的答案。因此,使用 Stanford Parser 3.6.0 版(maven 依赖项):

          <dependency>
             <groupId>edu.stanford.nlp</groupId>
             <artifactId>stanford-parser</artifactId>
             <version>3.6.0</version>
          </dependency>
          <dependency>
              <groupId>edu.stanford.nlp</groupId>
              <artifactId>stanford-corenlp</artifactId>
              <version>3.6.0</version>
          </dependency>
          <dependency>
              <groupId>edu.stanford.nlp</groupId>
              <artifactId>stanford-corenlp</artifactId>
              <version>3.6.0</version>
              <classifier>models</classifier>
          </dependency>
      

            private static MaxentTagger tagger = new MaxentTagger(MaxentTagger.DEFAULT_JAR_PATH);
            public String getTaggedString(String someString) {
      
                  String taggedString = tagger.tagString(someString);
                  return taggedString;
            }
      

      这将返回I_PRP claim_VBP the_DT rights_NNS'I claim the rights'

      因此,如果您想使用 java 和 stanford 解析器检测短语中的动词,您可以这样做:

      public boolean containsVerb(String someString) {
              String taggedString = tagger.tagString(someString);
              String[] tokens = taggedString.split(" ");
              for (String tok : tokens){
                  String[] taggedTokens = tok.split("_");
                  if (taggedTokens[1].startsWith("VB")){
                      return true;
                  }
      
              }
              return false;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-05-23
        • 1970-01-01
        • 2016-09-19
        • 1970-01-01
        • 1970-01-01
        • 2012-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多