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 "phrasedictionary.h"
23 
24 using namespace tcime;
25 
PhraseDictionary()26 PhraseDictionary::PhraseDictionary() :
27     WordDictionary()
28 {
29 }
30 
getWords(const QString & input) const31 QStringList PhraseDictionary::getWords(const QString &input) const
32 {
33     if (input.length() != 1)
34         return QStringList();
35 
36     // Phrases are stored in an array consisting of three character arrays.
37     // char[0][] contains a char[] of words to look for phrases.
38     // char[2][] contains a char[] of following words for char[0][].
39     // char[1][] contains offsets of char[0][] words to map its following words.
40     // For example, there are 5 phrases: Aa, Aa', Bb, Bb', Cc.
41     // char[0][] { A, B, C }
42     // char[1][] { 0, 2, 4 }
43     // char[2][] { a, a', b, b', c}
44     const Dictionary &dict = dictionary();
45     if (dict.length() != 3)
46         return QStringList();
47 
48     const DictionaryEntry &words = dict[0];
49 
50     DictionaryEntry::ConstIterator word = std::lower_bound(words.begin(), words.end(), input.at(0));
51     if (word == words.constEnd() || *word != input.at(0))
52         return QStringList();
53 
54     int index = word - words.constBegin();
55     const DictionaryEntry &offsets = dict[1];
56     const DictionaryEntry &phrases = dict[2];
57     int offset = (int)offsets[index].unicode();
58     int count = (index < offsets.length() - 1) ?
59         ((int)offsets[index + 1].unicode() - offset) : (phrases.length() - offset);
60 
61     QStringList result;
62     for (int i = 0; i < count; ++i)
63         result.append(phrases[offset + i]);
64 
65     return result;
66 }
67