-
Notifications
You must be signed in to change notification settings - Fork 0
/
linguistic_analysis.py
328 lines (268 loc) · 9.73 KB
/
linguistic_analysis.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
import os
from pathlib import Path
from typing import Callable, Dict, List, Optional, Set, Text, Tuple, Union
from pydantic import BaseModel, Field
from pydantic.error_wrappers import ValidationError
from pydantic.types import PositiveInt
import srsly
import spacy
from spacy.language import Language
from spacy.tokens import Doc, Token, MorphAnalysis
from spacy.glossary import GLOSSARY as TAGS_GLOSSARY
import streamlit
from stqdm import stqdm
os.environ["TOKENIZERS_PARALLELISM"] = str(True).lower()
class FeatureAnalysis(BaseModel):
name: Text
distribution: Optional[Dict[Text, PositiveInt]] = None
class Section(BaseModel):
title: Text
text: Text = Field(max_length=200_000)
doc: Optional[Doc] = None
features: Optional[
Dict[
Text,
Union[
FeatureAnalysis,
Dict[Text, FeatureAnalysis]
]
]
] = None
class Config:
arbitrary_types_allowed: bool = True
POS_TAGS: List[Tuple[Text, Text]] = [ # https://universaldependencies.org/u/pos/index.html
("ADJ", "Adjective"),
("ADV", "Adverb"),
("INTJ", "Interjection"),
("NOUN", "Noun"),
("PROPN", "Proper Noun"),
("VERB", "Verb")
]
POS_TAGS_DICT = dict(POS_TAGS)
def analyze_morphological_features(doc: Doc, pos_tag: Optional[Text] = None) -> Dict[Text, FeatureAnalysis]:
filtered_words: List[Token] = [
token
for token in doc
if (not pos_tag) or (token.pos_ == pos_tag)
]
features: Dict[Text, FeatureAnalysis] = dict()
for filtered_word in filtered_words:
morph_analysis: MorphAnalysis = filtered_word.morph
for feature, value in morph_analysis.to_dict().items():
if not feature in features:
features[feature] = FeatureAnalysis(
name=feature,
distribution=dict()
)
feature: FeatureAnalysis = features.get(feature)
distribution: Dict[
Text, PositiveInt
] = feature.distribution
if value not in distribution:
distribution[value] = 0
distribution[value] += 1
return {
feature_name: feature_analysis
for feature_name, feature_analysis in features.items()
if feature_analysis.distribution
}
def extract_word_families_by_lemma(doc: Doc) -> Dict[Text, Dict]:
word_families: Dict[Text, Set[Token]] = dict()
for token in doc:
lemma: Text = token.lemma_
if lemma and lemma.isalpha():
if not lemma in word_families:
word_families[lemma] = set()
word_families[lemma].add(token)
return {
lemma: {
f"{str(token.text).lower()} ({token.pos_})": {
"Part of Speech (PoS) Tag": (
f"{token.pos_} ({TAGS_GLOSSARY[token.pos_]})"
if token.pos_ else None
),
"Fine-grained Part of Speech (PoS) Tag": (
f"{token.tag_} ({TAGS_GLOSSARY[token.tag_]})"
if token.tag_ else None
)
}
for token in word_family
}
for lemma, word_family in word_families.items()
if word_family
}
TITLE = "Linguistic Analysis"
ICON = "📊"
streamlit.set_page_config(
page_title=TITLE,
page_icon=ICON
)
streamlit.title(ICON + " " + TITLE)
streamlit.subheader("Analyzers and Features")
ANALYZERS_COLUMN, POS_TAGS_COLUMN = streamlit.columns(2)
ANALYZERS: Dict[Text, Callable[[Doc, Text], Dict[Text, Union[FeatureAnalysis, Dict]]]] = {
"Morphological Features": analyze_morphological_features,
"Word Families (by Lemma)": extract_word_families_by_lemma
}
with ANALYZERS_COLUMN:
SELECTED_ANALYZER_NAMES: List[Text] = streamlit.multiselect(
"Select Analyzers",
options=list(
ANALYZERS.keys()
)
)
SELECTED_ANALYZERS: Dict[Text, Callable[[Doc, Text], Dict[Text, FeatureAnalysis]]] = {
name: ANALYZERS[name]
for name in SELECTED_ANALYZER_NAMES
}
with POS_TAGS_COLUMN:
if "Morphological Features" in SELECTED_ANALYZERS:
if streamlit.checkbox("Use all PoS Tags"):
SELECTED_POS_TAGS = POS_TAGS
else:
SELECTED_POS_TAGS: List[Tuple[Text, Text]] = streamlit.multiselect(
"Select Part of Speech (PoS) Tags",
options=POS_TAGS,
format_func=lambda option: f'{option[1]} ("{option[0]}")'
)
else:
SELECTED_POS_TAGS = POS_TAGS
streamlit.subheader("Language and Language Model")
LANGUAGE_COLUMN, LANGUAGE_MODEL_COLUMN = streamlit.columns(2)
MODEL_NAMES_BY_LANGUAGE: Dict[Text, List[Text]] = {
"English": [
"en_core_web_md",
# "en_core_web_trf",
]
}
with LANGUAGE_COLUMN:
SELECTED_LANGUAGE: Text = streamlit.selectbox(
"Select Language",
options=list(
MODEL_NAMES_BY_LANGUAGE.keys()
)
)
with LANGUAGE_MODEL_COLUMN:
SELECTED_MODEL_NAME: Text = streamlit.selectbox(
"Select Language Model",
options=MODEL_NAMES_BY_LANGUAGE[SELECTED_LANGUAGE]
)
with streamlit.spinner("Loading Language Model..."):
SELECTED_MODEL: Language = spacy.load(SELECTED_MODEL_NAME)
streamlit.header("Sections to analyze")
if (
"sections" not in streamlit.session_state
) or (
streamlit.session_state.sections and
streamlit.button(f"Clear {len(streamlit.session_state.sections)} Sections")
):
SECTIONS: Dict[Text, Section] = dict()
streamlit.session_state.sections = SECTIONS
USE_YAML_LIST_MODE: bool = streamlit.checkbox(
"Use YAML List Mode (Advanced)"
)
if USE_YAML_LIST_MODE:
YAML_SECTIONS_LIST: Text = streamlit.text_area(
"Insert YAML list with keys 'title' and 'text'"
)
if YAML_SECTIONS_LIST:
SECTION_LIST: List[Dict[Text, Text]] = srsly.yaml_loads(
YAML_SECTIONS_LIST
)
for section_data in SECTION_LIST:
streamlit.session_state.sections[section_data["title"]] = Section(
**section_data
)
else:
with streamlit.form(
key="section"
):
streamlit.subheader("Add Section")
SECTION_TITLE: Text = streamlit.text_input("Section Title")
SECTION_TEXT: Text = streamlit.text_area("Section Text")
SECTION_SUBMITTED: bool = streamlit.form_submit_button(
"Add"
)
try:
SECTION: Section = Section(
title=SECTION_TITLE,
text=SECTION_TEXT
)
except ValidationError:
streamlit.warning("Invalid Section")
SECTION = None
if SECTION_SUBMITTED and SECTION:
streamlit.session_state.sections[SECTION.title] = SECTION
for section_name, section in stqdm(
streamlit.session_state.sections.items()
):
streamlit.header(section.title)
section.doc = SELECTED_MODEL(section.text)
with streamlit.expander(
f"Text of Section '{section.title}' "
f"({len(section.doc)} Tokens)",
expanded=False
):
streamlit.text(section.text)
if not section.features:
section.features = dict()
for analyzer_name, analyzer in SELECTED_ANALYZERS.items():
if analyzer_name == "Morphological Features":
section.features.update(
{
pos_tag: analyzer(section.doc, pos_tag)
for pos_tag, pos_tag_description in SELECTED_POS_TAGS
}
)
elif analyzer_name == "Word Families (by Lemma)":
section.features["Word Families"] = analyzer(section.doc)
with streamlit.expander(
f"Feature Analysis of Section '{section.title}'",
expanded=False
):
if "Word Families (by Lemma)" in SELECTED_ANALYZERS:
WORD_FAMILY_SIZE_THRESHOLD: PositiveInt = streamlit.number_input(
"Only show word families of this size or higher",
min_value=1,
value=2,
key=f"word_family_size_threshold_{section_name}"
)
else:
WORD_FAMILY_SIZE_THRESHOLD: PositiveInt = 1
streamlit.write(
{
feature_group_key: (
{
feature_name: feature_analysis.distribution
for feature_name, feature_analysis in feature_group_features.items()
}
if not feature_group_key == "Word Families"
else {
word_family_lemma: word_family
for word_family_lemma, word_family in feature_group_features.items()
if len(word_family) >= WORD_FAMILY_SIZE_THRESHOLD
}
)
for feature_group_key, feature_group_features in section.features.items()
}
)
if "Morphological Features" in SELECTED_ANALYZERS:
streamlit.subheader("Relative Distribution")
streamlit.write(
{
feature_group: {
feature_name: {
key: (
round(
count / sum(feature_analysis.distribution.values()),
ndigits=2
)
)
for key, count in feature_analysis.distribution.items()
}
for feature_name, feature_analysis in feature_group_features.items()
}
for feature_group, feature_group_features in section.features.items()
if feature_group != "Word Families"
}
)