【问题标题】:How to find the index of named entity in stanford nlp如何在 stanford nlp 中找到命名实体的索引
【发布时间】:2017-08-24 09:41:46
【问题描述】:

我正在为 stanford nlp 使用 python 包装器 查找命名实体的代码是:

sentence = "Mr. Jhon was noted to have a cyst at his visit back in 2011."
result = nlp.ner(sentence)

for ne in result:
  if ne[1] == 'PERSON':
     print(ne)

输出是列表类型的结果: (u'Jhon', u'PERSON')

但它没有像 spaCy 或其他 nlp 工具那样给出命名实体的索引,它给出了带有索引的结果。

>> namefinder = NameFinder.getNameFinder("spaCy")
>> entities = namefinder.find(sentences)
List(List((PERSON,0,13), (DURATION,15,27), (DATE,76,83)),
  List((PERSON,4,10),  (LOCATION,77,86), (ORGANIZATION,26,39)),
  List((PERSON,0,13), (DURATION,16,28), (ORGANIZATION,52,80)))

【问题讨论】:

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


    【解决方案1】:

    我为此使用nltk。我改编了here 的答案。关键是调用 use WordPunctTokenizer 和方法 span_tokenize() 来生成一个单独的列表,我称之为 spans ,它保持每个令牌的跨度。

    from nltk.tag import StanfordNERTagger
    from nltk.tokenize import WordPunctTokenizer
    
    # Initialize Stanford NLP with the path to the model and the NER .jar
    st = StanfordNERTagger(r"C:\stanford-corenlp\stanford-ner\classifiers\english.all.3class.distsim.crf.ser.gz",
           r"C:\stanford-corenlp\stanford-ner\stanford-ner.jar",
           encoding='utf-8')
    
    sentence = "Mr. Jhon was noted to have a cyst at his visit back in 2011."
    
    tokens = WordPunctTokenizer().tokenize(sentence)
    
    # We have to compute the token spans in a separate list
    # Notice that span_tokenize(sentence) returns a generator 
    spans = list(WordPunctTokenizer().span_tokenize(sentence))
    
    # enumerate will help us keep track of the token index in the token lists 
    for i, ner in enumerate(st.tag(tokens)):
        if ner[1] == "PERSON":
            print spans[i], ner
    

    【讨论】:

      猜你喜欢
      • 2018-03-08
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-12
      • 2022-07-07
      • 1970-01-01
      • 2018-03-26
      相关资源
      最近更新 更多