-
Notifications
You must be signed in to change notification settings - Fork 8
/
difference-edited.txt
1719 lines (1586 loc) · 68 KB
/
difference-edited.txt
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
************************************************************************************
This is a ***edited*** patch file that compare revision 354acc1cfdd542142490afe40447cb6f40d2fd7c (Jul 6, 2017),
which produced our first h=2048,p=512 (code name "google") model, to a more recent
revision, 3a24bb0560b41e435bae5215c5c5556d5542134f (Dec 6, 2017).
The purpose is to identify any difference that could affect performance, should it
goes down.
************************************************************************************
diff --git a/model.py b/model.py
index 85f627a..b751d0c 100644
--- a/model.py
+++ b/model.py
@@ -1,15 +1,19 @@
import numpy as np
import tensorflow as tf
+import time
+import sys
+
+float_dtype = tf.float32
class DummyModelTrain(object):
'''
This is for testing GPU usage only. This model runs very trivial operations
- on GPU therefore its running time is mostly on CPU. Compared to WSDModelTrain,
+ on GPU therefore its running time is mostly on CPU. Compared to WSDModel,
this model should run much faster, otherwise you're spending too much time
on CPU.
'''
- def __init__(self, config, float_dtype):
+ def __init__(self, config):
self._x = tf.placeholder(tf.int32, shape=[None, None], name='x')
self._y = tf.placeholder(tf.int32, shape=[None], name='y')
self._subvocab = tf.placeholder(tf.int32, shape=[None], name='subvocab')
@@ -39,42 +43,77 @@ class DummyModelTrain(object):
def print_device_placement(self):
pass
-class WSDModelTrain(object):
+class WSDModel(object):
"""A LSTM WSD model designed for fast training."""
+ def _build_inputs(self):
+ # the names are for later reference when the model is loaded
+ # they might be used or not, doesn't hurt
+ self._lens = tf.placeholder(tf.int32, shape=[None], name='lens')
+ def _build_word_embeddings(self):
E_words = tf.get_variable("word_embedding",
- [config.vocab_size, config.emb_dims], dtype=float_dtype)
- outputs, _ = tf.nn.dynamic_rnn(cell, word_embs, dtype=float_dtype)
+ [self.config.vocab_size, self.config.emb_dims], dtype=float_dtype)
+
+ def _build_lstm_output(self):
+ if self.optimized and self.config.assume_same_lengths:
+ outputs, _ = tf.nn.dynamic_rnn(cell, self._word_embs,
+ dtype=float_dtype)
+ self._lstm_output = outputs[:,-1]
+ else:
+ outputs, _ = tf.nn.dynamic_rnn(cell, self._word_embs,
+ sequence_length=self._lens,
+ dtype=float_dtype)
+ last_output_indices = tf.stack([tf.range(tf.shape(self._x)[0]), self._lens-1], axis=1)
+ self._lstm_output = tf.gather_nd(outputs, last_output_indices)
+ self._initial_state = cell.zero_state(tf.shape(self._x)[0], float_dtype)
+
+ def _build_context_embs(self):
context_layer_weights = tf.get_variable("context_layer_weights",
- [config.hidden_size, config.emb_dims], dtype=float_dtype)
- self._predicted_context_embs = tf.matmul(outputs[:,-1], context_layer_weights,
+ [self.config.hidden_size, self.config.emb_dims], dtype=float_dtype)
+ self._predicted_context_embs = tf.matmul(self._lstm_output, context_layer_weights,
name='predicted_context_embs')
+
+ def _build_logits(self):
E_contexts = tf.get_variable("context_embedding",
- [config.vocab_size, config.emb_dims], dtype=float_dtype)
- subcontexts = tf.nn.embedding_lookup(E_contexts, self._subvocab)
- pre_probs = tf.matmul(self._predicted_context_embs, tf.transpose(subcontexts))
-
+ [self.config.vocab_size, self.config.emb_dims], dtype=float_dtype)
+ if self.optimized and self.config.sampled_softmax:
+ subcontexts = tf.nn.embedding_lookup(E_contexts, self._subvocab)
+ self._logits = tf.matmul(self._predicted_context_embs, tf.transpose(subcontexts))
+ else:
+ self._logits = tf.matmul(self._predicted_context_embs, tf.transpose(E_contexts))
+
+ def _build_cost(self):
self._cost = tf.reduce_mean(
tf.nn.sparse_softmax_cross_entropy_with_logits(
- logits=pre_probs, labels=self._y))
-
+ logits=self._logits, labels=self._y))
+ self._hit_at_100 = tf.reduce_mean(tf.cast(
+ tf.nn.in_top_k(self._logits, self._y, 100), float_dtype))
tvars = tf.trainable_variables()
grads, _ = tf.clip_by_global_norm(tf.gradients(self._cost, tvars),
- config.max_grad_norm)
- optimizer = tf.train.AdagradOptimizer(config.learning_rate)
+ self.config.max_grad_norm)
+ optimizer = tf.train.AdagradOptimizer(self.config.learning_rate)
+ self._global_step = tf.contrib.framework.get_or_create_global_step()
self._train_op = optimizer.apply_gradients(zip(grads, tvars),
- global_step=tf.contrib.framework.get_or_create_global_step())
- self._initial_state = cell.zero_state(tf.shape(self._x)[0], float_dtype)
-
- self.run_options = self.run_metadata = None
+ global_step=self._global_step)
def trace_timeline(self):
self.run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
@@ -87,17 +126,20 @@ class WSDModelTrain(object):
# resample the batches so that each token has equal chance to become target
# another effect is to randomize the order of batches
- sentence_lens = np.array([x.shape[1] for x, _, _, in data])
- samples = np.random.choice(len(data), size=len(data),
- p=sentence_lens/sentence_lens.sum())
+ if self.config.optimized_batches:
+ sentence_lens = np.array([x.shape[1] for x, _, _, _ in data])
+ samples = np.random.choice(len(data), size=len(data),
+ p=sentence_lens/sentence_lens.sum())
+ else:
+ samples = np.random.choice(len(data), size=len(data))
for batch_no, batch_id in enumerate(samples):
- x, y_all, subvocab = data[batch_id]
+ x, y_all, subvocab, lens = data[batch_id]
i = np.random.randint(x.shape[1])
y = y_all[:,i]
- old_xi = x[:,i].copy()
+ old_xi = x[:,i].copy() # old_xi might be different from y because of subvocab
x[:,i] = target_id
- feed_dict = {self._x: x, self._y: y, self._subvocab: subvocab}
+ feed_dict = {self._x: x, self._y: y, self._subvocab: subvocab, self._lens: lens}
state = session.run(self._initial_state, feed_dict)
c, h = self._initial_state
feed_dict[c] = state.c
@@ -130,64 +172,18 @@ class WSDModelTrain(object):
sess.run(self._train_op, feed_dict)
print("******** End of device placement ********")
diff --git a/perform_wsd.py b/perform_wsd.py
index 6e14dc4..2a7a027 100644
--- a/perform_wsd.py
+++ b/perform_wsd.py
@@ -1,21 +1,85 @@
import numpy as np
+import os
import tensorflow as tf
+import json
import argparse
import pickle
import pandas
from nltk.corpus import wordnet as wn
+from nltk.corpus.reader.wordnet import WordNetCorpusReader
from scipy import spatial
+import morpho_utils
+import tensor_utils as utils
parser = argparse.ArgumentParser(description='Perform WSD using LSTM model')
parser.add_argument('-m', dest='model_path', required=True, help='path to model trained LSTM model')
-# model_path = '/var/scratch/mcpostma/wsd-dynamic-sense-vector/output/lstm-wsd-small'
parser.add_argument('-v', dest='vocab_path', required=True, help='path to LSTM vocabulary')
-# vocab_path = '/var/scratch/mcpostma/wsd-dynamic-sense-vector/output/gigaword.1m-sents-lstm-wsd.index.pkl'
parser.add_argument('-c', dest='wsd_df_path', required=True, help='input path to dataframe wsd competition')
+parser.add_argument('-l', dest='log_path', required=True, help='path where exp settings are stored')
parser.add_argument('-s', dest='sense_embeddings_path', required=True, help='path where sense embeddings are stored')
parser.add_argument('-o', dest='output_path', required=True, help='path where output wsd will be stored')
+parser.add_argument('-r', dest='results', required=True, help='path where accuracy will be reported')
+parser.add_argument('-g', dest='gran', required=True, help='sensekey | synset')
+parser.add_argument('-f', dest='mfs_fallback', required=True, help='True or False')
+parser.add_argument('-t', dest='path_case_freq', help='path to pickle with case freq')
+parser.add_argument('-a', dest='use_case_strategy', help='set to True to use morphological strategy case')
+parser.add_argument('-p', dest='path_plural_freq', help='path to pickle with plural freq')
+parser.add_argument('-b', dest='use_number_strategy', help='set to True to use morphological strategy number')
+parser.add_argument('-y', dest='path_lp', help='path to lp output')
+parser.add_argument('-z', dest='use_lp', help='set to True to use label propagation')
+
+
args = parser.parse_args()
+args.mfs_fallback = args.mfs_fallback == 'True'
+case_strategy = args.use_case_strategy == 'True'
+number_strategy = args.use_number_strategy == 'True'
+lp_strategy = args.use_lp == 'True'
+
+case_freq = pickle.load(open(args.path_case_freq, 'rb'))
+plural_freq = pickle.load(open(args.path_plural_freq, 'rb'))
+lp_info = dict()
+
+the_wn_version = '30'
+# load relevant wordnet
+if '171' in args.wsd_df_path:
+ the_wn_version = '171'
+ cwd = os.path.dirname(os.path.realpath(__file__))
+ path_to_wn_dict_folder = os.path.join(cwd, 'scripts', 'wordnets', '171', 'WordNet-1.7.1', 'dict')
+ wn = WordNetCorpusReader(path_to_wn_dict_folder, None)
+
+
+with open(args.sense_embeddings_path + '.freq', 'rb') as infile:
+ meaning_freqs = pickle.load(infile)
+
+with open(args.log_path, 'w') as outfile:
+ json.dump(args.__dict__, outfile)
+
+
+def lp_output(row, lp_info, candidate_synsets, debug=False):
+ target_lemma = row['target_lemma']
+ target_pos = row['pos']
+ key = (target_lemma, target_pos)
+
+ if key not in lp_info:
+ if debug:
+ print(target_lemma, target_pos, 'not in lp_info')
+ return None
+
+ lp_index = row['lp_index']
+ if lp_index is None:
+ print('lp_index is None')
+ return None
+
+ sensekey = lp_info[(target_lemma, target_pos)][lp_index]
+ synset_identifier = None
+
+ for synset in candidate_synsets:
+ if any([lemma.key() == sensekey
+ for lemma in synset.lemmas()]):
+ synset_identifier = synset2identifier(synset, '30')
+
+ return synset_identifier
def synset2identifier(synset, wn_version):
"""
@@ -33,7 +97,7 @@ def synset2identifier(synset, wn_version):
offset_8_char = offset.zfill(8)
pos = synset.pos()
- if pos == 'j':
+ if pos in {'s', 'j'}:
pos = 'a'
identifier = 'eng-{wn_version}-{offset_8_char}-{pos}'.format_map(locals())
@@ -64,14 +128,14 @@ def extract_sentence_wsd_competition(row):
sentence_tokens.append(sentence_token.text)
assert len(sentence_tokens) >= 2
- assert pos is not None
- assert lemma is not None
- assert target_index is not None
+ #assert pos is not None # only needed for sem2013-aw
+ #assert lemma is not None, (lemma, pos)
+ #assert target_index is not None
return target_index, sentence_tokens, lemma, pos
-def score_synsets(target_embedding, candidate_synsets, sense_embeddings, instance_id, lemma, pos):
+def score_synsets(target_embedding, candidate_synsets, sense_embeddings, instance_id, lemma, pos, gran, synset2higher_level):
"""
perform wsd
@@ -85,30 +149,46 @@ def score_synsets(target_embedding, candidate_synsets, sense_embeddings, instanc
"""
highest_synsets = []
highest_conf = 0.0
+ candidate_freq = dict()
+ strategy = 'lstm'
+
+ for synset in candidate_synsets:
+ if gran == 'synset':
+ candidate = synset
+ candidate_freq[synset] = meaning_freqs[candidate]
+ elif gran in {'sensekey', 'blc20', 'direct_hypernym'}:
+ candidate = None
+ if synset in synset2higher_level:
+ candidate = synset2higher_level[synset]
+ candidate_freq[synset] = meaning_freqs[candidate]
+ candidate_freq[synset] = meaning_freqs[candidate]
- for candidate in candidate_synsets:
if candidate not in sense_embeddings:
- print('%s %s %s: candidate %s missing in sense embeddings' % (instance_id, lemma, pos, candidate))
+ #print('%s %s %s: candidate %s missing in sense embeddings' % (instance_id, lemma, pos, candidate))
continue
cand_embedding = sense_embeddings[candidate]
sim = 1 - spatial.distance.cosine(cand_embedding, target_embedding)
if sim == highest_conf:
- highest_synsets.append(candidate)
+ highest_synsets.append(synset)
elif sim > highest_conf:
- highest_synsets = [candidate]
+ highest_synsets = [synset]
highest_conf = sim
if len(highest_synsets) == 1:
highest_synset = highest_synsets[0]
elif len(highest_synsets) >= 2:
highest_synset = highest_synsets[0]
- print('%s %s %s: 2> synsets with same conf %s: %s' % (instance_id, lemma, pos, highest_conf, highest_synsets))
+ #print('%s %s %s: 2> synsets with same conf %s: %s' % (instance_id, lemma, pos, highest_conf, highest_synsets))
else:
- highest_synset = None
- print('%s: no highest synset' % instance_id)
- return highest_synset
+ if args.mfs_fallback:
+ highest_synset = candidate_synsets[0]
+ #print('%s: no highest synset -> mfs' % instance_id)
+ strategy = 'mfs_fallback'
+ else:
+ highest_synset = None
+ return highest_synset, candidate_freq, strategy
# load wsd competition dataframe
@@ -117,7 +197,11 @@ wsd_df = pandas.read_pickle(args.wsd_df_path)
# add output column
wsd_df['lstm_output'] = [None for _ in range(len(wsd_df))]
wsd_df['lstm_acc'] = [None for _ in range(len(wsd_df))]
-
+wsd_df['emb_freq'] = [None for _ in range(len(wsd_df))]
+wsd_df['#_cand_synsets'] = [None for _ in range(len(wsd_df))]
+wsd_df['#_new_cand_synsets'] = [None for _ in range(len(wsd_df))]
+wsd_df['gold_in_new_cand_synsets'] = [None for _ in range(len(wsd_df))]
+wsd_df['wsd_strategy'] = [None for _ in range(len(wsd_df))]
# load sense embeddings
with open(args.sense_embeddings_path, 'rb') as infile:
@@ -130,8 +214,9 @@ vocab = np.load(args.vocab_path)
with tf.Session() as sess: # your session object
saver = tf.train.import_meta_graph(args.model_path + '.meta', clear_devices=True)
saver.restore(sess, args.model_path)
- predicted_context_embs = sess.graph.get_tensor_by_name('Model/predicted_context_embs:0')
- x = sess.graph.get_tensor_by_name('Model/x:0')
+ x, predicted_context_embs, lens = utils.load_tensors(sess)
+ #predicted_context_embs = sess.graph.get_tensor_by_name('Model/predicted_context_embs:0')
+ #x = sess.graph.get_tensor_by_name('Model/Placeholder:0')
for row_index, row in wsd_df.iterrows():
target_index, sentence_tokens, lemma, pos = extract_sentence_wsd_competition(row)
@@ -139,25 +224,100 @@ with tf.Session() as sess: # your session object
target_id = vocab['<target>']
sentence_as_ids = [vocab.get(w) or vocab['<unkn>'] for w in sentence_tokens]
sentence_as_ids[target_index] = target_id
- target_embedding = sess.run(predicted_context_embs, {x: [sentence_as_ids]})[0]
- # load candidate synsets
- synsets = wn.synsets(lemma, pos=pos)
- candidate_synsets = {synset2identifier(synset, wn_version='30')
- for synset in synsets}
+ target_embeddings = sess.run(predicted_context_embs, {x: [sentence_as_ids],
+ lens: [len(sentence_as_ids)]})
+ for target_embedding in target_embeddings:
+ break
+
+ #target_embedding = sess.run(predicted_context_embs, {x: [sentence_as_ids]})[0]
+
+ # load token object
+ token_obj = row['tokens'][0]
+
+ # morphology reduced polysemy
+ pos = row['pos']
+ if the_wn_version in {'171'}:
+ pos = None
+
+
+ candidate_synsets, \
+ new_candidate_synsets, \
+ gold_in_candidates = morpho_utils.candidate_selection(wn,
+ token=token_obj.text,
+ target_lemma=row['target_lemma'],
+ pos=row['pos'],
+ morphofeat=token_obj.morphofeat,
+ use_case=case_strategy,
+ use_number=number_strategy,
+ gold_lexkeys=row['lexkeys'],
+ case_freq=case_freq,
+ plural_freq=plural_freq,
+ debug=False)
+
+ the_chosen_candidates = [synset2identifier(synset, wn_version=the_wn_version)
+ for synset in new_candidate_synsets]
+
+ print()
+ print(the_chosen_candidates, gold_in_candidates)
+ # get mapping to higher abstraction level
+ synset2higher_level = dict()
+ if args.gran in {'sensekey', 'blc20', 'direct_hypernym'}:
+ label = 'synset2%s' % args.gran
+ synset2higher_level = row[label]
+
+ # determine wsd strategy used
+ if len(candidate_synsets) == 1:
+ wsd_strategy = 'monosemous'
+ elif len(new_candidate_synsets) == 1:
+ wsd_strategy = 'morphology_solved'
+ elif len(candidate_synsets) == len(new_candidate_synsets):
+ wsd_strategy = 'lstm'
+ elif len(new_candidate_synsets) < len(candidate_synsets):
+ wsd_strategy = 'morphology+lstm'
+
+ # possibly include label propagation strategy
+ if lp_strategy:
+ lp_result = lp_output(row, lp_info, new_candidate_synsets, debug=False)
+
+ if lp_result:
+ the_chosen_candidates = [lp_result]
+ wsd_strategy = 'lp'
# perform wsd
- if len(candidate_synsets) >= 2:
- chosen_synset = score_synsets(target_embedding, candidate_synsets, sense_embeddings, instance_id, lemma, pos)
+ if len(the_chosen_candidates) >= 2:
+ chosen_synset, \
+ candidate_freq, \
+ strategy = score_synsets(target_embedding,
+ the_chosen_candidates,
+ sense_embeddings,
+ instance_id,
+ lemma,
+ pos,
+ args.gran,
+ synset2higher_level)
+
+ #if strategy == 'mfs_fallback':
+ # wsd_strategy = 'mfs_fallback'
+
else:
- chosen_synset = candidate_synsets.pop()
+ chosen_synset = None
+ if the_chosen_candidates:
+ chosen_synset = the_chosen_candidates[0]
+ candidate_freq = dict()
# add to dataframe
wsd_df.set_value(row_index, col='lstm_output', value=chosen_synset)
+ wsd_df.set_value(row_index, col='#_cand_synsets', value=len(candidate_synsets))
+ wsd_df.set_value(row_index, col='#_new_cand_synsets', value=len(new_candidate_synsets))
+ wsd_df.set_value(row_index, col='gold_in_new_cand_synsets', value=gold_in_candidates)
+ wsd_df.set_value(row_index, col='wsd_strategy', value=wsd_strategy)
# score it
- lstm_acc = chosen_synset in row['wn30_engs']
+ print(chosen_synset, row['source_wn_engs'])
+ lstm_acc = chosen_synset in row['source_wn_engs'] # used to be wn30_engs
wsd_df.set_value(row_index, col='lstm_acc', value=lstm_acc)
+ wsd_df.set_value(row_index, col='emb_freq', value=candidate_freq)
if lstm_acc:
num_correct += 1
@@ -167,6 +327,9 @@ print(num_correct)
# save it
wsd_df.to_pickle(args.output_path)
+with open(args.results, 'w') as outfile:
+ outfile.write('%s' % num_correct)
+
diff --git a/prepare-lstm-wsd.py b/prepare-lstm-wsd.py
index f3e4d1b..52db01c 100644
--- a/prepare-lstm-wsd.py
+++ b/prepare-lstm-wsd.py
@@ -7,6 +7,7 @@ Read a simple text file (one sentence per line) and produce these files:
- <fname>.train.npz: training batches (each batch contains roughly the same
number of tokens but differing number of sentences depends on sentence length)
- <fname>.dev.npz: development dataset (as big as one epoch)
+-
@author: Minh Le
'''
@@ -20,23 +21,25 @@ import pickle
import re
import numpy as np
import subprocess
-from tensorflow.contrib.labeled_tensor import batch
+from random import Random
+from collections import Counter
+from utils import progress, count_lines_fast
+from configs import preprocessed_gigaword_path, output_dir
+from version import version
dev_sents = 20000 # absolute maximum
dev_portion = 0.01 # relative maximum
-batch_size = 128000 # words
+# if you get OOM (out of memory) error, reduce this number
+batch_size = 60000 # words
vocab_size = 10**6
min_count = 5
-special_symbols = ['<target>', '<unkn>', '<pad>']
+inp_path = preprocessed_gigaword_path
+# inp_path = 'preprocessed-data/gigaword_1m-sents.txt' # for debugging
+out_dir = os.path.join('preprocessed-data', version)
+out_path = os.path.join(out_dir, 'gigaword-for-lstm-wsd')
-def progress(it):
- start = time()
- for i, val in enumerate(it):
- yield(val)
- if (i+1) % 1000000 == 0:
- sys.stderr.write('processed %d items, elapsed time: %.1f minutes...\n'
- %(i+1, (time()-start)/60))
+special_symbols = ['<target>', '<unkn>', '<pad>']
def _build_vocab(filename):
sys.stderr.write('Building vocabulary...\n')
@@ -55,87 +58,146 @@ def _build_vocab(filename):
return word2id, words
def sort_sentences(inp_path, out_path):
+ start = time()
cmd = ('cat %s | python3 scripts/sentlen.py --min 6 --max 100 '
- '| sort -T output -k1,1g -k2 | uniq > %s'
- %(inp_path, out_path))
+ '| sort -T %s -k1,1g -k2 | uniq > %s'
+ %(inp_path, output_dir, out_path))
sys.stderr.write('%s\n' %cmd)
status = subprocess.call(cmd, shell=True)
+ sys.stderr.write('sorting finished after %.1f minutes...\n' %((time()-start)/60))
assert status == 0
-def lookup_and_iter_sents(filename, word_to_id):
+def lookup_and_iter_sents(filename, word2id, include_ids=None, exclude_ids=None):
unkn_id = word2id['<unkn>']
with codecs.open(filename, 'r', 'utf-8') as f:
- for line in f:
- words = line.strip().split()
- yield [word_to_id.get(word) or unkn_id for word in words]
+ for sent_id, line in enumerate(f):
+ if ((include_ids is None or sent_id in include_ids) and
+ (exclude_ids is None or sent_id not in exclude_ids)):
+ words = line.strip().split()
+ yield [word2id.get(word) or unkn_id for word in words]
-class PadFunc(object):
-
- dry_run=False
-
- def __init__(self):
- self.total = 0
- self.pads = 0
- def __call__(self, sents, max_len, pad_id):
- if self.dry_run:
- arr = np.empty(0)
- value_count = sum(1 for s in sents for _ in s)
- size = len(sents) * max_len
- else:
- arr = np.zeros((len(sents), max_len), dtype=np.int32)
- size = arr.size
- arr.fill(pad_id)
- value_count = 0
- for i, s in enumerate(sents):
- for j, v in enumerate(s):
- arr[i,j] = v
- value_count += 1
- self.pads += (size - value_count)
- self.total += size
- return arr
-
-def pad_batches(inp_path, word2id):
+def pad(sents, max_len, pad_id):
+ arr = np.empty((len(sents), max_len), dtype=np.int32)
+ arr.fill(pad_id)
+ for i, s in enumerate(sents):
+ arr[i, :len(s)] = s
+ return arr
+
+def pad_batches(inp_path, word2id, include_ids, exclude_ids, max_sents=-1):
sys.stderr.write('Dividing and padding...\n')
- pad = PadFunc()
pad_id = word2id['<pad>']
- dev = []
batches = {}
- last_max_len = 0
- last_batch = []
- with open(inp_path) as f: total_sents = sum(1 for line in f)
- for sent in progress(lookup_and_iter_sents(inp_path, word2id)):
- if (len(dev) < dev_sents and len(dev) < dev_portion*total_sents
- and np.random.rand() < 0.01):
- dev.append(sent)
- else:
- last_max_len = max(last_max_len, len(sent))
- last_batch.append(sent)
- if len(last_batch)*last_max_len >= batch_size:
- batches['batch%d' %len(batches)] = pad(last_batch, last_max_len, pad_id)
- last_max_len = 0
- last_batch = []
- if last_max_len > 0:
- batches['batch%d' %len(batches)] = pad(last_batch, last_max_len, pad_id)
- dev_lens = np.array([len(s) for s in dev], dtype=np.int32)
- dev_padded = PadFunc()(dev, max(dev_lens), pad_id)
+ sent_lens = []
+ curr_max_len = 0
+ curr_batch = []
+ batch_id = 0
+ for sent in progress(lookup_and_iter_sents(inp_path, word2id,
+ include_ids, exclude_ids)):
+ new_size = (len(curr_batch)+1) * max(curr_max_len,len(sent))
+ if new_size > batch_size or (max_sents > 0 and len(curr_batch) >= max_sents):
+ batches['batch%d' %batch_id] = pad(curr_batch, curr_max_len, pad_id)
+ batches['lens%d' %batch_id] = np.array([len(s) for s in curr_batch], dtype=np.int32)
+ batch_id += 1
+ curr_max_len = 0
+ curr_batch = []
+ curr_max_len = max(curr_max_len, len(sent))
+ curr_batch.append(sent)
+ sent_lens.append(len(sent))
+ if curr_batch:
+ batches['batch%d' %batch_id] = pad(curr_batch, curr_max_len, pad_id)
+ batches['lens%d' %batch_id] = np.array([len(s) for s in curr_batch], dtype=np.int32)
+ batch_id += 1 # important to count num batches correctly
+ sent_lens = np.array(sent_lens, dtype=np.int32)
sys.stderr.write('Dividing and padding... Done.\n')
- sizes = np.array([b.size for b in batches.values()])
- if len(batches) >= 2:
+ sizes = np.array([batches['batch%d'%i].size for i in range(batch_id)])
+ if batch_id >= 2:
sys.stderr.write('Divided into %d batches (%d elements each, std=%d, '
'except last batch of %d).\n'
- %(len(batches), sizes[:-1].mean(), sizes[:-1].std(), sizes[-1]))
+ %(batch_id, sizes[:-1].mean(), sizes[:-1].std(), sizes[-1]))
else:
- assert len(batches) == 1
+ assert batch_id == 1
sys.stderr.write('Created 1 batch of %d elements.\n' %sizes[0])
- sys.stderr.write('Added %d elements as padding (%.2f%%).\n'
- %(pad.pads, pad.pads*100.0/pad.total))
- sys.stderr.write('Consumed roughly %.2f GiB.\n'
- %(pad.total*4/float(2**30)))
- return batches, dev_padded, dev_lens
+ sys.stderr.write('Sentence lengths: %.5f (std=%.5f)\n'
+ %(sent_lens.mean(), sent_lens.std()))
+ return batches
-if __name__ == '__main__':
- inp_path, out_path = sys.argv[1:]
+
+def shuffle_and_pad_batches(inp_path, word2id, dev_sent_ids):
+ sys.stderr.write('Reading lengths...\n')
+ lens = []
+ with codecs.open(inp_path, 'r', 'utf-8') as f:
+ for line in progress(f, label='sentences'):
+ # this is different from counting the blank spaces because some words
+ # are separated by double spaces and there might be an additional
+ # whitespace at the end of a line
+ lens.append(len(line.strip().split()))
+ lens = np.array(lens, dtype=np.int32)
+ sys.stderr.write('Reading lengths... Done.\n')
+
+ sys.stderr.write('Calculating batch shapes...\n')
+ indices = list(range(len(lens)))
+ rng = Random(29)
+ rng.shuffle(indices)
+ total_sents = len(lens)
+ batches = {}
+ curr_max_len = 0
+ curr_batch_lens = []
+ sent2batch = {}
+ batch_id = 0
+ for sent_id in progress(indices, label='sentences'):
+ l = lens[sent_id]
+ if sent_id not in dev_sent_ids:
+ new_size = (len(curr_batch_lens)+1) * max(curr_max_len,l)
+ if new_size >= batch_size:
+ batches['batch%d' %batch_id] = \
+ np.empty((len(curr_batch_lens), max(curr_batch_lens)), dtype=np.int32)
+ batches['lens%d' %batch_id] = np.array(curr_batch_lens, dtype=np.int32)
+ batch_id += 1
+ curr_max_len = 0
+ curr_batch_lens = []
+ curr_max_len = max(curr_max_len, l)
+ curr_batch_lens.append(l)
+ sent2batch[sent_id] = 'batch%d' %batch_id
+ if curr_batch_lens:
+ batches['batch%d' %batch_id] = \
+ np.empty((len(curr_batch_lens), max(curr_batch_lens)), dtype=np.int32)
+ batches['lens%d' %batch_id] = np.array(curr_batch_lens, dtype=np.int32)
+ batch_id += 1 # important to count num batches correctly
+ sys.stderr.write('Calculating batch shapes... Done.\n')
+ sys.stderr.write('Dividing and padding...\n')
+ pad_id = word2id['<pad>']
+ for i in range(batch_id): batches['batch%d'%i].fill(pad_id)
+ nonpad_count = 0
+ sent_counter = Counter()
+ for sent_id, sent in progress(enumerate(lookup_and_iter_sents(inp_path, word2id)), label='sentences'):
+ assert lens[sent_id] == len(sent)
+ batch_name = sent2batch.get(sent_id)
+ if batch_name is not None: # could be in dev set
+ batches[batch_name][sent_counter[batch_name],:len(sent)] = sent
+ nonpad_count += len(sent)
+ sent_counter[batch_name] += 1
+ # check that we filled all arrays
+ for batch_name in sent_counter:
+ assert sent_counter[batch_name] == batches[batch_name].shape[0]
+ sys.stderr.write('Dividing and padding... Done.\n')
+
+ sizes = np.array([batches['batch%d'%i].size for i in range(batch_id)])
+ if batch_id >= 2:
+ sys.stderr.write('Divided into %d batches (%d elements each, std=%d, '
+ 'except last batch of %d).\n'
+ %(batch_id, sizes[:-1].mean(), sizes[:-1].std(), sizes[-1]))
+ else:
+ assert batch_id == 1
+ sys.stderr.write('Created 1 batch of %d elements.\n' %sizes[0])
+ total = sum(sizes)
+ pad_count = total - nonpad_count
+ sys.stderr.write('Sentence lengths: %.5f (std=%.5f)\n'
+ %(lens.mean(), lens.std()))
+ return batches
+
+def run():
+ os.makedirs(out_dir, exist_ok=True)
index_path = out_path + '.index.pkl'
if os.path.exists(index_path):
sys.stderr.write('Reading vocabulary from %s... ' %index_path)
@@ -146,17 +208,43 @@ if __name__ == '__main__':
word2id, words = _build_vocab(inp_path)
with open(index_path, 'wb') as f: pickle.dump(word2id, f)
- sorted_sents_path = inp_path + '.sorted'
+ sorted_sents_path = out_path + '.sorted'
if os.path.exists(sorted_sents_path):
sys.stderr.write('Sentences are already sorted at %s\n' %sorted_sents_path)
else:
sort_sentences(inp_path, sorted_sents_path)
+
+ total_sents = count_lines_fast(sorted_sents_path)
+ real_num_dev_sents = int(min(dev_sents, dev_portion*total_sents))
+ np.random.seed(918)
+ dev_sent_ids = set(np.random.choice(total_sents, size=real_num_dev_sents, replace=False))
train_path = out_path + '.train.npz'
dev_path = out_path + '.dev.npz'
- if os.path.exists(train_path):
- sys.stderr.write('Result already exists: %s. Skipped.\n' %train_path)
+ shuffled_train_path = out_path + '-shuffled.train.npz'
+ if os.path.exists(shuffled_train_path):
+ sys.stderr.write('Result already exists: %s. Skipped.\n' %shuffled_train_path)
else:
- batches, dev_data, dev_lens = pad_batches(sorted_sents_path, word2id)
+ print("- Training set:")
+ batches = pad_batches(sorted_sents_path, word2id, None, dev_sent_ids)
np.savez(train_path, **batches)
- np.savez(dev_path, data=dev_data, lens=dev_lens)
+ print("- Development set:")
+ batches = pad_batches(sorted_sents_path, word2id, dev_sent_ids, None, 768)
+ np.savez(dev_path, **batches)
+ print("- Shuffled training set:")
+ batches = shuffle_and_pad_batches(sorted_sents_path, word2id, dev_sent_ids)
+ np.savez(shuffled_train_path, **batches)
+
+ for percent in (1, 10, 25, 50, 75):
+ num_lines = int(percent / 100.0 * total_sents)
+ sampled_ids = set(np.random.choice(total_sents, size=num_lines, replace=False))
+ pc_train_path = out_path + ('_%02d-pc.train.npz' %percent)
+ if os.path.exists(pc_train_path):
+ sys.stderr.write('%02d%% dataset already exists: %s. Skipped.\n' %pc_train_path)
+ else:
+ print("- Reduced training set (%02d%%):" %percent)
+ batches = pad_batches(sorted_sents_path, word2id, sampled_ids, dev_sent_ids)
+ np.savez(pc_train_path, **batches)
+
+if __name__ == '__main__':
+ run()
diff --git a/process-gigaword.py b/process-gigaword.py
index e568001..7bf2b0d 100644
--- a/process-gigaword.py
+++ b/process-gigaword.py
@@ -2,9 +2,17 @@ import os
import gzip
from bs4 import BeautifulSoup
import spacy
-nlp = spacy.load('en_default')
+from configs import gigaword_path, preprocessed_gigaword_path
+import codecs
+from utils import progress
+from version import version
import sys
+def custom_pipeline(nlp):
+ return (nlp.tagger, nlp.parser)
+
+nlp = spacy.load('en_default', create_pipeline=custom_pipeline)
+
def iter_paragraphs(paths):
for path in paths:
with gzip.open(path) as f:
@@ -13,7 +21,6 @@ def iter_paragraphs(paths):
paras = soup.find_all('p')
for p in paras: yield p.text.strip()
-
def iter_files(root_dir):
for root, dirs, files in os.walk(root_dir):
for fname in files:
@@ -21,21 +28,24 @@ def iter_files(root_dir):
yield os.path.join(root, fname)
def iter_sents(paragraphs):
- for i, doc in enumerate(nlp.pipe(paragraphs, batch_size=10000, n_threads=32)):
- assert isinstance(doc, spacy.tokens.doc.Doc) and doc.is_parsed
+ for doc in nlp.pipe(paragraphs, batch_size=10000):
for sent in doc.sents:
yield [str(tok).strip() for tok in sent]
- if (i+1) % 10000 == 0:
- sys.stderr.write('%10d' %(i+1))
- if (i+1) % 100000 == 0:
- sys.stderr.write('\n')
-gigaword_path = 'data/gigaword'
-example_file = 'data/gigaword/gigaword_eng_5_d1/data/afp_eng/afp_eng_200112.gz'
+
+# example_file = 'data/gigaword/gigaword_eng_5_d1/data/afp_eng/afp_eng_200112.gz'
if __name__ == '__main__':
- for sent in iter_sents(iter_paragraphs(iter_files(gigaword_path))):
- for tok in sent:
- sys.stdout.write(tok)
- sys.stdout.write(' ')
- sys.stdout.write('\n')
+ dir_ = os.path.join('preprocessed-data', version)
+ os.makedirs(dir_, exist_ok=True)
+ preprocessed_gigaword_path = os.path.join(dir_, 'gigaword.txt')
+ sys.stderr.write('Writing to %s\n' %preprocessed_gigaword_path)
+ with codecs.open(preprocessed_gigaword_path, 'w', 'utf-8') as f:
+ paths = list(iter_files(gigaword_path))
+ paths.sort() # remove difference between machines
+ paths = progress(paths, ticks=1, label='files', max_=len(paths))
+ for sent in iter_sents(iter_paragraphs(paths)):
+ for tok in sent:
+ f.write(tok)
+ f.write(' ')
+ f.write('\n')
diff --git a/scripts/semcor_format2LSTM_input.py b/scripts/semcor_format2LSTM_input.py
index dfc369f..3990f55 100644
--- a/scripts/semcor_format2LSTM_input.py
+++ b/scripts/semcor_format2LSTM_input.py
@@ -4,7 +4,7 @@ from nltk.corpus import wordnet as wn
from lxml import html, etree
from collections import defaultdict
import wn_utils
-
+from datetime import datetime
def get_lemma_pos_of_sensekey(sense_key):
"""
@@ -77,6 +77,7 @@ def load_instance_id2offset(mapping_path, sensekey2offset, debug=False):
:return: instance_id -> offset
"""
instance_id2offset = dict()
+ instance_id2sensekeys = dict()
more_than_one_offset = 0
no_offsets = 0
@@ -85,6 +86,8 @@ def load_instance_id2offset(mapping_path, sensekey2offset, debug=False):
for line in infile:
instance_id, *sensekeys = line.strip().split()
+ instance_id2sensekeys[instance_id] = sensekeys
+
offsets = {sensekey2offset[sensekey]
for sensekey in sensekeys
if sensekey in sensekey2offset}
@@ -104,18 +107,26 @@ def load_instance_id2offset(mapping_path, sensekey2offset, debug=False):
no_offsets += 1
- return instance_id2offset
+ return instance_id2offset, instance_id2sensekeys
# experiment settings
wn_version = '30'
corpora_to_include = ['semcor',
- #'mun'
+ #'mun'
] # semcor | mun
accepted_pos = {'NOUN'}
entailment_setting = 'any_hdn' # lemma_hdn | any_hdn
-lemma2annotations = defaultdict(dict)
+#lemma2annotations = defaultdict(dict)
+
+
+
+#path_wn20_to_wn30 = '/Users/marten/Downloads/mappings-upc-2007/mapping-20-30/wn20-30.noun'
+#path_wn20_to_domain = '/Users/marten/git/semantic_class_manager/resources/wn-domains-3.2/wn-domains-3.2-20070223'
+#wn30_domain, domain_wn30 = wn_utils.get_synset2domain(path_wn20_to_domain,
+# path_wn20_to_wn30)
+
if wn_version == '30':
path_to_wn_dict_folder = str(wn._get_root()) # change this for other wn versions
@@ -129,12 +140,15 @@ elif corpora_to_include == ['semcor']:
input_xml_path = '../data/WSD_Training_Corpora/SemCor/semcor.data.xml'
input_mapping_path = '../data/WSD_Training_Corpora/SemCor/semcor.gold.key.txt'
+sensekey_output_path = 'sensekey-' + '_'.join(corpora_to_include) + '.txt'
synset_output_path = 'synset-' + '_'.join(corpora_to_include) + '.txt'
hdn_output_path = '-'.join(['hdn',
'_'.join(corpora_to_include),
'_'.join(accepted_pos),
entailment_setting]) + '.txt'
+#domain_output_path = 'domain-' + '_'.join(corpora_to_include) + '.txt'
+#domain_mapping_path = domain_output_path + '.mapping'
# precompute all hdns
lemma_pos2offsets = wn_utils.load_lemma_pos2offsets(path_to_wn_index_sense)
@@ -172,14 +186,18 @@ my_wn_reader = WordNetCorpusReader(path_to_wn_dict_folder, None)
sensekey2offset = load_mapping_sensekey2offset(path_to_wn_index_sense,
wn_version)
-instance_id2offset = load_instance_id2offset(input_mapping_path,
- sensekey2offset,
- debug=False)
+instance_id2offset, instance_id2sensekeys = load_instance_id2offset(input_mapping_path,
+ sensekey2offset,
+ debug=False)
my_html_tree = html.parse(input_xml_path)
-hdn_outfile = open(hdn_output_path, 'w')
+sensekey_outfile = open(sensekey_output_path, 'w')
synset_outfile = open(synset_output_path, 'w')
+#domain_outfile = open(domain_output_path, 'w')
+hdn_outfile = open(hdn_output_path, 'w')
+
+domain2freq = defaultdict(int)
for corpus_node in my_html_tree.xpath('body/corpus'):
@@ -191,11 +209,16 @@ for corpus_node in my_html_tree.xpath('body/corpus'):
for sent_node in corpus_node.xpath('text/sentence'):
sentence_tokens = []
+ sensekey_annotations = []
synset_annotations = []
hdn_annotations = []
+ domain_annotations = []
for child_el in sent_node.getchildren():
+ if child_el.sourceline % 10000 == 0:
+ print(child_el.sourceline, datetime.now())
+
lemma = child_el.get('lemma')
token = child_el.text
pos = child_el.get('pos')
@@ -204,8 +227,11 @@ for corpus_node in my_html_tree.xpath('body/corpus'):
assert token is not None
sentence_tokens.append(token)
+
+ sent_sensekey_annotations = []
sent_synset_annotations = []
sent_hdn_annotations = []
+ sent_domain_annotations = []
if all([child_el.tag == 'instance',
pos in accepted_pos]):
@@ -214,13 +240,20 @@ for corpus_node in my_html_tree.xpath('body/corpus'):
synset_id = instance_id2offset[instance_id]
# update counter for logging purposes
- if synset_id not in lemma2annotations[lemma]:
- lemma2annotations[lemma][synset_id] = {'hdn': 0, 'synset': 0}
+ #if synset_id not in lemma2annotations[lemma]:
+ # lemma2annotations[lemma][synset_id] = {'hdn': 0, 'synset': 0}
+ #lemma2annotations[lemma][synset_id]['synset'] += 1
- lemma2annotations[lemma][synset_id]['synset'] += 1
+ sent_synset_annotations.append(synset_id)
+ sensekeys = instance_id2sensekeys[instance_id]
+ for sensekey in sensekeys:
+ sent_sensekey_annotations.append(sensekey)
- sent_synset_annotations.append(synset_id)
+ #if synset_id in wn30_domain:
+ # domain = wn30_domain[synset_id]