-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext_parser.py
410 lines (354 loc) · 12.7 KB
/
text_parser.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# -*- coding: utf-8 -*-
import re
from nltk import RegexpParser
from textblob import TextBlob, Word
from textblob.taggers import PatternTagger
from textblob.sentiments import NaiveBayesAnalyzer
from words.utility_text_sets import stopwords
from words import utility_text_sets
pattern_tagger = PatternTagger()
naive_bayes_analyzer = NaiveBayesAnalyzer()
NOUN = "n"
VERB = "v"
ADV = "r"
ADJ = "a"
class TextParser:
"""
Utility class for processing text content.
"""
substitutions = utility_text_sets.substitutions
# Skip if any of these is the *only* attribute - for instance,
# "I'm a big fan of Queen" makes sense, but "I'm a fan" doesn't.
skip_lone_attributes = [
"fan", "expert", "person", "advocate", "customer",
]
# A select set of attributes we want to exclude.
skip_attributes = utility_text_sets.skip_attributes
# A select set of attributes we want to include.
include_attributes = [
"geek", "nerd", "nurse", "cook", "student", "consultant", "mom", "dad",
"marine", "chef", "sophomore", "catholic", "mod",
# TODO - These make sense only when accompanied by
# at least another noun
# "person","enthusiast","fanboy","player","advocate",
]
# Super awesome logic - if noun ends in any of these, it's *probably*
# something we want to include/exclude. TODO - This is terrible logic,
# see if we can implement actual NLP.
include_attribute_endings = (
"er", "or", "ar", "ist", "an", "ert", "ese", "te", "ot"
)
exclude_attribute_endings = ("ing", "fucker")
# "Filler" words (in sentences such as "I think...", "I guess...", etc.)
skip_verbs = ["were", "think", "guess", "mean"]
skip_prepositions = ["that"]
skip_adjectives = ["sure", "glad", "happy", "afraid", "sorry", "certain"]
skip_nouns = [
"right", "way", "everything", "everyone", "things", "thing",
"mine", "stuff", "lot"
]
# Should _N include conjunctions?
grammar = r"""
# adverb* verb adverb*
# - really think, strongly suggest, look intensely
_VP:
{<RB.*>*<V.*>+<RB.*>*}
# determiner adjective noun(s)
# - a beautiful house, the strongest fighter
_N0:
{(<DT>*<JJ.*>*<NN.*>+(?!<POS>))+}
_N:
{<_N0>+}
# noun to/in noun
# - newcomer to physics, big fan of Queen, newbie in gaming
_N_PREP_N:
{<_N>((<TO>|<IN>)<_N>)+}
# my adjective noun(s)
# - my awesome phone
POSS:
{<PRP\$><_N>}
# I verb in* adjective* noun
# - I am a great chef, I like cute animals,
# - I work in beautiful* New York, I live in the suburbs
ACT1:
{<PRP><_VP><IN>*<_N>}
# Above + to/in noun
# - I am a fan of Jaymay, I have trouble with flannel
ACT2:
{<PRP><_VP><IN>*<_N_PREP_N>}
"""
chunker = RegexpParser(grammar)
def clean_up(self, text):
"""
Removes unnecessary words from text and replaces common
misspellings/contractions with expanded words.
"""
for original, rep in self.substitutions:
text = re.sub(original, rep, text, flags=re.I)
return text
def normalize(self, word, tag="N"):
"""
Normalizes word using given tag. If no tag is given, NOUN is assumed.
"""
kind = NOUN
if tag.startswith("V"):
kind = VERB
elif tag.startswith("RB"):
kind = ADV
elif tag.startswith("J"):
kind = ADJ
return Word(word).lemmatize(kind).lower()
def pet_animal(self, word):
"""
Returns word if word is in a predefined list of pet animals.
"""
word = word.lower()
if re.match(r"\b(dog|cat|hamster|fish|pig|snake|rat|parrot|rabbit)\b", word):
return word # TODO:// make the pet_animal function smarter
else:
return None
def family_member(self, word):
"""
Returns normalized word if word is in a predefined list
of family members.
"""
word = word.lower()
if re.match(r"\b(mom|mother|mum|mommy)\b", word):
return "mother"
elif re.match(r"\b(dad|father|pa|daddy)\b", word):
return "father"
elif re.match(r"\b(brother|sister|son|daughter)s?\b", word):
return word
else:
return None
def relationship_partner(self, word):
"""
Returns word if word is in a predefined list of relationship partners.
"""
word = word.lower()
if re.match(r"\b(ex-)*(boyfriend|girlfriend|so|wife|husband)\b", word):
return word
else:
return None
def gender(self, word):
"""
Returns normalized word if word is in a predefined list of genders.
"""
word = word.lower()
print(word)
if re.match(r"\b(girl|woman|female|lady|she)\b", word):
return "female"
elif re.match(r"\b(guy|man|male|he|dude)\b", word):
return "male"
else:
return None
def orientation(self, word):
"""
Returns word if word is in a predefined list of sexual orientations.
"""
word = word.lower()
if re.match(r"\b(gay|straight|bi|bisexual|homosexual)\b", word):
return word
else:
return None
def process_verb_phrase(self, verb_tree):
"""
Returns list of (word,tag) tuples given a verb tree.
"""
if verb_tree.label() != "_VP":
return None
verb_phrase = [(w.lower(), t) for w, t in verb_tree.leaves()]
return verb_phrase
def process_noun_phrase(self, noun_tree):
"""
Returns list of (word,tag) tuples given a noun tree.
"""
if noun_tree.label() != "_N":
return []
if any(
n in self.skip_nouns+stopwords
for n, t in noun_tree.leaves() if t.startswith("N")
):
return []
noun_phrase = [(w.lower(), t) for w, t in noun_tree.leaves()]
return noun_phrase
def process_npn_phrase(self, npn_tree):
"""
Given a phrase of the form noun-preposition-noun, returns noun
and preposition-noun phrases.
"""
if npn_tree.label() != "_N_PREP_N":
return None
noun_phrase = []
prep_noun_phrase = []
for i in range(len(npn_tree)):
node = npn_tree[i]
# we have hit the prepositions in a prep noun phrase
if type(node) is tuple:
w, t = node
w = w.lower()
prep_noun_phrase.append((w, t))
else:
if prep_noun_phrase:
prep_noun_phrase += self.process_noun_phrase(node)
else:
noun_phrase = self.process_noun_phrase(node)
return (noun_phrase, prep_noun_phrase)
def process_possession(self, phrase):
"""
Given a phrase, checks and returns a possession/belonging
(my <word>) if exists.
"""
noun_phrase = []
for i in range(len(phrase)):
node = phrase[i]
if type(node) is tuple: # word can only be pronoun
w, t = node
if t == "PRP$" and w.lower() != "my":
return None
else: # type has to be nltk.tree.Tree
if node.label() == "_N":
noun_phrase = self.process_noun_phrase(node)
else: # what could this be?
pass
if noun_phrase:
return {
"kind": "possession",
"noun_phrase": noun_phrase
}
else:
return None
def process_action(self, phrase):
"""
Given a phrase, checks and returns an action
(I <verb-phrase>) if exists.
"""
verb_phrase = []
prepositions = []
noun_phrase = []
prep_noun_phrase = []
for i in range(len(phrase)):
node = phrase[i]
if type(node) is tuple: # word is either pronoun or preposition
w, t = node
if t == "PRP" and w.lower() != "i":
return None
elif t == "IN":
prepositions.append((w.lower(), t))
else: # what could this be?!
pass
else:
if node.label() == "_VP":
verb_phrase = self.process_verb_phrase(node)
elif node.label() == "_N":
noun_phrase = self.process_noun_phrase(node)
elif node.label() == "_N_PREP_N":
noun_phrase, prep_noun_phrase = (
self.process_npn_phrase(node)
)
if noun_phrase:
return {
"kind": "action",
"verb_phrase": verb_phrase,
"prepositions": prepositions,
"noun_phrase": noun_phrase,
"prep_noun_phrase": prep_noun_phrase
}
else:
return None
def extract_chunks(self, text):
"""
Given a block of text, extracts and returns useful chunks.
TODO - Should sentiments be excluded here?
"""
chunks = []
sentiments = []
text = self.clean_up(text)
blob = TextBlob(
text,
pos_tagger=pattern_tagger,
analyzer=naive_bayes_analyzer
)
for sentence in blob.sentences:
has_i_or_why = re.search(r"\b(i|my)\b", str(sentence), re.I)
if not sentence.tags or not has_i_or_why:
continue
tree = self.chunker.parse(sentence.tags)
for subtree in tree.subtrees(
filter=lambda t: t.label() in ['POSS', 'ACT1', 'ACT2']
):
phrase = [(w.lower(), t) for w, t in subtree.leaves()]
phrase_type = subtree.label()
if not any(
x in [
("i", "PRP"), ("my", "PRP$")
] for x in [(w, t) for w, t in phrase]
) or (
phrase_type in ["ACT1", "ACT2"] and (
any(
word in self.skip_verbs for word in [
w for w, t in phrase if t.startswith("V")
]
) or any(
word in self.skip_prepositions for word in [
w for w, t in phrase if t == "IN"
]
) or any(
word in self.skip_adjectives for word in [
w for w, t in phrase if t == "JJ"
]
)
)
):
continue
if subtree.label() == "POSS":
chunk = self.process_possession(subtree)
if chunk:
chunks.append(chunk)
elif subtree.label() in ["ACT1", "ACT2"]:
chunk = self.process_action(subtree)
if chunk:
chunks.append(chunk)
return (chunks, sentiments)
def ngrams(self, text, n=2):
"""
Returns a list of ngrams for given text.
"""
return [" ".join(w) for w in TextBlob(text).ngrams(n=n)]
def noun_phrases(self, text):
"""
Returns list of TextBlob-derived noun phrases.
"""
return TextBlob(text).noun_phrases
def common_words(self, text):
"""
Given a text, splits it into words and returns as a list
after excluding stop words.
"""
return [
word for word in list(TextBlob(text).words) if (
word not in stopwords and word.isalpha()
)
]
def total_word_count(self, text):
"""
Returns total word count of a given text.
"""
return len(list(TextBlob(text).words))
def unique_word_count(self, text):
"""
Returns unique word count of a given text.
"""
return len(set(list(TextBlob(text).words)))
def longest_word(self, text):
"""
Returns longest word in a given text.
"""
return max((list(TextBlob(text).words)), key=len)
@staticmethod
def test_sentence(sentence):
"""
Prints TextBlob-derived tags for a given sentence.
For testing purposes only.
"""
print(TextBlob(sentence).tags)