Improve the dictionary to save more memory
[FreeRecite.git] / src / core / Dict.cpp
blobdbb29c21809e23136179daf918be80500dfd9967
1 #include <fstream>
2 #include <iostream>
4 #include "Dict.h"
5 #include "ConfigHolder.h"
7 namespace freeRecite {
9 Dict::~Dict() {
10 if(ifsgdic != 0)
11 delete ifsgdic;
14 bool Dict::load() {
15 std::string dictName = configHolder.dictFile().c_str();
16 std::ifstream ifs(dictName.c_str());
17 if(!ifs.is_open())
18 return false;
20 std::string lineStr;
21 while(ifs.good()) {
22 std::getline(ifs,lineStr);
23 if( dictItem.refer(lineStr) )
24 dict[dictItem.getW()] = lineStr;
27 ifsgdic = new std::ifstream("/usr/share/FreeRecite/freeRecite.dict");
28 if(!ifsgdic->is_open())
29 return false;
31 return true;
34 bool Dict::lookUp(const std::string &word) {
35 //Find in local dictionary.
36 std::map<std::string,std::string>::iterator itr = dict.find(word);
37 if( itr != dict.end() ) {
38 dictItem.refer(itr->second);
39 return true;
42 //Find in global dictionary.
43 return findInGlobl(word);
46 bool Dict::findInGlobl(const std::string &swatch) {
47 // get length of file
48 ifsgdic->seekg(0,std::ios::end);
49 int length = ifsgdic->tellg();
50 int before = 0;
51 int after = length;
52 int current = -1;
53 std::string line;
54 while(after-before>1) {
55 ifsgdic->seekg((after+before)/2);
56 ifsgdic->ignore(std::numeric_limits<int>::max(),'\n');
57 current = ifsgdic->tellg();
58 getline(*ifsgdic,line);
59 if(!dictItem.refer(line))
60 return false;
61 if(swatch > dictItem.getW())
62 before = (after+before)/2;
63 else if(swatch < dictItem.getW())
64 after = (after+before)/2;
65 else if( swatch == dictItem.getW() )
66 return true;
68 if(before == 0) {
69 ifsgdic->seekg(0);
70 getline(*ifsgdic,line);
71 if(swatch == dictItem.getW())
72 return true;
74 return false;
77 bool Dict::modify(const std::string &item) {
78 static DictItem itemAdd;
80 if(!itemAdd.refer(item)){
81 return false;
84 dict[itemAdd.getW()] = item;
85 if(save())
86 return true;
87 else
88 return false;
91 bool Dict::save() {
92 std::ofstream ofs(configHolder.dictFile().c_str());
93 if(!ofs.is_open()) {
94 return false;
96 std::map<std::string,std::string>::const_iterator itr = dict.begin();
97 while(itr != dict.end()) {
98 if(!ofs.good()){
99 return false;
101 ofs << itr->second << std::endl;
102 ++itr;
104 return true;
107 const std::string &Dict::phonetics() const {
108 static std::string __phonetics;
109 __phonetics = "";
110 for(unsigned i = 0; i < dictItem.getT().size(); ++i) {
111 switch(dictItem.getT().at(i)) {
112 case '0':
113 __phonetics += "θ";
114 break;
115 case '1':
116 __phonetics +="ɑ";
117 break;
118 case '2':
119 __phonetics += "ʌ";
120 break;
121 case '3':
122 __phonetics += "ә";
123 break;
124 case '4':
125 __phonetics +="є";
126 break;
127 case '5':
128 __phonetics +="æ";
129 break;
130 case '6':
131 __phonetics += "ɔ";
132 break;
133 case '7':
134 __phonetics += "ʃ";
135 break;
136 case '8':
137 __phonetics += "ð";
138 break;
139 case '9':
140 __phonetics += "ŋ";
141 break;
142 case '=':
143 __phonetics += "ʒ";
144 break;
145 case ';':
146 __phonetics += ",";
147 break;
148 default:
149 __phonetics += dictItem.getT().at(i);
152 return __phonetics;
155 //This is a global variable.
156 Dict dictionary;
158 } //namespace freeRecite end