Created
January 23, 2011 21:24
-
-
Save rgaidot/792451 to your computer and use it in GitHub Desktop.
Entity Extraction using NLTK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import nltk | |
text = """Barack Hussein Obama II (born August 4, 1961) is the 44th and current President of the United States. He is the first African American to hold the office. Obama previously served as a United States Senator from Illinois, from January 2005 until he resigned after his election to the presidency in November 2008.""" | |
sentences = nltk.sent_tokenize(text) | |
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) | |
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: | |
entity_names.extend(extract_entity_names(tree)) | |
print entity_names |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment