我正在尝试从txt文件中提取位置名称、国家名称、城市名称、旅游景点,方法是使用python中的nlp或scapy库。
我尝试了以下几点:
import spacy
en = spacy.load('en')
sents = en(open('subtitle.txt').read())
place = [ee for ee in sents.ents]获得产出:
[1,
, three, London,
,
,
,
, first,
,
, 00:00:20,520,
,
, London, the
4
00:00:20,520, 00:00:26,130
, Buckingham Palace,
, 我只想要位置名称,国家名称,城市名称和城市内的任何地方。
我也尝试使用NLP:
import nltk
nltk.download('maxent_ne_chunker')
nltk.download('words')
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
nltk.download('stopwords')
with open('subtitle.txt', 'r') as f:
sample = f.read()
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]
chunked_sentences = nltk.ne_chunk_sents(tagged_sentences, binary=True)
def extract_entity_names(t):
entity_names = []
if hasattr(t, 'label') and t.label:
if t.label() == 'NE':
entity_names.append(' '.join([child[0] for child in t]))
else:
for child in t:
entity_names.extend(extract_entity_names(child))
return entity_names
entity_names = []
for tree in chunked_sentences:
# Print results per sentence
#print (extract_entity_names(tree))
entity_names.extend(extract_entity_names(tree))
# Print all entity names
#print (entity_names)
# Print unique entity names
print (set(entity_names))输出得到:
{'Okay', 'Buckingham Palace', 'Darwin Brasserie', 'PDF', 'London', 'Local Guide', 'Big Ben'}在这里,还有一些不想要的词,比如“Okay”、“PDF”、“Local”,还有一些地方不见了。
请建议一下。
编辑-1
脚本
import spacy
nlp = spacy.load('en_core_web_lg')
gpe = [] # countries, cities, states
loc = [] # non gpe locations, mountain ranges, bodies of water
doc = nlp(open('subtitle.txt').read())
for ent in doc.ents:
if (ent.label_ == 'GPE'):
gpe.append(ent.text)
elif (ent.label_ == 'LOC'):
loc.append(ent.text)
cities = []
countries = []
other_places = []
import wikipedia
for text in gpe:
summary = str(wikipedia.summary(text))
if ('city' in summary):
cities.append(text)
print (cities)
elif ('country' in summary):
countries.append(text)
print (countries)
else:
other_places.append(text)
print (other_places)
for text in loc:
other_places.append(text)
print (other_places)通过使用应答脚本:获取以下输出
['London', 'London']
['London', 'London', 'London']
['London', 'London', 'London', 'London']
['London', 'London', 'London', 'London', 'London']
['London', 'London', 'London', 'London', 'London', 'London']
['London', 'London', 'London', 'London', 'London', 'London', 'London']
['London', 'London', 'London', 'London', 'London', 'London', 'London', 'London']
['London', 'London', 'London', 'London', 'London', 'London', 'London', 'London', 'London']
['London', 'London', 'London', 'London', 'London', 'London', 'London', 'London', 'London', 'London']
['London', 'London', 'London', 'London', 'London', 'London', 'London', 'London', 'London', 'London', 'London']
['London', 'London', 'London', 'London', 'London', 'London', 'London', 'London', 'London', 'London', 'London', 'London']发布于 2018-10-07 12:03:28
你在找有名字的实体。spaCy是一个有效的库,用于在文本中查找命名实体,但您应该相应地将它用于文档。
你正在寻找地点,国家和城市。这些地方属于GPE和LOC在spaCy标签中的类别。具体而言,GPE适用于国家、城市和州,LOC适用于非GPE地区、山区、水体等。
如果您只需要将这些名称放入列表中,则可以使用need并只查找这些标记。例如,如果您需要将城市与国家分开,那么您可以执行wikipedia查询并检查摘要,以确定它是一个城市还是一个国家。为此,您可能会发现python的wikipedia库很有用。
示例代码:
import spacy
nlp = spacy.load('en_core_web_lg')
gpe = [] # countries, cities, states
loc = [] # non gpe locations, mountain ranges, bodies of water
doc = nlp(open('subtitle.txt').read())
for ent in doc.ents:
if (ent.label_ == 'GPE'):
gpe.append(ent.text)
elif (ent.label_ == 'LOC'):
loc.append(ent.text)
cities = []
countries = []
other_places = []
import wikipedia
for text in gpe:
summary = str(wikipedia.summary(text))
if ('city' in summary):
cities.append(text)
elif ('country' in summary):
countries.append(text)
else:
other_places.append(text)
for text in loc:
other_places.append(text)如果您发现wikipedia方法不够或者慢,您也可以尝试使用自己的NER标记来培训NER。为此,请看一看这里。
https://stackoverflow.com/questions/52686159
复制相似问题