1 function AI_Memory(env){
5 this.root = new Array();
7 this.candidateWordList = new Array();
8 this.candidateWordListLastModifiedDate = new Date();
9 this.candidateWordListLastCleanedDate = new Date();
11 this.wordList = new Array();
12 this.wordListLastModifiedDate = new Date();
14 this.patternList = new Array();
16 AI_Memory.prototype = {
18 saveMemory: function(){
19 var m = this.env.IOManager;
20 var s = "#" + this.env.UUID_Mode_ReadMemory + "\n";
23 for(var i = 0, iLen = cl.length; i < iLen; i++){
24 if(cl[i] instanceof AI_MemoryTag){
25 k = cl[i].parseToStringData();
31 var d = new Blob([s]);
33 m.showDownloadLink(d);
36 loadMemory: function(str){
39 this.env.debug("Memory loading...\n");
40 a = str.splitByArray(["\n"]);
42 for(var i = 1, iLen = a.length; i < iLen; i++){
46 this.env.debug(i + ": " + e + "\n");
53 if(q == AI_MemoryTag.prototype.Type_CandidateWord){
54 t = new AI_CandidateWordTag();
55 } else if(q == AI_MemoryTag.prototype.Type_Word){
58 t = new AI_MemoryTag();
60 AI_MemoryTag.prototype.loadFromMemoryData.call(t, d);
61 this.appendMemoryTag(t);
63 this.verifyMemoryStructure();
64 this.env.debug("Memory loading done.\n" + this.root.length + " tags exist.\n");
66 appendMemoryTag: function(tag){
67 //同じUUIDのタグがあった場合はデバッグ表示をして、追加しようとしているものに置き換える。
68 //ただし、初期データに入っているものは警告を発さず上書きする。
69 var s = this.root.isIncluded(tag, function(a, b){ return (a.uuid == b.uuid); });
71 //タグに合わせて追加条件を満たしているか確認し、それぞれのサブリストに分配
72 if(tag instanceof AI_CandidateWordTag){
73 this.candidateWordList.push(tag);
74 this.candidateWordListLastModifiedDate = new Date();
76 if(tag instanceof AI_WordTag){
77 if(this.wordList.isIncluded(tag, function(a, b){ return ((a.str == b.str) && (a !== s)); })){
78 this.env.debug("appendMemoryTag: Duplicated word [" + tag.str + "].\n");
81 if(tag.str == undefined || tag.str.length == 0){
82 this.env.debug("appendMemoryTag: Invalid word [" + tag.str + "].\n");
85 this.wordList.push(tag);
86 this.wordListLastModifiedDate = new Date();
88 if(tag instanceof AI_PatternTag){
89 this.patternList.push(tag);
94 if(s.isBootstrap === undefined){
95 this.env.debug("appendMemoryTag: duplicated UUID " + tag.uuid + ", overwritten.\n");
97 this.removeMemoryTagByObject(s);
103 appendMemoryTagFromString: function(str){
111 this.env.debug(""i + ": " + e + "\n");
118 if(q == AI_MemoryTag.prototype.Type_CandidateWord){
119 t = new AI_CandidateWordTag();
121 t = new AI_MemoryTag();
123 AI_MemoryTag.prototype.loadFromMemoryData.call(t, d);
124 this.appendMemoryTag(t);
127 removeMemoryTagByObject: function(obj){
128 this.root.removeAnObject(obj);
129 if(this.candidateWordList.removeAnObject(obj)){
130 this.candidateWordListLastModifiedDate = new Date();
132 if(this.wordList.removeAnObject(obj)){
133 this.wordListLastModifiedDate = new Date();
136 verifyMemoryStructure: function(){
138 //単語が単語候補に残っていた場合は単語候補から削除
139 for(var i = 0, iLen = this.wordList.length; i < iLen; i++){
140 var w = this.wordList[i].str;
141 for(var j = 0, jLen = this.candidateWordList.length; j < jLen; j++){
142 if(this.candidateWordList[j].str == w){
143 this.env.debug("Word duplicated in CWL. Removed.\n");
144 this.removeMemoryTagByObject(this.candidateWordList[j]);
151 this.env.wordRecognition.cleanCandidateWordList();
153 this.env.wordRecognition.sortCandidateWordListByWordCount();
154 this.env.wordRecognition.computeEachWordLevel();
155 this.env.wordRecognition.sortCandidateWordListByWordLevel();
157 this.env.debug("Memory verifying done.\n");
159 getUUIDFromWord: function(str){
160 var t = this.wordList.isIncluded(str, function(a, b){ return a.str == b; });
162 return this.env.UUID_Meaning_UndefinedString;