File: interlinguish.py

package info (click to toggle)
cyphesis-cpp 0.5.16-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,084 kB
  • ctags: 3,627
  • sloc: cpp: 30,418; python: 4,812; xml: 4,674; sh: 4,118; makefile: 902; ansic: 617
file content (293 lines) | stat: -rw-r--r-- 10,301 bytes parent folder | download
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
#This file is distributed under the terms of the GNU General Public license.
#Copyright (C) 1999 Aloril (See the file COPYING for details).

import string,re
from mind.panlingua import panlingua
p=panlingua

#semnode ids from WordNet (english word+type+sense no)
#or it's made up (temporary until replaces with interlinguish sentence(s)
#describing goal or word (sense no=0)
word2node={}
def add_word(words,index):
    name=string.replace(words," ","_")
    word2node[words]=(words,index)
    globals()[name]=(words,index)

add_word('help','#help_verb1')
add_word('welcome','#welcome_verb1')
add_word('breakfast','#breakfast_verb1')
add_word('lunch','#lunch_verb1')
add_word('sup','#sup_verb1')
add_word('sleep','#sleep_verb1')
add_word('eat','#eat_verb2')
add_word('be','#be_verb1')
add_word('important','#important_adj1')
add_word('more','#more_adv1')
add_word('than','#than_prep0')
add_word('add_extinguish_fire','#add_extinguish_fire_sentence0') #CHEAT!
add_word('wander','#wander_verb1')
add_word('forage','#forage_verb1')
add_word('browse','#browse_verb1')
add_word('avoid','#avoid_verb1')
add_word('flock','#flock_verb1')
add_word('hunt','#hunt_verb1')
add_word('transport','#transport_verb1')
add_word('assemble','#assemble_verb1')
add_word('market','#market_verb1')
add_word('peck', '#peck_verb1')

#I would like to buy an axe.
add_word('I','#I_pron0')
#s/would like/desire/
add_word('desire','#desire_verb3')
add_word('axe','#axe_noun1')
add_word('pig','#pig_noun1')
add_word('lumber','#lumber_noun1')
add_word('ham','#ham_noun1')
add_word('coin','#coin_noun1')
add_word('deed','#deed_noun1')
add_word('shirt','#shirt_noun1')
add_word('cloak','#cloak_noun1')
add_word('trousers','#trousers_noun1')
add_word('services','#services_noun1')
add_word('buy','#buy_verb1')
add_word('hire','#hire_verb1')
add_word('trade','#trade_verb1')
add_word('keep','#keep_verb1')
add_word('sell','#sell_verb1')
add_word('patrol','#patrol_verb1')
add_word('defend','#defend_verb1')
add_word('herd','#herd_verb1')
add_word('vote','#vote_verb1')
add_word('execute','#execute_verb1')
add_word('shovel','#shovel_noun1')
add_word('tinderbox','#tinderbox_noun1')
add_word('pickaxe','#pickaxe_noun1')
add_word('bowl','#bowl_noun1')
add_word('pole','#pole_noun1')
add_word('hammer','#hammer_noun1')
add_word('bottle','#bottle_noun1')
add_word('cleaver','#cleaver_noun1')
add_word('trowel','#trowel_noun1')
add_word('scythe','#scythe_noun1')
add_word('bucksaw','#bucksaw_noun1')

#CHEAT!: temporary thing
add_word('build home','#build_home_verb0')
add_word('chop trees','#chop_trees0')
add_word('create axes','#create_axes0')
add_word('move lumber','#move_lumber0')
add_word('buy livestock','#buy_livestock0')


def importance(sub,cmp,obj):
    a1=p.atom("is",1,(be[1],"present tense"),(None,"declarative verb"))
    a2=p.atom(sub[0],2,(sub[1],"durative"),(a1,"verb subject"),"up")
    a3=p.atom("important",3,(important[1],"default"),
              (a2,"predicate adjective"),"left")
    a4=p.atom("more",4,(more[1],"default"),
              (a3,"adjective-modifying adverb"),"up")
    a5=p.atom("than",5,(than[1],"default"),
              (a4,"comparing preposition"),"left")
    a6=p.atom(obj[0],6,(obj[1],"durative"),(a5,"verb object"),"up")
    return (string.capitalize(sub[0])+\
            "ing is more important than "+obj[0]+"ing.",
            (a1,a2,a3,a4,a5,a6))

