~elementary-os/elementaryos/os-patch-onboard-trusty

« back to all changes in this revision

Viewing changes to Onboard/pypredict/tools/makemodels

  • Committer: RabbitBot
  • Date: 2014-08-31 20:00:45 UTC
  • Revision ID: rabbitbot@elementaryos.org-20140831200045-guqqu1s80isrm103
Initial import, version 1.0.0-0ubuntu4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
# Copyright © 2012, marmuta
 
3
#
 
4
# This file is part of Onboard.
 
5
#
 
6
# Onboard is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 3 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# Onboard is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
 
18
#
 
19
# Important directories:
 
20
#
 
21
# ./models/raw    - Input directory containing large raw models with only
 
22
#                   minimal processing (utf-8 encoded). Due to their large
 
23
#                   size, often in the GB range, raw models aren't
 
24
#                   included with Onboard's source package,
 
25
#
 
26
# ./models        - Output directory for final filtered system language models
 
27
#                   (utf-8 encoded).
 
28
#
 
29
 
 
30
LANGUAGES=
 
31
LANGUAGES+="en_US en_GB en_AU en_CA "
 
32
LANGUAGES+="de_DE de_CH de_AT "
 
33
LANGUAGES+="fr_FR "
 
34
LANGUAGES+="it_IT "
 
35
LANGUAGES+="es_ES "
 
36
LANGUAGES+="pt_PT pt_BR "
 
37
LANGUAGES+="ru_RU "
 
38
LANGUAGES+="gd_GB "
 
39
LANGUAGES+="ga_IE "
 
40
LANGUAGES+="tr_TR "
 
41
LANGUAGES+="pl_PL "
 
42
# All Dutch models are currently identical, but the spell checkers
 
43
# might have differences. Keep them for now so we can easily
 
44
# select all flavours in the language menu.
 
45
LANGUAGES+="nl_NL nl_BE nl_AW nl_AN nl_SR " 
 
46
LANGUAGES+="ro_RO "
 
47
LANGUAGES+="da_DK "
 
48
LANGUAGES+="el_GR "
 
49
LANGUAGES+="sv_SE "
 
50
LANGUAGES+="lb_LU "
 
51
 
 
52
MODELDIR="models"
 
53
RAWMODELDIR="../training/raw_models"
 
54
MODELEXT="lm"
 
55
 
 
56
TRAIN_CMD=Onboard/pypredict/tools/train
 
57
FILTER_CMD=Onboard/pypredict/tools/filter
 
58
 
 
59
ORDER=2
 
60
VERBOSE=0
 
61
 
 
62
set -e
 
63
 
 
64
help()
 
65
{
 
66
cat >&2 << END
 
67
Usage: `basename $0` [-e|E] [-i|I] [-v] [languages...]
 
68
Script to create language models.
 
69
Options:
 
70
 -v  More verbose output
 
71
 
 
72
 -h  Show this help.
 
73
END
 
74
}
 
75
 
 
76
 
 
77
# process command line arguments
 
78
while getopts "v" opt; do
 
79
        case "$opt" in
 
80
        v)
 
81
                VERBOSE=1
 
82
                ;;
 
83
        \?|*)
 
84
                help
 
85
                exit 1
 
86
                ;;
 
87
        esac
 
88
done
 
89
shift $(($OPTIND - 1))
 
90
 
 
91
 
 
92
# get languages from positional parameters
 
93
if [ $# -gt 0 ]; then
 
94
    langs=""
 
95
    for lang in $*; do
 
96
        lang_id=${lang:0:2}
 
97
        if [ "${lang}" == "${lang_id}" ]; then
 
98
 
 
99
            # expand language to full lang_country id
 
100
            for l in ${LANGUAGES}; do
 
101
                lid=${l:0:2}
 
102
                if [ "${lang_id}" == "${lid}" ]; then
 
103
                    langs="${langs} ${l}"
 
104
                fi
 
105
            done
 
106
        else
 
107
            # append language as is
 
108
            langs="${langs} ${lang}"
 
109
        fi
 
110
    done
 
111
 
 
112
    LANGUAGES="${langs}"
 
113
fi
 
114
 
 
115
# make sure the directories exist
 
116
[ -d "$MODELDIR" ] || mkdir $MODELDIR
 
117
 
 
118
# filter language models
 
119
for lang in $LANGUAGES; do
 
120
    lang_id=${lang:0:2}
 
121
    MODEL_IN="$RAWMODELDIR/$lang_id.$MODELEXT.sorted-pruned"
 
122
    MODEL_OUT="$MODELDIR/$lang.$MODELEXT"
 
123
 
 
124
    echo "Building '$MODEL_OUT'..."
 
125
 
 
126
    PRUNE_FREQ=49
 
127
    REGEX_DROP_UNIGRAM="\'s$|^\S$"
 
128
    REGEX_DROP_NGRAM="^\w{1}\ | \s\S{1,3}(?:\s|$)"
 
129
    NAME_EXCEPTIONS=
 
130
    MAX_LC_UC_RATIO=200
 
131
 
 
132
    case "$lang_id" in
 
133
        en)
 
