OSDN Git Service

パターン照合を実装。
[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                 if(this.env.input.sentenceList.length > 0){
11                         var tickStartTimeMs = new Date();
12                         this.env.debug("**** Think ****\n");
13                         for(var i = 0; i < 64; i++){
14                                 if((new Date()) - tickStartTimeMs > this.tickLimitMs){
15                                         //CPU時間占有防止
16                                         break;
17                                 }
18                                 //入力処理ループ
19                                 var s = this.env.input.getSentence();
20                                 if(s === undefined){
21                                         //単語候補をソート
22                                         this.env.wordRecognition.sortCandidateWordListByWordCount();
23                                         this.env.wordRecognition.computeEachWordLevel();
24                                         this.env.wordRecognition.sortCandidateWordListByWordLevel();
25                                         break;
26                                 }
27                                 if(this.env.input.lastSentenceSourceType){
28                                         this.env.message(this.env.input.lastSentenceSourceType, true);
29                                 } else{
30                                         this.env.message("Unknown", true);
31                                 }
32                                 this.env.message("> " + s + "\n", true);
33                                 
34                                 var separated = this.env.wordRecognition.splitByWord(s);
35                                 this.env.debug("[" + separated.join(" ") + "]\n");
36                                 
37                                 //パターン照合
38                                 this.checkPattern(separated);
39                                 
40                                 //ジョブに入力
41                                 if(this.processingJob && this.processingJob.input(s) === undefined){
42                                         this.processingJob = null;
43                                 }
44                         }
45                 } else if(this.processingJob || this.jobStack.length > 0){
46                         if(!this.processingJob){
47                                 this.processingJob = this.jobStack.pop();
48                         }
49                         if(this.processingJob.tick() === undefined){
50                                 this.processingJob = null;
51                         }
52                 }
53         },
54         checkPattern: function(separated){
55                 var separated_UUID = this.env.wordRecognition.getUUIDListFromSeparatedString(separated);
56                 this.env.debug("[\n" + separated_UUID.join("\n") + "\n]\n");
57                 var pList = this.env.memory.patternList.copy();
58                 
59                 if(pList.length <= 0 || separated.length <= 0 || separated_UUID.length <= 0){
60                         return new Array();
61                 }
62                 
63                 for(var i = 0, iLen = pList.length; i < iLen; i++){
64                         var p = pList[i].pattern;
65                         //単純完全一致
66                         if(separated_UUID.length != p.length){
67                                 pList.removeByIndex(i);
68                                 i--;
69                                 iLen--;
70                         } else{
71                                 for(var j = 0, jLen = separated_UUID.length; j < jLen; j++){
72                                         if(p[j] != separated_UUID[j]){
73                                                 pList.removeByIndex(i);
74                                                 i--;
75                                                 iLen--;
76                                                 break;
77                                         }
78                                 }
79                         }
80                 }
81                 
82                 for(var i = 0, iLen = pList.length; i < iLen; i++){
83                         var p = pList[i];
84                         if(p.func){
85                                 p.func(this.env, separated, separated_UUID);
86                         }
87                 }
88                 
89                 return pList;
90         },
91 }