def solve_np(G, words, pos):
    #print "solving np: " + " ".join(words) if verbose else "",
    nouns = []
    adjs = []
    atts = []
    vrbs = []
    objs = []
    nums = []
    for i in range(len(words)):
        if pos[i] == 'noun':
            if len(nouns) == 0:
                nouns.append(words[i])
            else:
                adjs.append(words[i])
        elif pos[i] == 'adj':
            adjs.append(words[i])
        elif pos[i] == 'att':
            atts.append(words[i])
        elif pos[i] == 'vrb':
            vrbs.append(words[i])
        elif pos[i] == 'number':
            nums.append(words[i])
        elif pos[i] == 'idf_pro':
            nouns.append('stuff')
    print 'nouns: ', nouns, '   adjs: ', adjs, '   vrbs: ', vrbs, '   atts: ', atts
    # collect all objects of class
    if len(nouns) > 0:
        obj_candidates = kb_services.all_objects(G, nouns[0])
        print 'candidate objects: ', obj_candidates
        # filter objects that has correct properties
        if len(adjs) > 0:
            for each_obj in obj_candidates:
                if kb_services.verify_satisfability_of_objclss(
                        G, each_obj, adjs):
                    objs.append(each_obj)
        # filter objects that are object of action verb
        elif len(vrbs) > 0:
            for each_obj in obj_candidates:
                obj_actions = kb_services.get_attribute(
                    G, each_obj, 'is_object_of_action')
                if "is_object_of_action" in obj_actions and vrbs[
                        0] in obj_actions["is_object_of_action"]:
                    objs.append(each_obj)

    return nouns[0], objs
def solve_np(G, words, pos):
	print "solving np: " + " ".join(words) if verbose else "",
	nouns = []
	adjs = []
	atts = []
	vrbs = []
	objs = []
	nums = []
	for i in range(len(words)):
		if pos[i] == 'noun':
			if  len(nouns) == 0:
				nouns.append(words[i])
			else:
				adjs.append(words[i])
		elif pos[i] == 'adj':
			adjs.append(words[i])
		elif pos[i] == 'att':
			atts.append(words[i])
		elif pos[i] == 'vrb':
			vrbs.append(words[i])
		elif pos[i] == 'number':
			nums.append(words[i])
		elif pos[i] == 'idf_pro':
			nouns.append('stuff')
	print 'nouns: ' , nouns , '   adjs: ' , adjs , '   vrbs: ' , vrbs , '   atts: ' , atts 
	# collect all objects of class
	if len(nouns) > 0:
		obj_candidates = kb_services.all_objects(G, nouns[0])
		print 'candidate objects: ', obj_candidates
		# filter objects that has correct properties
		if len(adjs) > 0:
			for each_obj in obj_candidates:
				if kb_services.verify_satisfability_of_objclss(G, each_obj, adjs):
					objs.append(each_obj)
		# filter objects that are object of action verb
		elif len(vrbs) > 0:
			for each_obj in obj_candidates:
				obj_actions = kb_services.get_attribute(G, each_obj, 'is_object_of_action')
				if "is_object_of_action" in obj_actions and vrbs[0] in obj_actions["is_object_of_action"]:
					objs.append(each_obj)		
		
	return nouns[0], objs
def load_facts_file_to_clips():
	G = kb_services.load_semantic_network()
	# get all nouns
	nouns = kb_services.all_subclasses(G, 'stuff') + kb_services.all_objects(G, 'stuff')
	facts_to_load = []
	for each_noun in nouns:
		att_dict = kb_services.get_attribute(G, each_noun, 'attribute')
		#print att_dict
		for each_attribute in att_dict:
			if each_attribute == 'nop':
				facts_to_load.append("(fact " + each_noun + " " + each_attribute + " " + att_dict[each_attribute][0] + ")")

			else:
				for each_value in att_dict[each_attribute]:
					#print "(fact " + each_noun + " " + each_attribute + " " + each_value + ")"
					facts_to_load.append("(fact " + each_noun + " " + each_attribute + " " + each_value + ")")

	for each_fact in facts_to_load:
		fi = clips.Assert(each_fact)
	fi = clips.Assert("(id_count 1)")
