OSDN Git Service

AI_DatabaseInfoTagを追加。
[chnosproject/AI004.git] / aithink.js
1 function AI_Think(env){
2         this.env = env;
3         this.jobStack = new Array();
4         this.processingJob = null;
5 }
6 AI_Think.prototype = {
7         tickLimitMs: 100,
8         tick: function(){
9                 //定期的に呼ばれることを期待する
10                 var tickStartTimeMs;
11                 var s;
12                 var separated;
13                 var separated_UUID;
14                 var t;
15                 tickStartTimeMs = new Date();
16                 if(this.env.input.sentenceList.length > 0){
17                         this.env.debug("**** Think ****\n");
18                         
19                         for(var i = 0; i < 64; i++){
20                                 if((new Date()) - tickStartTimeMs > this.tickLimitMs){
21                                         //CPU時間占有防止
22                                         break;
23                                 }
24                                 //入力処理ループ
25                                 s = this.env.input.getSentence();
26                                 
27                                 if(s === undefined){
28                                         //入力文字列が終了したので、単語候補をソート
29                                         this.env.wordRecognition.sortCandidateWordListByWordCount();
30                                         this.env.wordRecognition.computeEachWordLevel();
31                                         this.env.wordRecognition.sortCandidateWordListByWordLevel();
32                                         this.env.wordRecognition.cleanCandidateWordList();
33                                         break;
34                                 }
35                                 if(this.env.input.lastSentenceSourceType){
36                                         this.env.message(this.env.input.lastSentenceSourceType, true);
37                                 } else{
38                                         this.env.message("Unknown", true);
39                                 }
40                                 this.env.message("> " + s + "\n", true);
41                                 //ここで単語候補抽出を行っておく
42                                 this.env.wordRecognition.slideLookUpCandidateWordByHistory(s);
43                                 //分割
44                                 separated = this.env.wordRecognition.splitByWord(s);
45                                 this.env.debug("[" + separated.join(" ") + "]\n");
46                                 separated_UUID = this.env.wordRecognition.getUUIDListFromSeparatedString(separated);
47                                 //分割の結果、未定義文字列と判別された部分を候補単語から探し、候補単語にあれば、その出現回数を+100する。
48                                 for(var i = 0, iLen = separated_UUID.length;i < iLen; i++){
49                                         if(separated_UUID[i] == this.env.UUID_Meaning_UndefinedString){
50                                                 t = this.env.wordRecognition.getCandidateWordTagByString(separated[i]);
51                                                 if(t){
52                                                         t.wordCount += 100;
53                                                 }
54                                         }
55                                 }
56                                 //パターン照合
57                                 this.checkPattern(separated, separated_UUID);
58                                 
59                                 //ジョブに入力
60                                 if(this.processingJob && this.processingJob.input(s) === undefined){
61                                         this.processingJob = null;
62                                 }
63                         }
64                 } else if(this.processingJob || this.jobStack.length > 0){
65                         if(!this.processingJob){
66                                 this.processingJob = this.jobStack.pop();
67                         }
68                         if(this.processingJob.tick() === undefined){
69                                 this.processingJob = null;
70                         }
71                 }
72         },
73         checkPattern: function(separated, separated_UUID){
74                 //可変長の部分を含むパターンは、
75                 this.env.debug("[\n" + separated_UUID.join("\n") + "\n]\n");
76                 var pList = this.env.memory.patternList.copy();
77                 
78                 if(pList.length <= 0 || separated.length <= 0 || separated_UUID.length <= 0){
79                         return new Array();
80                 }
81                 
82                 for(var i = 0, iLen = pList.length; i < iLen; i++){
83                         var p = pList[i].pattern;
84                         if(p instanceof Function){
85                                 if(pList[i].pattern(separated, separated_UUID)){
86                                         continue;
87                                 }
88                         } else if(p instanceof Array){
89                                 //単純完全一致
90                                 if(separated_UUID.length == p.length){
91                                         for(var j = 0, jLen = separated_UUID.length; j < jLen; j++){
92                                                 if(p[j] != separated_UUID[j]){
93                                                         break;
94                                                 }
95                                         }
96                                         if(j == jLen){
97                                                 continue;
98                                         }
99                                 }
100                         }
101                         pList.removeByIndex(i);
102                         i--;
103                         iLen--;
104                 }
105                 //マッチしたパターンに設定された関数の呼び出し
106                 for(var i = 0, iLen = pList.length; i < iLen; i++){
107                         var p = pList[i];
108                         if(p.func){
109                                 p.func(this.env, separated, separated_UUID);
110                         }
111                 }
112                 
113                 return pList;
114         },
115 }