-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefix_suffix_improved.py
206 lines (155 loc) · 6.53 KB
/
prefix_suffix_improved.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
#!/usr/bin/env python
#
# File Name : prefix_suffix_improved.py
#
# Description : IMPORVED PREFIX SUFFIX APPROACH (IPS)
#
# Creation Date : 02-09-2022
# Authors : Omar Ikne & Zakaria Boulkhir
## libraries
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
from scipy import stats
from tqdm import tqdm
import codecs
import os
import sys
from utils.utils import *
from utils.eval import *
import unidecode
from sklearn.ensemble import RandomForestClassifier
def get_prefix(form, lemma):
"""return the prefix from the given form and lemma"""
if lemma in form:
idx = form.index(lemma)
return form[:idx]
return ''
def get_suffix(form, lemma):
"""return the suffix from the given lemma and form"""
if lemma in form:
idx = form.index(lemma)
return form[idx + len(lemma):]
return ''
def remove_prefix(form, prefix):
"""remove the prefix from the form"""
if form.startswith(prefix):
return form[len(prefix):]
return form
def remove_suffix(form, suffix):
"""remove the suffix from the form"""
if suffix and form.endswith(suffix):
return form[:-len(suffix)]
return form
def get_lemma(form, prefix, suffix):
"""return the lemma from the form given the prefix and the suffix"""
lemma = remove_suffix(form, suffix)
lemma = remove_prefix(lemma, prefix)
return lemma
def get_predictions(X_test, y_pred):
pred_forms = []
for lemma, y in zip(X_test, y_pred):
prefix, suffix = y
form = prefix + lemma + suffix
pred_forms.append(form)
return pred_forms
def encode_attributes(attributes, max_n_attrs, attributes_dict):
"""(multi-)onehot encoding for attributes"""
encoded = np.zeros(len(attributes_dict))
encoded[[attributes_dict[attr] for attr in attributes]] = 1
return encoded
def create_trainset(lemmas, forms, attributes, max_lemma, max_form, max_attrs, char_dict, attr_dict):
"""create a bag of words training set"""
## create X and y train
X_train, y_train = [], []
for lemma, form, set_attrs in zip(lemmas, forms, attributes):
x, y = [], []
l = []
for char in lemma:
l.append(char_dict[char])
for char in form:
y.append(char_dict[char])
at = []
for attr in set_attrs:
at.append(attr_dict[attr])
x = np.append(pad(l, max_lemma, 0), pad(at, max_attrs, 0))
X_train.append(x)
y_train.append(pad(y, max_form, 0))
return np.array(X_train), np.array(y_train)
def prefix_suffix(train_file, test_file, verbose=1):
""""""
## dataframe
df_train = read_file(train_file)
df_test = read_file(test_file)
## number of training & testing samples
n_train = df_train.shape[0]
n_test = df_test.shape[0]
## get the number of unique characters
text = ''.join(df_train[['lemma', 'form']].to_numpy().flatten())
## get (number of) unique characters
unique_chars = sorted(set(text))
n_chars = len(unique_chars)
## get unique morphological attributes
morph_attrs = ';'.join(df_train['attributes'].to_list()).split(';')
morph_attrs = np.asarray(morph_attrs)
unique_attrs = sorted(set(morph_attrs)) ## sort to keep same order
n_attrs = len(unique_attrs)
if verbose >= 1:
print(f'- Number of training samples: {n_train}')
print(f'- Number of testing samples : {n_test}')
print(f"- Number of unique characters: {n_chars}")
print(f"- Number of unique morphological attributes: {n_attrs}")
if verbose == 2:
print(f"- Morphological attributes: {', '.join(unique_attrs)}")
print(f"- Characters: {', '.join(unique_chars)}")
df_train['prefix'] = df_train.apply(lambda col: get_prefix(col.form, col.lemma), axis=1)
df_train['suffix'] = df_train.apply(lambda col: get_suffix(col.form, col.lemma), axis=1)
## bag of words for characters
char_dict = dict(zip(unique_chars, range(1, len(unique_chars)+1)))
inv_char_dict = {n:char for char, n in char_dict.items()}
## bag of words for attributes
attr_dict = {attr:i for i, attr in enumerate(unique_attrs, start=1)}
## compute maximum possible characters in a lemma and a form
max_lemma_length = df_train['lemma'].apply(list).apply(len).max()
max_form_length = df_train['form'].apply(lambda x: len(list(x))).max()
## compute maximum possible number of attributes
max_n_attrs = df_train['attributes'].apply(lambda x: len(x.split(';'))).max()
## get training & test set
X_train, y_train = create_trainset(df_train['lemma'].values,
df_train['form'].values,
df_train['attributes'].apply(lambda x:x.split(';')).values,
max_lemma_length, max_form_length, max_n_attrs,
char_dict, attr_dict)
X_test, y_test = create_trainset(df_test['lemma'].values,
df_test['form'].values,
df_test['attributes'].apply(lambda x:x.split(';')).values,
max_lemma_length, max_form_length, max_n_attrs,
char_dict, attr_dict)
## create target array
y_train = df_train[['prefix', 'suffix']].to_numpy()
## bag of words for attributes
attributes_dict = dict(zip(unique_attrs, range(n_attrs)))
## encode attributes
encoded_attributes = np.array([encode_attributes(attrs.split(';'), max_n_attrs, attributes_dict) for attrs in df_train['attributes'].to_list()])
encoded_test_attributes = np.array([encode_attributes(attrs.split(';'), max_n_attrs, attributes_dict) for attrs in df_test['attributes'].to_list()])
## new training data
new_X_train = np.concatenate((X_train[:, :max_lemma_length], encoded_attributes), axis=1)
new_X_test = np.concatenate((X_test[:, :max_lemma_length], encoded_test_attributes), axis=1)
## create & fit an RF model
clf = RandomForestClassifier(random_state=0)
clf.fit(new_X_train, y_train)
## make predictions
y_pred = clf.predict(new_X_test)
## vector to word
words_test = [vect2word(vect, inv_char_dict) for vect in y_test]
words_prediction = get_predictions(df_test['lemma'].to_numpy(), y_pred)
print(f'- The word by word accuracy : {word_accuracy(words_prediction, words_test)}')
print(f'- The character by character accuracy: {character_accuracy(words_prediction, words_test)}')
if __name__ == "__main__":
train_file = sys.argv[1]
test_file = sys.argv[2]
verbose = 1
if len(sys.argv) > 3:
verbose = int(sys.argv[3])
prefix_suffix(train_file, test_file, verbose)