OSDN Git Service

5cad1b86f26af6c0716dfcf23b106337d0f9f0a9
[wordring-tm/wordring-tm.git] / proxy / tmtext.h
1 #ifndef TMTEXT_H
2 #define TMTEXT_H
3
4 #include "text.h"
5
6 #include <QString>
7 #include <QList>
8
9 #include <QJsonDocument>
10 #include <QJsonObject>
11 #include <QJsonArray>
12
13 #include <memory>
14
15 namespace TM
16 {
17
18 /*!
19  * \brief データベースとの通信に使うデータ構造です。
20  */
21 class sentence_data_type
22 {
23 public:
24         typedef std::shared_ptr<sentence_data_type> pointer;
25         typedef std::shared_ptr<sentence_data_type const> const_pointer;
26
27         sentence_data_type();
28
29         bool operator < (sentence_data_type const &rhs);
30         bool operator == (sentence_data_type const &rhs);
31
32         static pointer create();
33
34         quint32 sentence_id; /*!< DBによって自動的に付与される文ID */
35         quint32 source_id;   /*!< 原文は0固定、訳文は原文のsentence_id */
36         QString sentence;    /*!< 文本体の文字列 */
37         QByteArray json;     /*!< 原文は空、訳文はリンク情報のJSON */
38         quint32 crc;         /*!< 検索に使う文のCRC */
39         quint32 previous_crc;/*!< 原文は文脈一致に使う前方文のCRC、訳文は0固定 */
40         quint32 next_crc;    /*!< 原文は文脈一致に使う後方文のCRC、訳文は0固定 */
41         quint32 user_id;     /*!< レコードを更新したユーザーのID */
42         quint64 time;        /*!< 更新時刻 */
43
44         quint32 quality; /*!< 訳文は訳語の品質、原文は0 */
45         QList<QPair<int,int> > words; /*!< データベース登録時に、sentenceの単語の範囲を示すリスト */
46 };
47
48 /*!
49  * \brief 単語間のリンクを表現するクラスです。
50  *
51  * 原単語と訳単語の多対多のリンクを表現します。
52  */
53 class WordLink
54 {
55 public:
56         typedef std::shared_ptr<WordLink> pointer;
57         typedef QList<Text::pointer> storage_type;
58
59 public:
60         enum : int { Source, Target, };
61
62 private:
63         WordLink();
64
65 public:
66         void clear();
67         void append(int place, Text::pointer word);
68         void remove(int place, Text::pointer word);
69
70         bool is_valid() const;
71         bool is_empty() const;
72         bool contains(int place, Text::pointer word);
73         int size(int place) const;
74
75         storage_type* sources();
76         storage_type* targets();
77
78         QJsonArray to_json_array() const;
79
80         QString debug_dump() const;
81
82         static pointer create();
83
84 private:
85         storage_type m_sources;
86         storage_type m_targets;
87 };
88
89 /*!
90  * \brief 単語間のリンクを作成・保持するクラスです。
91  *
92  * 複数の単語間リンクを保持するコンテナとしての機能を持ちます。
93  * 現在編集中の単語間リンクを明示する機能があります。
94  *
95  * ある特定の単語間リンクを編集するには、start()を呼び出し編集を開始し、
96  * append()呼び出しによってリンクへ単語を追加し、
97  * commit()呼び出しによって編集を終了します。
98  */
99 class WordLinker
100 {
101 public:
102         typedef QList<WordLink::pointer> storage_type;
103         typedef storage_type::iterator iterator;
104         typedef storage_type::const_iterator const_iterator;
105
106 public:
107         void clear();
108
109         void start();
110         void commit();
111         void append(int place, Text::pointer word);
112         void remove(int place, Text::pointer word);
113         void toggle(int place, Text::pointer word);
114
115         WordLink::pointer current();
116
117         WordLink::pointer find(Text::pointer word);
118         WordLink::pointer find(int place, Text::pointer word);
119
120         int index_of(WordLink::pointer link) const;
121
122         iterator begin();
123         iterator end();
124
125         QJsonArray to_json_array() const;
126
127         QString debug_dump() const;
128
129 private:
130         WordLink::pointer m_current_link;
131         storage_type m_links;
132 };
133
134 class TextSentence
135 {
136 public:
137         typedef std::shared_ptr<TextSentence> pointer;
138
139 private:
140         TextSentence(int segment_id, int index, quint32 crc, Text::pointer source_sentence);
141 public:
142         Text::pointer source_sentence();
143         Text::pointer target_sentence();
144         void set_target_sentence(Text::pointer target_sentence);
145
146         WordLinker* linker();
147         QByteArray to_json();
148         void set_json(QByteArray json);
149 private:
150         void set_json_links(QJsonObject const &jobject);
151         void set_json_link(QJsonArray const &link_array);
152         QList<QPair<int, int> > stuff_json_link(QJsonArray const &ranges);
153
154         QList<Text::pointer> find_words_by_range(Text::pointer sentence, int first, int tail);
155 public:
156         int segment_id() const;
157         int index() const;
158
159         quint32 crc() const;
160         quint32 previous_crc() const;
161         quint32 next_crc() const;
162         void set_previous_crc(quint32 crc);
163         void set_next_crc(quint32 crc);
164
165         quint32 source_id() const;
166         void set_source_id(quint32 source_id);
167         quint32 target_id() const;
168         void set_target_id(quint32 source_id);
169
170         void append(sentence_data_type::pointer sentence);
171         const QList<sentence_data_type::const_pointer> &candidates() const;
172
173         bool is_loaded() const;
174         void set_loaded(bool loaded);
175
176         QString debug_dump() const;
177
178         static pointer create(int segment_id, int index,
179                                                   quint32 crc, Text::pointer source_sentence);
180
181 private:
182         Text::pointer m_source_sentence;
183         Text::pointer m_target_sentence;
184         WordLinker m_linker;
185
186         int m_segment_id;
187         int m_index;
188
189         quint32 m_crc;
190         quint32 m_previous_crc;
191         quint32 m_next_crc;
192
193         quint32 m_source_id;
194         quint32 m_target_id;
195
196         QList<sentence_data_type::const_pointer> m_candidates; /*!< 訳文候補 */
197
198         bool m_loaded; /*!< 完全一致訳文の初期検索が終了していればtrue */
199 };
200
201 } // namespace TM
202
203 #endif // TMTEXT_H