-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextraction.py
More file actions
65 lines (52 loc) · 2 KB
/
Copy pathextraction.py
File metadata and controls
65 lines (52 loc) · 2 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import nltk
from collections import defaultdict
class Extraction:
def extract_nep(self, text):
'''
Function extracts the entities from the text
@param text stream of text to be parsed
@return set of nodes and their entities
'''
nep = []
try:
for sent in nltk.sent_tokenize(text):
for chunk in nltk.ne_chunk(nltk.pos_tag(nltk.word_tokenize(sent))):
if hasattr(chunk, 'node'):
rnep = ' '.join(c[0] for c in chunk.leaves())
nep.append(self._clean_node(chunk.node)+'::'+rnep)
except Exception:
print "Exception raised in the parsing ", Exception[0]
return nep
def extract_count(self, orig_term):
'''
Function to return a count of the number of times a term exists in text
@param orig_set the original set of terms and their node type
@return dictionary of terms and counts
'''
term_count = defaultdict(int)
for v in orig_term:
node,term = v.split('::')
term_count[term]+= 1
return term_count
def extract_author (self, termlist):
book = []
for v in termlist:
term, node = v.split('::')
if term == "http://xmlns.com/foaf/0.1/name":
book.append(node)
return book
def extract_book (self, termlist):
book = []
for v in termlist:
term, node = v.split('::')
print term
if term == "http://purl.org/dc/elements/1.1/title":
book.append(node)
return book
def _clean_node (self, node):
if node == "PERSON":
return node.replace("PERSON", "foaf:name")
elif node == "ORGANIZATION":
return node.replace("ORGANIZATION", "dc:title")
elif node == "GPE":
return node.replace("GPE", "dc:title")