提供自己的自定义标记以在nltk中标记数据?

时间:2018-04-19 12:48:54

标签: python nlp nltk spacy pos-tagger

运行以下代码

from nltk import word_tokenize, pos_tag, ne_chunk


sentence = "Antacids is given to Jhon,Sodium Bicarbonate is given to Carl,Folic Acid to Jeery  all works at Google " 
print(ne_chunk(pos_tag(word_tokenize(sentence))))

我得到了这个输出

(S
  (GPE Antacids/NNP)
  is/VBZ
  given/VBN
  to/TO
  (PERSON Jhon/NNP)
  ,/,
  (PERSON Sodium/NNP Bicarbonate/NNP)
  is/VBZ
  given/VBN
  to/TO
  (GPE Carl/NNP)
  ,/,
  (PERSON Folic/NNP Acid/NNP)
  to/TO
  (GPE Jeery/NNP)
  all/DT
  works/NNS
  at/IN
  (ORGANIZATION Google/NNP))

我想将(Antacid,Sodium,Folic)等药物分配到同一类别。

我可以为此目的使用哪个库?

1 个答案:

答案 0 :(得分:1)

你想在文字中留下一些拼写错误吗?例如 Jhon John Jeery Jerry ,普通名词的大写字母( Sodium Bicarbonate < / EM>  将是碳酸氢钠与较低的情况..)?

python库中的嵌入式NER(命名实体识别)使用干净的文本进行训练,因为你有一个拼写错误的文本,使用通用的NER很难达到100%的准确度。

使用正确的句子和spacy库,您可以获得正确的输出:

import spacy

nlp = spacy.load('en')
doc = nlp("Antacids is given to John, sodium bicarbonate is given to Carl, folic acid to Jerry all works at Google")

for token in doc:
    print('token.i: {2}\ttoken.idx: {0}\ttoken.pos: {3:10}token.text: {1}'.
          format(token.idx, token.text, token.i, token.pos_)

print('Entities', [(e.text, e.label_) for e in doc.ents])

结果(抗酸剂,碳酸氢钠和酸被标记为NOUN):

token.i: 0  token.idx: 0    token.pos: NOUN      token.text: Antacids
token.i: 1  token.idx: 9    token.pos: VERB      token.text: is
token.i: 2  token.idx: 12   token.pos: VERB      token.text: given
token.i: 3  token.idx: 18   token.pos: ADP       token.text: to
token.i: 4  token.idx: 21   token.pos: PROPN     token.text: John
token.i: 5  token.idx: 25   token.pos: PUNCT     token.text: ,
token.i: 6  token.idx: 27   token.pos: NOUN      token.text: sodium
token.i: 7  token.idx: 34   token.pos: NOUN      token.text: bicarbonate
token.i: 8  token.idx: 46   token.pos: VERB      token.text: is
token.i: 9  token.idx: 49   token.pos: VERB      token.text: given
token.i: 10 token.idx: 55   token.pos: ADP       token.text: to
token.i: 11 token.idx: 58   token.pos: PROPN     token.text: Carl
token.i: 12 token.idx: 62   token.pos: PUNCT     token.text: ,
token.i: 13 token.idx: 64   token.pos: ADJ       token.text: folic
token.i: 14 token.idx: 70   token.pos: NOUN      token.text: acid
token.i: 15 token.idx: 75   token.pos: ADP       token.text: to
token.i: 16 token.idx: 78   token.pos: PROPN     token.text: Jerry
token.i: 17 token.idx: 84   token.pos: DET       token.text: all
token.i: 18 token.idx: 88   token.pos: VERB      token.text: works
token.i: 19 token.idx: 94   token.pos: ADP       token.text: at
token.i: 20 token.idx: 97   token.pos: PROPN     token.text: Google

并且实体已正确标记:

Entities [('John', 'PERSON'), ('Carl', 'PERSON'), ('Jerry', 'PERSON'), ('Google', 'ORG')]