2 Copyright 2013 Karel Matas
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef __DATATYPES_HXX
18 #define __DATATYPES_HXX
20 #include "utils.hxx" // for utils::to_string()
21 #include "sqlite3.hxx" // for SQLite3::escape()
26 const char *const SEPARATOR_SQL
= "|";
34 SKIP ( const vector
<int> &v
, const char *m
)
35 :s1(v
[0]),s2(v
[1]),s3(v
[2]),misclass(m
){};
36 SKIP ( const string
&ss1
, const string
&ss2
, const string
&ss3
, const string
&m
)
37 :s1(std::stoi(ss1
)),s2(std::stoi(ss2
)),s3(std::stoi(ss3
)),misclass(m
){};
48 vector
<string
> onyomi_
;
49 vector
<string
> kunyomi_
;
50 // NOTE: flags: std::vector is needed, no std::set (sorting)
51 vector
<string
> flags_
; // jis
54 vector
<string
> nanori_
;
55 vector
<string
> meaning_
;
63 Kanji (): kanji_("") {};
64 Kanji ( const string
&kanji
) : kanji_(kanji
) {};
67 inline void kanji ( const string
&s
) { kanji_
= s
;}
68 inline void ucs ( const string
&s
) { ucs_
= s
; };
69 inline void onyomi ( const string
&s
) { onyomi_
.push_back(s
); };
70 inline void onyomi ( const vector
<string
> &v
) { onyomi_
=v
; };
71 inline void kunyomi ( const string
&s
) { kunyomi_
.push_back(s
); };
72 inline void kunyomi ( const vector
<string
> &v
) { kunyomi_
=v
; };
73 inline void flags ( const string
&s
) { flags_
.push_back(s
); };
74 inline void flags ( const vector
<string
> &v
) { flags_
=v
; };
75 inline void strokes ( const char *s
) { strokes_
= atoi(s
); };
76 inline void strokes ( int i
) { strokes_
= i
; };
77 inline void freq ( const char *s
) { freq_
= atoi(s
); };
78 inline void freq ( int i
) { freq_
= i
; };
79 inline void meaning ( const string
&s
) { meaning_
.push_back(s
); };
80 inline void meaning ( const vector
<string
> &v
) { meaning_
= v
; };
81 inline void nanori ( const string
&s
) { nanori_
.push_back(s
); };
82 inline void nanori ( const vector
<string
> &v
) { nanori_
= v
; };
83 inline void rad_classic ( const char *s
) { rad_classic_
= atoi(s
); };
84 inline void rad_classic ( int i
) { rad_classic_
= i
; };
85 inline void rad_nelson ( const char *s
) { rad_nelson_
= atoi(s
); };
86 inline void rad_nelson ( int i
) { rad_nelson_
= i
; };
87 inline void skip ( const string
&s1
, const string
&s2
, const string
&s3
,
88 const string
&m
="" ) {
89 skip_
.push_back( SKIP( s1
, s2
, s3
, m
) );
91 inline void skip ( const vector
<int> &v
, const char *misclass
=0 ) {
92 skip_
.push_back( SKIP( v
, misclass
? misclass
:"" ) );
94 inline void components ( const string
&s
) { components_
=s
; };
95 inline void jlpt ( int i
) { jlpt_
=i
; };
96 inline void grade ( int i
) { grade_
=i
; };
99 inline string
kanji () const { return kanji_
; };
100 inline string
ucs () const { return ucs_
; };
101 inline vector
<string
> onyomi () const { return onyomi_
; };
102 inline vector
<string
> kunyomi () const { return kunyomi_
; };
103 inline vector
<string
> flags () const { return flags_
; };
104 inline vector
<string
> meaning () const { return meaning_
; };
105 inline vector
<string
> nanori () const { return nanori_
; };
106 inline int freq () const { return freq_
; };
107 inline int strokes () const { return strokes_
; };
108 inline int rad_classic () const { return rad_classic_
; };
109 inline int rad_nelson () const { return rad_nelson_
; };
110 inline int jlpt () const { return jlpt_
; };
111 inline int grade () const { return grade_
; };
112 inline vector
<SKIP
> skip () const { return skip_
; };
113 inline string
components () const { return components_
;};
115 inline string
str_onyomi () const { return utils::to_string(onyomi_
,", "); };
116 inline string
str_kunyomi () const { return utils::to_string(kunyomi_
,", "); };
117 inline string
str_nanori () const { return utils::to_string(nanori_
,", "); };
118 inline string
str_meaning () const { return utils::to_string(meaning_
,", "); };
119 inline string
str_flags () const { return utils::to_string(flags_
,", "); };
120 inline string
str_radicals () const {
121 if (rad_classic_
&& rad_nelson_
){
122 std::stringstream ss
;
123 ss
<< rad_classic_
<< " (" << rad_nelson_
<< ")";
127 return std::to_string(rad_classic_
);
140 ElementKanji( int kid
, const string
&kanji
, const vector
<string
> &inf
,
142 :kid_(kid
), kanji_(kanji
), inf_(inf
), freq_(freq
){};
145 string
sql ( int did
, const char *sep
){
146 std::stringstream ss
;
147 ss
<< "INSERT INTO d_kanji (kid,did,kanji,inf,freq) VALUES ("
148 << kid_
<< "," << did
<< ",'" << kanji_
<< "','"
149 << utils::to_string(inf_
,sep
) << "'," << freq_
<< ");\n";
153 inline int kid() const {return kid_
;};
154 inline string
kanji() const {return kanji_
;};
155 inline vector
<string
> inf() const {return inf_
;};
156 inline bool freq() const {return freq_
;};
159 struct ElementReading
165 vector
<string
> restr_
;
169 ElementReading( int rid
, const string
&reading
, bool nokanji
,
170 const vector
<string
> &restr
, const vector
<string
> &inf
, bool freq
)
171 : rid_(rid
), reading_(reading
), nokanji_(nokanji
), restr_(restr
),
172 inf_(inf
), freq_(freq
) {};
175 string
sql ( int did
, const char *sep
){
176 std::stringstream ss
;
177 ss
<< "INSERT INTO d_reading (rid,did,reading,inf,nokanji,freq) VALUES ("
178 << rid_
<< "," << did
<< ",'" << reading_
<< "','"
179 << utils::to_string(inf_
,sep
) << "',"
180 << nokanji_
<< "," << freq_
<< ");\n";
186 inline int rid() const {return rid_
;};
187 inline string
reading() const {return reading_
;};
188 inline bool nokanji() const {return nokanji_
;};
189 inline vector
<string
> inf() const {return inf_
;};
190 inline vector
<string
> restr() const {return restr_
;};
191 inline bool freq() const {return freq_
;};
198 vector
<string
> gloss_
;
199 vector
<string
> stagk_
;
200 vector
<string
> stagr_
;
202 vector
<string
> xref_
;
204 vector
<string
> field_
;
205 vector
<string
> misc_
;
206 vector
<string
> dial_
;
207 vector
<string
> s_inf_
;
209 ElementSense( int sid
, const vector
<string
> &gloss
, const vector
<string
> &stagk
,
210 const vector
<string
> &stagr
, const vector
<string
> &pos
,
211 const vector
<string
> &xref
, const vector
<string
> &ant
,
212 const vector
<string
> &field
, const vector
<string
> &misc
,
213 const vector
<string
> &dial
, const vector
<string
> &s_inf
)
214 : sid_(sid
), gloss_(gloss
), stagk_(stagk
), stagr_(stagr
), pos_(pos
),
215 xref_(xref
), ant_(ant
), field_(field
), misc_(misc
), dial_(dial
),
220 string
sql ( int did
, const char *sep
){
221 std::stringstream ss
;
222 ss
<< "INSERT INTO d_sense (sid,did,gloss,xref,ant,inf,pos,field,misc,dial)"
223 << " VALUES (" << sid_
<< "," << did
<< ",'"
224 << SQLite3::escape(utils::to_string(gloss_
,sep
)) << "','"
225 << utils::to_string(xref_
,sep
) << "','"
226 << utils::to_string(ant_
,sep
) << "','"
227 << SQLite3::escape(utils::to_string(s_inf_
,sep
)) << "','"
228 << utils::to_string(pos_
,sep
) << "','"
229 << utils::to_string(field_
,sep
) << "','"
230 << utils::to_string(misc_
,sep
) << "','"
231 << utils::to_string(dial_
,sep
) << "'"
237 inline int sid() const {return sid_
;}
238 inline vector
<string
> gloss() const {return gloss_
;}
239 inline vector
<string
> stagk() const {return stagk_
;}
240 inline vector
<string
> stagr() const {return stagr_
;}
241 inline vector
<string
> pos() const {return pos_
;}
242 inline vector
<string
> xref() const {return xref_
;}
243 inline vector
<string
> ant() const {return ant_
;}
244 inline vector
<string
> field() const {return field_
;}
245 inline vector
<string
> misc() const {return misc_
;}
246 inline vector
<string
> dial() const {return dial_
;}
247 inline vector
<string
> s_inf() const {return s_inf_
;}
254 vector
<ElementKanji
> k_ele_
;
255 vector
<ElementReading
> r_ele_
;
256 vector
<ElementSense
> s_ele_
;
258 DicWord( int i
=-1, const vector
<ElementKanji
> &k
={},
259 const vector
<ElementReading
> &r
={}, const vector
<ElementSense
> &s
={} )
260 : did_(i
), k_ele_(k
), r_ele_(r
), s_ele_(s
) {}
264 inline void r_ele ( const ElementReading
&rele
)
265 { r_ele_
.push_back(rele
); }
266 inline void k_ele ( const ElementKanji
&kele
)
267 { k_ele_
.push_back(kele
); }
268 inline void s_ele ( const ElementSense
&sele
)
269 { s_ele_
.push_back(sele
); }
272 inline int did() const {return did_
;}
273 inline vector
<ElementKanji
> k_ele () const {return k_ele_
;}
274 inline vector
<ElementReading
> r_ele () const {return r_ele_
;}
275 inline vector
<ElementSense
> s_ele () const {return s_ele_
;}
277 string
html () const {
278 std::stringstream ss
;
280 if ( !k_ele_
.empty() )
281 ss
<< " <br>"; // to compensate kanji's size
282 for ( auto &k
: k_ele_
){
283 ss
<< "<font face=\"symbol\" size=\"6\"";
285 ss
<< " color=\"blue\"";
286 ss
<< ">" << k
.kanji() << "</font>";
287 if ( !k
.inf().empty() )
288 ss
<< "[" << utils::to_string(k
.inf()) << "]";
292 for ( auto &r
: r_ele_
){
293 ss
<< "<font face=\"symbol\" size=\"4\"";
295 ss
<< " color=\"blue\"";
296 ss
<< ">" << r
.reading() << "</font>";
297 if ( !r
.inf().empty() )
298 ss
<< "[" << utils::to_string(r
.inf()) << "]";
304 for ( auto &s
: s_ele_
){
306 if ( !s
.pos().empty() )
307 ss
<< "<font color=\"red\" size=\"2\"><i>"
308 << utils::to_string( s
.pos() ) << "</i></font> ";
309 if ( !s
.misc().empty() )
310 ss
<< "<font color=\"blue\" size=\"2\"><i>"
311 << utils::to_string( s
.misc() ) << "</i></font> ";
312 ss
<< utils::to_string( s
.gloss() );
313 if ( !s
.field().empty() )
314 ss
<< "<font color=\"green\" size=\"2\"><i>"
315 << utils::to_string( s
.field() ) << "</i></font> ";
316 if ( !s
.dial().empty() )
317 ss
<< "<font color=\"violet\" size=\"2\"><i>"
318 << utils::to_string( s
.dial() ) << "</i></font> ";
320 if ( !s
.ant().empty() )
321 ss
<< " <b>Antonyms:</b> " << "<font face=\"symbol\">"
322 << utils::to_string( s
.ant() ) << "</font><br>";
323 if ( !s
.xref().empty() )
324 ss
<< " <b>See also:</b> " << "<font face=\"symbol\">"
325 << utils::to_string( s
.xref() ) << "</font><br>";
326 if ( !s
.s_inf().empty() )
327 ss
<< " <b>Note:</b> " << "<font face=\"symbol\">"
328 << utils::to_string( s
.s_inf() ) << "</font><br>";
335 #endif // __DATATYPES_HXX