summaryrefslogtreecommitdiffstats
path: root/markpinyin.py
blob: 5903fefe70a56910ff9d5abb0a516f3f894e1b97 (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
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
#!/usr/bin/python3
import os
import sqlite3
from argparse import ArgumentParser
import utils
from myconfig import MyConfig
from dirwalk import walkIndex


config = MyConfig()

#default pinyin total frequency
default = config.getDefaultPinyinTotalFrequency()

#minimum pinyin frequency
minimum = config.getMinimumPinyinFrequency()

#change cwd to the word recognizer directory
words_dir = config.getWordRecognizerDir()
os.chdir(words_dir)
#chdir done


atomic_words_dict = {}
merged_words_dict = {}


def load_atomic_words(filename):
    wordsfile = open(filename)
    for oneline in wordsfile.readlines():
        oneline = oneline.rstrip(os.linesep)

        if len(oneline) == 0:
            continue

        (word, pinyin, freq) = oneline.split(None, 2)
        freq = int(freq)

        if word in atomic_words_dict:
            atomic_words_dict[word].append((pinyin, freq))
        else:
            atomic_words_dict[word] = [(pinyin, freq)]

    wordsfile.close()


def load_merged_words(filename):
    wordsfile = open(filename)
    for oneline in wordsfile.readlines():
        oneline = oneline.rstrip(os.linesep)

        if len(oneline) == 0:
            continue

        (word, prefix, postfix, freq) = oneline.split(None, 3)
        freq = int(freq)

        if word in merged_words_dict:
            merged_words_dict[word].append((prefix, postfix, freq))
        else:
            merged_words_dict[word] = [(prefix, postfix, freq)]

    wordsfile.close()


def mergePinyin(pinyin_list):
    print(pinyin_list)
    pinyins = {}

    for (pinyin, freq) in pinyin_list:
        if pinyin in pinyins:
            pinyins[pinyin] += freq
        else:
            pinyins[pinyin] = freq

    pinyins = list(pinyins.items())
    total_freq = sum([ freq for pinyin, freq in pinyins ])

    results = []
    for (pinyin, freq) in pinyins:
        freq = default * freq / total_freq
        freq = int(freq)
        if freq < minimum:
            freq = minimum
        results.append((pinyin, freq))
    print(results)
    return results


def markAtomicWord(word):
    assert word in atomic_words_dict
    results = atomic_words_dict[word]
    return mergePinyin(results)


def markMergedWord(word):
    assert word in merged_words_dict

    merged_list = merged_words_dict[word]
    print(merged_list)
    merged_sum = sum([ freq for prefix, postfix, freq in merged_list ])

    results = []
    for (prefix, postfix, freq) in merged_list:
        prefix_list = markPinyin(prefix)
        prefix_sum = sum([ freq for pinyin, freq in prefix_list ])

        postfix_list = markPinyin(postfix)
        postfix_sum = sum([ freq for pinyin, freq in postfix_list ])

        for prefix_pinyin, prefix_freq in prefix_list:
            for postfix_pinyin, postfix_freq in postfix_list:
                merged_pinyin = prefix_pinyin + "'" + postfix_pinyin
                merged_freq = default * freq * prefix_freq * postfix_freq / \
                    merged_sum / prefix_sum / postfix_sum
                results.append((merged_pinyin, merged_freq))

    return mergePinyin(results)


def markPinyin(word):
    print(word)

    if word in atomic_words_dict:
        return markAtomicWord(word)
    elif word in merged_words_dict:
        return markMergedWord(word)
    else:
        assert False, "missed word.\n"


#loading old words
load_atomic_words(config.getWordsWithPinyinFileName())
#print(atomic_words_dict)