Beispiel #1
0
	def find_var_opts_for_rules(self, goal_phrase, l_cat_names, l_rule_names, idb, var_obj_parent, calc_level):
		print('find_rules_matching_result rules for', goal_phrase)
		rphrase = self.__phrase_mgr.get_rphrase(goal_phrase)
		l_rperms = self.__phraseperms.get_perms(rphrase)
		num_poss_ret = len(self.__l_active_rules) * len(l_rperms)
		irule_arr = bitvecdb.intArray(num_poss_ret); rperms_ret_arr = bitvecdb.intArray(num_poss_ret)
		num_vars_ret_arr = bitvecdb.intArray(num_poss_ret)
		rperms_arr = utils.convert_intvec_to_arr(l_rperms)
		cat_arr, rid_arr, num_cats, num_rids  = 0, 0, 0, 0
		l_rcats, l_rids = [], []
		for cat_name in l_cat_names:
			cid = self.__d_rcats.get(cat_name, -1)
			if cid >= 0: l_rcats.append(cid)
			num_cats = len(l_rcats)
		if num_cats > 0: cat_arr = utils.convert_intvec_to_arr(l_rcats)
		for rule_name in l_rule_names:
			rid = self.__d_rnames.get(rule_name, -1)
			if rid >= 0: l_rids.append(rid)
			num_rids = len(l_rids)
		# if num_rids > 0:
		rid_arr = utils.convert_intvec_to_arr(l_rids)
		# bitvecdb.print_db_recs(self.__hcdb_rules, self.__el_bitvec_mgr.get_hcbdb())
		num_rules_found = bitvecdb.find_matching_rules_vo(	self.__hcdb_rules, self.__phraseperms.get_bdb_all_hcdb(),
															self.__el_bitvec_mgr.get_hcbdb(), irule_arr,
															num_vars_ret_arr, rperms_ret_arr, len(l_rperms),
															rperms_arr, num_cats, cat_arr, num_rids, rid_arr,
															False, -1)
		print('num_rules_found', num_rules_found)
		l_var_opt_objs = []
		for ifound in range(num_rules_found):
			l_var_opt_objs.append(self.find_var_opts(	idb, irule_arr[ifound], num_vars_ret_arr[ifound],
														rperms_ret_arr[ifound], var_obj_parent, calc_level))
		return l_var_opt_objs
Beispiel #2
0
	def test_rule(self, mpdbs, stmt, l_results, idb, l_rule_cats):
		phrase = utils.full_split(stmt)
		result_words = ''
		if l_results != []:
			result_words = ' '.join(utils.full_split(utils.convert_phrase_to_word_list(l_results[0][1:]))).lower()
		print('Testing rules for', phrase)
		rphrase = self.__phrase_mgr.get_rphrase(phrase)
		l_rperms = self.__phraseperms.get_perms(rphrase)
		# The maximum theoretical returns is the num of rules * the number of source perms
		num_poss_ret = len(self.__l_active_rules) * len(l_rperms)
		irule_arr = bitvecdb.intArray(num_poss_ret); rperms_ret_arr = bitvecdb.intArray(num_poss_ret)
		rperms_arr = utils.convert_intvec_to_arr(l_rperms)
		l_rule_cids = [self.get_cid(rule_cat) for rule_cat in l_rule_cats]
		num_vars_ret_arr = bitvecdb.intArray(num_poss_ret)
		# num_rules_found = bitvecdb.find_matching_rules(	self.__hcdb_rules, self.__phraseperms.get_bdb_all_hcdb(),
		# 												irule_arr, rperms_ret_arr, len(l_rperms), rperms_arr,
		# 												len(l_rule_cids), utils.convert_intvec_to_arr(l_rule_cids))
		num_rules_matched = 0
		num_rules_found = bitvecdb.find_matching_rules_vo(	self.__hcdb_rules, self.__phraseperms.get_bdb_all_hcdb(),
															self.__el_bitvec_mgr.get_hcbdb(), irule_arr,
															num_vars_ret_arr, rperms_ret_arr, len(l_rperms),
															rperms_arr, len(l_rule_cids), utils.convert_intvec_to_arr(l_rule_cids),
															0, utils.convert_intvec_to_arr([]), False, 0)
		print('num_rules_found', num_rules_found)
		# l_var_opt_objs = []
		# for ifound in range(num_rules_found):
		# 	l_var_opt_objs.append(self.find_var_opts(	idb, irule_arr[ifound], num_vars_ret_arr[ifound],
		# 												rperms_ret_arr[ifound], var_obj_parent, calc_level))
		# return l_var_opt_objs
		for iret in range(num_rules_found):
			# self.run_one_rule(irule_arr[iret], rperms_ret_arr[iret])
			irule = irule_arr[iret]
			bext, iactive = self.__l_active_rules[irule]
			rperm_ret = rperms_ret_arr[iret]
			num_vars_ret = num_vars_ret_arr[iret]
			if not bext:
				# assert False, 'lrules should be run just like ext rules'
				print('Will run learned rule', iactive, 'as standard rule', irule)
				# self.__lrule_mgr.test_rule(irule, rperm_ret, result_words, mpdbs, idb)
				# continue
			print('should run rule called', self.__l_names[irule])
			b_has_result, num_matched, ll_result_eids = self.run_one_rule(irule, rperm_ret, result_words, mpdbs, idb, num_vars_ret)
			if num_matched < 1:
				print('rule', irule, 'did not match the state of the story db for idb', idb)
				continue
			print('test rule produced: ', ' '.join([self.__el_bitvec_mgr.get_el_by_eid(el) for el in ll_result_eids[0]]))
			print('test expected result:', result_words)
			num_rules_matched += 1


		if num_rules_matched > 0:
			self.__test_stat_num_rules_found += 1
		else:
			self.__test_stat_num_rules_not_found += 1
		pass
