如何使用斯坦福解析器获得词性标记

发布于 2024-09-24 00:28:50 字数 415 浏览 9 评论 0原文

我正在使用斯坦福解析器来解析单词对之间的依赖关系,但我还需要单词的标签。然而,在ParseDemo.java中,程序只输出Tagging Tree。我需要每个单词的标签如下:

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))))))
    (. .)))

谁能帮助我?多谢。

I'm using Stanford Parser to parse the dependence relations between pair of words, but I also need the tagging of words. However, in the ParseDemo.java, the program only output the Tagging Tree. I need each word's tagging like this:

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

not like this:

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

Who can help me? thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冬天的雪花 2024-10-01 00:28:51

在命令行上运行 edu.stanford.nlp.parser.lexparser.LexicalizedParser 时,您需要使用:

-outputFormat "wordsAndTags"

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

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

When running edu.stanford.nlp.parser.lexparser.LexicalizedParser on the command line, you want to use:

-outputFormat "wordsAndTags"

Programatically, use the TreePrint class constructed with formatString="wordsAndTags" and call printTree, like this:

TreePrint posPrinter = new TreePrint("wordsAndTags", yourPrintWriter);
posPrinter.printTree(yourLexParser.getBestParse());
清醇 2024-10-01 00:28:51
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());
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());
行雁书 2024-10-01 00:28:51

这个答案有点过时了,所以我决定添加我自己的答案。因此,对于斯坦福解析器版本 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_PRPclaim_VBP the_DTrights_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;
}

This answer is a bit outdated so I decided to add my own. So with Stanford Parser version 3.6.0 (maven dependencies):

    <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;
      }

This will return I_PRP claim_VBP the_DT rights_NNS
for 'I claim the rights'

So If you want to detect verbs in a phrase using java and stanford parser you can do this:

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;
}
蓬勃野心 2024-10-01 00:28:50

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

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

If you're mainly interested in manipulating the tags in a program, and don't need the TreePrint functionality, you can just get the tagged words as a List:

LexicalizedParser lp =
  LexicalizedParser.loadModel("edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz");
Tree parse = lp.apply(Arrays.asList(sent));
List taggedWords = parse.taggedYield();    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文