OSDN Git Service

AIのDB連携機能を実装中。
[chnosproject/AI004.git] / aiwrcgnz.js
index e8d600f..027c130 100644 (file)
@@ -1,5 +1,7 @@
 function AI_WordRecognition(env){
        this.env = env;
+       this.wordListCache = null;
+       this.wordListCacheLastModifiedDate = new Date();
 }
 AI_WordRecognition.prototype = {
        slideLookUpCandidateWordByHistory: function(input){
@@ -23,14 +25,16 @@ AI_WordRecognition.prototype = {
                                }
                        }
                        if(cLen > 0){
-                               cList.pushUnique(new AI_CandidateWordTag(iStr.substr(0, cLen))).wordCount++;
+                               cList.pushUnique(new AI_CandidateWordTag(iStr.substr(0, cLen).trim())).wordCount++;
                        }
                }
                //フィルター
                this.filterCandidateWordList00(cList);
                this.filterCandidateWordList01(cList, 2);
+               this.filterCandidateWordList03(cList);
                //追加
                this.mergeCandidateWordList(cList);
+               
        },
        appendCandidateWordList: function(strTag){
                var s = this.env.memory.candidateWordList.isIncluded(strTag, function(a, b){ return (a.str == b.str); });
@@ -41,16 +45,44 @@ AI_WordRecognition.prototype = {
                        this.env.memory.appendMemoryTag(strTag);
                }
        },
+       getCandidateWordTagByString: function(str){
+               return this.env.memory.candidateWordList.isIncluded(str, function(a, b){ return (a.str == b); });
+       },
        mergeCandidateWordList: function(strTagList){
                for(var i = 0, iLen = strTagList.length; i < iLen; i++){
                        this.appendCandidateWordList(strTagList[i]);
                }
        },
+       cleanCandidateWordList: function(){
+               //不要な候補単語を削除
+               //出現回数の少ない候補単語
+               //単語度が1未満の単語(暫定)
+               var iLen = this.env.memory.candidateWordList.length;
+               for(var i = 0; i < iLen; i++){
+                       if(this.env.memory.candidateWordList[i].wordCount < 10){
+                               this.env.debug("Too small wordCount of candidateWord [" + this.env.memory.candidateWordList[i].str + "]. Removed.\n");
+                               this.env.memory.removeMemoryTagByObject(this.env.memory.candidateWordList[i]);
+                               i--;
+                               iLen--;
+                               continue;
+                       }
+                       if(this.env.memory.candidateWordList[i].wordLevel < 1){
+                               this.env.debug("Too small wordLevel of candidateWord [" + this.env.memory.candidateWordList[i].str + "]. Removed.\n");
+                               this.env.memory.removeMemoryTagByObject(this.env.memory.candidateWordList[i]);
+                               i--;
+                               iLen--;
+                               continue;
+                       }
+               }
+               this.env.memory.candidateWordListLastCleanedDate = new Date();
+       },
        debugShowCandidateWordList: function(){
-               this.env.debug("candidateWordList:\n");
-               var c = this.env.memory.candidateWordList;
+               var c = this.env.memory.candidateWordList.copy();
+               c.reverse();
+               this.env.debug("candidateWordList:" + c.length + "\n #:wCount:level:str\n");
+               
                for(var i = 0, iLen = c.length; i < iLen; i++){
-                       this.env.debug(c[i].wordCount.toString() + " :" + c[i].wordLevel.toString() + " :" + c[i].str + "\n");
+                       this.env.debug((i + 1) + ":\t" + c[i].wordCount.toString() + ":\t" + c[i].wordLevel.toString() + ":\t" + c[i].str + "\n");
                }
                this.env.debug("candidateWordList end\n");
        },
@@ -89,7 +121,6 @@ AI_WordRecognition.prototype = {
        },
        filterCandidateWordList01:function(cList, minLen){
                //01:minLenに満たない文字数の候補を削除
-               //削除処理
                var iLen = cList.length;
                for(var i = 0; i < iLen; i++){
                        if(cList[i].str.length < minLen){
@@ -101,7 +132,6 @@ AI_WordRecognition.prototype = {
        },
        filterCandidateWordList02:function(cList, minCount){
                //02:minCountに満たない出現回数の候補を削除
-               //削除処理
                var iLen = cList.length;
                for(var i = 0; i < iLen; i++){
                        if(cList[i].wordCount < minCount){
@@ -111,6 +141,17 @@ AI_WordRecognition.prototype = {
                        }
                }
        },
+       filterCandidateWordList03: function(cList){
+               //03:すでに単語と判明している候補を削除
+               var iLen = cList.length;
+               for(var i = 0; i < iLen; i++){
+                       if(this.env.memory.getUUIDFromWord(cList[i].str) != this.env.UUID_Meaning_UndefinedString){
+                               cList.removeByIndex(i);
+                               i--;
+                               iLen--;
+                       }
+               }
+       },
        sortCandidateWordListByWordCount: function(){
                this.env.memory.candidateWordList.stableSort(function(a, b){
                        return a.wordCount - b.wordCount;
@@ -121,6 +162,12 @@ AI_WordRecognition.prototype = {
                        return a.wordLevel - b.wordLevel;
                });
        },
+       sortWordListByLength: function(){
+               //文字数の大きい方がリストの最初に来るようにする。
+               this.env.memory.wordList.stableSort(function(a, b){
+                       return b.str.length - a.str.length;
+               });
+       },
        computeWordLevel: function(strTag){
                var s = strTag.str;
                var iLen = s.length;
@@ -154,5 +201,21 @@ AI_WordRecognition.prototype = {
                for(var i = 0; i < iLen; i++){
                        this.computeWordLevel(this.env.memory.candidateWordList[i]);
                }
-       }
+       },
+       splitByWord: function(s){
+               if(!this.wordListCache || this.wordListCacheLastModifiedDate < this.env.memory.wordListLastModifiedDate){
+                       //キャッシュが存在しないか古い場合、元のリストをソートしてからキャッシュを作成
+                       this.sortWordListByLength();
+                       this.wordListCache = this.env.memory.wordList.propertiesNamed("str");
+                       this.wordListCacheLastModifiedDate = new Date();
+               }
+               return s.splitByArraySeparatorSeparatedLong(this.wordListCache);
+       },
+       getUUIDListFromSeparatedString: function(separated){
+               var retv = new Array();
+               for(var i = 0, iLen = separated.length; i < iLen; i++){
+                       retv.push(this.env.memory.getUUIDFromWord(separated[i]));
+               }
+               return retv;
+       },
 }