forked from JSimple/logline_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparts_of_speech.py
65 lines (53 loc) · 2.49 KB
/
parts_of_speech.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
# #!/usr/bin/env python
# # coding: utf-8
import spacy, textacy, re
from textacy import extract
# youll need to download ('python -m spacy download en_core_web_sm')
# TURNS FILE INTO NLP ANALYZABLE OBJECT
def text_to_nlp(file_name):
with open (file_name, 'r') as file:
text = file.read()
#nlp = spacy.load('en_core_web_sm')
nlp = textacy.make_spacy_doc(text, lang="en_core_web_sm")
#nlp_text = nlp(text)
return nlp #_text
def tokenize_by_word(nlp_text):
return list(nlp_text)
def get_nouns(word_list):
nouns = filter(lambda token: token.pos_ == 'NOUN', word_list)
return list(nouns)
def get_article_noun_phrases(nlp_text):
noun_phrases = list(nlp_text.noun_chunks)
article_noun_phrases = []
for phrase in noun_phrases:
articles = r"\ba\b|\bA\b|\ban\b|\bAn\b|\bthe\b|\bThe\b"
if (len(phrase) >= 3) and (re.search(articles, str(phrase))):
article_noun_phrases.append(phrase)
return list(article_noun_phrases)
def get_verb_phrases(nlp_text):
patterns = [
[{"POS":"ADV"},{"POS":"ADV"},{"POS":"VERB"}],
[{"POS":"ADV"},{"POS":"VERB"},{"POS":"ADV"}],
[{"POS":"ADV"},{"POS":"VERB"},{"POS":"AUX"}],
[{"POS":"ADV"},{"POS":"VERB"},{"POS":"ADP"}],
[{"POS":"ADV"},{"POS":"VERB"},{"POS":"NOUN"}],
[{"POS":"ADV"},{"POS":"VERB"},{"POS":"PROPN"}],
[{"POS":"ADV"},{"POS":"VERB"},{"POS":"NUM"}],
[{"POS":"ADV"},{"POS":"VERB"},{"POS":"ADV"}],
[{"POS":"AUX"},{"POS":"AUX"},{"POS":"VERB"}],
[{"POS":"AUX"},{"POS":"VERB"},{"POS":"AUX"}],
[{"POS":"AUX"},{"POS":"ADV"},{"POS":"VERB"}],
[{"POS":"AUX"},{"POS":"VERB"},{"POS":"ADV"}],
[{"POS":"AUX"},{"POS":"VERB"},{"POS":"ADP"}],
[{"POS":"AUX"},{"POS":"VERB"},{"POS":"NOUN"}],
[{"POS":"AUX"},{"POS":"VERB"},{"POS":"PROPN"}],
[{"POS":"AUX"},{"POS":"VERB"},{"POS":"NUM"}],
[{"POS":"VERB"},{"POS":"AUX"},{"POS":"ADV"}],
[{"POS":"VERB"},{"POS":"ADV"},{"POS":"ADV"}],
[{"POS":"VERB"},{"POS":"ADV"},{"POS":"ADP"}],
[{"POS":"VERB"},{"POS":"ADV"},{"POS":"NOUN"}],
[{"POS":"VERB"},{"POS":"ADV"},{"POS":"PROPN"}],
[{"POS":"VERB"},{"POS":"ADV"},{"POS":"NUM"}]
]
verb_phrases = extract.matches.token_matches(nlp_text, patterns=patterns)
return list(verb_phrases)