def match_importance(say):
    #A is more important than B.
    a1=p.atom("",1)
    a2=p.atom("",2,("?sub","durative"),(a1,"verb subject"),"up")
    a3=p.atom("important",3,('#important_adj1',"default"),
              (a2,"predicate adjective"),"left")
    a4=p.atom("more",4,('#more_adv1',"default"),
              (a3,"adjective-modifying adverb"),"up")
    a5=p.atom("than",5,('#than_prep0',"default"),
              (a4,"comparing preposition"),"left")
    a6=p.atom("",6,("?obj","durative"),(a5,"verb object"),"up")
    return p.match_sentence(say,[a1,a2,a3,a4,a5,a6])


#>John loves Mary.
#>loves#1 sib(3), lexlink(present tense: ppt=yes)
#>        synlink(declarative verb)
#>John#2 sib(1), lexlink(default)
#>        synlink(noun subject) up to loves#1
#>Mary#3 sib(1), lexlink(default)
#        synlink(noun object) left to John#2

vso_dict={}
def add_vso_word(words,index):
    add_word(words,index)
    vso_dict[words]=(words,index)

add_vso_word('own','#own_verb1')
add_vso_word('learn','#learn_verb1')
add_vso_word('price','#price_verb1')
add_vso_word('list','#list_verb1')

#>This is for verbs like the above which require a predicate
# know pig price 5

vspo_dict={}
def add_vspo_word(words,index):
    add_word(words,index)
    vspo_dict[words]=(words,index)

add_vspo_word('know','#know_verb1')
add_vspo_word('tell','#tell_verb1')

def verb_subject_object(verb,subject,object):
    """this function is partially cheat and not real translation"""
    a1=p.atom(verb,1,(word2node[verb][1],"present tense"),(None,"declarative verb"))
    a2=p.atom(subject,2,("#subject","default"),(a1,"subject"),"up")
    a3=p.atom(object,3,("#object","default"),(a2,"object"),"left")
    return (verb+" "+subject+" "+object,
            (a1,a2,a3))

def verb_subject_predicate_object(verb,subject,predicate,object):
    """this function is partially cheat and not real translation"""
    a1=p.atom(verb,1,(word2node[verb][1],"present tense"),(None,"declarative verb"))
    a2=p.atom(subject,2,("#subject","default"),(a1,"subject"),"up")
    a3=p.atom(predicate,2,("#predicate","default"),(a1,"predicate"),"up")
    a4=p.atom(object,3,("#object","default"),(a2,"object"),"left")
    return (verb+" "+subject+" "+predicate+" "+object,
            (a1,a2,a3,a4))

# <verb> [ the | a ] <subject> [ is | has ] <object>
vso_pattern=re.compile(r"(\w+)\s+(the\s+)?(a\s+)?((\w+)|(\(.+?\)))\s+(the\s+)?(is\s+)?(has\s+)?(.*)")
# <verb> [ the | a ] <subject> [ is | has ] <predicate> [ of ] <object>
vspo_pattern=re.compile(r"(\w+)\s+(the\s+)?(a\s+)?((\w+)|(\(.+?\)))\s+(the\s+)?(is\s+)?(has\s+)?(\w+)\s+(of\s+)?(.*)")
def match_verb_subject_object_string(say):
    m=vso_pattern.match(say)
    if m:
        verb=m.group(1)
        if vso_dict.has_key(verb):
            subject,object=m.group(4),m.group(10)
            return verb_subject_object(verb,subject,object)
    m=vspo_pattern.match(say)
    if m:
        verb=m.group(1)
        if vspo_dict.has_key(verb):
            subject,predicate,object=m.group(4),m.group(10),m.group(12)
            return verb_subject_predicate_object(verb,subject,predicate,object)
    return None


