Changeset 5879:a20ad47efc37 in orange
- Timestamp:
- 03/24/09 16:20:53 (4 years ago)
- Branch:
- default
- Convert:
- 2a793564ddaf4299b2e3da90b322bfc6c3890c54
- File:
-
- 1 edited
-
orange/OrangeCanvas/orngHistory.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
orange/OrangeCanvas/orngHistory.py
r5566 r5879 1 1 # Author: Miha Stajdohar (miha.stajdohar@fri.uni-lj.si) 2 # 2 # This module 3 3 4 4 import os, sys, time, smtplib … … 103 103 smtp.close() 104 104 105 def cmpBySecondValue(x,y): 106 if x[1] > y[1]: 107 return -1 108 elif x[1] < y[1]: 109 return 1 110 else: 111 return 0 112 113 def historyFileToBasket(): 114 """Reads history file to a 'basket'. 115 116 Each element in a basket list contains a list of widgets in one session. 117 """ 118 fin = open(logFile, 'r') 119 120 basket = [] 121 for line in fin: 122 vals = line.split(',') 123 vals = [val.strip() for val in vals] 124 basket.append(vals) 125 126 fin.close() 127 return basket 128 129 def buildWidgetProbabilityTree(basket): 130 """Builds a widget probability 'tree' 131 132 Levels: 133 0 - probability of inserting a widget in empty canvas 134 1 - probability of inserting a widget after one widget 135 2 - probability of inserting a widget after two widgets 136 3 - probability of inserting a widget after three widgets 137 ... 138 """ 139 firstWidget = {} 140 for vals in basket: 141 firstWidget[vals[0]] = firstWidget[vals[0]] + 1 if vals[0] in firstWidget else 1 142 143 tree = {} 144 tree[0] = firstWidget 145 for i in range(1,10): 146 tree[i] = estimateWidgetProbability(basket, i) 147 148 return tree 149 150 def estimateWidgetProbability(basket, depth): 151 """Estimates the probability of inserting the widget after several (depth) widgets.""" 152 widgetProbs = {} 153 for widgets in basket: 154 if len(widgets) > depth: 155 for i in range(len(widgets) - depth): 156 c = '' 157 for j in range(i, i + depth + 1): 158 c += widgets[j] + ';' 159 c = c[:-1] 160 widgetProbs[c] = widgetProbs[c] + 1 if c in widgetProbs else 1 161 162 widgetProbs = widgetProbs.items() 163 c = 0 164 for l in widgetProbs: 165 c += l[1] 166 167 widgetProbs = [(widgets.split(';'),float(n)/c,n,c) for widgets, n in widgetProbs] 168 widgetProbs.sort(cmpBySecondValue) 169 return widgetProbs 170 171 def nextWidgetProbility(state, tree): 172 """Returns a list of candidate widgets and their probability. The list is sorted descending by probability.""" 173 predictions = [] 174 # calculate probabilities on levels in a tree up to the number of already inserted widgets 175 for i in range(1, len(state)+1): 176 predictions_tmp = [] 177 widgetCounts = tree[i] 178 count = 0 179 for widgets, p, c, n in widgetCounts: 180 181 if len(widgets) > i: 182 #print widgets[-2], state[-1] 183 flag = True 184 for j in range(i): 185 if widgets[-j-2] != state[-j-1]: 186 flag = False 187 188 if flag: 189 predictions_tmp.append((widgets, p, c, n)) 190 count += n 191 192 # compute the probability of next widget in current tree level 193 predictions_tmp = [(predictions_tmp[j][0][-1], float(predictions_tmp[j][2]) / count) for j in range(len(predictions_tmp))] 194 predictions.extend(predictions_tmp) 195 196 predictions.sort(cmpBySecondValue) 197 predictions_dict = set() 198 # remove double widget entries; leave the one with highest probability 199 todel = [] 200 for i in range(len(predictions)): 201 if predictions[i][0] in found_pred: 202 todel.append(i) 203 else: 204 found_pred.add(predictions[i][0]) 205 206 todel.sort() 207 for i in range(len(todel)): 208 del predictions[todel[len(todel) - i - 1]] 209 210 return predictions
Note: See TracChangeset
for help on using the changeset viewer.
