OSDN Git Service

エッジの描画を曲線に変更。
[chnosproject/AI004.git] / aiwrcgnz.js
1 function AI_WordRecognition(env){
2         this.env = env;
3         this.wordListCache = null;
4         this.wordListCacheLastModifiedDate = new Date();
5 }
6 AI_WordRecognition.prototype = {
7         slideLookUpCandidateWordByHistory: function(input){
8                 var h = this.env.input.historyList;
9                 var cList = new Array();
10                 for(var i = 0, iLen = input.length; i < iLen; i++){
11                         //input character loop
12                         var iStr = input.substr(i);
13                         var cLen = 0;
14                         var cStr = "";
15                         for(var j = 0, jLen = h.length; j < jLen; j++){
16                                 //history entry loop
17                                 var hStrBase = h[j];
18                                 for(var k = 0, kLen = hStrBase.length; k < kLen; k++){
19                                         //history character loop
20                                         var hStr = hStrBase.substr(k);
21                                         var m = hStr.compareLeftHand(iStr);
22                                         if(m > cLen && m != iStr.length){
23                                                 cLen = m;
24                                         }
25                                 }
26                         }
27                         if(cLen > 0){
28                                 cList.pushUnique(new AI_CandidateWordTag(iStr.substr(0, cLen).trim())).wordCount++;
29                         }
30                 }
31                 //フィルター
32                 this.filterCandidateWordList00(cList);
33                 this.filterCandidateWordList01(cList, 2);
34                 this.filterCandidateWordList03(cList);
35                 //追加
36                 this.mergeCandidateWordList(cList);
37                 
38         },
39         appendCandidateWordList: function(strTag){
40                 var s = this.env.memory.candidateWordList.isIncluded(strTag, function(a, b){ return (a.str == b.str); });
41                 if(s){
42                         s.wordCount++;
43                 } else{
44                         strTag.wordCount = 1;
45                         this.env.memory.appendMemoryTag(strTag);
46                 }
47         },
48         getCandidateWordTagByString: function(str){
49                 return this.env.memory.candidateWordList.isIncluded(str, function(a, b){ return (a.str == b); });
50         },
51         mergeCandidateWordList: function(strTagList){
52                 for(var i = 0, iLen = strTagList.length; i < iLen; i++){
53                         this.appendCandidateWordList(strTagList[i]);
54                 }
55         },
56         cleanCandidateWordList: function(){
57                 //不要な候補単語を削除
58                 var iLen = this.env.memory.candidateWordList.length;
59                 for(var i = 0; i < iLen; i++){
60                         /*
61                         //出現回数の少ない候補単語
62                         if(this.env.memory.candidateWordList[i].wordCount < 10){
63                                 this.env.debug("Too small wordCount of candidateWord [" + this.env.memory.candidateWordList[i].str + "]. Removed.\n");
64                                 this.env.memory.removeMemoryTagByObject(this.env.memory.candidateWordList[i]);
65                                 i--;
66                                 iLen--;
67                                 continue;
68                         }
69                         */
70                         //単語度が1未満の単語(暫定)
71                         if(this.env.memory.candidateWordList[i].wordLevel < 1){
72                                 this.env.debug("Too small wordLevel of candidateWord [" + this.env.memory.candidateWordList[i].str + "]. Removed.\n");
73                                 this.env.memory.removeMemoryTagByObject(this.env.memory.candidateWordList[i]);
74                                 i--;
75                                 iLen--;
76                                 continue;
77                         }
78                 }
79                 this.env.memory.candidateWordListLastCleanedDate = new Date();
80         },
81         debugShowCandidateWordList: function(){
82                 var c = this.env.memory.candidateWordList.copy();
83                 //c.reverse();
84                 this.env.debug("candidateWordList:" + c.length + "\n #:wCount:level:str\n");
85                 
86                 for(var i = 0, iLen = c.length; i < iLen; i++){
87                         this.env.debug((i + 1) + ":\t" + c[i].wordCount.toString() + ":\t" + c[i].wordLevel.toString() + ":\t" + c[i].str + "\n");
88                 }
89                 this.env.debug("candidateWordList end\n");
90         },
91         filterCandidateWordList00:function(cList){
92                 //00:長い単語に含まれており、かつ出現頻度が長い単語と等しい単語を削除
93                 //cList内の候補単語に対して、フィルターをかける。
94                 var iLen = cList.length;
95                 if(iLen < 1){
96                         return;
97                 }
98                 var baseStrTag = cList[0];
99                 for(var i = 1; i < iLen; i++){
100                         var c = cList[i];
101                         if(baseStrTag.str.indexOf(c.str) != -1){
102                                 //c.strはbaseStrTag.strに含まれている
103                                 if(baseStrTag.wordCount == c.wordCount){
104                                         //かつ出現回数が等しいので不要な単語
105                                         //後で削除する。出現回数を0にマークする。
106                                         c.wordCount = 0;
107                                 }
108                         }
109                         if(c.wordCount > 0){
110                                 //単語は削除されなかった、つまり異なる単語なので、baseStrTagを更新
111                                 var baseStrTag = c;
112                         }
113                 }
114                 //削除処理
115                 for(var i = 1; i < iLen; i++){
116                         var c = cList[i];
117                         if(c.wordCount == 0){
118                                 cList.removeByIndex(i);
119                                 i--;
120                                 iLen--;
121                         }
122                 }
123         },
124         filterCandidateWordList01:function(cList, minLen){
125                 //01:minLenに満たない文字数の候補を削除
126                 var iLen = cList.length;
127                 for(var i = 0; i < iLen; i++){
128                         if(cList[i].str.length < minLen){
129                                 cList.removeByIndex(i);
130                                 i--;
131                                 iLen--;
132                         }
133                 }
134         },
135         filterCandidateWordList02:function(cList, minCount){
136                 //02:minCountに満たない出現回数の候補を削除
137                 var iLen = cList.length;
138                 for(var i = 0; i < iLen; i++){
139                         if(cList[i].wordCount < minCount){
140                                 cList.removeByIndex(i);
141                                 i--;
142                                 iLen--;
143                         }
144                 }
145         },
146         filterCandidateWordList03: function(cList){
147                 //03:すでに単語と判明している候補を削除
148                 var iLen = cList.length;
149                 for(var i = 0; i < iLen; i++){
150                         if(this.env.memory.getUUIDFromWord(cList[i].str) != this.env.UUID_Meaning_UndefinedString){
151                                 cList.removeByIndex(i);
152                                 i--;
153                                 iLen--;
154                         }
155                 }
156         },
157         sortCandidateWordListByWordCount: function(){
158                 this.env.memory.candidateWordList.stableSort(function(a, b){
159                         return a.wordCount - b.wordCount;
160                 });
161         },
162         sortCandidateWordListByWordLevel: function(){
163                 this.env.memory.candidateWordList.stableSort(function(a, b){
164                         return a.wordLevel - b.wordLevel;
165                 });
166         },
167         sortWordListByLength: function(){
168                 //文字数の大きい方がリストの最初に来るようにする。
169                 this.env.memory.wordList.stableSort(function(a, b){
170                         return b.str.length - a.str.length;
171                 });
172         },
173         computeWordLevel: function(strTag){
174                 var s = strTag.str;
175                 var iLen = s.length;
176                 var f = 0;
177                 strTag.wordLevel = 0;
178                 //文字列中の文字種数を数える
179                 for(var i = 0; i < iLen; i++){
180                         if(s.isHiraganaAt(i)){
181                                 f |= 0x01;
182                         } else if(s.isKanjiAt(i)){
183                                 f |= 0x02;
184                         } else if(s.isKatakanaAt(i)){
185                                 f |= 0x04;
186                         } else if(s.isHankakuKanaAt(i)){
187                                 f |= 0x08;
188                         } else{
189                                 f |= 0x10;
190                         }
191                 }
192                 for(var i = 0; i < 5; i++){
193                         if((f & 0x01) != 0){
194                                 strTag.wordLevel++;
195                         }
196                         f >>>= 1;
197                 }
198                 strTag.wordLevel = 1 / strTag.wordLevel;
199                 return;
200         },
201         computeEachWordLevel: function(){
202                 var iLen = this.env.memory.candidateWordList.length;
203                 for(var i = 0; i < iLen; i++){
204                         this.computeWordLevel(this.env.memory.candidateWordList[i]);
205                 }
206         },
207         splitByWord: function(s){
208                 if(!this.wordListCache || this.wordListCacheLastModifiedDate < this.env.memory.wordListLastModifiedDate){
209                         //キャッシュが存在しないか古い場合、元のリストをソートしてからキャッシュを作成
210                         this.sortWordListByLength();
211                         this.wordListCache = this.env.memory.wordList.propertiesNamed("str");
212                         this.wordListCacheLastModifiedDate = new Date();
213                 }
214                 return s.splitByArraySeparatorSeparatedLong(this.wordListCache);
215         },
216         getUUIDListFromSeparatedString: function(separated){
217                 var retv = new Array();
218                 for(var i = 0, iLen = separated.length; i < iLen; i++){
219                         retv.push(this.env.memory.getUUIDFromWord(separated[i]));
220                 }
221                 return retv;
222         },
223 }