-
Notifications
You must be signed in to change notification settings - Fork 2
/
verbFinder.py
135 lines (116 loc) · 3.89 KB
/
verbFinder.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import sys, pickle
import numpy as np
import random as rand
class verbFinder:
def __init__(self):
self.verbs = {}
self.nouns = {}
self.preps = {}
self.unique_mod = ''
def saveNounsToFile(self):
pickle.dump(self.nouns, open(self.unique_mod + '_nouns.p', 'wb'))
def addNounsFromFile(self, otherNounPickleFilename):
otherNouns = pickle.load(open(otherNounPickleFilename, 'rb'))
for noun in otherNouns:
if noun in self.nouns:
for verb in otherNouns[noun]:
if verb in self.nouns[noun]:
self.nouns[noun][verb] += otherNouns[noun][verb]
else:
self.inc_noun(noun, verb)
else:
for verb in otherNouns[noun]:
self.nouns[noun] = {}
if verb not in self.nouns[noun]:
self.nouns[noun][verb] = 0
self.nouns[noun][verb] += otherNouns[noun][verb]
def savePrepsToFile(self):
pickle.dump(self.preps, open(self.unique_mod + '_preps.p', 'wb'))
def addPrepsFromFile(self, otherPrepPickleFilename):
otherPreps = pickle.load(open(otherPrepPickleFilename, 'rb'))
for Prep in otherPreps:
if prep in self.preps:
for verb in otherPreps[prep]:
if verb in self.preps[prep]:
self.prep[prep][verb] += otherPreps[prep][verb]
else:
self.inc_prep(prep, verb)
else:
for verb in otherPreps[prep]:
self.preps[prep] = {}
if verb not in self.preps[prep]:
self.preps[prep][verb] = 0
self.preps[prep][verb] += otherPreps[prep][verb]
def saveVerbsToFile(self):
pickle.dump(self.verbs, open(self.unique_mod + '_verbs.p', 'wb'))
def addVerbsFromFile(self, otherVerbPickleFilename):
otherVerbs = pickle.load(open(otherVerbPickleFilename, 'rb'))
for verb in otherVerbs:
if verb in self.verbs:
for word in otherVerbs[verb]:
if word in self.verbs[verb]:
self.verbs[verb][word] += otherVerbs[verb][word]
else:
self.inc_verbs(verb, word)
else:
for word in otherVerbs[verb]:
self.verbs[verb] = {}
if word not in self.verbs[verb]:
self.verbs[verb][word] = 0
self.verbs[verb][word] += otherVerbs[verb][word]
def verbsForNoun(self, noun):
return self.nouns[noun]
def verbsForPrep(self, prep):
return self.preps[prep]
def wordsForVerb(self, verb):
if verb in self.verbs.keys():
return self.verbs[verb]
return {}
def numDependencies(self, verb, word):
return self.verbs[verb][word]
def inc_verb(self, verb, word):
if verb not in self.verbs:
self.verbs[verb] = {}
if word not in self.verbs[verb]:
self.verbs[verb][word] = 0
self.verbs[verb][word] += 1
def inc_noun(self, noun, verb):
if noun not in self.nouns:
self.nouns[noun] = {}
if verb not in self.nouns[noun]:
self.nouns[noun][verb] = 0
self.nouns[noun][verb] += 1
def inc_prep(self, prep, verb):
if prep not in self.preps:
self.preps[prep] = {}
if verb not in self.preps[prep]:
self.preps[prep][verb] = 0
self.preps[prep][verb] += 1
# Parse a pre-processed set of lines
def parseFile(self, inputFile):
f = open(inputFile, 'r')
sentence = []
for line in f:
if len(line) > 10:
word = {}
textLine = line.split()
word['index'] = textLine[0]
word['text'] = textLine[1]
word['type'] = textLine[3]
word['parent'] = textLine[6]
if word['type'] == 'NOUN' or word['type'] == 'VERB' or word['type'] == 'ADP':
sentence.append(word)
else:
if len(sentence) > 0:
for w1 in sentence:
#find all words with a verb as the parent
if w1['type'] == 'VERB':
for w2 in sentence:
if w2['parent'] == w1['index'] and w2['type'] != 'VERB':
self.inc_verb(w1['text'], w2['text'])
if w2['type'] == 'NOUN':
self.inc_noun(w2['text'], w1['text'])
if w2['type'] == 'ADP':
self.inc_prep(w2['text'], w1['text'],)
sentence = [] #beginning of new sentence
f.close()