sell_pattern=re.compile(r"sell\s+(an?\s+|the\s+|this\s+|that\s+)?(.*)")
def match_sell(say):
    if say and say[-1]=='.': say=say[:-1]
    m=sell_pattern.match(say)
    if not m: return
    verb="sell"
    verb_id=word2node.get(verb)
    if not verb_id: return
    a1=p.atom(verb,1,(verb_id[1],"default"),(None,"verb"))
    object=m.group(2)
    object_id=word2node.get(object)
    if object_id:
        a2=p.atom(object,2,(object_id[1],"default"),(a1,"object"),"up")
    else:
        a2=p.atom(object,2)
    return (say,(a1,a2))

buy_pattern=re.compile(r"buy\s+(an?\s+|the\s+|this\s+|that\s+)?(.*)")
def match_buy(say):
    if say and say[-1]=='.': say=say[:-1]
    m=buy_pattern.match(say)
    if not m: return
    verb="buy"
    verb_id=word2node.get(verb)
    if not verb_id: return
    object=m.group(2)
    object_id=word2node.get(object)
    if not object_id: return
    a1=p.atom(verb,1,(verb_id[1],"default"),(None,"verb"))
    a2=p.atom(object,2,(object_id[1],"default"),(a1,"object"),"up")
    return (say,(a1,a2))

hire_pattern=re.compile(r"hire\s+(\w+\s+)?(.*)")
def match_hire(say):
    if say and say[-1]=='.': say=say[:-1]
    m=hire_pattern.match(say)
    if not m: return
    verb="hire"
    verb_id=word2node.get(verb)
    if not verb_id: return
    object=m.group(2)
    object_id=word2node.get(object)
    if not object_id: return
    a1=p.atom(verb,1,(verb_id[1],"default"),(None,"verb"))
    a2=p.atom(object,2,(object_id[1],"default"),(a1,"object"),"up")
    return (say,(a1,a2))

vote_pattern=re.compile(r"vote\s+for\s+(.*)")
def match_vote(say):
    if say and say[-1]=='.': say=say[:-1]
    m=vote_pattern.match(say)
    if not m: return
    verb="vote"
    verb_id=word2node.get(verb)
    if not verb_id: return
    object=m.group(2)
    object_id=word2node.get(object)
    if not object_id: return
    a1=p.atom(verb,1,(verb_id[1],"default"),(None,"verb"))
    a2=p.atom(object,2,(object_id[1],"default"),(a1,"object"),"up")
    return (say,(a1,a2))

desire_pattern=re.compile("(.*)\s+(would like|want)\s+to\s+(.*)")
def match_desire(say):
    m=desire_pattern.match(say)
    if not m: return
    verb="desire"
    verb_id=word2node.get(verb)
    if not verb_id: return
    subject=m.group(1)
    subject_id=word2node.get(subject)
    if not subject_id: return
    object=m.group(3)
    object_sentence=match_buy(object)
    if not object_sentence: object_sentence=match_sell(object)
    if not object_sentence: object_sentence=match_vote(object)
    if not object_sentence: object_sentence=match_hire(object)
    if not object_sentence: return
    a1=p.atom(verb,1,(verb_id[1],"default"),(None,"verb"))
    a2=p.atom(subject,2,(subject_id[1],"default"),(a1,"subject"),"up")
    a3=object_sentence[1][0]
    a3.link(a2,"left")
    a3.synlink_type="object"
    return (say,(a1,a2)+object_sentence[1])

importance_pattern=re.compile("(.*)ing is more important than (.*)ing.")
def convert_english_to_interlinguish(me, say_entity):
    """convert English text to Interlinguish array"""
    try:
        say=say_entity.say
    except AttributeError:
        return 0
    if hasattr(me,'name'):
        name_begin=me.name+", "
        if say[:len(name_begin)]==name_begin:
            say=say[len(name_begin):]
    match=importance_pattern.match(say)
    if match:
        sub=word2node.get(string.lower(match.group(1)))
        obj=word2node.get(match.group(2))
        if not sub or not obj: return 0
        res=importance(sub,'>',obj)
        say_entity.interlinguish=res[1]
        return 1
    res=match_verb_subject_object_string(say)
    if res:
        say_entity.interlinguish=res[1]
        return 1
    res=match_desire(say)
    if res:
        say_entity.interlinguish=res[1]
        return 1
    return 0

def get_verb(interlinguish):
    return interlinguish[0].lexlink.id[1:]