-
Notifications
You must be signed in to change notification settings - Fork 0
/
NE_extractor.py
executable file
·44 lines (34 loc) · 1.31 KB
/
NE_extractor.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
41
42
43
44
import nltk
def ne(sent):
# with open(filename, '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)
#print list(chunked_sentences)
words=nltk.word_tokenize(sent)
tagged=nltk.pos_tag(words)
chunked=nltk.ne_chunk(tagged, 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:
# # Print results per sentence
# # print extract_entity_names(tree)
#
# entity_names.extend(extract_entity_names(tree))
entity_names = extract_entity_names(chunked)
#Print all entity names
#print entity_names
# Print unique entity names
return list(set(entity_names))
#print(ne('Pranav Goel is as good at coding as Tom Cruise is at stunts.'))