使用NLP或Spacy,我们如何从文本给定实体中提取上下文数据作为输入?

问题描述 投票:2回答:1

例如,有一个文本(以文档的形式)和人名“John”。我们需要从文本中提取所有句子,其中提到约翰的名字或其他。

nlp nltk stanford-nlp spacy ner
1个回答
0
投票

你用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)/ VBD出生/ VBN in / IN(GPE Gary / NNP),/,(GPE Indiana / NNP),/,近/ IN (GPE Chicago / NNP),/ / 8月/ / NNP 29 / CD,/,1958 / CD

© www.soinside.com 2019 - 2024. All rights reserved.