OSDN Git Service

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