-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueryKB.py
979 lines (939 loc) · 38.2 KB
/
queryKB.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
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
### Copyright 2011, 2012 Margaret Mitchell
### Distributed under the terms of the GNU General Public License
###
### This file is part of the vision-to-language system Midge.
###
### Midge is free software: you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation, either version 3 of the License, or
### (at your option) any later version.
###
### Midge is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
### GNU General Public License for more details.
###
### You should have received a copy of the GNU General Public License
### along with Midge. If not, see <http://www.gnu.org/licenses/>.
###
### Please cite the relevant work:
### Mitchell et al. (2012). "Midge: Generating Image Descriptions From Computer Vision Detections." Proceedings of EACL 2012.
###
### Questions/Comments, send to m.mitchell@abdn.ac.uk
import sys
import re
import glob
import math
import itertools
from nltk.corpus import wordnet as wn
import pickle
class queryKB():
def __init__(self, reserved_words=[], word_thresh=.001, cnt=10, read_pickle=True, read_db=False, db=None):
self.read_pickle = read_pickle
#print "read_pickle is", read_pickle
self.word_thresh = float(word_thresh)
self.cnt = cnt
# Only generate contentful verbs for now.
self.light_verbs = ("may", "might", "must", "would", "be", "being", "been", "am", "are", "were", "do", "does", "did", "should", "could", "would", "may", "is", "'s", "was", "has", "had", "will", "can", "shall")
# So we only read in info about nouns we've run detectors for.
self.reserved_words = reserved_words
# Maps detection names to noun names.
self.label_hash = {'motorbike':'motorcycle', 'television':'tv', 'pottedplant':'plant'}
self.present_tense = ("VBG", "VBZ", "VBN")
self.visual_thresh_hash = {}
self.hypernym_hash = {}
self.obj_probs = {}
self.mod_ngram_hash = {5:{}, 4:{},3:{},2:{},1:{}}
self.plural_hash = {}
self.mod_hash = {}
self.det_hash = {}
self.att_hash = {}
self.preps = {}
self.verb_hash = {'a':{}, 'b':{}}
self.prep_hash = {'verb-prep':{}, 'prep-noun':{}, 'noun-prep':{}}
self.verb_trans_hash = {}
self.noun_freq_hash = {}
self.noun_noun_hash = {}
self.ins_hash = {}
# -- Text files we learn from -- #
self.visual_thresh_file = None
self.mod_ngram_file = None
self.prep_file = None
self.det_file = None
self.mod_file = None
self.att_files = None
self.verb_noun_file = None
self.noun_verb_file = None
self.verb_prep_file = None
self.prep_noun_file = None
self.noun_prep_file = None
self.verb_trans_file = None
self.nouns_file = None
self.plurals_file = None
self.ins_file = None
if read_db:
self.read_as_db(db)
elif self.read_pickle:
self.read_as_pickle()
else:
self.read_raw_data()
def read_as_pickle(self):
print "loading models..."
print "1"
self.visual_thresh_hash = pickle.load(open("pickled_files/visual_thresh_hash.pk", "rb"))
print "2"
self.hypernym_hash = pickle.load(open("pickled_files/hypernym_hash.pk", "rb"))
print "3"
self.obj_probs = pickle.load(open("pickled_files/obj_probs.pk", "rb"))
print "4"
self.mod_ngram_hash = pickle.load(open("pickled_files/mod_ngram_hash.pk", "rb"))
print "5"
self.plural_hash = pickle.load(open("pickled_files/plural_hash.pk", "rb"))
print "6"
self.mod_hash = pickle.load(open("pickled_files/mod_hash.pk", "rb"))
print "7"
self.det_hash = pickle.load(open("pickled_files/det_hash.pk", "rb"))
print "8"
self.att_hash = pickle.load(open("pickled_files/att_hash.pk", "rb"))
print "9"
self.preps = pickle.load(open("pickled_files/preps.pk", "rb"))
print "10"
self.verb_hash = pickle.load(open("pickled_files/verb_hash.pk", "rb"))
print "11"
self.prep_hash = pickle.load(open("pickled_files/prep_hash.pk", "rb"))
print "12"
self.verb_trans_hash = pickle.load(open("pickled_files/verb_trans_hash.pk", "rb"))
print "13"
self.noun_freq_hash = pickle.load(open("pickled_files/noun_freq_hash.pk", "rb"))
print "14"
#self.noun_noun_hash = pickle.load(open("pickled_files/noun_noun_hash.pk", "rb"))
print "15"
self.ins_hash = pickle.load(open("pickled_files/ins_hash.pk", "rb"))
print "Done!"
self.db = {1: self.visual_thresh_hash, 2: self.hypernym_hash, 3: self.obj_probs, \
4: self.mod_ngram_hash, 5: self.plural_hash, 6: self.mod_hash, \
7: self.det_hash, 8: self.att_hash, 9: self.preps, 10: self.verb_hash, \
11: self.prep_hash, 12: self.verb_trans_hash, 13: self.noun_freq_hash,
15: self.ins_hash}
def read_raw_data(self):
#dir = "/data/share/corpora_stats/"
dir = "KB/"
a = open(dir + "thresholds", "r")
self.visual_thresh_file = a.readlines()
a.close()
b = open(dir + "NYT+WSJ.auto.mod.noOOV", "r")
self.mod_ngram_file = b.readlines()
b.close()
c = open(dir + "flickr.wn_hyps")
self.wn_hyps = c.readlines()
c.close()
d = open(dir + "preps", "r")
self.prep_file = d.readlines()
d.close()
# Swap in NYT data here...closed-class word, so you can use a larger corpus.
#self.det_file = \
#open(dir + "flickr_stats/flickr.det_heads", "r").readlines()
e = open(dir + "nyt_stats/nyt.det_heads", "r")
self.det_file = e.readlines()
e.close()
f = open(dir + "flickr_stats/flickr.mod_nouns", "r")
self.mod_file = f.readlines()
f.close()
g = open(dir + "att_groups", "r")
self.att_files = g.readlines()
g.close()
h = open(dir + "flickr_stats/flickr.verb_nouns", "r")
self.verb_noun_file = h.readlines()
h.close()
i = open(dir + "flickr_stats/flickr.noun_verbs", "r")
self.noun_verb_file = i.readlines()
i.close()
# Swap in NYT data here?
j = open(dir + "flickr_stats/flickr.verb_preps", "r")
self.verb_prep_file = j.readlines()
j.close()
#self.verb_prep_file = \
#open(dir + "corpora_stats/redo/nyt.verb_preps", "r").readlines()
# Swap in a NYT data here
k = open(dir + "flickr_stats/flickr.prep_nouns", "r")
self.prep_noun_file = k.readlines()
k.close()
#self.prep_noun_file = \
#open(dir + "corpora_stats/redo/nyt.prep_nouns", "r").readlines()
l = open(dir + "flickr_stats/flickr.noun_preps", "r")
self.noun_prep_file = l.readlines()
l.close()
#self.noun_prep_file = \
#open(dir + "corpora_stats/redo/nyt.noun_preps", "r").readlines()
m = open(dir + "flickr_stats/flickr.verb_transitivity", "r")
self.verb_trans_file = m.readlines()
m.close()
n = open(dir + "flickr_stats/flickr.noun_cooccurrences", "r")
self.nouns_file = n.readlines()
n.close()
o = open(dir + "plurals", "r")
self.plurals_file = o.readlines()
o.close()
p = open(dir + "in_list", "r")
self.ins_file = p.readlines()
p.close()
print "Creating models..."
print "1"
self.get_vis_thresh()
print "2"
self.read_mod_ngram()
#self.mod_ngram_hash = pickle.load(open("pickled_files/mod_ngram_hash.pk", "rb"))
print "3"
self.read_plurals()
print "4"
self.read_det()
print "5"
self.read_mod()
print "6"
self.read_att()
print "7"
self.read_preps()
print "8"
self.read_noun_freq()
print "9"
self.read_verb_noun()
print "10"
self.read_noun_verb()
print "11"
self.read_verb_prep()
print "12"
self.read_prep_noun()
print "13"
self.read_noun_prep()
print "14"
self.read_verb_trans()
#self.read_noun_noun()
print "15"
self.read_in_wn()
print "16"
self.read_ins()
def get_as_db(self):
return self.db
def read_as_db(self, db):
self.visual_thresh_hash = db[1]
self.hypernym_hash = db[2]
self.obj_probs = db[3]
self.mod_ngram_hash = db[4]
self.plural_hash = db[5]
self.mod_hash = db[6]
self.det_hash = db[7]
self.att_hash = db[8]
self.preps = db[9]
self.verb_hash = db[10]
self.prep_hash = db[11]
self.verb_trans_hash = db[12]
self.noun_freq_hash = db[13]
self.ins_hash = db[15]
def get_vis_thresh(self):
for line in self.visual_thresh_file:
split_line = line.split()
label = split_line[0]
thresh = float(split_line[1])
self.visual_thresh_hash[label] = thresh
if not self.read_pickle:
pickle.dump(self.visual_thresh_hash, open("pickled_files/visual_thresh_hash.pk", "wb"))
def read_mod_ngram(self):
x = 1
for line in self.mod_ngram_file[8:]:
inc = False
line = line.strip()
if line == "":
continue
split_line = line.split("\t")
if len(split_line) == 1:
ngram = x
x += 1
else:
words = split_line[1].split()
score = split_line[0]
# Speeds up everything by only grabbing ngram evidence for detections we're looking at.
for word in words:
if self.reserved_words != [] and (word not in self.reserved_words) and (word not in ["<s>", "</s>"]):
inc = False
break
inc = True
if inc:
self.mod_ngram_hash[ngram][tuple(words)] = float(score)
if not self.read_pickle:
pickle.dump(self.mod_ngram_hash, open("pickled_files/mod_ngram_hash.pk", "wb"))
def read_plurals(self):
for line in self.plurals_file:
split_line = line.split()
self.plural_hash[split_line[0]] = split_line[1].strip()
if not self.read_pickle:
pickle.dump(self.plural_hash, open("pickled_files/plural_hash.pk", "wb"))
def read_det(self):
x = 0
for line in self.det_file:
split_line = line.split()
# Mass noun or count noun decision.
if split_line[0] == "-":
new_noun = True
NoDet = False
no_prob = float(split_line[7])
plus_split_line = self.det_file[x+1].split()
yes_prob = float(plus_split_line[7])
if yes_prob > no_prob:
# Then don't store the no prob case.
x += 1
continue
else:
NoDet=True
x += 1
if split_line[0] == "#":
continue
w_mod = False
det_jj = split_line[0]
if det_jj not in ["+", "-"]:
split_det_jj = det_jj.split("_")
det = split_det_jj[0]
jj = split_det_jj[-1].split("=")
if jj[-1] == "True":
w_mod = True
elif jj[-1] == "False":
w_mod = False
else:
sys.stderr.write("Something weird happened with mods.\n")
sys.exit()
else:
det = det_jj
noun_tag_split = split_line[1].split("_")
noun = "_".join(noun_tag_split[:-1])
noun_tag = noun_tag_split[-1].upper()
# Just NNs
if noun_tag != "NN" and noun_tag != "NNS":
continue
try:
cnt = float(split_line[4])
except ValueError:
continue
if cnt < self.cnt:
continue
prob = float(split_line[7])
if prob < self.word_thresh:
continue
elif NoDet and det != "-":
continue
try:
# Without this, this is going to get redefined
# when a noun has a different tag. Let's stick
# with the first one, the highest-scoring one...
if (det, "DT", w_mod) in self.det_hash[(noun, noun_tag)]:
sys.stderr.write("This never happens -- remove.\n")
(old_cnt, old_prob) = self.det_hash[(noun, noun_tag)][(det, "DT", w_mod)]
# No, let's stick with the tag that has the highest count.
if old_cnt > cnt:
continue
# Otherwise, over-ride the current entry.
self.det_hash[(noun, noun_tag)][(det, "DT", w_mod)] = (cnt, prob)
except KeyError:
self.det_hash[(noun, noun_tag)] = {(det, "DT", w_mod): (cnt, prob)}
if not self.read_pickle:
pickle.dump(self.det_hash, open("pickled_files/det_hash.pk", "wb"))
def read_mod(self):
for line in self.mod_file:
split_line = line.split()
if split_line[0] == "#":
continue
noun_tag_split = split_line[0].split("_")
noun = "_".join(noun_tag_split[:-1])
noun_tag = noun_tag_split[-1].upper()
mod_tag_split = split_line[1].split("_")
mod = "_".join(mod_tag_split[:-1])
mod_tag = mod_tag_split[-1].upper()
cnt = float(split_line[4])
occur = float(split_line[7])
cooccur = float(split_line[10])
if cnt < self.cnt:
continue
# If this occurs more than just
# noise level, add it to our knowledge
# of possible modifiers for this object.
if cooccur < self.word_thresh:
continue
try:
# Without this, this is going to get redefined
# when a noun has a different tag. Let's stick
# with the first one, the highest-scoring one...
if (mod, mod_tag) in self.mod_hash[noun]:
continue
self.mod_hash[noun][(mod, mod_tag)] = (cnt, cooccur, noun_tag)
except KeyError:
self.mod_hash[noun] = {(mod, mod_tag): (cnt, cooccur, noun_tag)}
if not self.read_pickle:
pickle.dump(self.mod_hash, open("pickled_files/mod_hash.pk", "wb"))
def read_att(self):
for line in self.att_files:
split_line = line.split()
if line[0] == "#":
continue
att = split_line[0]
self.att_hash[att] = split_line[1:]
if not self.read_pickle:
pickle.dump(self.att_hash, open("pickled_files/att_hash.pk", "wb"))
def read_preps(self):
for line in self.prep_file:
split_line = line.split("-")
first_rel = split_line[0]
first_rel = first_rel.strip()
split_rel = first_rel.split(" ")
# main_prep is the basic spatial relation,
# corresponding to a bunch of possible prepositions.
main_prep = split_rel[1]
self.preps[main_prep] = {'ab':[], 'ba':[]}
for rel in split_line[1:]:
rel = rel.strip()
split_rel = rel.split(" ")
x = split_rel[0]
prep = split_rel[1]
y = split_rel[2]
# "ab" or "ba"
key = x + y
self.preps[main_prep][key] += [prep]
if not self.read_pickle:
pickle.dump(self.preps, open("pickled_files/preps.pk", "wb"))
def read_noun_freq(self):
for line in self.mod_file:
if "Occurs: " in line:
split_line = line.split()
noun_tag_split = split_line[1].split("_")
noun = "_".join(noun_tag_split[:-1])
tag = noun_tag_split[-1]
freq = split_line[3]
if noun in self.noun_freq_hash:
continue
self.noun_freq_hash[noun] = float(freq)
if not self.read_pickle:
pickle.dump(self.noun_freq_hash, open("pickled_files/noun_freq_hash.pk", "wb"))
def read_verb_noun(self):
for line in self.verb_noun_file:
split_line = line.split()
if line[0] == "#":
continue
verb_tag_split = split_line[0].split("_")
verb = "_".join(verb_tag_split[:-1])
verb_tag = verb_tag_split[-1].upper()
if verb in self.light_verbs or verb_tag not in self.present_tense:
continue
node = (verb_tag, verb)
#-- now the second column --#
noun_tag_split = split_line[1].split("_")
noun = "_".join(noun_tag_split[:-1])
noun_tag = noun_tag_split[-1].upper()
if noun_tag != "NN" and noun_tag != "NNS":
continue
try:
cnt = float(split_line[4])
except ValueError:
continue
# Ignore things with few counts.
# Do we want to back off to vlc here?
if cnt < self.cnt:
continue
# Can change this to PMI, etc.
try:
prob = float(split_line[7])
except ValueError: # This happens with odd parses that have spaces...just forget it.
continue
if prob < self.word_thresh:
continue
# verb_hash = {noun : {verb : {...}}}
# ordered with noun first.
# Probability of n occurring after v
try:
# Without this, this is going to get redefined
# when a noun has a different tag. Let's stick
# with the first one, the highest-scoring one...
if node in self.verb_hash['a'][noun]:
continue
self.verb_hash['a'][noun][node] = (cnt, prob, noun_tag)
except KeyError:
self.verb_hash['a'][noun] = {node: (cnt, prob, noun_tag)}
#pickle.dump(self.verb_hash, open("verb_hash.pk", "wb"))
def read_noun_verb(self):
for line in self.noun_verb_file:
split_line = line.split()
if split_line[0] == "#":
continue
noun_tag_split = split_line[0].split("_")
noun = "_".join(noun_tag_split[:-1])
noun_tag = noun_tag_split[-1].upper()
if noun_tag != "NN" and noun_tag != "NNS":
continue
verb_tag_split = split_line[1].split("_")
verb = "_".join(verb_tag_split[:-1])
# Getting rid of light verbs here;
# we can just add them ourselves.
# Also, if the subject can't do it,
# then the object won't either.
if verb in self.light_verbs:
continue
verb_tag = verb_tag_split[-1].upper()
if verb_tag not in self.present_tense:
continue
node = (verb_tag, verb)
cnt = float(split_line[4])
# Ignore things with few counts.
# Do we want to back off to vlc here?
if cnt < self.cnt:
continue
try:
prob = float(split_line[7])
except ValueError:
continue
if prob < self.word_thresh:
continue
try:
# Without this, this is going to get redefined
# when a noun has a different tag. Let's stick
# with the first one, the highest-scoring one...
if node in self.verb_hash['b'][noun]:
continue
self.verb_hash['b'][noun][node] = (cnt, prob, noun_tag)
except KeyError:
self.verb_hash['b'][noun] = {node: (cnt, prob, noun_tag)}
if not self.read_pickle:
pickle.dump(self.verb_hash, open("pickled_files/verb_hash.pk", "wb"))
def read_verb_prep(self):
for line in self.verb_prep_file:
split_line = line.split()
if split_line[0] == "#":
continue
verb_tag_split = split_line[0].split("_")
verb = "_".join(verb_tag_split[:-1])
verb_tag = verb_tag_split[-1].upper()
if verb_tag not in self.present_tense or verb in self.light_verbs:
continue
prep_tag_split = split_line[1].split("_")
prep = "_".join(prep_tag_split[:-1])
prep_tag = prep_tag_split[-1].upper()
node = (prep_tag, prep)
cnt = float(split_line[4])
# Ignore things with few counts.
# Do we want to back off to vlc here?
if cnt < self.cnt:
continue
prob = float(split_line[7])
if prob < self.word_thresh:
continue
try:
# Without this, this is going to get redefined
# when a noun has a different tag. Let's stick
# with the first one, the highest-scoring one...
if node in self.prep_hash['verb-prep'][verb]:
continue
self.prep_hash['verb-prep'][verb][node] = (cnt, prob, verb_tag)
except KeyError:
self.prep_hash['verb-prep'][verb] = {node: (cnt, prob, verb_tag)}
#pickle.dump(self.prep_hash, open("prep_hash.pk", "wb"))
def read_prep_noun(self):
for line in self.prep_noun_file:
split_line = line.split()
if split_line[0] == "#":
continue
noun_tag_split = split_line[0].split("_")
noun = "_".join(noun_tag_split[:-1])
noun_tag = noun_tag_split[-1].upper()
if noun_tag != "NN" and noun_tag != "NNS":
continue
prep_tag_split = split_line[1].split("_")
prep = "_".join(prep_tag_split[:-1])
if prep == "from" or prep == "to" or prep == "of":
continue
prep_tag = prep_tag_split[-1].upper()
node = (prep_tag, prep)
try:
cnt = float(split_line[4])
except ValueError:
continue
# Ignore things with few counts.
# Do we want to back off to vlc here?
if cnt < self.cnt:
continue
try:
prob = float(split_line[7])
except ValueError:
continue
if prob < self.word_thresh:
continue
try:
# The noun is listed first.
# Without the following, this is going to get redefined
# when a noun has a different tag. Let's stick
# with the first one, the highest-scoring one...
if node in self.prep_hash['prep-noun'][noun]:
continue
self.prep_hash['prep-noun'][noun][node] = (cnt, prob, noun_tag)
except KeyError:
self.prep_hash['prep-noun'][noun] = {node: (cnt, prob, noun_tag)}
#pickle.dump(self.prep_hash, open("prep_hash.pk", "wb"))
def read_noun_prep(self):
for line in self.noun_prep_file:
split_line = line.split()
if split_line[0] == "#":
continue
noun_tag_split = split_line[0].split("_")
noun = "_".join(noun_tag_split[:-1])
noun_tag = noun_tag_split[-1].upper()
if noun_tag != "NN" and noun_tag != "NNS":
continue
prep_tag_split = split_line[1].split("_")
prep = "_".join(prep_tag_split[:-1])
# Directional/state preps, not spatial.
if prep == "from" or prep == "to" or prep == "of":
continue
prep_tag = prep_tag_split[-1].upper()
node = (prep_tag, prep)
try:
cnt = float(split_line[4])
except ValueError:
continue
# Ignore things with few counts.
# Do we want to back off to vlc here?
if cnt < self.cnt:
continue
try:
prob = float(split_line[7])
except ValueError:
continue
if prob < self.word_thresh:
continue
try:
# The noun is listed first.
# Without the following, this is going to get redefined
# when a noun has a different tag. Let's stick
# with the first one, the highest-scoring one...
if node in self.prep_hash['noun-prep'][noun]:
continue
self.prep_hash['noun-prep'][noun][node] = (cnt, prob, noun_tag)
except KeyError:
self.prep_hash['noun-prep'][noun] = {node: (cnt, prob, noun_tag)}
if not self.read_pickle:
pickle.dump(self.prep_hash, open("pickled_files/prep_hash.pk", "wb"))
def read_verb_trans(self):
for line in self.verb_trans_file:
split_line = line.split()
if split_line[0] == "#":
continue
# Pol = whether an object noun follows.
pol = split_line[0]
verb = split_line[1]
cnt = split_line[4]
if cnt < self.cnt:
continue
prob = float(split_line[7])
# HARD-CODED prob here to decided if verb is
# transitive or intransitive: If less than
# a quarter of the evidence points to (in)transitive,
# then let's not store it as (in)transitive.
# Finesse this as needed.
if prob < 0.25:
continue
try:
self.verb_trans_hash[verb][pol] = prob
except KeyError:
self.verb_trans_hash[verb] = {pol:prob}
if not self.read_pickle:
pickle.dump(self.verb_trans_hash, open("pickled_files/verb_trans_hash.pk", "wb"))
def read_noun_noun(self):
for line in self.nouns_file:
split_line = line.split()
n1_split = split_line[0].split("_")
n2_split = split_line[1].split("_")
n1 = "_".join(n1_split[:-1])
n2 = "_".join(n2_split[:-1])
npmi = float(split_line[-1])
self.noun_noun_hash[(n1, n2)] = npmi
if not self.read_pickle:
pickle.dump(self.noun_noun_hash, open("pickled_files/noun_noun_hash.pk", "wb"))
def read_ins(self):
for line in self.ins_file:
line = line.strip()
split_line = line.split()
obj = split_line[0]
in_objs = split_line[1:]
self.ins_hash[obj] = in_objs
if not self.read_pickle:
pickle.dump(self.ins_hash, open("pickled_files/ins_hash.pk", "wb"))
def get_determiners(self, obj, tag="NN"):
try:
return self.det_hash[(obj, tag)]
except KeyError:
return self.det_hash[(obj, "NN")]
def get_mods(self, obj):
try:
return self.mod_hash[obj]
except KeyError:
return {}
def get_noun_freq_hash(self, obj):
try:
return self.noun_freq_hash[obj]
except KeyError:
return 0.0
def most_prob_word(self, word_hash):
most_prob = 0.0
c_x = None
for x in word_hash:
prob = word_hash[x]
if prob > most_prob:
most_prob = prob
c_x = x
return c_x
def order_mods(self, mods, obj):
# Orders mods from Mitchell et al. 2011 N-gram model.
iter_permuts = itertools.permutations(mods)
permuts = []
found_order = ()
max_score = -99999999
for perm in iter_permuts:
perm_list = list(perm)
order_mods = ["<s>"] + perm_list + [obj, "</s>"]
permuts += [perm_list]
num = len(order_mods)
max = num
min = 0
score = 0.0
ordered = []
last_max = 0
while ordered != order_mods:
t_order_mods = tuple(order_mods[min:max])
# If we've hit the unigram horizon, continue (Pruning hack)
if max == last_max:
break
if t_order_mods in self.mod_ngram_hash[len(t_order_mods)]:
ordered = order_mods[:max]
last_max = max
score += self.mod_ngram_hash[len(t_order_mods)][t_order_mods]
if ordered[-1] == "</s>":
if score > max_score:
max_score = score
found_order = perm_list
else:
min += 1
max = num
else:
max -= 1
return found_order
def get_att(self, mod):
for att in self.att_hash:
if mod in self.att_hash[att]:
# If this happens more than
# once?
return att
return None
def get_intrans_VP(self, i):
verb_hash = {}
try:
for verb in self.verb_hash['b'][i]:
verb_hash[verb] = self.verb_hash['b'][i][verb][1]
return verb_hash
except KeyError:
return {}
def is_verb_intrans(self, verb):
try:
# HARD-CODED transitive prob from developing on fake data:
# Finesse as needed. If the verb is often
# intransitive (40% of the time or more),
# then say it can be used intransitively.
if self.verb_trans_hash[verb]["-"] > 0.4:
return True
except KeyError:
return False
return False
def get_VPs(self, i, j, PP_js={}):
""" Change this so the VP hash is actually getting
sent here -- thus when action detections fire, we're ok.
"""
# At this point, we just want the set
# that can go NP -> V and V -> PP OR V -> NP....
if PP_js != {}:
verb_prep_hash = {}
for prep_tuple in PP_js:
prep = prep_tuple[1]
try:
for verb_tuple in self.verb_hash['b'][i]:
verb = verb_tuple[1]
# From the set of prepositions leading up to
# the noun, grab the prepositions that follow this verb,
# And get the probabilities; also get the probability
# of this noun following the verb.
if prep_tuple in self.prep_hash['verb-prep'][verb]:
prep_prob = self.prep_hash['prep-noun'][j][prep_tuple][1]
verb_prob = self.verb_hash['b'][i][verb_tuple][1]
verb_prep_hash[(prep_tuple, verb_tuple)] = round(math.sqrt(prep_prob * verb_prob), 4)
except KeyError:
pass
return verb_prep_hash
(unk_i, unk_j) = (False, False)
verb_list_i = {}
verb_list_j = {}
try:
verb_list_i = self.verb_hash['b'][i]
except KeyError:
unk_i = True
try:
verb_list_j = self.verb_hash['a'][j]
except KeyError:
unk_j = True
if unk_i or unk_j:
# This should compute a whole bunch of
# spatial relations, actually.
# Just returns nothing for now.
return {}
verb_hash = {}
for verb_tuple in verb_list_i:
i_prob = verb_list_i[verb_tuple][1]
if verb_tuple in verb_list_j:
# Combining probs.
j_prob = verb_list_j[verb_tuple][1]
verb_hash[verb_tuple] = round(math.sqrt(i_prob * j_prob), 4)
return verb_hash
def get_preps(self, prep, order):
return self.preps[prep][order]
def get_PPs(self, i, j=None, c_preps=None):
prep_hash = {}
# For each of the verbs we're considering for
# an obj complement, see if that verb can also appear with a PP.
# (e.g., boy sees store / boy sees in a store)
if j == None:
if c_preps == None:
return self.prep_hash['prep-noun'][i]
else:
# If we have no preps in our corpus data
# between these words at this threshold,
# just use the visual data verbatim.
# Otherwise, grab the intersection of what
# the vision guesses and what we've seen in our corpus.
try:
for o_prep in self.prep_hash['prep-noun'][i]:
for c_prep in c_preps:
if c_prep == o_prep[1]:
prep_hash[o_prep] = self.prep_hash['prep-noun'][i][o_prep][1]
else: # Just ignore it.
pass
except KeyError:
# Not in our corpus: Make all the vision preps
# equiprobable.
for c_prep in c_preps:
prep_hash[c_prep] = self.word_thresh
return prep_hash
# Gets the intersection of:
# 1. Prepositions the vision system suggests
# 2. Prepositions that head a PP complement of the subject noun.
# 3. Prepositions that head a PP complemented by an NP headed by the object noun.
try:
for node in self.prep_hash['prep-noun'][j]:
if node in self.prep_hash['noun-prep'][i]:
i_prob = self.prep_hash['noun-prep'][i][node][1]
j_prob = self.prep_hash['prep-noun'][j][node][1]
ij_prob = round(math.sqrt(i_prob * j_prob), 4)
if c_preps != None:
# Vision system can confuse "in" with "against" or
# "in front of" in a 2-d image. Fixes this issue when
# the language model suggests "against".
if node[1] in c_preps or (node[1] == "against" and "in" in c_preps):
prep_hash[node] = ij_prob #j_prob
else:
# Prep is not in c_preps; ignore it.
pass
else:
prep_hash[node] = ij_prob
except KeyError:
pass # When some NN or NNS is not in our database.
return prep_hash
def get_noun_noun(self, n1, n2):
n_list = [n1, n2]
n_list.sort()
try:
return self.noun_noun_hash[tuple(n_list)]
except KeyError:
return None
def read_in_wn(self):
for line in self.wn_hyps:
if line[0] == "#":
continue
line = line.strip()
split_line = line.split()
num = int(split_line[0])
pos = int(split_line[1])
prob = float(split_line[-1])
hypernym = split_line[2]
try:
self.hypernym_hash[num][hypernym][pos] = prob
except KeyError:
try:
self.hypernym_hash[num][hypernym] = {pos:prob}
except KeyError:
self.hypernym_hash[num] = {hypernym:{pos:prob}}
if not self.read_pickle:
pickle.dump(self.hypernym_hash, open("pickled_files/hypernym_hash.pk", "wb"))
def get_hyp_probs(self, obj_set):
self.num_objs = len(obj_set)
found = False
for obj in obj_set:
if obj in self.obj_probs:
continue
obj_syn = wn.synset(obj + ".n.01")
found = False
for hypernym_path in obj_syn.hypernym_paths():
if found:
break
hypernym_path = list(hypernym_path)
hypernym_path.reverse()
for hypernym in hypernym_path:
if found:
break
# Return p(pos|num_objs) for all possible pos.
# e.g., {1:0.4, 2:.3, 3:.3}
self.obj_probs[obj] = self.hypernym_hash[self.num_objs][str(hypernym)]
found = True
break
if not self.read_pickle:
pickle.dump(self.obj_probs, open("pickled_files/obj_probs.pk", "wb"))
def order_set(self, obj_list):
final_set = []
# For each position...
tuple_list = []
for i in range(1, self.num_objs + 1):
# Grab the probability of each object in this position.
for obj in self.obj_probs:
try:
obj_prob = self.obj_probs[obj][i]
except KeyError:
obj_prob = .000000000001
tuple_thing = (obj_prob, obj, i)
tuple_list += [tuple_thing]
tuple_list.sort()
tuple_list.reverse()
order_hash = {}
for tuple_thing in tuple_list:
obj = tuple_thing[1]
pos = tuple_thing[2]
if pos not in order_hash:
while obj in obj_list:
try:
order_hash[pos][obj] += 1
except KeyError:
try:
order_hash[pos][obj] = 1
except KeyError:
order_hash[pos] = {obj:1}
obj_index = obj_list.index(obj)
obj_list.pop(obj_index)
for pos in sorted(order_hash):
obj = order_hash[pos].keys()
obj = obj[0]
for i in range(order_hash[pos][obj]):
final_set += [obj]
# Chosen object is the most probable one.
return final_set
def cluster_objs(self, obj_list):
""" Nominal ordering from hypernyms. """
self.get_hyp_probs(obj_list)
final_set = self.order_set(obj_list)
return final_set