【问题标题】:First and Last name tagged as one token by using Stanford NER with Python通过使用带有 Python 的斯坦福 NER 将名字和姓氏标记为一个标记
【发布时间】:2016-09-12 16:41:02
【问题描述】:

我正在使用带有 Python 的斯坦福命名实体识别器来查找小说“一百年的孤独”中的专有名称。其中有许多由名字和姓氏组成,例如“奥雷里亚诺布恩迪亚”或“圣索非亚德拉彼达”。这些令牌总是分开的,例如“Aureliano”“Buendia”,因为我正在使用标记器。 我想将它们作为令牌放在一起,这样它们就可以与斯坦福 NER 一起标记为“PERSON”。

我写的代码:

import nltk

from nltk.tag import StanfordNERTagger

from nltk import word_tokenize

from nltk import FreqDist

sentence1 = open('book1.txt').read()

sentence = sentence1.split()

path_to_model = "C:\Python34\stanford-ner-2015-04-20\classifiers\english.muc.7class.distsim.crf.ser"

path_to_jar = "C:\Python34\stanford-ner-2015-04-20\stanford-ner.jar"

st = StanfordNERTagger(model_filename=path_to_model, path_to_jar=path_to_jar)

taggedSentence = st.tag(sentence)

def findtags (tagged_text,tag_prefix):

    cfd = nltk.ConditionalFreqDist((tag, word) for (word, tag) in taggedSentence

                                   if tag.endswith(tag_prefix))

    return dict((tag, cfd[tag].most_common(1000)) for tag in cfd.conditions())


print (findtags('_','PERSON'))

结果如下:

{'PERSON': [('Aureliano', 397), ('José', 294), ('Arcadio', 286), ('Buendía', 251), ...

有人有解决办法吗?我会很感激的

【问题讨论】:

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


    【解决方案1】:
    import nltk
    
    from nltk.tag import StanfordNERTagger
    
    sentence1 = open('book1.txt').read()
    
    sentence = sentence1.split()
    
    path_to_model = "C:\Python34\stanford-ner-2015-04-20\classifiers\english.muc.7class.distsim.crf.ser"
    
    path_to_jar = "C:\Python34\stanford-ner-2015-04-20\stanford-ner.jar"
    
    st = StanfordNERTagger(model_filename=path_to_model, path_to_jar=path_to_jar)
    
    taggedSentence = st.tag(sentence)
    
    test = [] 
    
    test_dict = {}
    
    for element in range(len(taggedSentence)):
    
        a = ''
    
        if element < len(taggedSentence):
           while taggedSentence[element][1] == 'PERSON':
              a += taggedSentence[element][0] + ' '
              taggedSentence.pop(element)
              if len(a) > 1:
                 test.append(a.strip())
    
    test_dict[data.split('.')[0]] = tuple(test)
    
    print(test_dict)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多