Beispiel #3
0
 def get_close_recs(self, idb, plen, hd_thresh, l_qbits):
     num_ret, len_arr = self.get_plen_irecs(idb, plen)
     ret_arr = bitvecdb.intArray(num_ret)
     num_ret = bitvecdb.get_thresh_recs_by_list(
         self.__hcbdb, ret_arr, plen,
         utils.convert_intvec_to_arr(hd_thresh), len_arr, num_ret,
         utils.convert_charvec_to_arr(l_qbits))
     return num_ret, ret_arr
Beispiel #4
0
	def write_crec(self, irule, b_change_not_add = False):
		phrase_data, ll_vars, ll_el_hds = self.__lll_phrase_data[irule], self.__lll_vars[irule], self.__lll_el_hds[irule]
		phrase_bitvec = []; plen = 0; phrase_offsets = []; l_hds = []; phrase_offset = 0
		l_add_notifies = []
		for phrase in phrase_data:
			self.__phrase_mgr.add_phrase(phrase)
		for iphrase, (phrase, l_el_hds) in enumerate(zip(phrase_data, ll_el_hds)):
			for iel, (el, hd) in enumerate(zip(phrase, l_el_hds)):
				rel = self.__el_bitvec_mgr.get_el_id(el)
				assert rel != -1, 'Error! External rules at this point should have registered all their names.'
				if not b_change_not_add:
					l_add_notifies.append(rel)
					# s_irules = self.__d_iel_to_l_irules.get(rel, set())
					# s_irules.add(irule)
					# self.__d_iel_to_l_irules[rel] = s_irules
					# self.__nlb_mgr_notifier.notify_on_iel(rel)
				el_bitvec = self.__el_bitvec_mgr.get_bin_by_id(rel)
				assert el_bitvec.count(1) > 0, 'Any external rule may only use one word not found in samples in a rule'
				phrase_bitvec += el_bitvec
				plen += 1
				l_hds.append(hd) # (int(((1.0 - cd)*self.__bitvec_size)))
			phrase_offsets.append(phrase_offset)
			phrase_offset += len(phrase)
		if b_change_not_add:
			bitvecdb.change_rec(self.__hcdb_rules, plen, convert_charvec_to_arr(phrase_bitvec),
								self.__d_irule_to_iactive[irule])
			return
		print('adding rec plen', plen, 'len', len(phrase_bitvec), '. bits', phrase_bitvec)
		bitvecdb.add_rec(self.__hcdb_rules, plen, convert_charvec_to_arr(phrase_bitvec))
		self.__d_irule_to_iactive[irule] = len(self.__l_active_rules)
		print('phrase_offsets', phrase_offsets)
		bitvecdb.set_rule_data(	self.__hcdb_rules, len(self.__l_active_rules), len(phrase_offsets),
									utils.convert_intvec_to_arr(phrase_offsets),
									utils.convert_intvec_to_arr(l_hds), len(ll_vars),
									utils.convert_intvec_to_arr([vv for var_def in ll_vars for vv in var_def]),
									int(self.__l_bresults[irule]),
									self.__d_rcats[self.__l_categories[irule]], self.__d_rnames[self.__l_names[irule]],
									True)

		for rel in l_add_notifies:
			s_irules = self.__d_iel_to_l_irules.get(rel, set())
			s_irules.add(irule)
			self.__d_iel_to_l_irules[rel] = s_irules
			self.__nlb_mgr_notifier.notify_on_iel(rel)

		pass
Beispiel #5
0
 def get_rec_rule_names(self, nd_cent, hd_thresh, plen, num_recs,
                        l_rule_names):
     iperm_arr = bitvecdb.intArray(num_recs)
     cent_arr = utils.convert_charvec_to_arr(nd_cent.tolist())
     hd_arr = utils.convert_intvec_to_arr(hd_thresh)
     num_ret = bitvecdb.get_cluster(self.__hcbdb, iperm_arr, num_recs,
                                    cent_arr, plen, hd_arr)
     l_ret = [
         l_rule_names[self.__l_phrase_rphrases[self.__l_perm_iphrase[
             iperm_arr[iperm]]]] for iperm in range(num_ret)
     ]
     return l_ret
Beispiel #6
0
	def process_clusters(self):
		print('Cluster closest els:')
		for plen, l_cent_hd_thresh in reversed(list(enumerate(self.__ll_cent_hd_thresh))):
			for i_lencent, hd_thresh in enumerate(l_cent_hd_thresh):
				irec = len(self.__ll_centroids)
				close_phrase = []
				for iel in range(plen):
					# print('iel', iel, 'plen:', plen, 'i_lencent', i_lencent, 'len', len(self.__l_nd_centroids[plen][i_lencent])) # , 'bits:', self.__l_nd_centroids[plen][i_lencent])
					word = self.__nlb_mgr.dbg_closest_word(self.__l_nd_centroids[plen][i_lencent][iel*self.__bitvec_size:(iel+1)*self.__bitvec_size])
					close_phrase.append(word)
					# print(close_phrase)
				print('rcent:', irec, 'plen:', plen, ', hd:', hd_thresh, ',', close_phrase)
				self.__ll_centroids.append(np.reshape(self.__l_nd_centroids[plen][i_lencent], -1).tolist())
				self.__l_cent_hd.append(hd_thresh)
				bitvecdb.add_rec(self.__hcdb_cent, plen,
								 utils.convert_charvec_to_arr(self.__ll_centroids[-1]))
				hd_arr = utils.convert_intvec_to_arr(hd_thresh)
				bitvecdb.set_hd_thresh(self.__hcdb_cent, irec, hd_arr, len(hd_thresh))