update dict
[QFreeRecite.git] / src / core / Dict.cpp
blob102ed163456849334fbae32c4703a57f82a48bcc
1 #include <fstream>
2 #include <limits>
3 #include <iostream>
5 #include "Dict.h"
6 #include "ConfigHolder.h"
8 namespace freeRecite {
10 Dict::~Dict() {
11 if(ifsgdic != 0)
12 delete ifsgdic;
15 bool Dict::load() {
16 validFlag = false;
17 plainTran.clear();
18 if(ifsgdic != 0)
19 delete ifsgdic;
20 dict.clear();
21 std::string dictName = configHolder.localDictFile().c_str();
22 std::ifstream ifs(dictName.c_str());
23 while(!ifs.is_open()){
24 if(!save())
25 return false;
26 ifs.open(dictName.c_str());
29 std::string lineStr;
30 while(std::getline(ifs,lineStr)) {
31 if( dictItem.refer(lineStr) )
32 if(dict[dictItem.getW()].size() <= lineStr.size())
33 dict[dictItem.getW()] = lineStr;
36 ifsgdic = new std::ifstream(configHolder.globalDictFile().c_str());
37 if(!ifsgdic->is_open())
38 return false;
39 validFlag = true;
40 return true;
43 bool Dict::lookUp(const std::string &word) {
44 if(validFlag == false)
45 return false;
46 //Find in local dictionary.
47 std::map<std::string,std::string>::iterator itr = dict.find(word);
48 if( itr != dict.end() ) {
49 dictItem.refer(itr->second);
50 setPlainTranslation();
51 return true;
54 //Find in global dictionary.
55 if(findInGlobl(word) == true) {
56 setPlainTranslation();
57 return true;
60 return false;
63 bool Dict::findInGlobl(const std::string &swatch) {
64 // get length of file
65 ifsgdic->seekg(0,std::ios::end);
66 int length = ifsgdic->tellg();
67 int before = 0;
68 int after = length;
69 int current = -1;
70 std::string line;
71 while(after-before>1) {
72 ifsgdic->seekg((after+before)/2);
73 ifsgdic->ignore(std::numeric_limits<int>::max(),'\n');
74 current = ifsgdic->tellg();
75 getline(*ifsgdic,line);
76 if(!dictItem.refer(line))
77 return false;
78 if(swatch > dictItem.getW())
79 before = (after+before)/2;
80 else if(swatch < dictItem.getW())
81 after = (after+before)/2;
82 else if( swatch == dictItem.getW() )
83 return true;
85 if(before == 0) {
86 ifsgdic->seekg(0);
87 getline(*ifsgdic,line);
88 if(swatch == dictItem.getW())
89 return true;
91 return false;
94 bool Dict::merge(const char *newDictName) {
95 std::ifstream localDic(configHolder.localDictFile().c_str());
96 std::ifstream globalDic(configHolder.globalDictFile().c_str());
97 std::ofstream tempDic(newDictName);
98 if(!localDic.is_open() || !globalDic.is_open() || !tempDic.is_open())
99 return false;
100 std::string localLine, globalLine;
101 std::string localWord, globalWord;
103 * 1: getline from local.
104 * 2: getline from global.
105 * 3: getline from both local and global.
107 short int getFromFlag = 3;
108 do {
109 if((getFromFlag%2) != 0) { //getFromFlag == 1,3
110 if(std::getline(localDic,localLine)) {
111 localWord = dictItem.refer(localLine)
112 ? dictItem.getW() : std::string("");
113 }else { //getFromFlag == 2
114 while(std::getline(globalDic,globalLine))
115 tempDic << globalLine << std::endl;
116 break;
119 if((getFromFlag - 1) > 0) { //getFromFlag == 2,3
120 if(std::getline(globalDic,globalLine)) {
121 globalWord = dictItem.refer(globalLine)
122 ? dictItem.getW() : std::string("");
123 }else {
124 while(std::getline(globalDic,localLine))
125 tempDic << localLine << std::endl;
126 break;
129 if(localWord < globalWord) {
130 getFromFlag = 1;
131 tempDic << localLine << std::endl;
132 }else if(localWord > globalWord){
133 getFromFlag = 2;
134 tempDic << globalLine << std::endl;
135 }else { //Both have the same word.
136 getFromFlag = 3;
137 tempDic << localLine << std::endl;
139 }while(true);
141 return true;
144 bool Dict::modify(const std::string &item) {
145 static DictItem itemAdd;
147 if(!itemAdd.refer(item)){
148 return false;
151 dict[itemAdd.getW()] = item;
152 if(save())
153 return true;
154 else
155 return false;
158 bool Dict::save() {
159 std::ofstream ofs(configHolder.localDictFile().c_str());
160 if(!ofs.is_open()) {
161 return false;
163 std::map<std::string,std::string>::const_iterator itr = dict.begin();
164 while(itr != dict.end()) {
165 if(!ofs.good()){
166 return false;
168 ofs << itr->second << std::endl;
169 ++itr;
171 return true;
174 const std::string &Dict::phonetics() const {
175 static std::string __phonetics;
176 __phonetics = "";
177 for(unsigned i = 0; i < dictItem.getT().size(); ++i) {
178 switch(dictItem.getT().at(i)) {
179 case '0':
180 __phonetics += "θ";
181 break;
182 case '1':
183 __phonetics +="ɑ";
184 break;
185 case '2':
186 __phonetics += "ʌ";
187 break;
188 case '3':
189 __phonetics += "ә";
190 break;
191 case '4':
192 __phonetics +="є";
193 break;
194 case '5':
195 __phonetics +="æ";
196 break;
197 case '6':
198 __phonetics += "ɔ";
199 break;
200 case '7':
201 __phonetics += "ʃ";
202 break;
203 case '8':
204 __phonetics += "ð";
205 break;
206 case '9':
207 __phonetics += "ŋ";
208 break;
209 case '=':
210 __phonetics += "ʒ";
211 break;
212 case ';':
213 __phonetics += ",";
214 break;
215 default:
216 __phonetics += dictItem.getT().at(i);
219 return __phonetics;
222 void Dict::setPlainTranslation() {
223 const std::string &orgTran = dictItem.getM();
224 plainTran.clear();
225 if(plainTran.capacity() < orgTran.size())
226 plainTran.reserve(orgTran.size());
227 plainTran = orgTran.substr(0,orgTran.find_first_of(std::string("<")));
231 //This is a global variable.
232 Dict dictionary;
234 } //namespace freeRecite end