aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/openwnn/3rdparty/openwnn/wnnEngine/include/wnnword.h
blob: c7674ca4c675bfa5ecfae71f85914758063d3fd5 (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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * Qt implementation of OpenWnn library
 * This file is part of the Qt Virtual Keyboard module.
 * Contact: http://www.qt.io/licensing/
 *
 * Copyright (C) 2015  The Qt Company
 * Copyright (C) 2008-2012  OMRON SOFTWARE Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef WNNWORD_H
#define WNNWORD_H

#include <QString>
#include <QList>

class WnnPOS
{
public:
    WnnPOS() :
        left(0),
        right(0)
    { }

    WnnPOS(int left, int right) :
        left(left),
        right(right)
    { }

    /** The part of speech at left side */
    int left;

    /** The part of speech at right side */
    int right;
};

class WnnWord
{
public:
    WnnWord() :
        id(0),
        candidate(),
        stroke(),
        frequency(0),
        partOfSpeech(),
        attribute(0)
    { }

    WnnWord(const QString &candidate, const QString &stroke) :
        id(0),
        candidate(candidate),
        stroke(stroke),
        frequency(0),
        partOfSpeech(),
        attribute(0)
    { }

    WnnWord(const QString &candidate, const QString &stroke, int frequency) :
        id(0),
        candidate(candidate),
        stroke(stroke),
        frequency(frequency),
        partOfSpeech(),
        attribute(0)
    { }

    WnnWord(const QString &candidate, const QString &stroke, const WnnPOS &posTag) :
        id(0),
        candidate(candidate),
        stroke(stroke),
        frequency(0),
        partOfSpeech(posTag),
        attribute(0)
    { }

    WnnWord(const QString &candidate, const QString &stroke, const WnnPOS &posTag, int frequency) :
        id(0),
        candidate(candidate),
        stroke(stroke),
        frequency(frequency),
        partOfSpeech(posTag),
        attribute(0)
    { }

    WnnWord(int id, const QString &candidate, const QString &stroke, const WnnPOS &posTag, int frequency) :
        id(id),
        candidate(candidate),
        stroke(stroke),
        frequency(frequency),
        partOfSpeech(posTag),
        attribute(0)
    { }

    WnnWord(int id, const QString &candidate, const QString &stroke, const WnnPOS &posTag, int frequency, int attribute) :
        id(id),
        candidate(candidate),
        stroke(stroke),
        frequency(frequency),
        partOfSpeech(posTag),
        attribute(attribute)
    { }

    virtual ~WnnWord()
    { }

    virtual bool isClause() const
    {
        return false;
    }

    virtual bool isSentence() const
    {
        return false;
    }

    int      id;
    QString  candidate;
    QString  stroke;
    int      frequency;
    WnnPOS   partOfSpeech;
    int      attribute;
};

class WnnClause : public WnnWord
{
public:
    WnnClause(const QString &candidate, const QString &stroke, const WnnPOS &posTag, int frequency) :
        WnnWord(candidate, stroke, posTag, frequency)
    { }

    WnnClause(const QString &stroke, const WnnWord& stem) :
        WnnWord(stem.id, stem.candidate, stroke, stem.partOfSpeech, stem.frequency)
    { }

    WnnClause(const QString &stroke, const WnnWord& stem, const WnnWord& fzk) :
        WnnWord(stem.id, stem.candidate + fzk.candidate, stroke, WnnPOS(stem.partOfSpeech.left, fzk.partOfSpeech.right), stem.frequency, 1)
    { }

    bool isClause() const
    {
        return true;
    }
};

class WnnSentence : public WnnWord
{
public:
    WnnSentence(const QString &input, const QList<WnnClause> &clauses) :
        WnnWord()
    {
        if (!clauses.isEmpty()) {
            elements = clauses;
            const WnnClause &headClause = clauses.first();

            if (clauses.size() == 1) {
                id = headClause.id;
                candidate = headClause.candidate;
                stroke = input;
                frequency = headClause.frequency;
                partOfSpeech = headClause.partOfSpeech;
                attribute = headClause.attribute;
            } else {
                QString candidateBuilder;
                for (QList<WnnClause>::ConstIterator ci = clauses.constBegin();
                     ci != clauses.constEnd(); ci++) {
                    candidateBuilder.append(ci->candidate);
                }
                const WnnClause &lastClause = (WnnClause)clauses.last();

                id = headClause.id;
                candidate = candidateBuilder;
                stroke = input;
                frequency = headClause.frequency;
                partOfSpeech = WnnPOS(headClause.partOfSpeech.left, lastClause.partOfSpeech.right);
                attribute = 2;
            }
        }
    }

    WnnSentence(const QString &input, const WnnClause &clause) :
        WnnWord(clause.id, clause.candidate, input, clause.partOfSpeech, clause.frequency, clause.attribute)
    {
        elements.append(clause);
    }

    WnnSentence(const WnnSentence &prev, const WnnClause &clause) :
        WnnWord(prev.id, prev.candidate + clause.candidate, prev.stroke + clause.stroke, WnnPOS(prev.partOfSpeech.left, clause.partOfSpeech.right), prev.frequency + clause.frequency, prev.attribute)
    {
        elements.append(prev.elements);
        elements.append(clause);
    }

    WnnSentence(const WnnClause &head, const WnnSentence *tail = 0) :
        WnnWord()
    {
        if (tail == 0) {
            // single clause
            id = head.id;
            candidate = head.candidate;
            stroke = head.stroke;
            frequency = head.frequency;
            partOfSpeech = head.partOfSpeech;
            attribute = head.attribute;
            elements.append(head);
        } else {
            // consecutive clauses
            id = head.id;
            candidate = head.candidate + tail->candidate;
            stroke = head.stroke + tail->stroke;
            frequency = head.frequency + tail->frequency;
            partOfSpeech = WnnPOS(head.partOfSpeech.left, tail->partOfSpeech.right);
            attribute = 2;

            elements.append(head);
            elements.append(tail->elements);
        }
    }

    bool isSentence() const
    {
        return true;
    }

    QList<WnnClause> elements;
};

#endif // WNNWORD_H