Repository navigation
Expand file tree
/
Copy pathvalidation.py
More file actions
198 lines (154 loc) · 6.67 KB
/
Copy pathvalidation.py
File metadata and controls
198 lines (154 loc) · 6.67 KB
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
# -*- coding: utf-8 -*-
#
# This file is part of ibus-bogo project.
#
# Copyright (C) 2012 Long T. Dam <longdt90@gmail.com>
# Copyright (C) 2012-2013 Trung Ngo <ndtrung4419@gmail.com>
# Copyright (C) 2013 Duong H. Nguyen <cmpitg@gmail.com>
#
# ibus-bogo is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ibus-bogo is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ibus-bogo. If not, see <http://www.gnu.org/licenses/>.
#
"""
Utility functions to check whether a word looks like Vietnamese
or not (i.e. can be pronounced by a Vietnamese speaker).
"""
from __future__ import unicode_literals
import collections
from bogo import accent, mark, utils
Accent = accent.Accent
# Auto-generated lists from dictionary
# FIXME:
# Think about words composed entirely of vowels, like 'yá'.
# Perhaps let the user customize these lists?
CONSONANTS = set([
'b', 'c', 'ch', 'd', 'g', 'gh', 'gi', 'h', 'k', 'kh', 'l', 'm', 'n', 'ng',
'ngh', 'nh', 'p', 'ph', 'qu', 'r', 's', 't', 'th', 'tr', 'v', 'x', 'đ'
])
TERMINAL_CONSONANTS = set([
'c', 'ch', 'm', 'n', 'ng', 'nh', 'p', 't'
])
VOWELS = set([
'a', 'ai', 'ao', 'au', 'ay', 'e', 'eo', 'i', 'ia', 'iu', 'iê', 'iêu',
'o', 'oa', 'oai', 'oao', 'oay', 'oe', 'oeo', 'oi', 'oo', 'oă', 'u', 'ua',
'ui', 'uy', 'uya', 'uyu', 'uyê', 'uâ', 'uây', 'uê', 'uô', 'uôi',
'uơ', 'y', 'yê', 'yêu', 'â', 'âu', 'ây', 'ê', 'êu', 'ô', 'ôi',
'ă', 'ơ', 'ơi', 'ư', 'ưa', 'ưi', 'ưu', 'ươ', 'ươi', 'ươu'
])
TERMINAL_VOWELS = set([
'ai', 'ao', 'au', 'ay', 'eo', 'ia', 'iu', 'iêu', 'oai', 'oao', 'oay',
'oeo', 'oi', 'ua', 'ui', 'uya', 'uyu', 'uây', 'uôi', 'uơ', 'yêu', 'âu',
'ây', 'êu', 'ôi', 'ơi', 'ưa', 'ưi', 'ưu', 'ươi', 'ươu'
])
STRIPPED_VOWELS = set(map(mark.strip, VOWELS))
# 'uo' may clash with 'ươ' and prevent typing 'thương'
# 'ua' may clash with 'uâ' and prevent typing 'luật'
STRIPPED_TERMINAL_VOWELS = set(map(mark.strip, TERMINAL_VOWELS)) - \
set(['uo', 'ua'])
SoundTuple = \
collections.namedtuple('SoundTuple',
['first_consonant', 'vowel', 'last_consonant'])
def is_valid_string(string, final_form=True):
return is_valid_combination(utils.separate(string), final_form)
def is_valid_combination(comp, final_form=True):
return is_valid_sound_tuple(comp, final_form)
def is_valid_sound_tuple(sound_tuple, final_form=True):
"""
Check if a character combination complies to Vietnamese phonology.
The basic idea is that if one can pronunce a sound_tuple then it's valid.
Sound tuples containing consonants exclusively (almost always
abbreviations) are also valid.
Input:
sound_tuple - a SoundTuple
final_form - whether the tuple represents a complete word
Output:
True if the tuple seems to be Vietnamese, False otherwise.
"""
# We only work with lower case
sound_tuple = SoundTuple._make([s.lower() for s in sound_tuple])
# Words with no vowel are always valid
# FIXME: This looks like it should be toggled by a config key.
if not sound_tuple.vowel:
result = True
elif final_form:
result = \
has_valid_consonants(sound_tuple) and \
has_valid_vowel(sound_tuple) and \
has_valid_accent(sound_tuple)
else:
result = \
has_valid_consonants(sound_tuple) and \
has_valid_vowel_non_final(sound_tuple)
return result
def has_valid_consonants(sound_tuple):
def has_invalid_first_consonant():
return (sound_tuple.first_consonant != "" and
not sound_tuple.first_consonant in CONSONANTS)
def has_invalid_last_consonant():
return (sound_tuple.last_consonant != "" and
not sound_tuple.last_consonant in TERMINAL_CONSONANTS)
return not (has_invalid_first_consonant() or
has_invalid_last_consonant())
def has_valid_vowel_non_final(sound_tuple):
# If the sound_tuple is not complete, we only care whether its vowel
# position can be transformed into a legit vowel.
stripped_vowel = mark.strip(sound_tuple.vowel)
if sound_tuple.last_consonant != '':
return stripped_vowel in STRIPPED_VOWELS - STRIPPED_TERMINAL_VOWELS
else:
return stripped_vowel in STRIPPED_VOWELS
def has_valid_vowel(sound_tuple):
# Check our vowel.
# First remove all accents
vowel_wo_accent = accent.remove_accent_string(sound_tuple.vowel)
def has_valid_vowel_form():
return vowel_wo_accent in VOWELS and not \
(sound_tuple.last_consonant != '' and
vowel_wo_accent in TERMINAL_VOWELS)
def has_valid_ch_ending():
# 'ch' can only go after a, ê, uê, i, uy, oa
return not (sound_tuple.last_consonant == 'ch' and
not vowel_wo_accent in
('a', 'ê', 'uê', 'i', 'uy', 'oa'))
def has_valid_c_ending():
# 'c' can't go after 'i' or 'ơ'
return not (sound_tuple.last_consonant == 'c' and
vowel_wo_accent in ('i', 'ơ'))
def has_valid_ng_ending():
# 'ng' can't go after i, ơ
return not (sound_tuple.last_consonant == 'ng' and
vowel_wo_accent in ('i', 'ơ'))
def has_valid_nh_ending():
# 'nh' can only go after a, ê, uy, i, oa, quy
has_y_but_is_not_quynh = vowel_wo_accent == 'y' and \
sound_tuple.first_consonant != 'qu'
has_invalid_vowel = not vowel_wo_accent in \
('a', 'ê', 'i', 'uy', 'oa', 'uê', 'y')
return not \
(sound_tuple.last_consonant == 'nh' and
(has_invalid_vowel or has_y_but_is_not_quynh))
# The ng and nh rules are not really phonetic but spelling rules.
# Including them may hinder typing freedom and may prevent typing
# unique local names.
# FIXME: Config key, anyone?
return \
has_valid_vowel_form() and \
has_valid_ch_ending() and \
has_valid_c_ending()
# has_valid_ng_ending() and \
# has_valid_nh_ending()
def has_valid_accent(sound_tuple):
akzent = accent.get_accent_string(sound_tuple.vowel)
# These consonants can only go with ACUTE, DOT accents
return not (sound_tuple.last_consonant in ('c', 'p', 't', 'ch') and
not akzent in (Accent.ACUTE, Accent.DOT))