def solve_simple_np(G, words, pos):
	print "solving np: " + " ".join(words) if verbose else "",
	

	if "rel_pro" in pos:
		print "ENUNCIADO CON NP COMPLEJA, RECHUNKEANDO..."
		words = words[0:pos.index("rel_pro")] + words[pos.index("rel_pro")+1:len(words)]
		pos = pos[0:pos.index("rel_pro")] + pos[pos.index("rel_pro")+1:len(pos)]
		np_complex_postchunk = parsing.constituent_chunker(parsing.grammar_np_simple, words, pos)
		
		print "NP COMPLEJA chunked pos: ", " ".join(np_complex_postchunk[0]) 
		print "NP COMPLEJA chunked words: ", " ".join(np_complex_postchunk[1]) 
		print "NP COMPLEJA noun phrases: ", np_complex_postchunk[2] 
		sol_com_np = []
		for each_np in np_complex_postchunk[2]:
			sol_com_np.append(solve_simple_np(G, each_np[0], each_np[1]))
		
		print "NP COMPLEJA sub NPs aterrizados: ", sol_com_np

		adjs = []
		complex_noun = []
		for i in range(len(np_complex_postchunk[0])):
			if np_complex_postchunk[0][i] == 'adj' or np_complex_postchunk[0][i] == 'att':
				adjs.append(np_complex_postchunk[1][i])
			elif np_complex_postchunk[0][i] == 'noun':
				if re.match('NOUN_PHRASE_[0-9]+', np_complex_postchunk[1][i]):
					current_np = np_complex_postchunk[2].pop(0)
					complex_noun.append(solve_simple_np(G, current_np[0], current_np[1]).pop())
				else:
					complex_noun.append("(type: " + np_complex_postchunk[1][i] + ")")

		print "NP COMPLEJA: ATTTTENCION", complex_noun
		conformed_np = complex_noun[0] + "(spatial relation: (relation: " + adjs[0] + " ) (entity: " + complex_noun[1] + "))" 
		print "OOOORALEEEEE   ", conformed_np
		return [conformed_np]


	nouns = []
	adjs = []
	atts = []
	vrbs = []
	objs = []
	nums = []
	for i in range(len(words)):
		if pos[i] == 'noun':
			if  len(nouns) == 0:
				nouns.append(words[i])
			else:
				adjs.append(words[i])
		elif pos[i] == 'adj':
			adjs.append(words[i])
		elif pos[i] == 'att' or pos[i] == 'prep_loc':
			atts.append(words[i])
		elif pos[i] == 'vrb':
			vrbs.append(words[i])
		elif pos[i] == 'number':
			nums.append(words[i])
		elif pos[i] == 'idf_pro':
			nouns.append('stuff')
	print 'nouns: ' , nouns , '   adjs: ' , adjs , '   vrbs: ' , vrbs , '   atts: ' , atts 
	# collect all objects of class
	if len(nouns) > 0:
		obj_candidates = kb_services.all_objects(G, nouns[0])
		#print 'candidate objects: ', obj_candidates
		# filter objects that has correct properties
		if len(adjs) > 0:
			for each_obj in obj_candidates:
				if kb_services.verify_satisfability_of_objclss(G, each_obj, adjs):
					objs.append(each_obj)
		# filter objects that are object of action verb
		elif len(vrbs) > 0:
			for each_obj in obj_candidates:
				obj_actions = kb_services.get_attribute(G, each_obj, 'is_object_of_action')
				if "is_object_of_action" in obj_actions and vrbs[0] in obj_actions["is_object_of_action"]:
					objs.append(each_obj)
					
		else:
			objs = obj_candidates[:]
	print "solved np:" if verbose else "",
	print objs if verbose else ""
	if objs == [] and ((len(nouns)> 0) or len(atts)>0):
		# generate expression for lazy evaluation in planner
		if len(nouns)> 0:
			exp = "(type: " + nouns[0] + ") "
		else: 
			exp = ""
		exp_adj = ""
		if nums != []:
			for each in nums:
				exp_adj += "(cardinal: " + each + ") "

		if adjs != []:
			for each in adjs:
				exp_adj += "(" + kb_services.all_superclasses(G, each)[0] + ": " + each + ") "
		if atts != []:
			for each in atts:
				exp_adj += "(" + kb_services.all_superclasses(G, each)[0] + ": " + each + ") "
		objs = [exp_adj + exp]
	return objs