def bayes_filter(pri_prob, tr_wd_list, total_wd_list, te_set_files): sort_num = {} rpt = {} for iter_basename in te_set_files: for iter_file in te_set_files[iter_basename]: wd_list = TextFilter.get_wd_list(iter_file) _count = [] num = 0 for iter_sort in tr_wd_list: sort_num.setdefault(num, iter_sort) num += 1 wd_count = len(tr_wd_list[iter_sort]) _count_10 = 0 for wd in wd_list.keys(): if wd in tr_wd_list[iter_sort]: nij = tr_wd_list[iter_sort][wd] for iter_ in range(wd_list[wd]): _p = pri_prob[iter_sort] * (nij + 1) * 1.0 / ( wd_count + len(total_wd_list)) while _p < 1: _p *= 10 _count_10 += 1 _count.append(_count_10) print sort_num[_count.index(max(_count))].decode('gb2312') Log.log(iter_file.decode('gb2312')) rpt.setdefault( iter_file.decode('gb2312'), sort_num[_count.index(max(_count))].decode('gb2312')) Log.log(_count) return rpt
def _load_config(self): cfg_path = os.path.abspath('./bayes.cfg') if os.path.exists(cfg_path): f = open(cfg_path) settings = f.readlines() train_set_dir = re.match(re.compile(r'train-set-dir = (.+?) #'), settings[0]).group(1) test_set_dir = re.match(re.compile(r'test-set-dir = (.+?) #'), settings[1]).group(1) self._tr_set_dir = os.path.abspath(train_set_dir) self._te_set_dir = os.path.abspath(test_set_dir) f.close() else: self._tr_set_dir = os.path.abspath('./train_set') self._te_set_dir = os.path.abspath('./test_set') Log.log('Retrieved set path.')
def _set_attr(self, _attr, _attr_name): if _attr_name is 'train': total = 0 for _sub_attr in _attr: total += len(_attr[_sub_attr]) self._tr_set_files = _attr self._total_files_num = total Log.log('Retrieved train set files.') elif _attr_name is 'test': self._te_set_files = _attr Log.log('Retrieved test set files.') elif _attr_name is 'prob': self._prior_prob = _attr Log.log('Solved prior probability.') elif _attr_name is 'tr_wd_list': self._tr_wd_list = _attr Log.log('Retrieved train word lists.') elif _attr_name is 'rpt': self.rpt = _attr Log.log('Text-sort succeed.')
class TeddyBear: def __init__(self): #100 ticks = (100*0.1) = 10 secs self.cooldownMotionMin = 0 self.cooldownMotionMax = 500 self.cooldownAccelMin = 0 self.cooldownAccelMax = 500 self.cooldownSoundMin = 0 self.cooldownSoundMax = 500 self.cooldownBarkMin = 0 self.cooldownBarkMax = 50 self.tapDetected = False self.motionDetected = False self.soundHighVal = 0 self.barking = False self.processSound = None sound.init() accel.init() motion.init() self.soundLogger = Logger(SensorNames.SOUND) self.motionLogger = Logger(SensorNames.MOTION) self.accelLogger = Logger(SensorNames.ACCELEROMETER) time.sleep(2) def bark(self): self.processSound = subprocess.Popen( ["aplay /home/pi/Desktop/smartteddy/bark3.wav"], shell=True) def monitor(self): while True: try: if not self.motionDetected and self.cooldownMotionMin == 0: if motion.inRange(): self.motionLogger.log('{};{}'.format( str( datetime.datetime.fromtimestamp( time.time()).strftime('%H:%M')), 1)) self.motionDetected = True self.cooldownMotionMin = self.cooldownMotionMax isTapped = True if accel.isTapped() else False if not self.tapDetected and self.cooldownAccelMin == 0: if isTapped: self.accelLogger.log('{};{}'.format( str( datetime.datetime.fromtimestamp( time.time()).strftime('%H:%M')), 2)) self.tapDetected = True self.cooldownAccelMin = self.cooldownAccelMax if isTapped: self.cooldownBarkMin = self.cooldownBarkMax if self.soundHighVal == 0: self.cooldownSoundMin = self.cooldownSoundMax tmpSoundLevel = sound.getSoundLevel() tmpSoundLevel = 2000 if tmpSoundLevel > 1200 else tmpSoundLevel if self.soundHighVal < tmpSoundLevel: self.soundHighVal = tmpSoundLevel if self.motionDetected and self.cooldownMotionMin >= 0 and self.cooldownMotionMin <= self.cooldownMotionMax: self.cooldownMotionMin = self.cooldownMotionMin - 1 if self.cooldownMotionMin == 0: self.motionDetected = False if self.tapDetected and self.cooldownAccelMin >= 0 and self.cooldownAccelMin <= self.cooldownAccelMax: if self.processSound != None and self.barking == True: self.processSound.terminate() self.barking = False self.cooldownAccelMin = self.cooldownAccelMin - 1 if self.cooldownAccelMin == 0: self.tapDetected = False if self.cooldownBarkMin >= 0 and self.cooldownBarkMin <= self.cooldownBarkMax: self.cooldownBarkMin = self.cooldownBarkMin - 1 print self.cooldownBarkMin if self.cooldownBarkMin > 0: if tmpSoundLevel > 700: self.barking = True self.bark() time.sleep(1) if self.cooldownSoundMin >= 0 and self.cooldownSoundMin <= self.cooldownSoundMax: self.cooldownSoundMin = self.cooldownSoundMin - 1 if self.cooldownSoundMin == 0: self.soundLogger.log('{};{}'.format( str( datetime.datetime.fromtimestamp( time.time()).strftime('%H:%M')), self.soundHighVal)) self.soundHighVal = 0 except IOError: print("Error") time.sleep(0.1)
# s = ''.join(jieba.analyse.textrank(all_text)) # for kw in jieba.analyse.textrank(all_text, topK=5): # print kw # a = [] # a.append(jieba.analyse.textrank(all_text, topK=5)) # a.append(jieba.analyse.textrank(bll_text, topK=3)) # # s = '' # for i in a: # s += ' '.join(i) # s += ' ' # print s set_files = tf.get_sets(os.path.abspath('./train_set')) tmp = {} Log.log('From sort: Military:') Log.log('Total size:', len(set_files['Military'])) for iter_file in set_files['Military']: with open(iter_file) as f: all_text = f.read() for kw in jieba.analyse.textrank(all_text, topK=50, allowPOS=('ns', 'n')): if kw not in tmp: tmp[kw] = 0 tmp[kw] += 1 kw_dic = collections.OrderedDict(sorted((copy.deepcopy(tmp).items()), key=lambda t: -t[-1])) iter_ = 0 for kw in kw_dic: iter_ += 1