24 ''' |
24 ''' |
25 Constructor |
25 Constructor |
26 ''' |
26 ''' |
27 self.theoryModels = {} |
27 self.theoryModels = {} |
28 self.theoryDict = {} |
28 self.theoryDict = {} |
29 self.accessibleTheories = [] |
29 self.accessibleTheories = set([]) |
30 self.currentTheory = None |
30 self.currentTheory = None |
31 |
31 |
32 def init(self,depFile,dicts): |
32 def init(self,depFile,dicts): |
33 logger = logging.getLogger('TheoryModels') |
33 logger = logging.getLogger('TheoryModels') |
34 IS = open(depFile,'r') |
34 IS = open(depFile,'r') |
44 nameId = dicts.nameIdDict[name] |
44 nameId = dicts.nameIdDict[name] |
45 features = dicts.featureDict[nameId] |
45 features = dicts.featureDict[nameId] |
46 if not self.theoryDict.has_key(theory): |
46 if not self.theoryDict.has_key(theory): |
47 assert not theory == self.currentTheory |
47 assert not theory == self.currentTheory |
48 if not self.currentTheory == None: |
48 if not self.currentTheory == None: |
49 self.accessibleTheories.append(self.currentTheory) |
49 self.accessibleTheories.add(self.currentTheory) |
50 self.currentTheory = theory |
50 self.currentTheory = theory |
51 self.theoryDict[theory] = set([nameId]) |
51 self.theoryDict[theory] = set([nameId]) |
52 theoryModel = singleNBClassifier() |
52 theoryModel = singleNBClassifier() |
53 self.theoryModels[theory] = theoryModel |
53 self.theoryModels[theory] = theoryModel |
54 else: |
54 else: |
73 for a in self.accessibleTheories: |
73 for a in self.accessibleTheories: |
74 self.theoryModels[a].update(dicts.featureDict[nameId],a in usedtheories) |
74 self.theoryModels[a].update(dicts.featureDict[nameId],a in usedtheories) |
75 IS.close() |
75 IS.close() |
76 |
76 |
77 def overwrite(self,problemId,newDependencies,dicts): |
77 def overwrite(self,problemId,newDependencies,dicts): |
78 pass |
78 features = dicts.featureDict[problemId] |
|
79 unExpAccessibles = dicts.accessibleDict[problemId] |
|
80 accessibles = dicts.expand_accessibles(unExpAccessibles) |
|
81 accTheories = [] |
|
82 for x in accessibles: |
|
83 xArt = (dicts.idNameDict[x]).split('.')[0] |
|
84 accTheories.append(xArt) |
|
85 oldTheories = set([x.split('.')[0] for x in dicts.dependenciesDict[problemId]]) |
|
86 newTheories = set([x.split('.')[0] for x in newDependencies]) |
|
87 for a in self.accTheories: |
|
88 self.theoryModels[a].overwrite(features,a in oldTheories,a in newTheories) |
79 |
89 |
80 def delete(self): |
90 def delete(self): |
81 pass |
91 pass |
82 |
92 |
83 def update(self,problemId,dicts): |
93 def update(self,problemId,dicts): |
87 tmp = [dicts.idNameDict[x] for x in dicts.dependenciesDict[problemId]] |
97 tmp = [dicts.idNameDict[x] for x in dicts.dependenciesDict[problemId]] |
88 usedTheories = set([x.split('.')[0] for x in tmp]) |
98 usedTheories = set([x.split('.')[0] for x in tmp]) |
89 currentTheory = (dicts.idNameDict[problemId]).split('.')[0] |
99 currentTheory = (dicts.idNameDict[problemId]).split('.')[0] |
90 # Create new theory model, if there is a new theory |
100 # Create new theory model, if there is a new theory |
91 if not self.theoryDict.has_key(currentTheory): |
101 if not self.theoryDict.has_key(currentTheory): |
92 assert not currentTheory == self.currentTheory |
102 assert not currentTheory == self.currentTheory |
93 if not currentTheory == None: |
103 if not currentTheory == None: |
94 self.theoryDict[currentTheory] = [] |
104 self.theoryDict[currentTheory] = [] |
95 self.currentTheory = currentTheory |
105 self.currentTheory = currentTheory |
96 theoryModel = singleNBClassifier() |
106 theoryModel = singleNBClassifier() |
97 self.theoryModels[currentTheory] = theoryModel |
107 self.theoryModels[currentTheory] = theoryModel |
|
108 self.accessibleTheories.add(self.currentTheory) |
98 if not len(usedTheories) == 0: |
109 if not len(usedTheories) == 0: |
99 for a in self.accessibleTheories: |
110 for a in self.accessibleTheories: |
100 self.theoryModels[a].update(features,a in usedTheories) |
111 self.theoryModels[a].update(features,a in usedTheories) |
101 |
112 |
102 def predict(self,features,accessibles,dicts): |
113 def predict(self,features,accessibles,dicts): |