-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
179 lines (144 loc) · 6.26 KB
/
utils.py
File metadata and controls
179 lines (144 loc) · 6.26 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
import numpy as np
import scipy as sp
from scipy.optimize import minimize
import pandas as pd
import matplotlib.pyplot as plt
import re, csv, string
from tqdm import tqdm
import pickle
import datetime
from itertools import starmap
from collections import Counter
def start_cap(token: str, curr_pos=None, t=None, T=None, y=None, y_=None) -> bool:
return token[:1].isupper()
def end_ing(token: str, curr_pos=None, t=None, T=None, y=None, y_=None) -> bool:
return token[-3:] == 'ing'
def is_punct(token: str, curr_pos=None, t=None, T=None, y=None, y_=None) -> bool:
return token in ['.', ',', '``', '`', ':', '$']
def is_digit(token: str, curr_pos=None, t=None, T=None, y=None, y_=None) -> bool:
return token.isdigit()
def is_start(token: str, curr_pos=None, t=None, T=None, y=None, y_=None) -> bool:
return t==0
def is_end(token: str, curr_pos=None, t=None, T=None, y=None, y_=None) -> bool:
return t==T-1
obs_funcs = [
start_cap,
end_ing,
is_punct,
is_digit,
is_start,
is_end
]
def parse(filename:str, numlines=None):
parsed_data = {}
all_NER_tags = set()
all_POS = set()
i = 0
with open(filename, 'r', encoding='utf-8') as file:
reader = csv.DictReader(file)
for row in reader:
i+=1
if (i==numlines): break
match = re.match(r'Sentence: (\d+)', row['Sentence #'])
sentence_id = int(match.group(1)) if match else None
words = row['Sentence'].split()
pos_tags = eval(row['POS']) # convert POS tags string to list
ner_tags = eval(row['Tag']) # convert NER tags string to list
if (len(words) != len(pos_tags)) or (len(ner_tags) != len(pos_tags)) or ((len(words) != len(ner_tags))):
print(len(words), len(pos_tags), len(ner_tags))
else:
if sentence_id not in parsed_data:
parsed_data[sentence_id] = {"Tokens": [], "POS": [], "NER_tags": []}
parsed_data[sentence_id]["Tokens"].extend(words)
parsed_data[sentence_id]["POS"].extend(pos_tags)
parsed_data[sentence_id]["NER_tags"].extend(ner_tags)
# print(f'{len(ner_tags) == len(pos_tags)}')
for t in ner_tags:
all_NER_tags.add(t)
for pos in pos_tags:
all_POS.add(pos)
all_NER_tags = list(all_NER_tags)
all_POS = list(all_POS)
parsed_data = pd.DataFrame(parsed_data).T
return parsed_data, all_NER_tags, all_POS
import numpy as np
import matplotlib.pyplot as plt
def plot_weights(W, n, p, o, ner_list, pos_list, obs_funcs):
# Ensure correct indexing of W
n2 = n * n
n_p = n*p
W = W.detach().numpy()
matrix_part = np.reshape(W[:n2], (n, n)) # Reshape properly
extra_rows = np.reshape(W[n2:n2 + 2 * n], (2, n)) # Reshape properly
p_array = np.reshape(W[n2 + 2 * n: n2 + 2 * n + n_p], (p, n)) # Ensure it’s 2D
o_list = np.transpose(np.reshape(W[n2 + 2 * n + n_p:], (n, o))) # Ensure it’s 2D
# Create a unified color scale for all parts
all_values = np.concatenate([
matrix_part.flatten(),
extra_rows.flatten(),
p_array.flatten(),
o_list.flatten()
])
vmin, vmax = np.min(all_values), np.max(all_values)
# Create a new figure for the visualization
fig, axs = plt.subplots(4, 1, figsize=(10, 7), gridspec_kw={'height_ratios': [n, 2, p, o]})
#Define common tick label styling
tick_label_fontsize = 6 # Smaller font
tick_label_rotation = 0 # Slanted labels
# Plot the n x n matrix as a heatmap
ax = axs[0]
im1 = ax.imshow(matrix_part, cmap="viridis", aspect="auto")
ax.set_title("Transition matrix", fontsize=tick_label_fontsize)
ax.set_xticks(range(n))
ax.set_yticks(range(n))
ax.set_xticklabels(ner_list, fontsize=tick_label_fontsize, rotation=tick_label_rotation)
ax.set_yticklabels(ner_list, fontsize=tick_label_fontsize)
fig.colorbar(im1, ax=ax, orientation="vertical")
# Plot the extra rows as a heatmap
ax = axs[1]
im2 = ax.imshow(extra_rows, cmap="viridis", aspect="auto")
ax.set_title("Special transitions", fontsize=tick_label_fontsize)
ax.set_xticks(range(n))
ax.set_yticks(range(2))
ax.set_xticklabels(ner_list, fontsize=tick_label_fontsize, rotation=tick_label_rotation)
ax.set_yticklabels(["From BOS", "To EOS"], fontsize=tick_label_fontsize)
fig.colorbar(im2, ax=ax, orientation="vertical")
# Plot the list p as a row matrix heatmap
ax = axs[2]
im3 = ax.imshow(p_array, cmap="viridis", aspect="auto", vmin=vmin, vmax=vmax)
ax.set_title("POS emission", fontsize=tick_label_fontsize)
ax.set_yticks(range(p))
ax.set_xticks(range(n))
ax.set_xticklabels(ner_list, fontsize=tick_label_fontsize, rotation=tick_label_rotation)
ax.set_yticklabels(pos_list, fontsize=tick_label_fontsize)
fig.colorbar(im3, ax=ax, orientation="vertical")
# Plot the list of size o as a row matrix heatmap
ax = axs[3]
im4 = ax.imshow(o_list, cmap="viridis", aspect="auto", vmin=vmin, vmax=vmax)
ax.set_title("Observation functions", fontsize=tick_label_fontsize)
# ax.set_xticks(range(o))
ax.set_yticks(range(o))
ax.set_xticks(range(n))
# ax.set_yticks([0])
# ax.set_xticklabels(obs_funcs)
ax.set_xticklabels(ner_list, fontsize=tick_label_fontsize, rotation=tick_label_rotation)
ax.set_yticklabels(obs_funcs, fontsize=tick_label_fontsize)
fig.colorbar(im4, ax=ax, orientation="vertical")
# Show the updated visualization
plt.tight_layout()
plt.show()
def compute_class_weights(Y_train, smoothing_factor=5.0):
"""Computes smoothed class weights and normalizes so that the highest weight is 1."""
label_counts = Counter(label for sentence in Y_train for label in sentence)
total_labels = sum(label_counts.values())
# Compute raw class weights
raw_weights = {
# label: np.log(1 + smoothing_factor * (total_labels / count))
label: (total_labels / count)
for label, count in label_counts.items()
}
# Normalize so that the largest weight is 1
alpha = 1.00
max_weight = max(raw_weights.values())
class_weights = {label: alpha*weight / max_weight for label, weight in raw_weights.items()}
return class_weights