【问题标题】:How to extract the location name, country name, city name, tourist places by using nlp or spacy in python如何在python中使用nlp或spacy提取地名、国名、城市名、旅游地
【发布时间】:2019-03-12 04:10:19
【问题描述】:

我正在尝试通过在 python 中使用 nlp 或 scapy 库从 txt 文件中提取位置名称、国家名称、城市名称、旅游地点。

我在下面尝试过:

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'}

在这里,还会收到不需要的字词,例如“好的”、“PDF”、“本地向导”和一些地方丢失。

请提出建议。

Edit-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']

【问题讨论】:

标签: python-3.x machine-learning nlp stanford-nlp spacy


【解决方案1】:

您正在寻找命名实体。 spaCy 是一个用于在文本中查找命名实体的高效库,但您应该根据文档使用它。

您正在寻找地点、国家和城市。这些地方属于 spaCy NER 标记器中的 GPE 和 LOC 类别。具体来说,GPE 适用于国家、城市和州,而 LOC 适用于非 GPE 地点、山脉、水体等。

如果您只需要将这些名称放入列表中,则可以使用 NER 标记器并仅查找这些标记。例如,如果您需要将城市与国家/地区分开,您可以执行维基百科查询并检查摘要以了解它是城市还是国家。为此,您可能会发现 Python 的维基百科库很有用。

示例代码:

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 标注器。为此,请查看here

【讨论】:

    猜你喜欢
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 2017-06-25
    相关资源
    最近更新 更多