-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguage_processing.py
More file actions
204 lines (148 loc) · 6.19 KB
/
Copy pathlanguage_processing.py
File metadata and controls
204 lines (148 loc) · 6.19 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
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
import streamlit as st
import spacy
from spacy.matcher import Matcher, PhraseMatcher
from spacy.tokens import Span
from spacytextblob.spacytextblob import SpacyTextBlob
detect_model = 'en_core_web_sm'
@st.cache(allow_output_mutation=True)
def detect_ner(text):
ner_nlp = spacy.load(detect_model)
full_text = ner_nlp(text)
verses = [ner_nlp(verse) for verse in text.split('\n')]
return {'text': full_text, 'lines': verses}
@st.cache(allow_output_mutation=True)
def detect_pos(text):
pos_nlp = spacy.load(detect_model, disable=["ner"])
full_text = pos_nlp(text)
verses = [pos_nlp(verse) for verse in text.split('\n')]
patterns = [
[{'POS': 'ADJ'}],
[{'POS': 'ADP'}],
[{'POS': 'ADV'}],
[{'POS': 'AUX'}],
[{'POS': 'CONJ'}],
[{'POS': 'DET'}],
[{'POS': 'INTJ'}],
[{'POS': 'NOUN'}],
[{'POS': 'NUM'}],
[{'POS': 'PART'}],
[{'POS': 'PRON'}],
[{'POS': 'PROPN'}],
[{'POS': 'PUNCT'}],
[{'POS': 'SYM'}],
[{'POS': 'VERB'}],
[{'POS': 'X'}],
[{'POS': 'SPACE'}],
[{'POS': 'CCONJ'}],
[{'POS': 'SCONJ'}]
]
matcher = Matcher(pos_nlp.vocab)
for pattern in patterns:
matcher.add(key=pattern[0]['POS'], patterns=[pattern])
for verse in verses:
matches = matcher(verse)
for id, start, end in matches:
new_ent = Span(verse, start, end, label=id)
verse.ents = list(verse.ents) + [new_ent]
matches = matcher(full_text)
for id, start, end in matches:
new_ent = Span(full_text, start, end, label=id)
full_text.ents = list(full_text.ents) + [new_ent]
return {'text': full_text, 'lines': verses}
@st.cache(allow_output_mutation=True)
def detect_quantity(text):
quantity_nlp = spacy.load(detect_model, disable=["ner"])
full_text = quantity_nlp(text)
verses = [quantity_nlp(verse) for verse in text.split('\n')]
matcher = Matcher(quantity_nlp.vocab)
for token in full_text:
number = token.morph.get("Number")
if number:
label = number[0].upper()
matcher.add(label, patterns=[[{'MORPH': str(token.morph)}]])
for verse in verses:
matches = matcher(verse)
for match_id, start, end in matches:
new_ent = Span(verse, start, end, label=match_id)
verse.ents = list(verse.ents) + [new_ent]
return {'text': full_text, 'lines': verses}
@st.cache(allow_output_mutation=True)
def detect_persons(text):
persons_nlp = spacy.load(detect_model, disable=["ner"])
full_text = persons_nlp(text)
verses = [persons_nlp(verse) for verse in text.split('\n')]
matcher = Matcher(persons_nlp.vocab)
text_to_num = {'first': '1', 'second': '2', 'third': '3',
'one': '1', 'two': '2', 'three': '3'}
for token in full_text:
person = token.morph.get('Person')
number = token.morph.get('Number')
if person and person[0].isalpha():
person = [text_to_num[person[0].lower()]]
if not person == []:
label = ' '.join(person + number).upper()
matcher.add(label, patterns=[[{'MORPH': str(token.morph)}]])
for verse in verses:
matches = matcher(verse)
for match_id, start, end in matches:
new_ent = Span(verse, start, end, label=match_id)
verse.ents = list(verse.ents) + [new_ent]
return {'text': full_text, 'lines': verses}
@st.cache(allow_output_mutation=True)
def detect_tenses(text):
tenses_nlp = spacy.load(detect_model, disable=["ner"])
full_text = tenses_nlp(text)
verses = [tenses_nlp(verse) for verse in text.split('\n')]
matcher = Matcher(tenses_nlp.vocab)
for token in full_text:
if token.pos_ == 'VERB':
tense = token.morph.get("Tense")
if tense:
label = tense[0].upper()
if label == 'PRES':
label = 'PRESENT'
matcher.add(label, patterns=[[{'MORPH': str(token.morph)}]])
else:
matcher.add('OTHER', patterns=[[{'MORPH': str(token.morph)}]])
for verse in verses:
matches = matcher(verse)
for match_id, start, end in matches:
new_ent = Span(verse, start, end, label=match_id)
verse.ents = list(verse.ents) + [new_ent]
return {'text': full_text, 'lines': verses}
@st.cache(allow_output_mutation=True)
def detect_sentiments(text):
sentiments_nlp = spacy.load(detect_model, disable=["ner"])
sentiments_nlp.add_pipe('spacytextblob')
full_text = sentiments_nlp(text)
verses = [sentiments_nlp(verse) for verse in text.split('\n')]
matcher = Matcher(sentiments_nlp.vocab)
for token in full_text:
if token._.polarity != 0:
label = str(round(token._.polarity, 1))
matcher.add(key=label, patterns=[[{'TEXT': token.text}]])
for verse in verses:
matches = matcher(verse)
for match_id, start, end in matches:
new_ent = Span(verse, start, end, label=match_id)
verse.ents = list(verse.ents) + [new_ent]
return {'text': full_text, 'lines': verses}
@st.cache(allow_output_mutation=True)
def detect_subjectivity(text):
subjectivity_nlp = spacy.load(detect_model, disable=["ner"])
subjectivity_nlp.add_pipe('spacytextblob')
full_text = subjectivity_nlp(text)
verses = [subjectivity_nlp(verse) for verse in text.split('\n')]
matcher = PhraseMatcher(subjectivity_nlp.vocab)
for line in full_text.sents:
if line._.subjectivity:
score = line._.subjectivity
label = str(round(score, 1))
text = line.text.replace('\n', '')
matcher.add(key=label, docs=[subjectivity_nlp(text)])
for verse in verses:
matches = matcher(verse)
for match_id, start, end in matches:
new_ent = Span(verse, start, end, label=match_id)
verse.ents = list(verse.ents) + [new_ent]
return {'text': full_text, 'lines': verses}