OSDN Git Service

AI004
authorhikarupsp <hikarupsp@users.sourceforge.jp>
Mon, 30 Dec 2013 16:15:53 +0000 (01:15 +0900)
committerhikarupsp <hikarupsp@users.sourceforge.jp>
Mon, 30 Dec 2013 16:15:53 +0000 (01:15 +0900)
候補単語フィルタのバグを修正。
パターンマッチのパターンに関数も受け取れるようにした。

ai.js
aiboot.js
aimemory.js
aimemtag.js
aisub.js
aithink.js
aiwrcgnz.js
mgcanvas/mgcanvas.js

diff --git a/ai.js b/ai.js
index 85b9e97..a6b485b 100644 (file)
--- a/ai.js
+++ b/ai.js
@@ -63,6 +63,7 @@ AI.prototype = {
        UUID_Mode_InternalConsole:                      "4ca6ed1a-e62e-470b-9d7b-e332f709e48f",
        UUID_Mode_CompileELCHNOS_OSECPU:        "17ddde48-7d4c-498f-98d8-3e73f8845028",
        UUID_Meaning_UndefinedString :          "f9080ed9-1fd4-4982-a979-092d1852298a",
+       UUID_Meaning_UndefinedStrings :         "24393cc6-e6c6-4da2-ae19-8e74ff71d390",
        sendToAI: function(str, srctype){
                var p, strbaseindex;
                
@@ -207,6 +208,7 @@ AI.prototype = {
                        this.memory.wordList.logEachPropertyNamed("str", function(s){ that.debug(s); });
                } else if(str.indexOf("inputFromURL ") == 0){
                        //webページを読み込む
+                       //inputFromURL http://www.aozora.gr.jp/cards/000148/files/773_14560.html
                        //inputFromURL http://www.aozora.gr.jp/cards/000035/files/1567_14913.html
                        //inputFromURL http://ja.wikipedia.org/wiki/%E3%83%A1%E3%82%A4%E3%83%B3%E3%83%9A%E3%83%BC%E3%82%B8
                        //inputFromURL http://ja.wikipedia.org/wiki/%E6%9D%B1%E4%BA%AC%E5%AD%A6%E8%8A%B8%E5%A4%A7%E5%AD%A6%E9%99%84%E5%B1%9E%E9%AB%98%E7%AD%89%E5%AD%A6%E6%A0%A1
index 01d3f22..426f697 100644 (file)
--- a/aiboot.js
+++ b/aiboot.js
@@ -22,12 +22,14 @@ function AI_Bootstrap(env){
        //意味タグ
        //
        append(m(env.UUID_Meaning_UndefinedString, "未定義文字列"));
+       append(m(env.UUID_Meaning_UndefinedStrings, "未定義文字列を含む複数の文字列"));
        
        //
        //パターンタグ
        //
        
        //単語教示
+       /*
        append(p([
                        env.UUID_Meaning_UndefinedString,
                        wid("は"),
@@ -45,7 +47,28 @@ function AI_Bootstrap(env){
                        }
                }
        ));
-       
+       */
+       append(p(
+               function(separated, separated_UUID){
+                       if(separated.length < 5){
+                               return false;
+                       }
+                       var a = separated.slice(-4);
+                       
+                       return (a.join("") == "は単語です。");
+               },
+               "72d5f5b2-7943-4ea0-8a91-b2c84ed856f6", 
+               function(env, separated, UUIDList){
+                       var a = separated.slice(0, -4).join("");
+                       if(env.memory.getUUIDFromWord(a) == env.UUID_Meaning_UndefinedString){
+                               env.message("「" + a + "」は単語なんですね!\n");
+                               env.memory.appendMemoryTag(new AI_WordTag(a));
+                               env.message(env.memory.wordList.length + "個目の単語を登録しました!\n");
+                       } else{
+                               env.message("「" + a + "」が単語なのは知ってますよ…。\n");
+                       }
+               }
+       ));
        //記憶保存
        append(p([
                        wid("記憶"),
index 09cb62b..3e29d3a 100644 (file)
@@ -6,6 +6,7 @@ function AI_Memory(env){
        //サブリスト
        this.candidateWordList = new Array();
        this.wordList = new Array();
+       this.wordListLastModifiedDate = new Date();
        this.patternList = new Array();
 }
 AI_Memory.prototype = {
@@ -77,6 +78,7 @@ AI_Memory.prototype = {
                                return;
                        }
                        this.wordList.push(tag);
+                       this.wordListLastModifiedDate = new Date();
                }
                if(tag instanceof AI_PatternTag){
                        this.patternList.push(tag);
@@ -120,7 +122,9 @@ AI_Memory.prototype = {
        removeMemoryTagByObject: function(obj){
                this.root.removeAnObject(obj);
                this.candidateWordList.removeAnObject(obj);
-               this.wordList.removeAnObject(obj);
+               if(this.wordList.removeAnObject(obj)){
+                       this.wordListLastModifiedDate = new Date();
+               }
        },
        verifyMemoryStructure: function(){
                //メモリ構造検査・修復
index 5ba5bec..d0f659b 100644 (file)
@@ -49,6 +49,8 @@ var AI_WordTag = function(str, uuid){
 
 var AI_PatternTag = function(pattern, uuid, func){
        // p.func(this.env, separated, separated_UUID);
+       //patternには関数も指定できる。その場合、関数の形式は
+       //f(separated, separated_UUID)となる。戻り値がtrueの場合、パターンはマッチしたとみなされる
        if(pattern){
                this.pattern = pattern;
        }
@@ -60,7 +62,7 @@ var AI_PatternTag = function(pattern, uuid, func){
        }
 }.extend(AI_MemoryTag, {
        parseToStringData: function(){
-               
+               //Not implemented.
        },
 });
 
@@ -73,6 +75,6 @@ var AI_MeaningTag = function(uuid, description){
        }
 }.extend(AI_MemoryTag, {
        parseToStringData: function(){
-               
+               //Not implemented.
        },
 });
index 43f5230..6f80ff7 100644 (file)
--- a/aisub.js
+++ b/aisub.js
@@ -23,7 +23,7 @@ function AI_Input(env){
        this.sentenceList = new Array();
 }
 AI_Input.prototype = {
-       maxHistoryLength: 16,
+       maxHistoryLength: 32,
        sentenceSeparator: [
                "。",
                "!",
@@ -34,7 +34,12 @@ AI_Input.prototype = {
        ],
        appendInput: function(input, srctype){
                //inputはStringとArrayが使用できる
-               var sList = input.splitByArray(this.sentenceSeparator);
+               var sList;
+               if(srctype != "User"){
+                       sList = input.splitByArray(this.sentenceSeparator);
+               } else{
+                       sList = [input];
+               }
                console.log(sList);
                this.sentenceList.push([srctype]);
                this.sentenceList = this.sentenceList.concat(sList);
index e25fd36..01b8031 100644 (file)
@@ -52,6 +52,7 @@ AI_Think.prototype = {
                }
        },
        checkPattern: function(separated){
+               //可変長の部分を含むパターンは、
                var separated_UUID = this.env.wordRecognition.getUUIDListFromSeparatedString(separated);
                this.env.debug("[\n" + separated_UUID.join("\n") + "\n]\n");
                var pList = this.env.memory.patternList.copy();
@@ -62,23 +63,28 @@ AI_Think.prototype = {
                
                for(var i = 0, iLen = pList.length; i < iLen; i++){
                        var p = pList[i].pattern;
-                       //単純完全一致
-                       if(separated_UUID.length != p.length){
-                               pList.removeByIndex(i);
-                               i--;
-                               iLen--;
-                       } else{
-                               for(var j = 0, jLen = separated_UUID.length; j < jLen; j++){
-                                       if(p[j] != separated_UUID[j]){
-                                               pList.removeByIndex(i);
-                                               i--;
-                                               iLen--;
-                                               break;
+                       if(p instanceof Function){
+                               if(pList[i].pattern(separated, separated_UUID)){
+                                       continue;
+                               }
+                       } else if(p instanceof Array){
+                               //単純完全一致
+                               if(separated_UUID.length == p.length){
+                                       for(var j = 0, jLen = separated_UUID.length; j < jLen; j++){
+                                               if(p[j] != separated_UUID[j]){
+                                                       break;
+                                               }
+                                       }
+                                       if(j == jLen){
+                                               continue;
                                        }
                                }
                        }
+                       pList.removeByIndex(i);
+                       i--;
+                       iLen--;
                }
-               
+               //マッチしたパターンに設定された関数の呼び出し
                for(var i = 0, iLen = pList.length; i < iLen; i++){
                        var p = pList[i];
                        if(p.func){
index c182f1b..b35c47f 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){
@@ -117,7 +119,7 @@ AI_WordRecognition.prototype = {
                //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){
+                       if(this.env.memory.getUUIDFromWord(cList[i].str) != this.env.UUID_Meaning_UndefinedString){
                                cList.removeByIndex(i);
                                i--;
                                iLen--;
@@ -134,6 +136,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;
@@ -169,11 +177,13 @@ AI_WordRecognition.prototype = {
                }
        },
        splitByWord: function(s){
-               var separatorList = this.env.memory.wordList.propertiesNamed("str");
-               separatorList.stableSort(function(a, b){
-                       return a.length - b.length;
-               });
-               return s.splitByArraySeparatorSeparated(separatorList);
+               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();
index e2fa298..36e6541 100644 (file)
@@ -24,6 +24,9 @@ function MGCanvas(canvasDOMObj){
                //console.log("x:" + loc.x);
                //console.log("y:" + loc.y);
        };
+       //ファイルの入力を受け付ける場合はコメントを外す
+       //this.canvas.addEventListener('dragover', function(evt){ return that.handleDragOver(evt); }, false);
+       //this.canvas.addEventListener('drop', function(evt){ return that.handleFileSelect(evt); }, false);
 }
 MGCanvas.prototype = {
        setGraph: function(gArray){
@@ -247,6 +250,35 @@ MGCanvas.prototype = {
                this.context.translate(w, h);
                this.displayRect = new Rectangle(-w, -h, this.canvas.width, this.canvas.height);
        },
+       loadAIMemory: function(str){
+               console.log(str);
+       },
+       // http://www.html5rocks.com/ja/tutorials/file/dndfiles/
+       handleFileSelect: function(evt){
+               evt.stopPropagation();
+               evt.preventDefault();
+       
+               var files = evt.dataTransfer.files; // FileList object.
+               var that = this;
+               
+               // files is a FileList of File objects. List some properties.
+               var output = [];
+               for(var i = 0, f; f = files[i]; i++){
+                       var r = new FileReader();
+                       r.onload = (function(file){
+                               return function(e){
+                                       //mainAI.sendTextFromFileToAI(r.result, file.name, file.lastModifiedDate, "File");
+                                       that.loadAIMemory(r.result);
+                               }
+                       })(f);
+                       r.readAsText(f);
+               }
+       },
+       handleDragOver: function(evt){
+               evt.stopPropagation();
+               evt.preventDefault();
+               evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
+       },
 }
 
 function MGNode(env, identifier){