summaryrefslogtreecommitdiff
path: root/boot/vocabcompiler.py
blob: d5455d3b9e3a49ed8883922e1fbecdb1d1b62986 (plain)
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
"""
    This iterates over all the WORDS variables in the modules and creates a dictionary that the client program will use
"""

import os
import sys
import pkgutil

lib_path = os.path.abspath('../client')
sys.path.append(lib_path)
import modules
import g2p


def compile():
    """
        Gets the words and creates the dictionary
    """

    m = dir(modules)

    words = []
    for module_name in m:
        try:
            eval("words.extend(modules.%s.WORDS)" % module_name)
        except:
            pass  # module probably doesn't have the property

    words = list(set(words))

    # for spotify module
    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:
        f.write("\n".join(lines) + "\n")

    # create the language model
    with open("../client/sentences.txt", "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")