-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGPSRsentence_generator2015jp.py
executable file
·224 lines (204 loc) · 7.08 KB
/
GPSRsentence_generator2015jp.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#######################################################
# GPSR sentence generator
# version: 15.05
#
# Programmed by:
# Tijn van der Zant, Komei Sugiura
# email: tijn@ieee.org, komei.sugiura@gmail.com
#
#######################################################
# imports
import random
import sys
import copy
import os
import string
import base64
import urllib2
import json
import wave
# service URL
tts_url ='http://rospeex.ucri.jgn-x.jp/nauth_json/jsServices/VoiceTraSS'
# read the locations, objects and sentences files
# and clean up the lists from the files
rooms = []
locations = []
items = []
class_items = []
cat1Sentences = []
cat2Sentences = []
cat3Situations = []
names = []
# get rid of empty lines and do not use anything that starts with a '#'
for room in [line.strip('\n') for line in open('rooms.txt', 'r').readlines()]:
if room != '':
if room[0] != '#':
rooms.append(room)
for loc in [location.strip('\n') for location in open('locations.txt', 'r').readlines()]:
if loc != '':
if loc[0] != '#':
locations.append(loc)
for it in [item.strip('\n') for item in open('items.txt', 'r').readlines()]:
if it != '':
if it[0] != '#':
items.append(it)
for ci in [class_item.strip('\n') for class_item in open('class_items.txt', 'r').readlines()]:
if ci != '':
if ci[0] != '#':
class_items.append(ci)
for sentence in [str(sent).strip('\n') for sent in open('cat1Sentences.txt' , 'r').readlines()]:
if sentence != '':
if sentence[0] != '#':
cat1Sentences.append(sentence)
for sentence in [str(sent).strip('\n') for sent in open('cat2Sentences.txt' , 'r').readlines()]:
if sentence != '':
if sentence[0] != '#':
cat2Sentences.append(sentence)
situations = []
questions = []
for sit in [str(sent).strip('\n') for sent in open('cat3Situations.txt' , 'r').readlines()]:
if sit != '':
if sit[0] != '#':
if sit.split()[0] == 'situation:':
situations.append( sit )
if sit.split()[0] == 'question:':
questions.append( sit )
cat3Situations = zip( situations, questions )
for name in [nam.strip('\n') for nam in open('names.txt', 'r').readlines()]:
if name != '':
if name[0] != '#':
names.append(name)
# are there at least two locations?
if len(locations) < 2:
print 'Not enough locations. Exiting program'
sys.exit(1)
# are there at least two items?
if len(items) < 2:
print 'Not enough items. Exiting program'
sys.exit(1)
# the function 'fillIn' takes a sentence and replaces
# the word 'location' for an actual location
# and replaces the word 'item' for an actual item
# as defined in the files:
# locations.txt
# and items.txt
def fillIn(sentence):
#shuffle the items and the locations
random.shuffle(rooms)
random.shuffle(items)
random.shuffle(class_items)
random.shuffle(locations)
random.shuffle(names)
#fill in the locations and items in the sentence
# the counters are used so an item or location is not used twice
# hence the shuffeling for randomization
roomCounter = 0
itemCounter = 0
class_itemCounter = 0
locationCounter = 0
nameCounter = 0
finalSentence = []
for word in sentence.split(' '):
# print word
# fill in a room
if word == 'ROOM':
finalSentence.append( rooms[roomCounter] )
roomCounter += 1
# or fill in a location
elif word == 'LOCATION':
finalSentence.append( locations[locationCounter] )
locationCounter += 1
# or an item
elif word == 'ITEM':
finalSentence.append( items[itemCounter] )
itemCounter += 1
# or an item class
elif word == 'CLASS_ITEM':
finalSentence.append( class_items[class_itemCounter] )
class_itemCounter += 1
# is it a name?
elif word == 'NAME':
finalSentence.append( names[nameCounter] )
nameCounter += 1
# perhaps a location with a comma or dot?
elif word[:-1] == 'LOCATION':
finalSentence.append( locations[locationCounter] + word[-1])
locationCounter += 1
# or an item with a comma or dot or whatever
elif word[:-1] == 'ITEM':
finalSentence.append( items[itemCounter] + word[-1])
itemCounter += 1
# is it a namewith a comma, dot, whatever?
elif word[:-1] == 'NAME':
finalSentence.append( names[nameCounter] + word[-1] )
nameCounter += 1
# or else just the word
else:
finalSentence.append( word )
# then make a sentence again out of the created list
# print finalSentence
out = ' '.join(finalSentence)
out = out.replace(' ', ' ')
out = out.replace(' ', ' ')
# out = out.replace(' ', ' ')
# return ' '.join(finalSentence)
return out
# the tests are defined here
def testOne():
sentence = fillIn( random.choice(cat1Sentences) )
print('\n%s\n\n' % sentence)
# say(sentence)
# Category 2
def testTwo():
sentence = fillIn( random.choice(cat2Sentences) )
print('\n%s\n\n' % sentence)
# say(sentence)
# Category 3
def testThree():
print 'This is the situation for category 3, press enter for the question.\n\n'
situation = random.choice( cat3Situations )
print fillIn(situation[0].split(':')[1])
# print situation[0].split(':')[1]
# raw_input()
sentence = fillIn(situation[1].split(':')[1])
print('%s\n\n' % sentence)
# print '\n\n'
# say(sentence)
############################################# MAIN LOOP ####################################
def say(sentence):
# command
tts_command = { "method":"speak", "params":["1.1", {"language":"ja","text":sentence,"voiceType":"*","audioType":"audio/x-wav"}]}
obj_command = json.dumps(tts_command) # string to json object
req = urllib2.Request(tts_url, obj_command)
received = urllib2.urlopen(req).read() # get data from server
# extract wav file
obj_received = json.loads(received)
tmp = obj_received['result']['audio'] # extract result->audio
speech = base64.decodestring(tmp.encode('utf-8'))
f = open ("out.wav",'wb')
f.write(speech)
f.close
os.system("aplay out.wav")
# ask the user which test this program should generate
def mainLoop():
answer = 'begin'
while True:
answer = raw_input('Which category do you want to do?\nPossible answers are: 1, 2, 3 or q(uit)')
if answer == 'q':
print 'Exiting program.'
sys.exit(1)
elif answer == '1':
print 'Category 1:\n',
testOne()
elif answer == '2':
print 'Category 2:\n'
testTwo()
elif answer == '3':
print 'Category 3:\n'
testThree()
else:
print '\nNot a valid input, please try 1, 2, 3 or q(uit)\n'
if __name__ == "__main__":
mainLoop()