-
Notifications
You must be signed in to change notification settings - Fork 1
/
Creating_story_v2.py
3994 lines (3251 loc) · 159 KB
/
Creating_story_v2.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
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
from dataclasses import replace
import json
import random
import re
# from matplotlib.cbook import simple_linear_interpolation
import numpy as np
from Vocabulary import vocabulary, relation_vocabulary
from Generate_graph import relation_formalism
# from random_choice import random_choice
_final_story = ""
annotation = {"story": "", "annotations": []}
temp_annot_sent, traj, land, sp_ind, sp_type, block_temp, sen_temp = "",[],[],[],[],[],""
land_obj_id, traj_obj_id = [], []
scn_rels, scn_objs, objs_rels = {}, {}, {} # [],[],[]
all_objects_data = []
key_sentence = {}
#described_objects and described_relations are a storing dictionary to remember which objects and relations are described.
described_objects, described_relations, obj_scn_queue = {},{}, []
vocab_selection = None
_relation_vocab_selection = None
temp_checked_obj_sc, checked_obj_sc, checked_obj_obj, finished_objs , seen_objects = [], [], [], [], {}
objs_attrs_f, objs_objs_f = [], []
object_shared_prop, shared_prop = [], []
recent_objs = []
treshhold = 0
cannot_have_which = 0
cannot_have_with = 0
has_with = False
G_color, G_size, G_type, G_scene_rel = "", "", "", ""
End_of_block = 0
factor = 0.5
max_treshhold_for_rels = 4
num_of_blocks = 0 # number of scenes
block_id = None # the scene number
entity_phrases = {}
prepositions_relations = [
"LEFT",
"RIGHT",
"ABOVE",
"BELOW",
"BEHIND",
"FRONT",
"NEAR",
"FAR",
"DC",
"NTPP",
"TPP",
]
verb_relations = ["EC", "PO", "TPPI", "NTPPI"]
"""
TODO:
For having the supporting fact sentences :
Just check wherever that we create a sentence and check which triplets this sentence is related to, then assigne that sentence to the triplet
We are ignoring spatial property but it should be considered and added to the object description
A medium grey hexagon is north of the medium red hexagon. Block HHH has this shape. -- > Block HHH has A medium gre ... which is north ...
"""
def nullification():
global scn_rels, scn_objs, objs_rels, num_of_blocks, _final_story, objs_attrs_f, objs_objs_f, checked_obj_sc, checked_obj_obj, finished_objs, object_shared_prop, shared_prop, recent_objs, treshhold, cannot_have_which, G_color, G_size, G_type, G_scene_rel, End_of_block, annotation, described_objects, described_relations, obj_scn_queue, key_sentence
_final_story, objs_attrs_f, objs_objs_f = "", [], []
scn_rels, scn_objs, objs_rels = {}, {}, {}
all_objects_data = []
annotation = {"story": "", "annotations": []}
described_objects, described_relations = {},{}
temp_annot_sent, traj, land, sp_ind, block_temp, sen_temp = "", [], [], [], [], ""
all_objects_data = all_objects_data
checked_obj_sc, checked_obj_obj, finished_objs = [], [], []
obj_scn_queue = []
objs_attrs_f, objs_objs_f = [], []
object_shared_prop, shared_prop = [], []
recent_objs = []
key_sentence = {}
treshhold = 0
cannot_have_which = 0
cannot_have_with = 0
has_with = False
G_color, G_size, G_type, G_scene_rel = "", "", "", ""
End_of_block = 0
num_of_blocks = 0
described_objects, described_relations = {},{}
def creating_story(story_triplets, _all_objects_data, _vocab_selection, _relation_vocab_selection, f):
global scn_rels, scn_objs, objs_rels, num_of_blocks, vocab_selection, all_objects_data, relation_vocab_selection
all_objects_data = _all_objects_data
vocab_selection = _vocab_selection
relation_vocab_selection = _relation_vocab_selection
nullification()
# x = 1
# while x:
# try :
# _temp = random.choice(story)
# if _temp == -1: return -1
# elif _temp == []: continue
# else: scn_rels, scn_objs, objs_rels = _temp
# if scn_objs != []:
# x=0
# except:
# raise
# return -1
# categorize data in the story triplet based on the scene-scene relations, object-scene relations, object-object relations in each scene
for key in story_triplets:
if len(key[0]) == 1 and len(key[1]) == 1:
scn_rels[key] = story_triplets[key]
elif len(key[0]) == 1:
if key[0] in scn_objs:
scn_objs[key[0]][key] = story_triplets[key]
else:
scn_objs[key[0]] = {key: story_triplets[key]}
elif len(key[1]) == 1:
if key[1] in scn_objs:
scn_objs[key[1]][key] = story_triplets[key]
else:
scn_objs[key[1]] = {key: story_triplets[key]}
else: # both are objects
if key[0][0] in objs_rels:
objs_rels[key[0][0]][key] = story_triplets[key]
else:
objs_rels[key[0][0]] = {key: story_triplets[key]}
compute_objects_features()
# number of blocks
# num_of_blocks = 0
# for obj in all_objects_data:
# if len(obj) == 1 : num_of_blocks += 1
num_of_blocks = len([ i for i in all_objects_data if len(i) == 1]) # blocks id has length of 1 eg: "0", "1", "2"
#####for checking
# if num_of_blocks != 3:
# return -1
_final_story = start()
# reshape the text
_final_story = edit_text_form(_final_story)
SOT(_final_story)
# print('##', objs_attrs_f, objs_objs_f)
# print('final story\n', _final_story)
return [_final_story, all_objects_data, objs_attrs_f, objs_objs_f, annotation, num_of_blocks]
# **********************************************************
# *************Start describing functions*******************
# **********************************************************
"""
******************************************************
First we start with mentioning blocks names:
B is for first sentence with description introducing the blocks.
D is for relation between blocks
B0 --> B0D0 (1 block) B0D01 (2 blocks) B0D03 (3 blocks) just name the block then start describe each block's object and when want to describe the objects in a new block describe their relations
possible combinations:
BD
BD0
B0D0
******************************************************
"""
def start():
sentence = ""
# block_id = "0"
#0 = B0D0, 1 = BD0, 2 = BD
y = random.choice(range(3))
# y = 2
if y == 0:
sentence = B0D0()
elif y == 1:
sentence = BD0()
else: #y ==2
sentence = BD()
return sentence
def B0D0():
"""
based on the number of blocks goes to B0D01, B0D02, or B0D03
"""
sen = ''
sen += (
D(block_id = "0",blocks = ['0'], B_is_Null= True)
# + BD_obj("0")
+ P("0")
# + P(scn_objs["0"])
)
if num_of_blocks >1:
sen += (
# "\n"
D(block_id = "1",blocks = ['0', '1'], B_is_Null= True)
+ P("1")
# + P(scn_objs["1"])
)
if num_of_blocks > 2:
sen += (
# "\n"
D(block_id = "2",blocks = ['0', '1', '2'], B_is_Null= True)
+ P("2")
# + P(scn_objs["2"])
)
return sen
def BD0():
sen = ''
sen += (
B()
# + "\n"
+ D(block_id = "0",blocks = ['0'])
# + BD_obj("0")
+ P("0")
# + P(scn_objs["0"])
)
if num_of_blocks >1:
sen += (
# "\n"
D(block_id = "1",blocks = ['0', '1'])
+ P("1")
# + P(scn_objs["1"])
)
if num_of_blocks > 2:
sen += (
# "\n"
D(block_id = "2",blocks = ['0', '1', '2'])
+ P("2")
# + P(scn_objs["2"])
)
return sen
def BD():
sen = ''
#id num of blocks == 1 D return null
sen += (
B()
# + "\n"
+ D()
# + "\n"
+ BD_obj("0")
+ P("0")
)
if num_of_blocks > 1:
sen += (
# "\n"
BD_obj("1")
+ P("1")
)
if num_of_blocks > 2:
sen += (
# "\n"
BD_obj("2")
+ P("2")
)
return sen
def B():
#checked
global recent_objs, traj, traj_obj_id, sp_ind, sp_type, land, land_obj_id, all_objects_data
sentence = ''
_sen = ''
y = random.choice([0, 1,2, 3, 4]) # y == 1 There [is/are] y == 0 We have, y== 2 there exists.
_sen = name_block_all(num_of_blocks)
if num_of_blocks == 1:
recent_objs = ["0"]
if y == 0:
sentence += ("There "
+ ("are " if num_of_blocks > 1 else "is ")
+ _sen
+". "
)
land += [["There"]]
land_obj_id += [["-1"]]
sp_ind += [["are" if num_of_blocks > 1 else "is"]]
sp_type += [["NTPP"]]
traj += [[_sen]]
traj_obj_id += [[str(m) for m in range(num_of_blocks)]]
all_objects_data
elif y == 1:
sentence += ("We have "
+ _sen
+". "
)
traj += [['We']]
traj_obj_id += [["-1"]]
sp_ind += [["have"]]
sp_type += [["NTPPI"]]
land += [[_sen]]
land_obj_id += [[str(m) for m in range(num_of_blocks)]]
elif y == 2: # y == 2
sentence += ("There "
+( "exist " if num_of_blocks > 1 else "exists ")
+ _sen
+". "
)
land += [["There"]]
land_obj_id += [["-1"]]
sp_ind += [["exist" if num_of_blocks > 1 else "exists"]]
sp_type += [["NTPP"]]
traj += [[_sen]]
traj_obj_id += [[str(m) for m in range(num_of_blocks)]]
elif y == 3:
sentence += (start_word(_sen)
+( " exist. " if num_of_blocks > 1 else " exists. ")
)
traj += [[_sen]]
traj_obj_id += [[str(m) for m in range(num_of_blocks)]]
sp_ind += [["exist" if num_of_blocks > 1 else "exists"]]
sp_type += [["NTPP"]]
land += [[""]]
land_obj_id += [["-1"]]
else: #y ==4
sentence += (start_word(_sen)
+( " exist in an image. " if num_of_blocks > 1 else " exists in an image. ")
)
traj += [[_sen]]
traj_obj_id += [[str(m) for m in range(num_of_blocks)]]
sp_ind += [["exist in" if num_of_blocks > 1 else "exists in"]]
sp_type += [["NTPP"]]
land += [["an image"]]
land_obj_id += [["-1"]]
#add the idea of group (e.g. three blocks) in to the all objects data as "other_id"
# for i in range(num_of_blocks):
# all_objects_data[i]["other_id"] = [num_of_blocks+1]
described_objects['0'] = []
if num_of_blocks > 1: described_objects['1'] = []
if num_of_blocks > 2: described_objects['2'] = []
create_annotation(sentence)
return start_word(sentence)
def B_id(block_id):
#checked
global recent_objs, traj, traj_obj_id, sp_ind, sp_type, land, land_obj_id
sentence = ''
_sen = ''
y = random.choice([0, 1,2, 3, 4]) # y == 1 There [is/are] y == 0 We have, y== 2 there exists.
# _sen = name_block_all(num_of_blocks)
_sen = "a "+select_vocab("block")+" "+name_call()+select_vocab("block_name", block_id)
if num_of_blocks == 1:
recent_objs = ["0"]
if y == 0:
sentence += ("There "
+ "is "
+ _sen
+". "
)
land += [["There"]]
land_obj_id += [["-1"]]
sp_ind += [["is"]]
sp_type += [["NTPP"]]
traj += [[_sen]]
traj_obj_id += [[block_id]]
elif y == 1:
sentence += ("We have "
+ _sen
+". "
)
traj += [['We']]
traj_obj_id += [["-1"]]
sp_ind += [["have"]]
sp_type += [["NTPPI"]]
land += [[_sen]]
land_obj_id += [[block_id]]
elif y == 2: # y == 2
sentence += ("There "
+"exists "
+ _sen
+". "
)
land += [["There"]]
land_obj_id += [["-1"]]
sp_ind += [["exists"]]
sp_type += [["NTPP"]]
traj += [[_sen]]
traj_obj_id += [[block_id]]
elif y == 3:
sentence += (start_word(_sen)
+" exists. "
)
traj += [[_sen]]
traj_obj_id += [[block_id]]
sp_ind += [["exists"]]
sp_type += [["NTPP"]]
land += [[""]]
land_obj_id += [["-1"]]
else: #y ==4
sentence += (start_word(_sen)
+" exists in an image. "
)
traj += [[_sen]]
traj_obj_id += [[block_id]]
sp_ind += [["exists in"]]
sp_type += [["NTPP"]]
land += [["an image"]]
land_obj_id += [["-1"]]
described_objects[block_id] = []
create_annotation(sentence)
return sentence
def BD_obj(block_id, between_blocks_relations = False, B_is_Null = False):
"""
We already describe each blocks and their relation (eather in D or D0 situation)
We want to describe the objects in each block in the beginning of of the block description.
"""
sen = ""
G_name = select_vocab("block")
global recent_objs,traj, sp_ind, traj_obj_id, land, land_obj_id, block_temp, sen_temp, has_with
_temp_block_name = G_name + " " + name_block(block_id)
if sen_temp or between_blocks_relations:
#CHECKED
"""
mentioned objects that this block have in the relation description between blocks
if not cannot_have_which and cannot_have_with create this
when it comes in this part it means that it should have which or with
we check with if it is possible select between them if not go with which/that
"""
object_block_rel_description = obj_rel_scene(block_id, is_traj= False, check_with = False if cannot_have_with else True, fixed_relation = ["NTPPI"]) # is_traj is for object so if block_as_traj == 1 obj should be land
if object_block_rel_description != None:
if has_with:
sen += ' '+ object_block_rel_description
has_with = False
else:
sen += ' '+wh_th()+' '+ object_block_rel_description
# traj += [[block_temp]]* len(sp_ind)
# traj_obj_id += [[block_id]]* len(sp_ind)
else: return None
else: # we want to start a new sentence
#CHECKED
block_as_traj = random.choice([False,True])
object_block_rel_description = obj_rel_scene(block_id, is_traj= False if block_as_traj else True ) # is_traj is for object so if block_as_traj == 1 obj should be land
if object_block_rel_description == None: #there isn't any rel with block as traj
block_as_traj = not block_as_traj
object_block_rel_description = obj_rel_scene(block_id, is_traj= False if block_as_traj else True)
if object_block_rel_description == None: #if it is still none it means that this block doesn't have object
return ""
# if this object is the subject of previous sentence we can use pronouns
block_mention = select_pronoun(is_subject= block_as_traj, is_block=True, plural= True if len(recent_objs) > 1 else False) if [block_id] == recent_objs else _temp_block_name
if block_as_traj:
if B_is_Null:
_, new_mention = B0_call_block(block_id, is_traj = True)
block_mention = new_mention
# else:
# starter_phrase = block_mention
sen += start_word(block_mention) + ' '+ object_block_rel_description+'. '
traj += [[block_mention]]* len(sp_ind)
traj_obj_id += [[block_id]]* len(sp_ind)
recent_objs = [block_id]
else:
if B_is_Null:
_, new_mention = B0_call_block(block_id)
block_mention = new_mention
sen += start_word(object_block_rel_description)+ ' '+block_mention+'. '
land += [[block_mention]]* len(sp_ind)
land_obj_id += [[block_id]]* len(sp_ind)
create_annotation(sen)
sen = start_word(sen)
return sen
def D(block_id = None,blocks = [], B_is_Null = False): # D is a description about positions of block to each other right after first block description. for BD
# global traj, sp_ind, land, land_obj_id, traj_obj_id, sp_type, recent_objs
sen = ""
"""
D is for describing the relation between objects
if block_id, it means it is D0 else it is D which we describe all relations between blocks
D0:
blocks list show which blocks we are describing their relation
between the describing the relations we can also add BD_obj(with/which)
"""
if block_id == None: # D != Null and we want to describe all relations between blocks at the beginning of story.
# if blocks == ['0']: #we don't have relation between blocks yet
# return ""
shared_relations_keys = find_shared_rel(
searching_list= scn_rels, num_objects= num_of_blocks
)
for shared_rel in shared_relations_keys:
relation_need_verb = shared_rel["need_verb"] # if True we need to add verb to the relation and we can have Sp_ind+land+traj sentences
shared_info = describe_shared_scn_scn(shared_rel)
"""
shared_info:{
"land_obj_desc": land_obj,
"traj_obj_desc": traj_obj,
"relation_text": relation_text,
}
"""
#concat the information, create the sentence, create the annotation
_sen = ''
if shared_rel["has_common_key"]: #elif shared_rel["has_common_key"]
if shared_rel["is_traj"]:
#connect the obj and rel
obj_desc = ''
for ind, desc in enumerate(shared_info["land_obj_desc"]):
if obj_desc: obj_desc += ', '
if relation_need_verb[ind]:
obj_desc += check_verb("[is/are]", is_singular=True)+" "+shared_info["relation_text"][ind]+ " " +desc
else:
obj_desc += shared_info["relation_text"][ind]+ " " +desc
land_obj = add_and(obj_desc)
traj_obj = shared_info["traj_obj_desc"][0]
else:
obj_desc = ''
for ind, desc in enumerate(shared_info["traj_obj_desc"]):
if obj_desc: obj_desc += ', '
if relation_need_verb[ind]:
obj_desc += desc +" "+check_verb("[is/are]", is_singular=True)+" "+ shared_info["relation_text"][ind]
else:
obj_desc += desc +" "+ shared_info["relation_text"][ind]
traj_obj = add_and(obj_desc)
land_obj = shared_info["land_obj_desc"][0]
_sen = start_word(traj_obj)+" "+land_obj+'. '
#create sentence
create_annotation(_sen)
sen += _sen
else: #if shared_rel["has_common_rel"]: or no common part
if shared_rel["is_traj"]:
#connect the shared part
obj_desc = ''
for desc in shared_info["land_obj_desc"]:
if obj_desc: obj_desc += ', '
obj_desc += desc
land_obj = add_and(obj_desc)
traj_obj = shared_info["traj_obj_desc"][0]
else:
obj_desc = ''
for desc in shared_info["traj_obj_desc"]:
if obj_desc: obj_desc += ', '
obj_desc += desc
traj_obj = add_and(obj_desc)
land_obj = shared_info["land_obj_desc"][0]
#create sentence
if relation_need_verb[0]:
#We need to add [is/are] verb and also have sp_id+land+traj sentences
h = random.choice([0,1,1])
# if h:
# verb_singularity = True if len(shared_info["traj_obj_desc"]) == 1 else False
# else:
# verb_singularity = True if len(shared_info["land_obj_desc"]) == 1 else False
verb_singularity = False if shared_rel["has_common_rel"] else True
verb_text = check_verb("[is/are]", is_singular= verb_singularity)
if h:
_sen = (start_word(shared_info["relation_text"][0])
+" "
+land_obj
+" "
+("there " if random.choice([0,1]) else "")
+ verb_text
+" "
+traj_obj+". "
)
else:
_sen = start_word(traj_obj)+" "+verb_text +" "+shared_info["relation_text"][0]+" "+land_obj+'. '
else:
_sen = start_word(traj_obj)+" "+shared_info["relation_text"][0]+" "+land_obj+'. '
create_annotation(_sen)
sen += _sen
# else: # we only have one triplet and no common part
# if relation_need_verb[0]:
# #We need to add [is/are] verb and also have sp_id+land+traj sentences
# verb_text = check_verb("[is/are]", is_singular= True)
# if random.choice([0,1,1]):
# _sen = (start_word(shared_info["relation_text"][0])
# +" "
# +land_obj
# +" "
# +("there " if random.choice([0,1]) else "")
# + verb_text
# +traj_obj+". "
# )
# else:
# _sen = start_word(traj_obj)+" "+verb_text +" "+shared_info["relation_text"][0]+" "+land_obj+'.'
# else:
# _sen = start_word(traj_obj)+" "+shared_info["relation_text"][0]+" "+land_obj+'.'
else:
"""
we have block_id
it means that we want to describe the relation between the current block and others
also if it is possible add some objects in the block in between
if not add the obj_rel description (BD_obj) at the end
"""
if blocks == ['0']: #we don't have relation between blocks yet
sen = BD_obj('0', B_is_Null= B_is_Null)
# if _sen == '' and B_is_Null: _sen = B_id('0')
else:
shared_relations_keys = find_shared_rel(
searching_list= limit_the_list(scn_rels, block_id,blocks), num_objects=len(blocks)
)
for shared_rel in shared_relations_keys:
relation_need_verb = shared_rel["need_verb"]
shared_info = describe_shared_scn_scn(shared_rel, scn_id = block_id , check_having_obj_scn_rels= True, B_is_Null = B_is_Null)
"""
shared_info:{
"land_obj_desc": land_obj,
"traj_obj_desc": traj_obj,
"relation_text": relation_text,
# "new_block_description": B0_value
}
"""
#concat the information, create the sentence, create the annotation
_sen = ''
if shared_rel["has_common_key"]:
if shared_rel["is_traj"]:
#connect the obj and rel
obj_desc = ''
for ind, desc in enumerate(shared_info["land_obj_desc"]):
if obj_desc: obj_desc += ', '
if relation_need_verb[ind]:
obj_desc += check_verb("[is/are]", is_singular=True)+" "+shared_info["relation_text"][ind]+ " " +desc
else:
obj_desc += shared_info["relation_text"][ind]+ " " +desc
land_obj = add_and(obj_desc)
traj_obj = shared_info["traj_obj_desc"][0]
else:
obj_desc = ''
for ind, desc in enumerate(shared_info["traj_obj_desc"]):
if obj_desc: obj_desc += ', '
if relation_need_verb[ind]:
obj_desc += desc +" "+check_verb("[is/are]", is_singular=True)+" "+ shared_info["relation_text"][ind]
else:
obj_desc += desc +" "+ shared_info["relation_text"][ind]
traj_obj = add_and(obj_desc)
land_obj = shared_info["land_obj_desc"][0]
_sen = start_word(traj_obj)+" "+land_obj+'. '
create_annotation(_sen)
sen += _sen
else: #if shared_rel["has_common_rel"]: or no common part
if shared_rel["is_traj"]:
#connect the shared part
obj_desc = ''
for desc in shared_info["land_obj_desc"]:
if obj_desc: obj_desc += ', '
obj_desc += desc
land_obj = add_and(obj_desc)
traj_obj = shared_info["traj_obj_desc"][0]
else:
obj_desc = ''
for desc in shared_info["traj_obj_desc"]:
if obj_desc: obj_desc += ', '
obj_desc += desc
traj_obj = add_and(obj_desc)
land_obj = shared_info["land_obj_desc"][0]
if relation_need_verb[0]:
#We need to add [is/are] verb and also have sp_id+land+traj sentences
h = random.choice([0,1,1])
# if h:
verb_singularity = False if shared_rel["has_common_rel"] else True
# else:
# verb_singularity = True if len(shared_info["land_obj_desc"]) == 1 else False
verb_text = check_verb("[is/are]", is_singular= verb_singularity)
if h:
_sen = (start_word(shared_info["relation_text"][0])
+" "
+land_obj
+" "
+("there " if random.choice([0,1]) else "")
+ verb_text
+" "
+traj_obj+". "
)
else:
_sen = start_word(traj_obj)+" "+verb_text +" "+shared_info["relation_text"][0]+" "+land_obj+'. '
else:
_sen = start_word(traj_obj)+" "+shared_info["relation_text"][0]+" "+land_obj+'. '
# _sen = start_word(traj_obj)+" "+shared_info["relation_text"]+" "+land_obj+'.'
create_annotation(_sen)
sen += _sen
if sen == '' and B_is_Null: sen = B_id(block_id)
return sen
def describe_shared_scn_scn(shared_rel, scn_id = '0' , check_having_obj_scn_rels= False, B_is_Null = False):
"""
This function is for generating the shared and un-shared relations between blocks
Compared to described shared_relation in obj-scn, this one can have pronouns as objects, also we have both the land and trajector descriptiopn
Also since this the only place that we decribe the relation between blocks and the find_shared find all possible relations there is no need to pop the described relations
The verb is based on the is_traj value. If the shared part is_traj verb shoudl be singular if not it may be plural.
The output of this function is traj_objects, relation, and land_object description, alongside their annotation.
Check_with means if the block_id object can have with relation or not
We also check if it can have
"""
global all_objects_data, described_objects, described_relations, recent_objs
global sp_ind, sp_type, traj, traj_obj_id, land_obj_id, land
relation_text = ''
_recent_objs = []
is_traj = shared_rel["is_traj"]
land_obj, traj_obj = [], []
B0_value = ""
if shared_rel["has_common_rel"] == True:
if is_traj:
traj_desc = select_vocab("block")+" "+ select_vocab("block_name",key = shared_rel["keys"][0][0]) if [shared_rel["keys"][0][0]] != recent_objs else select_pronoun()
extra_relation = ""
if shared_rel["keys"][0][0] == scn_id:
if B_is_Null:
"""
we can have scn_obj rels
If this a new block we add the definition sentence or phrase:
"We have another block" "called --" "let's call it --"
"There is another block"
+ "which"
or + ". It is .. "
Simiplify B0_call_block and only return another or a block called ....
"""
_, new_block_mention = B0_call_block(scn_id, is_traj = True)
traj_desc = new_block_mention
previous_len_of_land = len(land)# we want to see how many objects are added afer BD_obj
extra_relation = BD_obj(scn_id, between_blocks_relations= True)
if extra_relation:
num_of_added_obj = len(land) - previous_len_of_land
# the extra relation and obejct are landmark so we add another one to trajector
traj_obj_id += [[scn_id]]*num_of_added_obj
traj += [[traj_desc]]*num_of_added_obj # repeat the traj as the trajector of the new triplet we added
else: extra_relation = ""
# traj_obj = [B0_value + extra_relation] if B0_value else [traj_desc+extra_relation]
traj_obj = [traj_desc+extra_relation]
traj += [[traj_desc]]*len(shared_rel["keys"])
traj_obj_id +=[[shared_rel["keys"][0][0]]]*len(shared_rel["keys"])
described_objects[shared_rel["keys"][0][0]] = []
_recent_objs = [shared_rel["keys"][0][0]]
else:
land_desc = select_vocab("block")+" "+ select_vocab("block_name",key = shared_rel["keys"][0][1]) if [shared_rel["keys"][0][1]] != recent_objs else select_pronoun(is_subject= False)
extra_relation = ""
if shared_rel["keys"][0][1] == scn_id: # we can have scn_obj rels
if B_is_Null:
"""
"another block"
"""
_, new_block_mention = B0_call_block(scn_id)
land_desc = new_block_mention
previous_len_of_land = len(land)# we want to see how many objects are added afer BD_obj
extra_relation = BD_obj(scn_id, between_blocks_relations= True)
if extra_relation:
num_of_added_obj = len(land) - previous_len_of_land
# the extra relation and obejct are landmark so we add another one to trajector
traj_obj_id += [[scn_id]]*num_of_added_obj
traj += [[land_desc]]*num_of_added_obj # repeat the traj as the trajector of the new triplet we added
else: extra_relation = ""
land_obj = [land_desc+extra_relation]
land += [[land_desc]]*len(shared_rel["keys"])
land_obj_id +=[[shared_rel["keys"][0][1]]]*len(shared_rel["keys"])
described_objects[shared_rel["keys"][0][1]] = []
#check the verb singularity
# if is_traj: verb_is_singular = True
# elif len(shared_rel["keys"]) == 1: verb_is_singular = True
# else: verb_is_singular = False
#start describing the objects and their shared relations
sp_ind += check_verb(shared_rel["spatial_indicators"], is_singular=True )*len(shared_rel["keys"])
sp_type += shared_rel["relation"]*len(shared_rel["keys"])
relation_text = check_verb(shared_rel["relation_text"], is_singular=True) #check verb correct the plural or singular verbs
#shared_part
for i_ind, i in enumerate(shared_rel["keys"]):
expanded_rel = expand_rel_describtion(shared_rel["relation"][0])
if i not in described_relations:
described_relations[i] = expanded_rel
else:
described_relations[i] += expanded_rel
for r in expanded_rel:
if i in scn_rels and r in scn_rels[i]:
scn_rels[i].remove(r)
if i in scn_rels and scn_rels[i] == []: scn_rels.pop(i,None)
if is_traj: # we have multiple land
if i[1] in described_objects:
obj_desc = select_vocab("block")+" "+ select_vocab("block_name",key = i[1]) if [i[1]] != recent_objs else select_pronoun(is_subject=False)
else: _, obj_desc = B0_call_block(i[1])
land_obj += [obj_desc]
land += [[obj_desc]]
land_obj_id += [[i[1]]]
described_objects[i[1]]=[]
else: # we have multiple traj
if i[0] in described_objects:
obj_desc = select_vocab("block")+" "+ select_vocab("block_name",key = i[0]) if [i[0]] != recent_objs else select_pronoun()
else: _,obj_desc = B0_call_block(i[0])
traj_obj += [obj_desc]
traj += [[obj_desc]]
traj_obj_id += [[i[0]]]
_recent_objs += [i[0]]
described_objects[i[0]]=[]
#describing the shared part
else: #elif shared_rel["has_common_key"] == True or only one object has the relation and is the trajector or landmark of this relation (based on is_traj value)
if is_traj:
traj_desc = select_vocab("block")+" "+ select_vocab("block_name",key = shared_rel["keys"][0][0]) if [shared_rel["keys"][0][0]] != recent_objs else select_pronoun()
extra_relation = ""
if shared_rel["keys"][0][0] == scn_id: # we can have scn_obj rels
if B_is_Null:
"""
If this a new block we add the definition sentence or phrase:
"We have another block" "called --" "let's call it --"
"There is another block"
+ "which"
or + ". It is .. "
make it simple like land part
"""
_, new_block_mention = B0_call_block(scn_id, is_traj = True)
traj_desc = new_block_mention
previous_len_of_land = len(land)# we want to see how many objects are added afer BD_obj
extra_relation = BD_obj(scn_id, between_blocks_relations= True)
if extra_relation:
num_of_added_obj = len(land) - previous_len_of_land
# the extra relation and obejct are landmark so we add another one to trajector
traj_obj_id += [[scn_id]]*num_of_added_obj
traj += [[traj_desc]]*num_of_added_obj # repeat the traj as the trajector of the new triplet we added
else: extra_relation = ""
# traj_obj = [B0_value + extra_relation] if B0_value else [traj_desc+extra_relation]
traj_obj = [traj_desc+extra_relation]
traj += [[traj_desc]]*len(shared_rel["keys"])
traj_obj_id +=[[shared_rel["keys"][0][0]]]*len(shared_rel["keys"])
described_objects[shared_rel["keys"][0][0]] = []
_recent_objs = [shared_rel["keys"][0][0]]
else:
land_desc = select_vocab("block")+" "+ select_vocab("block_name",key = shared_rel["keys"][0][1]) if [shared_rel["keys"][0][1]] != recent_objs else select_pronoun(is_subject=False)
extra_relation = ""
if shared_rel["keys"][0][1] == scn_id: # we can have scn_obj rels
if B_is_Null:
"""
"another block"
"""
_, new_block_mention = B0_call_block(scn_id)
land_desc = new_block_mention
previous_len_of_land = len(land)# we want to see how many objects are added afer BD_obj
extra_relation = BD_obj(scn_id, between_blocks_relations= True)
if extra_relation:
num_of_added_obj = len(land) - previous_len_of_land
# the extra relation and obejct are landmark so we add another one to trajector
traj_obj_id += [[scn_id]]*num_of_added_obj
traj += [[land_desc]]*num_of_added_obj # repeat the traj as the trajector of the new triplet we added
else: extra_relation = ""
land_obj = [land_desc+extra_relation]
land += [[land_desc]]*len(shared_rel["keys"])
land_obj_id +=[[shared_rel["keys"][0][1]]]*len(shared_rel["keys"])
described_objects[shared_rel["keys"][0][1]] = []
#the verb remain singular since each has own relations
sp_ind += check_verb(shared_rel["spatial_indicators"], is_singular=True)
sp_type += shared_rel["relation"]
relation_text = check_verb(shared_rel["relation_text"], is_singular=True)
obj_rel_description = ''
for i_ind, i in enumerate(shared_rel["keys"]):
"""
traj, traj_obj_id, described_objects, described_rel, obj_description
"""
expanded_rel = expand_rel_describtion(shared_rel["relation"][i_ind])
if i not in described_relations:
described_relations[i] = expanded_rel