【问题标题】:How to clean sentences for StanfordNER如何为 StanfordNER 清理句子
【发布时间】:2016-01-26 15:07:49
【问题描述】:

我想在 python 中使用StanfordNER 来检测命名实体。我应该如何清理句子?

例如,考虑

qry="In the UK, the class is relatively crowded with Zacc competing with Abc's Popol (market leader) and Xyz's Abcvd."

如果我愿意

st = StanfordNERTagger('english.all.3class.distsim.crf.ser.gz') 
print st.tag(qry.split())

我明白了

[
    (u'In', u'O'), (u'the', u'O'), (u'UK,', u'O'), (u'the', u'O'), 
    (u'class', u'O'), (u'is', u'O'), (u'relatively', u'O'), (u'crowded', u'O'), 
    (u'with', u'O'), (u'Zacc', u'PERSON'), (u'competing', u'O'), (u'with', u'O'), 
    (u"Abc's", u'O'), (u'Popol', u'O'), (u'(market', u'O'), (u'leader)', u'O'), 
    (u'and', u'O'), (u"Xyz's", u'O'), (u'Abcvd.', u'O')
]

`

所以只检测到 1 个命名实体。但是,如果我通过用空格替换所有特殊字符来进行一些清理

qry="In the UK the class is relatively crowded with Zacc competing with Abc s Popol market leader and Xyz s Abcvd"

我明白了

[
    (u'In', u'O'), (u'the', u'O'), (u'UK', u'LOCATION'), (u'the', u'O'), 
    (u'class', u'O'), (u'is', u'O'), (u'relatively', u'O'), (u'crowded', u'O'), 
    (u'with', u'O'), (u'Zacc', u'PERSON'), (u'competing', u'O'), (u'with', u'O'), 
    (u'Abc', u'ORGANIZATION'), (u's', u'O'), (u'Popol', u'PERSON'), (u'market', u'O'), 
    (u'leader', u'O'), (u'and', u'O'), (u'Xyz', u'ORGANIZATION'), (u's', u'O'), (u'Abcvd', u'PERSON')]

`

很明显,这样比较合适。有没有关于如何清理StanfordNER 的句子的一般规则?最初我认为根本不需要清理!

【问题讨论】:

    标签: python nlp nltk stanford-nlp named-entity-recognition


    【解决方案1】:

    您可以根据自己的目的使用 Stanford Tokenizer。 您可以使用下面的代码。

    from nltk.tokenize.stanford import StanfordTokenizer
    token = StanfordTokenizer('stanford-ner-2014-06-16/stanford-ner.jar')
    qry="In the UK, the class is relatively crowded with Zacc competing with Abc's Popol (market leader) and  Xyz's Abcvd."
    tok = token.tokenize(qry)
    print tok
    

    您将获得所需的令牌。

    [你在',
    你的,
    你'英国',
    你',',
    你的,
    你'类',
    你是',
    你'相对',
    你'拥挤',
    你'与',
    你'扎克',
    你'竞争',
    你'与',
    u'Abc',
    你“的”,
    u'Popol',
    u'-LRB-',
    你'市场',
    你'领导',
    u'-RRB-',
    你和',
    u'Xyz',
    你“的”,
    u'Abcvd',
    你'。']

    【讨论】:

      【解决方案2】:

      您应该确保您正在对句子进行标记——这是第一次调用(您使用 qry.split() 隐式错误标记错误)与您手动标记的第二次调用(例如,posessive @ 987654323@ 作为自己的令牌)。斯坦福does have a tokenizer,这是训练NER系统的标记器,尽管我不是如何从Python调用它的专家。只是不拆分句子为您标记它吗?

      【讨论】:

        【解决方案3】:

        请在处理文本之前对文本进行词标记。另外,请注意,大多数注释系统都是从句子中训练出来的,因此您可以在词标记化之前进行句子标记化。

        alvas@ubi:~$ export STANFORDTOOLSDIR=$HOME
        alvas@ubi:~$ export CLASSPATH=$STANFORDTOOLSDIR/stanford-ner-2015-12-09/stanford-ner.jar
        alvas@ubi:~$ export STANFORD_MODELS=$STANFORDTOOLSDIR/stanford-ner-2015-12-09/classifiers
        alvas@ubi:~$ python
        Python 2.7.11 (default, Dec 15 2015, 16:46:19) 
        [GCC 4.8.4] on linux2
        Type "help", "copyright", "credits" or "license" for more information.
        >>> from nltk import word_tokenize
        >>> from nltk.tag import StanfordNERTagger
        >>> from nltk.internals import find_jars_within_path
        >>> st = StanfordNERTagger('english.all.3class.distsim.crf.ser.gz')
        >>> stanford_dir = st._stanford_jar.rpartition('/')[0]
        >>> stanford_jars = find_jars_within_path(stanford_dir)
        >>> st._stanford_jar = ':'.join(stanford_jars)
        >>> 
        >>> text = "In the UK, the class is relatively crowded with Zacc competing with Abc's Popol (market leader) and  Xyz's Abcvd."
        >>> text = word_tokenize(text)
        >>> text
        ['In', 'the', 'UK', ',', 'the', 'class', 'is', 'relatively', 'crowded', 'with', 'Zacc', 'competing', 'with', 'Abc', "'s", 'Popol', '(', 'market', 'leader', ')', 'and', 'Xyz', "'s", 'Abcvd', '.']
        >>> st.tag(text)
        [(u'In', u'O'), (u'the', u'O'), (u'UK', u'LOCATION'), (u',', u'O'), (u'the', u'O'), (u'class', u'O'), (u'is', u'O'), (u'relatively', u'O'), (u'crowded', u'O'), (u'with', u'O'), (u'Zacc', u'PERSON'), (u'competing', u'O'), (u'with', u'O'), (u'Abc', u'PERSON'), (u"'s", u'O'), (u'Popol', u'O'), (u'(', u'O'), (u'market', u'O'), (u'leader', u'O'), (u')', u'O'), (u'and', u'O'), (u'Xyz', u'ORGANIZATION'), (u"'s", u'O'), (u'Abcvd', u'O'), (u'.', u'O')]
        

        【讨论】:

          猜你喜欢
          • 2011-11-09
          • 1970-01-01
          • 2016-04-29
          • 1970-01-01
          • 2014-07-11
          • 1970-01-01
          • 2021-03-18
          • 1970-01-01
          • 2017-10-19
          相关资源
          最近更新 更多