forked from awasthiabhijeet/PIE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform_suffixes.py
691 lines (538 loc) · 22.1 KB
/
transform_suffixes.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
"""Transform functions for replace."""
import pickle
def is_append_suffix(suffix,w1,w2):
l = len(suffix)
if (w2[-l:] == suffix) and (w1 == w2[:-l]):
return True
else:
return False
def is_transform_suffix(suffix_1,suffix_2, w1, w2):
l1 = len(suffix_1)
l2 = len(suffix_2)
if (w1[-l1:] == suffix_1) and (w2[-l2:] == suffix_2) and (w1[:-l1] == w2[:-l2]):
return True
else:
return False
def append_suffix(word, suffix):
#print("word: {}, suffix: {}".format(word, suffix))
if len(word) == 1:
print("We currently fear appending suffix: {} to a one letter word, but still doing it: {}".format(suffix, word))
#return word
l = len(suffix)
#if word[-l:] == suffix:
#print("**** WARNING: SUFFIX: {} ALREADY PRESENT in WORD, BUT still adding it: {} ****".format(suffix, word))
if word[-1] == "s" and suffix == "s":
return word+"es"
if word[-1] == "y" and suffix == "s" and len(word)>2:
if word[-2] not in ["a","e","i","o","u"]:
return word[0:-1] + "ies"
#if word[-1] == "h" and suffix == "s":
# return word + "es"
#if word[-1] == "t" and suffix == "d":
# return word + "ed"
#if word[-1] == "k" and suffix == "d":
# return word + "ed"
return word+suffix
def remove_suffix(word, suffix):
if len(suffix) > len(word):
print("suffix: {} to be removed has larger length than word: {}".format(suffix, word))
return word
l = len(suffix)
if word[-l:] == suffix:
return word[:-l]
else:
print("**** WARNING: SUFFIX : {} NOT PRESENT in WORD: {} ****".format(suffix, word))
return word
def transform_suffix(word, suffix_1, suffix_2):
if len(suffix_1) > len(word):
print("suffix: {} to be replaced has larger length than word: {}".format(suffix_1, word))
return word
l1 = len(suffix_1)
l2 = len(suffix_2)
if word[-l1:] == suffix_1:
return word[:-l1] + suffix_2
else:
print("transform")
print("**** WARNING: SUFFIX : {} NOT PRESENT in WORD: {} ****".format(suffix_1, word))
return word
class SuffixTransform():
"""Helper to find if a replacement in a sentence matches a predefined transform."""
def __init__(self, src_word, tgt_word, opcodes):
"""Create a new transform matching instance
:src_word : original word
:tgt_word : modified word
"""
self.src_word = src_word
self.tgt_word = tgt_word
self.opcodes = opcodes
def match(self):
"""Returns an opcode if matches."""
if self.src_word == self.tgt_word:
return None
'''
return self.pluralization() or self.singularization() \
or self.capitalization() or self.decapitalization() \
or self.verb_transform() or None
'''
return self.append_s() or self.remove_s() \
or self.append_d() or self.remove_d() \
or self.append_es() or self.remove_es() \
or self.append_ing() or self.remove_ing() \
or self.append_ed() or self.remove_ed() \
or self.append_ly() or self.remove_ly() \
or self.append_er() or self.remove_er() \
or self.append_al() or self.remove_al() \
or self.append_n() or self.remove_n() \
or self.append_y() or self.remove_y() \
or self.append_ation() or self.remove_ation() \
or self.e_to_ing() or self.ing_to_e() \
or self.d_to_t() or self.t_to_d() \
or self.d_to_s() or self.s_to_d() \
or self.s_to_ing() or self.ing_to_s() \
or self.n_to_ing() or self.ing_to_n() \
or self.nce_to_t() or self.t_to_nce() \
or self.s_to_ed() or self.ed_to_s() \
or self.ing_to_ed() or self.ed_to_ing() \
or self.ing_to_ion() or self.ion_to_ing() \
or self.ing_to_ation() or self.ation_to_ing() \
or self.t_to_ce() or self.ce_to_t() \
or self.y_to_ic() or self.ic_to_y() \
or self.t_to_s() or self.s_to_t() \
or self.e_to_al() or self.al_to_e() \
or self.y_to_ily() or self.ily_to_y() \
or self.y_to_ied() or self.ied_to_y() \
or self.y_to_ical() or self.ical_to_y() \
or self.y_to_ies() or self.ies_to_y() \
or None
def e_to_ing(self):
if is_transform_suffix("e","ing",self.src_word,self.tgt_word):
return self.opcodes.E_TO_ING
else:
return None
def ing_to_e(self):
if is_transform_suffix("ing","e",self.src_word,self.tgt_word):
return self.opcodes.ING_TO_E
else:
return None
def d_to_t(self):
if is_transform_suffix("d","t",self.src_word,self.tgt_word):
return self.opcodes.D_TO_T
else:
return None
def t_to_d(self):
if is_transform_suffix("t","d",self.src_word,self.tgt_word):
return self.opcodes.T_TO_D
else:
return None
def d_to_s(self):
if is_transform_suffix("d","s",self.src_word,self.tgt_word):
return self.opcodes.D_TO_S
else:
return None
def s_to_d(self):
if is_transform_suffix("s","d",self.src_word,self.tgt_word):
return self.opcodes.S_TO_D
else:
return None
def s_to_ing(self):
if is_transform_suffix("s","ing",self.src_word,self.tgt_word):
return self.opcodes.S_TO_ING
else:
return None
def ing_to_s(self):
if is_transform_suffix("ing","s",self.src_word,self.tgt_word):
return self.opcodes.ING_TO_S
else:
return None
def n_to_ing(self):
if is_transform_suffix("n","ing",self.src_word,self.tgt_word):
return self.opcodes.N_TO_ING
else:
return None
def ing_to_n(self):
if is_transform_suffix("ing","n",self.src_word,self.tgt_word):
return self.opcodes.ING_TO_N
else:
return None
def t_to_nce(self):
if is_transform_suffix("t","nce",self.src_word,self.tgt_word):
return self.opcodes.T_TO_NCE
else:
return None
def nce_to_t(self):
if is_transform_suffix("nce","t",self.src_word,self.tgt_word):
return self.opcodes.NCE_TO_T
else:
return None
def s_to_ed(self):
if is_transform_suffix("s","ed",self.src_word,self.tgt_word):
return self.opcodes.S_TO_ED
else:
return None
def ed_to_s(self):
if is_transform_suffix("ed","s",self.src_word,self.tgt_word):
return self.opcodes.ED_TO_S
else:
return None
def ing_to_ed(self):
if is_transform_suffix("ing","ed",self.src_word,self.tgt_word):
return self.opcodes.ING_TO_ED
else:
return None
def ed_to_ing(self):
if is_transform_suffix("ed","ing",self.src_word,self.tgt_word):
return self.opcodes.ED_TO_ING
else:
return None
def ing_to_ion(self):
if is_transform_suffix("ing","ion",self.src_word,self.tgt_word):
return self.opcodes.ING_TO_ION
else:
return None
def ion_to_ing(self):
if is_transform_suffix("ion","ing",self.src_word,self.tgt_word):
return self.opcodes.ION_TO_ING
else:
return None
def ing_to_ation(self):
if is_transform_suffix("ing","ation",self.src_word,self.tgt_word):
return self.opcodes.ING_TO_ATION
else:
return None
def ation_to_ing(self):
if is_transform_suffix("ation","ing",self.src_word,self.tgt_word):
return self.opcodes.ATION_TO_ING
else:
return None
def t_to_ce(self):
if is_transform_suffix("t","ce",self.src_word,self.tgt_word):
return self.opcodes.T_TO_CE
else:
return None
def ce_to_t(self):
if is_transform_suffix("ce","t",self.src_word,self.tgt_word):
return self.opcodes.CE_TO_T
else:
return None
def y_to_ic(self):
if is_transform_suffix("y","ic",self.src_word,self.tgt_word):
return self.opcodes.Y_TO_IC
else:
return None
def ic_to_y(self):
if is_transform_suffix("ic","y",self.src_word,self.tgt_word):
return self.opcodes.IC_TO_Y
else:
return None
def t_to_s(self):
if is_transform_suffix("t","s",self.src_word,self.tgt_word):
return self.opcodes.T_TO_S
else:
return None
def s_to_t(self):
if is_transform_suffix("s","t",self.src_word,self.tgt_word):
return self.opcodes.S_TO_T
else:
return None
def e_to_al(self):
if is_transform_suffix("e","al",self.src_word,self.tgt_word):
return self.opcodes.E_TO_AL
else:
return None
def al_to_e(self):
if is_transform_suffix("al","e",self.src_word,self.tgt_word):
return self.opcodes.AL_TO_E
else:
return None
def y_to_ily(self):
if is_transform_suffix("y","ily",self.src_word,self.tgt_word):
return self.opcodes.Y_TO_ILY
else:
return None
def ily_to_y(self):
if is_transform_suffix("ily","y",self.src_word,self.tgt_word):
return self.opcodes.ILY_TO_Y
else:
return None
def y_to_ied(self):
if is_transform_suffix("y","ied",self.src_word,self.tgt_word):
return self.opcodes.Y_TO_IED
else:
return None
def ied_to_y(self):
if is_transform_suffix("ied","y",self.src_word,self.tgt_word):
return self.opcodes.IED_TO_Y
else:
return None
def y_to_ical(self):
if is_transform_suffix("y","ical",self.src_word,self.tgt_word):
return self.opcodes.Y_TO_ICAL
else:
return None
def ical_to_y(self):
if is_transform_suffix("ical","y",self.src_word,self.tgt_word):
return self.opcodes.ICAL_TO_Y
else:
return None
def y_to_ies(self):
if is_transform_suffix("y","ies",self.src_word,self.tgt_word):
return self.opcodes.Y_TO_IES
else:
return None
def ies_to_y(self):
if is_transform_suffix("ies","y",self.src_word,self.tgt_word):
return self.opcodes.IES_TO_Y
else:
return None
def append_s(self):
if is_append_suffix("s",self.src_word, self.tgt_word) and (self.src_word not in ["a","A","I","i"]):
return self.opcodes.APPEND_s
else:
return None
def remove_s(self):
if is_append_suffix("s",self.tgt_word,self.src_word) and (self.src_word not in ["As","as","is","Is"]):
return self.opcodes.REMOVE_s
else:
return None
def append_d(self):
if is_append_suffix("d",self.src_word, self.tgt_word):
return self.opcodes.APPEND_d
else:
return None
def remove_d(self):
if is_append_suffix("d",self.tgt_word,self.src_word):
return self.opcodes.REMOVE_d
else:
return None
def append_es(self):
if is_append_suffix("es",self.src_word, self.tgt_word):
return self.opcodes.APPEND_es
else:
return None
def remove_es(self):
if is_append_suffix("es",self.tgt_word,self.src_word):
return self.opcodes.REMOVE_es
else:
return None
def append_ing(self):
if is_append_suffix("ing",self.src_word, self.tgt_word):
return self.opcodes.APPEND_ing
else:
return None
def remove_ing(self):
if is_append_suffix("ing",self.tgt_word,self.src_word):
return self.opcodes.REMOVE_ing
else:
return None
def append_ed(self):
if is_append_suffix("ed",self.src_word, self.tgt_word):
return self.opcodes.APPEND_ed
else:
return None
def remove_ed(self):
if is_append_suffix("ed",self.tgt_word,self.src_word):
return self.opcodes.REMOVE_ed
else:
return None
def append_ly(self):
if is_append_suffix("ly",self.src_word, self.tgt_word):
return self.opcodes.APPEND_ly
else:
return None
def remove_ly(self):
if is_append_suffix("ly",self.tgt_word,self.src_word):
return self.opcodes.REMOVE_ly
else:
return None
def append_er(self):
if is_append_suffix("er",self.src_word, self.tgt_word):
return self.opcodes.APPEND_er
else:
return None
def remove_er(self):
if is_append_suffix("er",self.tgt_word,self.src_word):
return self.opcodes.REMOVE_er
else:
return None
def append_al(self):
if is_append_suffix("al",self.src_word, self.tgt_word):
return self.opcodes.APPEND_al
else:
return None
def remove_al(self):
if is_append_suffix("al",self.tgt_word,self.src_word):
return self.opcodes.REMOVE_al
else:
return None
def append_n(self):
if is_append_suffix("n",self.src_word, self.tgt_word) and (self.src_word not in ["a","A","i","I"]):
return self.opcodes.APPEND_n
else:
return None
def remove_n(self):
if is_append_suffix("n",self.tgt_word,self.src_word) and (self.src_word not in ["an","An","in","In"]):
return self.opcodes.REMOVE_n
else:
return None
def append_y(self):
if is_append_suffix("y",self.src_word, self.tgt_word) and (self.src_word not in ["m","M"]):
return self.opcodes.APPEND_y
else:
return None
def remove_y(self):
if is_append_suffix("y",self.tgt_word,self.src_word) and (self.src_word not in ["My","my"]):
return self.opcodes.REMOVE_y
else:
return None
def append_ation(self):
if is_append_suffix("ation",self.src_word, self.tgt_word):
return self.opcodes.APPEND_ation
else:
return None
def remove_ation(self):
if is_append_suffix("ation",self.tgt_word,self.src_word):
return self.opcodes.REMOVE_ation
else:
return None
def apply_transform(uncorrected, uposition, opcode, opcodes):
"""Tries to apply an opcode to a word or returns None
:param uncorrected: Tokenized uncorrected sentence
:uposition: Position of replaced in uncorrected sentence
:opcode: Opcode to try applying
"""
art = ApplySuffixTransorm(uncorrected,uposition, opcode, opcodes)
return art.apply()
class ApplySuffixTransorm():
def __init__(self, uncorrected, uposition, opcode, opcodes):
"""Tries to apply an opcode to a word or returns None
:param uncorrected: Tokenized uncorrected sentence
:uposition: Position of replaced in uncorrected sentence
:opcode: Opcode to try applying
"""
self.uncorrected = uncorrected
self.uposition = uposition
self.opcode = opcode
self.src_word = uncorrected[uposition]
self.opcodes = opcodes
def apply(self):
"""Try to apply the transform
:return: Transformed word or None if cannot transform
"""
transformed = None
if self.opcode == self.opcodes.APPEND_s:
transformed = append_suffix(self.src_word, "s")
if self.opcode == self.opcodes.APPEND_d:
transformed = append_suffix(self.src_word, "d")
if self.opcode == self.opcodes.APPEND_es:
transformed = append_suffix(self.src_word, "es")
if self.opcode == self.opcodes.APPEND_ing:
transformed = append_suffix(self.src_word, "ing")
if self.opcode == self.opcodes.APPEND_ed:
transformed = append_suffix(self.src_word, "ed")
if self.opcode == self.opcodes.APPEND_ly:
transformed = append_suffix(self.src_word, "ly")
if self.opcode == self.opcodes.APPEND_er:
transformed = append_suffix(self.src_word, "er")
if self.opcode == self.opcodes.APPEND_al:
transformed = append_suffix(self.src_word, "al")
if self.opcode == self.opcodes.APPEND_n:
transformed = append_suffix(self.src_word, "n")
if self.opcode == self.opcodes.APPEND_y:
transformed = append_suffix(self.src_word, "y")
if self.opcode == self.opcodes.APPEND_ation:
transformed = append_suffix(self.src_word, "ation")
if self.opcode == self.opcodes.REMOVE_s:
transformed = remove_suffix(self.src_word, "s")
if self.opcode == self.opcodes.REMOVE_d:
transformed = remove_suffix(self.src_word, "d")
if self.opcode == self.opcodes.REMOVE_es:
transformed = remove_suffix(self.src_word, "es")
if self.opcode == self.opcodes.REMOVE_ing:
transformed = remove_suffix(self.src_word, "ing")
if self.opcode == self.opcodes.REMOVE_ed:
transformed = remove_suffix(self.src_word, "ed")
if self.opcode == self.opcodes.REMOVE_ly:
transformed = remove_suffix(self.src_word, "ly")
if self.opcode == self.opcodes.REMOVE_er:
transformed = remove_suffix(self.src_word, "er")
if self.opcode == self.opcodes.REMOVE_al:
transformed = remove_suffix(self.src_word, "al")
if self.opcode == self.opcodes.REMOVE_n:
transformed = remove_suffix(self.src_word, "n")
if self.opcode == self.opcodes.REMOVE_y:
transformed = remove_suffix(self.src_word, "y")
if self.opcode == self.opcodes.REMOVE_ation:
transformed = remove_suffix(self.src_word, "ation")
if self.opcode == self.opcodes.E_TO_ING:
transformed = transform_suffix(self.src_word, "e", "ing")
if self.opcode == self.opcodes.ING_TO_E:
transformed = transform_suffix(self.src_word, "ing", "e")
if self.opcode == self.opcodes.D_TO_T:
transformed = transform_suffix(self.src_word, "d", "t")
if self.opcode == self.opcodes.T_TO_D:
transformed = transform_suffix(self.src_word, "t", "d")
if self.opcode == self.opcodes.D_TO_S:
transformed = transform_suffix(self.src_word, "d", "s")
if self.opcode == self.opcodes.S_TO_D:
transformed = transform_suffix(self.src_word, "s", "d")
if self.opcode == self.opcodes.S_TO_ING:
transformed = transform_suffix(self.src_word, "s", "ing")
if self.opcode == self.opcodes.ING_TO_S:
transformed = transform_suffix(self.src_word, "ing", "s")
if self.opcode == self.opcodes.N_TO_ING:
transformed = transform_suffix(self.src_word, "n", "ing")
if self.opcode == self.opcodes.ING_TO_N:
transformed = transform_suffix(self.src_word, "ing", "n")
if self.opcode == self.opcodes.T_TO_NCE:
transformed = transform_suffix(self.src_word, "t", "nce")
if self.opcode == self.opcodes.NCE_TO_T:
transformed = transform_suffix(self.src_word, "nce", "t")
if self.opcode == self.opcodes.S_TO_ED:
transformed = transform_suffix(self.src_word, "s", "ed")
if self.opcode == self.opcodes.ED_TO_S:
transformed = transform_suffix(self.src_word, "ed", "s")
if self.opcode == self.opcodes.ING_TO_ED:
transformed = transform_suffix(self.src_word, "ing", "ed")
if self.opcode == self.opcodes.ED_TO_ING:
transformed = transform_suffix(self.src_word, "ed", "ing")
if self.opcode == self.opcodes.ING_TO_ION:
transformed = transform_suffix(self.src_word, "ing", "ion")
if self.opcode == self.opcodes.ION_TO_ING:
transformed = transform_suffix(self.src_word, "ion", "ing")
if self.opcode == self.opcodes.ING_TO_ATION:
transformed = transform_suffix(self.src_word, "ing", "ation")
if self.opcode == self.opcodes.ATION_TO_ING:
transformed = transform_suffix(self.src_word, "ation", "ing")
if self.opcode == self.opcodes.T_TO_CE:
transformed = transform_suffix(self.src_word, "t", "ce")
if self.opcode == self.opcodes.CE_TO_T:
transformed = transform_suffix(self.src_word, "ce", "t")
if self.opcode == self.opcodes.Y_TO_IC:
transformed = transform_suffix(self.src_word, "y", "ic")
if self.opcode == self.opcodes.IC_TO_Y:
transformed = transform_suffix(self.src_word, "ic", "y")
if self.opcode == self.opcodes.T_TO_S:
transformed = transform_suffix(self.src_word, "t", "s")
if self.opcode == self.opcodes.S_TO_T:
transformed = transform_suffix(self.src_word, "s", "t")
if self.opcode == self.opcodes.E_TO_AL:
transformed = transform_suffix(self.src_word, "e", "al")
if self.opcode == self.opcodes.AL_TO_E:
transformed = transform_suffix(self.src_word, "al", "e")
if self.opcode == self.opcodes.Y_TO_ILY:
transformed = transform_suffix(self.src_word, "y", "ily")
if self.opcode == self.opcodes.ILY_TO_Y:
transformed = transform_suffix(self.src_word, "ily", "y")
if self.opcode == self.opcodes.Y_TO_IED:
transformed = transform_suffix(self.src_word, "y", "ied")
if self.opcode == self.opcodes.IED_TO_Y:
transformed = transform_suffix(self.src_word, "ied", "y")
if self.opcode == self.opcodes.Y_TO_ICAL:
transformed = transform_suffix(self.src_word, "y", "ical")
if self.opcode == self.opcodes.ICAL_TO_Y:
transformed = transform_suffix(self.src_word, "ical", "y")
if self.opcode == self.opcodes.Y_TO_IES:
transformed = transform_suffix(self.src_word, "y", "ies")
if self.opcode == self.opcodes.IES_TO_Y:
transformed = transform_suffix(self.src_word, "ies", "y")
return transformed or None