Here is my problem. I have a sample text file where I store the text data by crawling various html pages. This text contains information about various events and its time and location. I want to fetch the coordinates of these locations. I have no idea on how I can do that in python. I am using nltk to recognize named entities in this sample text. Here is the code:
import nltk
with open('sample.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.batch_ne_chunk(tagged_sentences, binary=True)
#print chunked_sentences
#print tokenized_sentences
#print tagged_sentences
def extract_entity_names(t):
entity_names = []
if hasattr(t, 'node') and t.node:
if t.node == '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)
Sample file is something like this:
La bohème at Covent Garden
When: 18 Jan 2013 (various dates) , 7.30pm Where: Covent Garden, London, John Copley's perennially popular Royal Opera production of Puccini's La bohème is revived for the first of two times this season, aptly over the Christmas period. Sir Mark Elder conducts Rolando Villazón as Rodolfo and Maija Kovalevska as Mimì. Mimì meets poet Rodolfo (Dmytro Popov sings the role on 5 and 18 January) one cold Christmas Eve in Paris' Latin Quarter. Fumbling around in the dark after her candle has gone out, they fall in love. Rodolfo lives with three other lads: philosopher Colline (Nahuel di Pierro/Jihoon Kim on 18 January), musician Schaunard (David Bizic) and painter Marcello (Audun Iversen), who loves Musetta (Stefania Dovhan). Both couples break up and the opera ends in tragedy as Rodolfo finds Mimì dying of consumption in a freezing garret.
I want to fetch coordinates for Covent Garden,London from this text. How can I do it ?