Initial Commit
[Projects.git] / ai / ai.py
blob881d056b16ac7d615034444768fa393857f54543
1 import utils.file, random, grammar
2 import random
4 class ai:
5 history = []
6 def __init__(self,file,grammar=grammar):
7 self.file=file
8 self.grammar=grammar
9 def input(self,data):
10 file = utils.file.read(self.file)
11 d = file.split(self.grammar.line)
12 d.append(gramar.time.join(self.history) + self.grammar.att + data)
13 utils.file.write(self.file, self.grammar.line.join(d))
14 def output(self, options):
15 data = utils.file.read(self.file)
16 top = False
17 opts = []
18 for option in options:
19 n = self.find(data, option)
20 if n > top:
21 opts = []
22 opts.append(option)
23 top = n
24 elif n == top:
25 opts.append(option)
26 ret = opts[random.randint(0, len(opts)-1)]
27 self.history.append(ret)
28 return ret
29 def find(self,data,att):
30 score = 0
31 for line in data.split(self.grammar.line):
32 if line.split(self.grammar.att)[0] == att:
33 if line.split(self.grammar.att)[1] == self.grammar.good:
34 score += +1
35 elif line.split(self.grammar.att)[1] == self.grammar.bad:
36 score += -1
37 else:
38 score += self.find(data, line.split(self.grammar.att)[1])
39 return score