Nltk: CoreNLPParser tag()は、プロパティのオーバーロードを許可する必要があります

作成日 2018年09月10日  ·  3コメント  ·  ソース: nltk/nltk

現在のCoreNLPParser.tag()では、StanfordCoreNLPによる「再トークン化」は予想外です。

>>> from nltk.parse.corenlp import CoreNLPParser
>>> ner_tagger = CoreNLPParser(url='http://localhost:9000', tagtype='ner')
>>> sent = ['my', 'phone', 'number', 'is', '1111', '1111', '1111']
>>> ner_tagger.tag(sent)
[('my', 'O'),
 ('phone', 'O'),
 ('number', 'O'),
 ('is', 'O'),
 ('1111\xa01111\xa01111', 'NUMBER')]

予想される動作は次のとおりです。

>>> from nltk.parse.corenlp import CoreNLPParser
>>> ner_tagger = CoreNLPParser(url='http://localhost:9000', tagtype='ner')
>>> sent = ['my', 'phone', 'number', 'is', '1111', '1111', '1111']
>>> ner_tagger.tag(sent)
[('my', 'O'), ('phone', 'O'), ('number', 'O'), ('is', 'O'), ('1111', 'DATE'), ('1111', 'DATE'), ('1111', 'DATE')]

提案されている解決策は、 .tag().tag_sents() properties引数のオーバーロードを許可することです。つまり、 https://github.com/nltk/nltk/blob/develop/nltk/parse/ corenlp.py#L348で、デフォルトではproperties = {'tokenize.whitespace':'true'}使用します。これは、トークンをtag_sents()スペースで連結しているためです。


    def tag_sents(self, sentences, properties=None):
        """
        Tag multiple sentences.

        Takes multiple sentences as a list where each sentence is a list of
        tokens.

        :param sentences: Input sentences to tag
        :type sentences: list(list(str))
        :rtype: list(list(tuple(str, str))
        """
        # Converting list(list(str)) -> list(str)
        sentences = (' '.join(words) for words in sentences)
        if properties == None:
            properties = {'tokenize.whitespace':'true'}
        return [sentences[0] for sentences in self.raw_tag_sents(sentences, properties)]

    def tag(self, sentence, properties=None):
        """
        Tag a list of tokens.

        :rtype: list(tuple(str, str))

        >>> parser = CoreNLPParser(url='http://localhost:9000', tagtype='ner')
        >>> tokens = 'Rami Eid is studying at Stony Brook University in NY'.split()
        >>> parser.tag(tokens)
        [('Rami', 'PERSON'), ('Eid', 'PERSON'), ('is', 'O'), ('studying', 'O'), ('at', 'O'), ('Stony', 'ORGANIZATION'),
        ('Brook', 'ORGANIZATION'), ('University', 'ORGANIZATION'), ('in', 'O'), ('NY', 'O')]

        >>> parser = CoreNLPParser(url='http://localhost:9000', tagtype='pos')
        >>> tokens = "What is the airspeed of an unladen swallow ?".split()
        >>> parser.tag(tokens)
        [('What', 'WP'), ('is', 'VBZ'), ('the', 'DT'),
        ('airspeed', 'NN'), ('of', 'IN'), ('an', 'DT'),
        ('unladen', 'JJ'), ('swallow', 'VB'), ('?', '.')]
        """
        return self.tag_sents([sentence], properties)[0]

    def raw_tag_sents(self, sentences, properties=None):
        """
        Tag multiple sentences.

        Takes multiple sentences as a list where each sentence is a string.

        :param sentences: Input sentences to tag
        :type sentences: list(str)
        :rtype: list(list(list(tuple(str, str)))
        """
        default_properties = {'ssplit.isOneSentence': 'true',
                              'annotators': 'tokenize,ssplit,' }

        default_properties.update(properties or {})

        # Supports only 'pos' or 'ner' tags.
        assert self.tagtype in ['pos', 'ner']
        default_properties['annotators'] += self.tagtype
        for sentence in sentences:
            tagged_data = self.api_call(sentence, properties=default_properties)
            yield [[(token['word'], token[self.tagtype]) for token in tagged_sentence['tokens']]
                    for tagged_sentence in tagged_data['sentences']]

これにより、ユーザーが入力した文字列トークンのリストが適用されます。

https://stackoverflow.com/questions/52250268/why-do-corenlp-ner-tagger-and-ner-tagger-join-the-separated-numbers-togetherの詳細

.tag()raw_tag_sents前にプロパティをオーバーロードできるようにすると、ユーザーは#1876のようなケースを簡単に処理できるようになります。

bug goodfirstbug stanford api

最も参考になるコメント

いいね。

ちょっとしたコメント。 それはする必要がありますif properties is None 、ないif properties == Noneassert self.tagtype in ['pos', 'ner']assert self.tagtype in ['pos', 'ner'], "CoreNLP tagger supports only 'pos' or 'ner' tags."必要があります。

文字列を結合して分割するというアイデアはあまり好きではありません。単純な文字列ではなく、単語のリストを文としてCoreNLPに渡す方法があるかもしれません。

全てのコメント3件

いいね。

ちょっとしたコメント。 それはする必要がありますif properties is None 、ないif properties == Noneassert self.tagtype in ['pos', 'ner']assert self.tagtype in ['pos', 'ner'], "CoreNLP tagger supports only 'pos' or 'ner' tags."必要があります。

文字列を結合して分割するというアイデアはあまり好きではありません。単純な文字列ではなく、単語のリストを文としてCoreNLPに渡す方法があるかもしれません。

こんにちは、これを私の最初の問題として取り上げたいと思います。

あなたがこの問題に興味を持っているのは素晴らしいことです。 ご不明な点がございましたら、こちらからお問い合わせください。

このページは役に立ちましたか?
0 / 5 - 0 評価