1 | /* |
---|---|
2 | * Qt implementation of TCIME library |
3 | * This file is part of the Qt Virtual Keyboard module. |
4 | * Contact: http://www.qt.io/licensing/ |
5 | * |
6 | * Copyright (C) 2015 The Qt Company |
7 | * Copyright 2010 Google Inc. |
8 | * |
9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
10 | * you may not use this file except in compliance with the License. |
11 | * You may obtain a copy of the License at |
12 | * |
13 | * http://www.apache.org/licenses/LICENSE-2.0 |
14 | * |
15 | * Unless required by applicable law or agreed to in writing, software |
16 | * distributed under the License is distributed on an "AS IS" BASIS, |
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | * See the License for the specific language governing permissions and |
19 | * limitations under the License. |
20 | */ |
21 | |
22 | #include "zhuyindictionary.h" |
23 | #include "zhuyintable.h" |
24 | |
25 | using namespace tcime; |
26 | |
27 | ZhuyinDictionary::ZhuyinDictionary() : |
28 | WordDictionary() |
29 | { |
30 | } |
31 | |
32 | QStringList ZhuyinDictionary::getWords(const QString &input) const |
33 | { |
34 | // Look up the syllables index; return empty string for invalid syllables. |
35 | auto strippedTones = ZhuyinTable::stripTones(input); |
36 | int syllablesIndex = strippedTones.ok ? ZhuyinTable::getSyllablesIndex(syllables: strippedTones.pair[0]) : -1; |
37 | if (syllablesIndex < 0 || syllablesIndex >= dictionary().size()) |
38 | return QStringList(); |
39 | |
40 | // [22-initials * 39-finals] syllables array; each syllables entry points to |
41 | // a char[] containing words for that syllables. |
42 | const DictionaryEntry &data = dictionary()[syllablesIndex]; |
43 | if (data.isEmpty()) |
44 | return QStringList(); |
45 | |
46 | // Counts of words for each tone are stored in the array beginning. |
47 | int tone = ZhuyinTable::getTones(c: strippedTones.pair[1].at(n: 0)); |
48 | int length = (int) data[tone].unicode(); |
49 | if (length == 0) |
50 | return QStringList(); |
51 | |
52 | int start = ZhuyinTable::getTonesCount(); |
53 | for (int i = 0; i < tone; ++i) |
54 | start += (int) data[i].unicode(); |
55 | |
56 | QStringList words; |
57 | for (int i = 0; i < length; ++i) |
58 | words.append(t: data[start + i]); |
59 | |
60 | return words; |
61 | } |
62 |