OSDN Git Service

Initial commit
[wordring-tm/wordring-tm.git] / languages / english / english.cpp
1 #include "text.h"
2 #include "english.h"
3
4 #include "debug.h"
5
6 EnglishPlugin::EnglishPlugin(QObject *parent)
7         : QObject(parent)
8 {
9 }
10
11 void EnglishPlugin::set_settings(QSettings const *settings)
12 {
13         m_settings = settings;
14 }
15
16 QString EnglishPlugin::name() const { return QString("English"); }
17
18 int EnglishPlugin::code() const { return QLocale::Language::English; }
19
20 QIcon EnglishPlugin::icon() const
21 {
22         return QIcon(":/image/united_kingdom.svg");
23 }
24
25 Text::pointer EnglishPlugin::divide_into_sentences(QString string_)
26 {
27         Text::pointer result = Text::create();
28         result->set_data(RangeData::create(0, string_.size() - 1));
29         QString cstring;
30         int begin_ = 0;
31         int tail_ = 0;
32
33         int state = 0;
34         for(int i = 0; i < string_.size(); i++)
35         {
36                 unsigned short c = string_.at(i).unicode();
37                 switch(state)
38                 {
39                 case 0: s0: state = 0;// 開始位置
40                         begin_ = i;
41                         tail_ = i;
42                 case 1: state = 1;
43                         if(c == '.' || c == '?' || c == '!') state = 2;
44                         break;
45                 case 2:
46                         if(c == ' ') state = 3;
47                         else state = 1;
48                         break;
49                 case 3:
50                         if('A' <= c && c <= 'Z')
51                         {
52                                 Text::pointer s = Text::create(result, cstring);
53                                 s->set_data(RangeData::create(begin_, tail_));
54                                 result->append(s);
55                                 cstring.clear();
56                                 goto s0;
57                         }
58                         state = 1;
59                         break;
60                 }
61                 tail_ = i;
62                 cstring.push_back(c);
63         }
64         if(!cstring.isEmpty())
65         {
66                 Text::pointer s = Text::create(result, cstring);
67                 s->set_data(RangeData::create(begin_, tail_));
68                 result->append(s);
69         }
70         return result;
71 }
72
73 Text::pointer EnglishPlugin::divide_into_words(Text::pointer sentence)
74 {
75         assert(sentence->size() == 0);
76         Text::pointer result = Text::create();
77         result->set_data(sentence->data());
78
79         QString const &string_ = sentence->string();
80         QString cstring;
81         int begin_ = 0;
82         int tail_ = 0;
83         for(int i = 0; i < string_.size(); i++)
84         {
85                 unsigned short ch = string_.at(i).unicode();
86                 if(('a' <= ch && ch <= 'z')
87                                 || ('A' <= ch && ch <= 'Z')
88                                 || ('0' <= ch && ch <= '9')
89                                 || ch == '-'
90                                 || ch == '_')
91                 {
92                         tail_ = i;
93                         cstring.append(ch);
94                 }
95                 else
96                 {
97                         if(!cstring.isEmpty())
98                                 result->append(stuff_word(result, cstring, begin_, tail_));
99                         cstring.clear();
100                         result->append(stuff_word(result, QString(ch), i, i));
101                         begin_ = i + 1;
102                         tail_ = begin_;
103                 }
104         }
105         if(!cstring.isEmpty()) result->append(stuff_word(result, cstring, begin_, tail_));
106         return result;
107 }
108