-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalyses.py
370 lines (267 loc) · 11.1 KB
/
analyses.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
# Implement linguistic analyses using spacy
# Run them on data/preprocessed/train/sentences.txt
from cmath import sin
import spacy
import numpy as np
from tabulate import tabulate
# from spacy import textacy
from spacy import displacy
from spacy.tokens.doc import Doc
from collections import Counter
import pandas as pd
from wordfreq import word_frequency
import matplotlib.pyplot as plt
from scipy.stats import pearsonr
def num_tokens(doc: Doc):
return len(doc)
def num_types(doc: Doc):
word_frequencies = Counter()
for sentence in doc.sents:
words = [token.text for token in sentence if not token.is_punct]
word_frequencies.update(words)
return len(word_frequencies.keys())
def num_words(doc: Doc): # Think about punctuation
word_frequencies = Counter()
for sentence in doc.sents:
words = [token.text for token in sentence if not token.is_punct]
word_frequencies.update(words)
return sum(word_frequencies.values())
def avg_words_sentence(doc: Doc):
word_count_sentences = []
for sentence in doc.sents:
word_frequencies = Counter()
words = [token.text for token in sentence if not token.is_punct]
word_frequencies.update(words)
word_count_sentences.append(sum(word_frequencies.values()))
return np.mean(word_count_sentences)
def avg_word_length(doc: Doc):
word_lengths = []
for sentence in doc.sents:
words = [token.text for token in sentence if not token.is_punct]
[word_lengths.append(len(word)) for word in words]
return np.mean(word_lengths)
def token_bigrams(doc: Doc):
bigram_frequencies = Counter()
for i in range(len(doc) - 1):
bigram = (doc[i].text, doc[i + 1].text)
bigram_frequencies.update([bigram])
return bigram_frequencies.most_common(3)
def token_trigrams(doc: Doc):
trigram_frequencies = Counter()
for i in range(len(doc) - 2):
trigram = (doc[i].text, doc[i + 1].text, doc[i + 2].text)
trigram_frequencies.update([trigram])
return trigram_frequencies.most_common(3)
def pos_bigrams(doc: Doc):
bigram_frequencies = Counter()
for i in range(len(doc) - 1):
bigram = (doc[i].pos_, doc[i + 1].pos_)
bigram_frequencies.update([bigram])
return bigram_frequencies.most_common(3)
def pos_trigrams(doc: Doc):
trigram_frequencies = Counter()
for i in range(len(doc) - 2):
trigram = (doc[i].pos_, doc[i + 1].pos_, doc[i + 2].pos_)
trigram_frequencies.update([trigram])
return trigram_frequencies.most_common(3)
def n_named_entites(doc: Doc):
return len(doc.ents)
def n_unique_labels(doc: Doc):
return len(np.unique([ent.label_ for ent in doc.ents]))
def visualize(doc: Doc):
first_five = list(doc.sents)[:5]
displacy.serve(first_five, style="ent", port=5001)
def lemmas(doc: Doc):
lemma_dict = dict()
for sent, i in zip(doc.sents, range(len(list(doc.sents)))):
for token in sent:
lemma = token.lemma_
text = token.text
if lemma not in lemma_dict:
lemma_dict[lemma] = {text: {i}}
else:
if text not in lemma_dict[lemma]:
lemma_dict[lemma][text] = {i}
else:
lemma_dict[lemma][text].add(i)
print("A lemma that appears in more than 2 forms:")
for lemma in lemma_dict:
if lemma == "murder":
print("***", lemma)
for inflection in lemma_dict[lemma]:
print("\t***", inflection)
for i in lemma_dict[lemma][inflection]:
print("\t\t", list(doc.sents)[i])
break
def counter_to_relative(counter):
"""
get relative frequencies of counter
"""
total_count = sum(counter.values())
relative = {}
for key in counter:
relative[key] = counter[key] / total_count
return relative
def part_of_the_speech(doc):
pos_frequencies = Counter()
pos_tags_dict = {}
pos_tags = []
for sentence in doc.sents:
# pos_tags = [token.pos_ for token in sentence]
for token in sentence:
tokens = []
pos_tags.append(token.pos_)
tokens.append(token.text)
if not token.pos_ in pos_tags_dict.keys():
pos_tags_dict[token.pos_] = Counter()
pos_tags_dict[token.pos_].update(tokens)
pos_frequencies.update(pos_tags)
relative_freq = counter_to_relative(pos_frequencies)
# print(pos_tags_dict)
# print("most common verbs: \n",pos_tags_dict['VERB'].most_common(3),"\n")
# print("less common verbs: \n",pos_tags_dict['VERB'].most_common()[-1],"\n")
# print("pos_frecuencies:\n",pos_frequencies,"\n")
# print("relative freq:\n",relative_freq,"\n")
# print("sum: \n",sum(pos_frequencies.values()))
return tabulate(
[
[
pos,
pos_frequencies[pos],
round(relative_freq[pos], 2),
pos_tags_dict[pos].most_common(3),
pos_tags_dict[pos].most_common()[-1],
]
for pos in pos_tags_dict.keys()
],
headers=[
"Finegrained POS-tag",
"Occurrences",
"Relative Tag Frequency (%)",
"3 Most frequent tokens",
"Infrequent token",
],
)
def extract_basic_statistics():
"""
exercise 7
"""
nlp = spacy.load("en_core_web_sm")
df = pd.read_csv("data/original/english/WikiNews_Train.tsv", sep="\t", header=None)
# Number of instances labeled with 0:
print("The number of instances labeled with 0: ", len(df.loc[df.iloc[:, 9] == 0]))
# Number of instances labeled with 1:
print("\nThe number of instances labeled with 1: ", len(df.loc[df.iloc[:, 9] == 1]))
# Min, max, median, mean, and stdev of the probabilistic label
print("\nProbabilistic Label:")
print("\t max=", df.iloc[:, 10].max())
print("\t min=", df.iloc[:, 10].min())
print("\t median=", df.iloc[:, 10].median())
print("\t mean=", round(df.iloc[:, 10].mean(), 2))
print("\t stdev=", round(df.iloc[:, 10].std(), 2))
# Number of instances consisting of more than one token
target_word_column = df.iloc[:,4]
tokens = []
lemma = []
pos = []
for doc in nlp.pipe(target_word_column.astype('unicode').values, batch_size=50):
if doc.is_parsed:
tokens.append([n.text for n in doc])
lemma.append([n.lemma_ for n in doc])
pos.append([n.pos_ for n in doc])
else:
# We want to make sure that the lists of parsed results have the
# same number of entries of the original Dataframe, so add some blanks in case the parse fails
tokens.append(None)
lemma.append(None)
pos.append(None)
df['species_tokens'] = tokens
df['species_lemma'] = lemma
df['species_pos'] = pos
print(
"\nNumber of instances consisting of more than one token: ", len(df.loc[df['species_tokens'].str.len() > 1])
)
print(
"Maximum number of tokens for an instance: ", df['species_tokens'][df['species_tokens'].str.len() > 9]
)
def explore_linguistic_characteristics():
"""
exercise 8
"""
nlp = spacy.load("en_core_web_sm")
df = pd.read_csv("data/original/english/WikiNews_Train.tsv", sep="\t", header=None)
target_word_column = df.iloc[:,4]
tokens = []
lemma = []
pos = []
for doc in nlp.pipe(target_word_column.astype('unicode').values, batch_size=50):
# if doc.is_parsed:
if doc.has_annotation("DEP"):
tokens.append([n.text for n in doc])
pos.append([n.pos_ for n in doc][0])
else:
tokens.append(None)
df.loc[:,'tokens'] = tokens
df.loc[:,'POS'] = pos
# We will focus on the instances which consist only of a single token and have been labeled as complex by at least one annotator.
# First, single token
single_token_df = df.loc[df['tokens'].str.len() == 1]
# then, 7th and 8th (starting at 0) columns show the number of native annotators and the number of non-native annotators who marked the target word as difficult.
single_token_df = single_token_df.rename(columns={single_token_df.columns[7]:'native',single_token_df.columns[8]:'no-native'})
# complex-sigle-token (cst)
cst_df = single_token_df[(single_token_df['native'] > 0) | (single_token_df['no-native'] > 0)]
token_lengths = []
token_freqs = []
for token in cst_df['tokens']:
token_lengths.append(len(token[0]))
token_freqs.append(word_frequency(token[0], 'en', wordlist='best', minimum=0.0))
cst_df.loc[:,'length'] = token_lengths
cst_df.loc[:,'freq'] = token_freqs
print(cst_df.head(20))
pearson_length_prob , pvalue1 = pearsonr(cst_df['length'], cst_df.iloc[:,10])
pearson_freq_prob , pvalue2 = pearsonr(cst_df['freq'], cst_df.iloc[:,10])
print("\n\n-Pearson correlation between length and complexity: ",round(pearson_length_prob,2))
print("-Pearson correlation between frequency and complexity: ",round(pearson_freq_prob,2))
plt.scatter(cst_df['length'], cst_df.iloc[:,10])
plt.title("Scatter Plot of Probabilistic Complexity Label vs Word Length")
plt.xlabel("Word Length")
plt.ylabel("Probabilistic Complexity Label")
# plt.show()
plt.title("Scatter Plot of Probabilistic Complexity Label vs Word Frequency")
plt.xlabel("Word Frequency")
plt.scatter(cst_df['freq'], cst_df.iloc[:,10])
# plt.show()
plt.title("Scatter Plot of Probabilistic Complexity Label vs POS Tag")
plt.xlabel("POS Tag")
plt.scatter(cst_df['POS'], cst_df.iloc[:,10])
# plt.show()
def main():
nlp = spacy.load("en_core_web_sm")
with open(
"data/preprocessed/train/sentences.txt", "r", encoding="utf8"
) as text_file:
text_data = text_file.read()
# Replacing newline characters with a space, otherwise the newline character becomes a very common token
text_data = text_data.replace("\n", " ")
# Getting rid of the escape backslash, because we do not think it is a part of Natural Language
text_data = text_data.replace("\\", "")
doc = nlp(text_data)
# print("Number of tokens:", num_tokens(doc))
# print("Number of types:", num_types(doc))
# print("Number of words:", num_words(doc))
# print("Average number of words per sentence:", round(avg_words_sentence(doc), 2))
# print("Average word length:", round(avg_word_length(doc), 2))
# print("\nToken bigrams:", token_bigrams(doc))
# print("Token trigrams:", token_trigrams(doc))
# print("\nPOS bigrams:", pos_bigrams(doc))
# print("POS trigrams:", pos_trigrams(doc))
# print("\nNumber of named entities:", n_named_entites(doc))
# print("Number of different entity labels:", n_unique_labels(doc))
# # Uncomment to visualize NER on first 5 sentences
# # visualize(doc)
# lemmas(doc)
# print("Word Classes - Most frequent POS tags: \n",part_of_the_speech(doc))
# extract_basic_statistics()
# explore_linguistic_characteristics()
if __name__ == "__main__":
main()