#python #nlp #spacy
#python #nlp #spacy
Вопрос:
Я использую SpaCy для получения именованных объектов. Однако он всегда неправильно помечает символы новой строки как именованные объекты.
Ниже приведен текст ввода.
mytxt = """<?xml version="1.0"?>
<nitf>
<head>
<title>KNOW YOUR ROLE ON SUPER BOWL LIII.</title>
</head>
<body>
<body.head>
<hedline>
<hl1>KNOW YOUR ROLE ON SUPER BOWL LIII.</hl1>
</hedline>
<distributor>Gale Group</distributor>
</body.head>
<body.content>
<p>Montpelier: <org>Department of Motor Vehicles</org>, has issued the following
news release:</p>
<p>Be a designated sober driver, help save lives. Remember these tips
on game night:</p>
<p>Know your Stateamp;apos;s laws: refusing to take a breath test in many
jurisdictions could result in arrest, loss of your driveramp;apos;s
license, and impoundment of your vehicle. Not to mention the
embarrassment in explaining your situation to family, friends, and
employers.</p>
<p>In case of any query regarding this article or other content needs
please contact: <a href="mailto:editorial@plusmediasolutions.com">editorial@plusmediasolutions.com</a></p>
</body.content>
</body>
</nitf>
"""
Ниже приведен мой код:
CONTENT_XML_TAG = ('p', 'ul', 'h3', 'h1', 'h2', 'ol')
soup = BeautifulSoup(mytxt, 'xml')
spacy_model = spacy.load('en_core_web_sm')
content = "n".join([p.get_text() for p in soup.find('body.content').findAll(CONTENT_XML_TAG)])
print(content)
section_spacy = spacy_model(content)
tokenized_sentences = []
for sent in section_spacy.sents:
tokenized_sentences.append(sent)
for s in tokenized_sentences:
labels = [(ent.text, ent.label_) for ent in s.ents]
print(Counter(labels))
Распечатка:
Counter({('n', 'GPE'): 2, ('Department of Motor Vehicles', 'ORG'): 1})
Counter({('n', 'GPE'): 1})
Counter({('n', 'GPE'): 2, ('State', 'ORG'): 1})
Counter({('n', 'GPE'): 3})
Counter({('n', 'GPE'): 1})
Я не могу поверить, что SpaCy имеет такую неправильную классификацию. Я что-нибудь пропустил?
Ответ №1:
from bs4 import BeautifulSoup
import spacy
CONTENT_XML_TAG = ('p', 'ul', 'h3', 'h1', 'h2', 'ol')
soup = BeautifulSoup(mytxt, 'xml')
spacy_model = spacy.load('en_core_web_sm')
content = "n".join([p.get_text() for p in soup.find('body.content').findAll(CONTENT_XML_TAG)])
section_spacy = spacy_model(content)
def remove_whitespace_entities(doc):
doc.ents = [e for e in doc.ents if not e.text.isspace()]
return doc
spacy_model.add_pipe(remove_whitespace_entities, after='ner')
doc = spacy_model(content)
print(doc.ents)
Комментарии:
1. Одно обновление, которое, возможно, стоит добавить к вашему комментарию: в только что выпущенной версии spaCy v2.1 распознавателю сущностей теперь «запрещено» предсказывать чистые маркеры пробелов как объекты. Таким образом, это должно устранить необходимость в
remove_whitespace_entities
взломе. (Конечно, для этого конкретного примера предварительная обработка для удаления HTML-тегов, вероятно, все еще является хорошей идеей.)