134
            PRUNE_FREQ=499,499
 
135
            REGEX_DROP_UNIGRAM="\'s$|^[^IaA]$"
 
136
            NAME_EXCEPTIONS=Union,Kingdom,Nations,New,Barack,Obama,Bush,South,West,North,East,Southern,Western,Northern,Eastern,Mean,Standard,Coast,Time,Grand,Central,Station,Large,Cloud,Collider,Psychology,Geographic \
 
137
            MAX_LC_UC_RATIO=2.0
 
138
            ;;
 
139
 
 
140
        de)
 
141
            PRUNE_FREQ=399
 
142
            MAX_LC_UC_RATIO=300.0
 
143
            ;;
 
144
 
 
145
        fr)
 
146
            PRUNE_FREQ=169
 
147
            MAX_LC_UC_RATIO=150.0
 
148
            ;;
 
149
 
 
150
        es)
 
151
            PRUNE_FREQ=149
 
152
            MAX_LC_UC_RATIO=50.0
 
153
            ;;
 
154
 
 
155
        pt)
 
156
            PRUNE_FREQ=69
 
157
            MAX_LC_UC_RATIO=50.0
 
158
            ;;
 
159
 
 
160
        it)
 
161
            PRUNE_FREQ=99
 
162
           ;;
 
163
 
 
164
        ru)
 
165
            PRUNE_FREQ=249
 
166
            ;;
 
167
 
 
168
        tr)
 
169
            PRUNE_FREQ=34
 
170
            ;;
 
171
 
 
172
        pl)
 
173
            PRUNE_FREQ=199,199,-1
 
174
            ;;
 
175
 
 
176
        gd)
 
177
            PRUNE_FREQ=1,1,-1
 
178
            MAX_LC_UC_RATIO=50.0
 
179
            ;;
 
180
 
 
181
        ga)
 
182
            PRUNE_FREQ=4,4,-1
 
183
            ;;
 
184
 
 
185
        nl)
 
186
            PRUNE_FREQ=89,89,-1
 
187
            ;;
 
188
 
 
189
        da)
 
190
            PRUNE_FREQ=24,24,-1
 
191
            ;;
 
192
 
 
193
        ro)
 
194
            PRUNE_FREQ=27,27,-1
 
195
            ;;
 
196
 
 
197
        el)
 
198
            PRUNE_FREQ=24,24,-1
 
199
            ;;
 
200
 
 
201
        sv)
 
202
            PRUNE_FREQ=54,54,-1
 
203
            ;;
 
204
 
 
205
        lb)
 
206
            PRUNE_FREQ=1,1,-1
 
207
            ;;
 
208
 
 
209
       *)
 
210
            echo "Unsupported language $lang, Aborting"
 
211
            exit 1
 
212
    esac
 
213
 
 
214
    REMAINING_OPTIONS=""
 
215
    if [ ! "${NAME_EXCEPTIONS}" == "" ]; then
 
216
        REMAINING_OPTIONS="${REMAINING_OPTIONS} -x ${NAME_EXCEPTIONS}"
 
217
    fi
 
218
 
 
219
    $FILTER_CMD -p "${PRUNE_FREQ}" \
 
220
                -r "${REGEX_DROP_UNIGRAM}" \
 
221
                -n "${REGEX_DROP_NGRAM}" \
 
222
                -t \
 
223
                -l $lang \
 
224
                -N \
 
225
                -i ${MAX_LC_UC_RATIO} \
 
226
                ${REMAINING_OPTIONS} \
 
227
                --save-sorted $MODEL_IN $MODEL_OUT
 
228
    echo
 
229
 
 
230
done