例如,有一个文本(以文档的形式)与人名"John“一起给出。我们需要从文本中提取所有提到约翰的句子。
发布于 2019-03-22 12:50:50
您是否使用NLTK来提取实体?我在下面做了一个类似的例子,
import nltk
import re
from nltk.sem import extract_rels,rtuple
from nltk.chunk import tree2conlltags
sample = """"Michael Joseph Jackson was born in Gary, Indiana, near Chicago, on August 29, 1958.
He was the eighth of ten children in the Jackson family, a working-class African-American family living in a two-bedroom house on Jackson Street.
His mother, Katherine Esther Jackson (née Scruse), left the Baptist tradition in 1963 to become a devout Jehovah's Witness.She played clarinet and piano and had aspired to be a country-and-western performer; she worked part-time at Sears to support the family.
His father, Joseph Walter 'Joe' Jackson, a former boxer, was a steelworker at U.S. Steel.
Joe played guitar with a local rhythm and blues band, the Falcons, to supplement the family's income.
Despite being a convinced Lutheran, Joe followed his wife's faith, as did all their children.
His father's great-grandfather, July 'Jack' Gale, was a Native American medicine man and US Army scout.
Michael grew up with three sisters (Rebbie, La Toya, and Janet) and five brothers (Jackie, Tito, Jermaine, Marlon, and Randy).
A sixth brother, Marlon's twin Brandon, died shortly after birth."""
sentences = nltk.sent_tokenize(sample)
tokenized_sentences = [nltk.word_tokenize(sentence) for sentence in sentences]
tagged_sentences = [nltk.pos_tag(sentence) for sentence in tokenized_sentences]
for i, sent in enumerate(tagged_sentences):
sent = nltk.ne_chunk(sent)
print(sent)
这将打印以下内容,(S /
(PERSON Michael/NNP Joseph/NNP Jackson/NNP) was/VBD born/VBN in/IN (GPE Gary/NNP),/,(GPE Indiana/NNP),/,near/IN (GPE芝加哥/NNP),/,on/IN /NNP 29/CD,/,1958/CD
https://stackoverflow.com/questions/55297016
复制相似问题