-
Notifications
You must be signed in to change notification settings - Fork 0
/
language_generator.py
183 lines (155 loc) · 6.98 KB
/
language_generator.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
from relation import *
# from random import choice
import sys
sys.path.append("..")
from myrandom import random
choice = random.choice
from landmark import Landmark, ObjectClass, Color
from itertools import product
# point_words = ['bottle', 'cup', 'computer', 'laptop', 'keyboard', 'book', 'box', 'monitor',
# 'disc', 'CD', 'camera', 'lens', 'motor', 'screwdriver', 'pen', 'pencil']
class_to_words = {
ObjectClass.TABLE: {'N' : ['table']},
ObjectClass.CHAIR: {'N' : ['chair']},
ObjectClass.CUP: {'N' : ['cup']},
ObjectClass.BOTTLE: {'N' : ['bottle']},
ObjectClass.PRISM: {'N' : ['prism', 'triangle']},
ObjectClass.BOX: {'N' : ['box','rectangle','block']},
ObjectClass.CYLINDER: {'N' : ['cylinder']},
ObjectClass.SPHERE: {'N' : ['sphere']},
Color.RED: {'A' : ['red']},
Color.GREEN: {'A' : ['green']},
Color.PURPLE: {'A' : ['purple']},
Color.BLUE: {'A' : ['blue']},
Color.PINK: {'A' : ['pink']},
Color.ORANGE: {'A' : ['orange']},
Color.YELLOW: {'A' : ['yellow']},
Color.BLACK: {'A' : ['black']},
Color.WHITE: {'A' : ['white']},
Landmark.EDGE: {'N' : ['edge']},
Landmark.CORNER: {'N' : ['corner']},
Landmark.MIDDLE: {'N' : ['middle']},
Landmark.HALF: {'N' : ['half']},
Landmark.END: {'N' : ['end']},
Landmark.SIDE: {'N' : ['side']},
Landmark.LINE: {'N' : ['line']},
Landmark.POINT: {'N' : ['point']},
FromRelation: {'P' : ['from']},
ToRelation: {'P' : ['to']},
# NextToRelation: {'P' : ['at', 'by']},#['next to', 'at', 'by']},
OnRelation: {'P' : ['on','in']},
InFrontRelation: {'P' : ['in front of'], 'A' : ['front', 'near']},
BehindRelation: {'P' : ['behind'], 'A' : ['back', 'far']},
LeftRelation: {'P' : ['to the left of'], 'A' : ['left']},
RightRelation: {'P' : ['to the right of'], 'A' : ['right']},
Degree.NONE: {'R' : ['']},
Degree.SOMEWHAT: {'R' : ['somewhat']},
Degree.VERY: {'R' : ['very']},
Measurement.NONE: {'A' : ['']},
Measurement.FAR: {'A' : ['far']},
Measurement.NEAR: {'A' : ['near', 'close','next']},
}
phrase_to_class = {
'table': ObjectClass.TABLE,
'table surface': ObjectClass.TABLE,
'chair': ObjectClass.CHAIR,
'cup': ObjectClass.CUP,
'bottle': ObjectClass.BOTTLE,
'edge': Landmark.EDGE,
'corner': Landmark.CORNER,
'middle': Landmark.MIDDLE,
'half': Landmark.HALF,
'end': Landmark.END,
'side': Landmark.SIDE,
'line': Landmark.LINE,
'from': FromRelation,
'to': ToRelation,
# 'next to': NextToRelation,
# 'at': NextToRelation,
# 'by': NextToRelation,
'on': OnRelation,
'in front of': InFrontRelation,
'front': InFrontRelation,
'near': InFrontRelation,
'behind': BehindRelation,
'back': BehindRelation,
'far': BehindRelation,
'to the left of': LeftRelation,
'left': LeftRelation,
'to the right of': RightRelation,
'right': RightRelation,
'somewhat': Degree.SOMEWHAT,
'very': Degree.VERY,
'close': Measurement.NEAR,
'of': 'OF',
'the': 'DT',
}
def get_landmark_description(perspective, landmark, delimit_chunks=False):
noun = choice(class_to_words[landmark.object_class]['N']) + (' * ' if delimit_chunks else ' ')
desc = 'the' + (' * ' if delimit_chunks else ' ')
ori = color = nounclass = True
do = [ori,color,nounclass]
# if random.random() > 0.5:
# do[choice([0,1,2])] = False
# ori,color,nounclass = do
if ori:
for option in landmark.ori_relations:
desc += choice( class_to_words[option]['A'] ) + (' * ' if delimit_chunks else ' ')
desc += (choice(class_to_words[landmark.color]['A']) + ' ' if color and landmark.color else '') + (noun if nounclass else '')
if landmark.parent and landmark.parent.parent_landmark:
p_desc = get_landmark_description(perspective, landmark.parent.parent_landmark)
if p_desc:
desc += 'of' + (' * ' if delimit_chunks else ' ') + p_desc
return desc
def get_relation_description(relation, delimit_chunks=False):
desc = ''
if hasattr(relation, 'measurement'):# and not isinstance(relation,VeryCloseDistanceRelation): #TODO create another class called AdjacentRelation
m = relation.measurement
degree = choice(class_to_words[m.best_degree_class]['R'])
distance = choice(class_to_words[m.best_distance_class]['A'])
desc += degree + ( (' * ' if delimit_chunks else ' ') if degree else '') + \
distance + ( (' * ' if delimit_chunks else ' ') if distance else '')
return desc + choice(class_to_words[type(relation)]['P']) + (' * ' if delimit_chunks else ' ')
def describe(perspective, trajector, landmark, relation, delimit_chunks=False):
# return 'The ' + \
# (choice(class_to_words[trajector.color]['A']) + ' ' if trajector.color else '') + \
# choice(class_to_words[trajector.object_class]['N']) + ' is' + \
# (' * ' if delimit_chunks else ' ') + \
return get_relation_description(relation, delimit_chunks) + \
get_landmark_description(perspective, landmark, delimit_chunks)
def get_all_landmark_descriptions(perspective, trajector, landmark):
lists = [['the','a']]
lists.extend([class_to_words[option]['A'] for option in landmark.ori_relations])
lists.append(class_to_words[landmark.color]['A'] if landmark.color else [])
lists.append(class_to_words[landmark.object_class]['N'])
lists = filter(None,lists)
if landmark.parent and landmark.parent.parent_landmark:
lists.append( ['of'] )
lists.append( get_all_landmark_descriptions(perspective, trajector, landmark.parent.parent_landmark) )
return [' '.join(tup) for tup in product(*lists)]
def get_all_relation_descriptions(relation):
lists = []
if hasattr(relation, 'measurement'):# and not isinstance(relation,VeryCloseDistanceRelation):
m = relation.measurement
lists.append(class_to_words[m.best_degree_class]['R'])
lists.append(class_to_words[m.best_distance_class]['A'])
lists.append(class_to_words[type(relation)]['P'])
lists = filter(None, lists)
return [' '.join(tup) for tup in product(*lists)]
def get_all_descriptions(perspective, trajector, landmark, relation, the_point_is=False):
lmk_descs = get_all_landmark_descriptions(perspective, trajector, landmark)
rel_descs = get_all_relation_descriptions(relation)
return [' '.join(tup).strip() for tup in product(rel_descs, lmk_descs)]
def phrases_to_meaning(phrases):
m = []
for p in phrases:
p = p.strip()
if p in phrase_to_class:
m.append(phrase_to_class[p])
print m
if __name__ == '__main__':
f = open('/home/anton/github/bolt/voting-experts/input/word/bolt_all_3k.txt')
for l in f:
phrases = l.split('*')
print phrases
phrases_to_meaning(phrases)