summaryrefslogtreecommitdiff
path: root/boot
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2014-05-27 20:23:30 -0400
committerschneefux <schneefux+commit@schneefux.xyz>2014-05-27 20:23:30 -0400
commite3f46a7a9222d951141ca6b6a932891ae7b20079 (patch)
tree01ec3ebfbf9492198b07f8e280b6d8eca47cc03a /boot
parentd0c909dab1a06f5970216befd2cd0839327914fe (diff)
parent5deb1799cefe9351b90d3457898990d454d4a4dc (diff)
downloadjasper-client-e3f46a7a9222d951141ca6b6a932891ae7b20079.tar.gz
jasper-client-e3f46a7a9222d951141ca6b6a932891ae7b20079.zip
fixed merge conflicts
Diffstat (limited to 'boot')
-rwxr-xr-xboot/boot.py5
-rw-r--r--boot/test.py42
-rw-r--r--boot/vocabcompiler.py44
3 files changed, 75 insertions, 16 deletions
diff --git a/boot/boot.py b/boot/boot.py
index 317b4c0..ac52bdd 100755
--- a/boot/boot.py
+++ b/boot/boot.py
@@ -2,7 +2,6 @@
import os, json
import urllib2
-import subprocess
import yaml
from wifi import *
@@ -18,13 +17,13 @@ def configure():
print "CONNECTED TO INTERNET"
print "COMPILING DICTIONARY"
- vocabcompiler.compile()
+ vocabcompiler.compile("../client/sentences.txt", "../client/dictionary.dic", "../client/languagemodel.lm")
print "STARTING CLIENT PROGRAM"
os.system("/home/pi/jasper/client/start.sh &")
except:
-
+
print "COULD NOT CONNECT TO NETWORK"
say("Hello, I could not connect to a network. Please read the documentation to configure your Raspberry Pi.")
os.system("sudo shutdown -r now")
diff --git a/boot/test.py b/boot/test.py
new file mode 100644
index 0000000..c6bd3ff
--- /dev/null
+++ b/boot/test.py
@@ -0,0 +1,42 @@
+import os
+import sys
+import unittest
+from mock import patch
+import vocabcompiler
+
+lib_path = os.path.abspath('../client')
+mod_path = os.path.abspath('../client/modules/')
+
+sys.path.append(lib_path)
+sys.path.append(mod_path)
+
+import g2p
+
+
+class TestVocabCompiler(unittest.TestCase):
+
+ def testWordExtraction(self):
+ sentences = "temp_sentences.txt"
+ dictionary = "temp_dictionary.dic"
+ languagemodel = "temp_languagemodel.lm"
+
+ words = [
+ 'HACKER', 'LIFE', 'FACEBOOK', 'THIRD', 'NO', 'JOKE',
+ 'NOTIFICATION', 'MEANING', 'TIME', 'TODAY', 'SECOND',
+ 'BIRTHDAY', 'KNOCK KNOCK', 'INBOX', 'OF', 'NEWS', 'YES',
+ 'TOMORROW', 'EMAIL', 'WEATHER', 'FIRST', 'MUSIC', 'SPOTIFY'
+ ]
+
+ with patch.object(g2p, 'translateWords') as translateWords:
+ with patch.object(vocabcompiler, 'text2lm') as text2lm:
+ vocabcompiler.compile(sentences, dictionary, languagemodel)
+
+ # 'words' is appended with ['MUSIC', 'SPOTIFY']
+ # so must be > 2 to have received WORDS from modules
+ translateWords.assert_called_once_with(words)
+ self.assertTrue(text2lm.called)
+ os.remove(sentences)
+ os.remove(dictionary)
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/boot/vocabcompiler.py b/boot/vocabcompiler.py
index d5455d3..1d2a51d 100644
--- a/boot/vocabcompiler.py
+++ b/boot/vocabcompiler.py
@@ -1,52 +1,70 @@
"""
- This iterates over all the WORDS variables in the modules and creates a dictionary that the client program will use
+ Iterates over all the WORDS variables in the modules and creates a dictionary for the client.
"""
import os
import sys
-import pkgutil
+import glob
lib_path = os.path.abspath('../client')
+mod_path = os.path.abspath('../client/modules/')
+
sys.path.append(lib_path)
-import modules
+sys.path.append(mod_path)
+
import g2p
-def compile():
+def text2lm(in_filename, out_filename):
+ """Wrapper around the language model compilation tools"""
+ def text2idngram(in_filename, out_filename):
+ cmd = "text2idngram -vocab %s < %s -idngram temp.idngram" % (out_filename,
+ in_filename)
+ os.system(cmd)
+
+ def idngram2lm(in_filename, out_filename):
+ cmd = "idngram2lm -idngram temp.idngram -vocab %s -arpa %s" % (
+ in_filename, out_filename)
+ os.system(cmd)
+
+ text2idngram(in_filename, in_filename)
+ idngram2lm(in_filename, out_filename)
+
+
+def compile(sentences, dictionary, languagemodel):
"""
Gets the words and creates the dictionary
"""
- m = dir(modules)
+ m = [os.path.basename(f)[:-3]
+ for f in glob.glob(os.path.dirname("../client/modules/") + "/*.py")]
words = []
for module_name in m:
try:
- eval("words.extend(modules.%s.WORDS)" % module_name)
+ exec("import %s" % module_name)
+ eval("words.extend(%s.WORDS)" % module_name)
except:
pass # module probably doesn't have the property
words = list(set(words))
# for spotify module
- words.extend(["MUSIC","SPOTIFY"])
+ words.extend(["MUSIC", "SPOTIFY"])
# create the dictionary
pronounced = g2p.translateWords(words)
zipped = zip(words, pronounced)
lines = ["%s %s" % (x, y) for x, y in zipped]
- with open("../client/dictionary.dic", "w") as f:
+ with open(dictionary, "w+") as f:
f.write("\n".join(lines) + "\n")
# create the language model
- with open("../client/sentences.txt", "w") as f:
+ with open(sentences, "w+") as f:
f.write("\n".join(words) + "\n")
f.write("<s> \n </s> \n")
f.close()
# make language model
- os.system(
- "text2idngram -vocab ../client/sentences.txt < ../client/sentences.txt -idngram temp.idngram")
- os.system(
- "idngram2lm -idngram temp.idngram -vocab ../client/sentences.txt -arpa ../client/languagemodel.lm")
+ text2lm(sentences, languagemodel)