-
Notifications
You must be signed in to change notification settings - Fork 1
/
entityExtractor.py
40 lines (32 loc) · 1.12 KB
/
entityExtractor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'''
Created on 01-Dec-2013
@author: Sumukha TV
'''
import nltk
def entityExtractor(text):
stopWords = list(set(nltk.corpus.stopwords.words('english')))
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)
entities = []
for tree in chunked_sentences:
entities.extend(get_entity_names(tree))
entities = list(set(entities))
entsPerfect = []
for e in entities:
if e not in stopWords:
entsPerfect.append(e)
return entsPerfect
def get_entity_names(t):
entities = []
if hasattr(t, 'node') and t.node:
if t.node == 'NE':
entities.append(' '.join([childNode[0] for childNode in t]))
else:
for childNode in t:
entities.extend(get_entity_names(childNode))
return entities
if __name__ == '__main__':
text = open("text.txt").read()
print entityExtractor(text)