示例#1
0
def common_relations(concept_strs): 
  concepts = [Concept.get(concept_str,'en') for concept_str in concept_strs]

  set_relations = [set([ast.concept2.text for ast in concept.get_assertions_forward()[:20] \
    if ast.relation.name == "IsA"]) for concept in concepts]

  all_sets = []
  for set_relation in set_relations:
    concepts = [] 
    for concept in set_relation: 
      concepts += concept.split(" ")
    all_sets.append(set(concepts)) 

  # common = set.intersection(*all_sets)
  count_relations = Counter(set_to_all_list(set_relations))

  print count_relations
  commonality = count_relations.most_common(1)[0][1]/float(len(set_relations))
  print "attr, common values: ", count_relations.most_common(1)[0][0], ", ", commonality

  # If noun concepts worked reliability in NLTK: 
  # noun_concepts = [set([getNoun(concept) for concept in set_relation]) for set_relation in set_relations]
  # print noun_concepts

  return commonality
示例#2
0
def doBFS(inpC1str,inpC2str,q): 
	# Convert strings to concepts in ConceptNet 
	inpC1 = Concept.get(inpC1str,'en')
	inpC2 = Concept.get(inpC2str,'en')
	exp = [inpC1]
	print "added inpc1 to exp"
	q += [exp]
	print "exp enqueued"
	while q!=[]: 
		tmp = q[0]
		q = q[1:]
		last_node = tmp[len(tmp)-1]
		print tmp 
		# print "last node ", last_node
		# print "lasr node text", last_node.text
		# print "inp2 text", inpC2.text
		if last_node.text == inpC2.text or last_node.text in inpC2.text: 
			print "valid path done, found ", tmp 
			print "length of path is ", len(tmp)-1 
			return len(tmp[1:])

		for ast in last_node.get_assertions()[:10]:
			print "assertion is ", ast
			print "corresponding relation name is ", ast.relation.name
			searchC21 = Concept.get(ast.concept1.text, 'en')
			searchC22 = Concept.get(ast.concept2.text, 'en')
			if searchC21.text == last_node.text: 
				searchC2 = searchC22 
			else: 
				searchC2 = searchC21 
			# print "searchc2 is ", searchC2
			if searchC2 not in tmp: 
				# print "searched c2"
				new = []
				new = tmp + [searchC2]
				q += [new]
示例#3
0
def common_noun_relations(concept_strs): 
  concepts = [Concept.get(concept_str,'en') for concept_str in concept_strs]
  print concepts 

  set_relations = [set([ast.concept2.text for ast in concept.get_assertions_forward()[:20] \
    if ast.relation.name == "IsA"]) for concept in concepts]
  print set_relations

  all_nouns = []
  for set_relation in set_relations:
    noun_concepts = [] 
    for concept in set_relation: 
      noun_concept = getNoun(concept)
      if noun_concept != []:
        noun_concepts += noun_concept
    all_nouns += [noun_concepts]
  print all_nouns  
示例#4
0
def denormalize(concept_text):
    '''
    Returns the canonical denormalized (user-visible) form of a
    concept, given its normalized text of a concept.
    '''
    from conceptnet.models import Concept

    if isinstance(concept_text, tuple):
        text, lang = concept_text
    else:
        text, lang = concept_text, 'en'
    try:
        concept = Concept.get_raw(text, lang)
        result = concept.canonical_name.lower()
    except Concept.DoesNotExist:
        result = text
    if lang != 'en': return '%s [%s]' % (result, lang)
    else: return result
示例#5
0
def denormalize(concept_text):
    '''
    Returns the canonical denormalized (user-visible) form of a
    concept, given its normalized text of a concept.
    '''
    from conceptnet.models import Concept

    if isinstance(concept_text, tuple):
        text, lang = concept_text
    else:
        text, lang = concept_text, 'en'
    try:
        concept = Concept.get_raw(text, lang)
        result = concept.canonical_name.lower()
    except Concept.DoesNotExist:
        result = text
    if lang != 'en': return '%s [%s]' % (result, lang)
    else: return result
示例#6
0
import sys
from Queue import PriorityQueue

from conceptnet.models import Concept


# Take input from user for quick searches
sys.stderr.write("\x1b[2J\x1b[H")
for arg in sys.argv[1:]: 
	argConcept = Concept.get(arg,'en')
	print "concept ", arg
	for ass in argConcept.get_assertions(): 
		print ass 
	print len(argConcept.get_assertions())

# Testing
testC1 = Concept.get('bond', 'en')
for ass in testC1.get_assertions():
	print ass
print len(testC1.get_assertions())
print "------------------------------"
testC2 = Concept.get('bark', 'en')
for ass in testC2.get_assertions()[:10]:
	print ass

# do BFS 
def doBFS(inpC1str,inpC2str,q): 
	# Convert strings to concepts in ConceptNet 
	inpC1 = Concept.get(inpC1str,'en')
	inpC2 = Concept.get(inpC2str,'en')
示例#7
0
def concept_lookup(concept, lang):
    return Concept.get_raw(concept, lang)
示例#8
0
def getConcept(arg, lang='en'):
  try:
    argConcept = Concept.get(arg,'en')
  except:
    argConcept = None 
  return argConcept
示例#9
0
from conceptnet.models import Concept
dog = Concept.get('dog', 'en')
for fwd in dog.get_assertions_forward()[:20]:
    print fwd

示例#10
0
from conceptnet.models import Concept
import os

objectsList = [
    "cylinder"
]  #["office","room","corridor","toilet","door","window","exit","table","chair","fire extinguisher","socket","power","printer","emergency exit","closet","book","chair","bench","trash bin","plant","hydrant","printer","professor"]

if (os.path.exists("conceptnetPropertyList.txt")):
    os.remove("conceptnetPropertyList.txt")

file_name = open('conceptnetPropertyList.txt', 'a')

for i in xrange(len(objectsList)):
    print "retrieving predicates of %s from concpetNet" % objectsList[i]
    file_name.write("#### OBJECT %s: %s\n" % (i + 1, objectsList[i]))
    try:
        obj = Concept.get('%s' % objectsList[i], 'en')
        for fwd in obj.get_assertions():
            #if "Property" in "%s"%fwd:
            file_name.write("%s\n" % fwd)

        file_name.write("\n")
    except:
        print "could not retrieve anything for %s from conceptNet" % objectsList[
            i]
from conceptnet.models import Concept
import os

objectsList = ["cylinder"]#["office","room","corridor","toilet","door","window","exit","table","chair","fire extinguisher","socket","power","printer","emergency exit","closet","book","chair","bench","trash bin","plant","hydrant","printer","professor"]

if(os.path.exists("conceptnetPropertyList.txt")):
	os.remove("conceptnetPropertyList.txt")
	
file_name = open('conceptnetPropertyList.txt', 'a')

for i in xrange(len(objectsList)):  
  print "retrieving predicates of %s from concpetNet"%objectsList[i]
  file_name.write("#### OBJECT %s: %s\n"%(i+1,objectsList[i]))
  try:
    obj = Concept.get('%s'%objectsList[i], 'en')
    for fwd in obj.get_assertions():
      #if "Property" in "%s"%fwd:
      	file_name.write("%s\n"%fwd)

    file_name.write("\n")
  except:
    print "could not retrieve anything for %s from conceptNet"%objectsList[i]
            self.reload()

        print "Loading Tensor"
        fl = file(self.file_name, 'rb')
        tensor = pickle.load(fl)
        fl.close()
        return tensor





if __name__ == '__main__':
    #--------Some example concepts to use
    #print "Getting test concepts"
    dog = Concept.get('dog', 'en').text
    leash = Concept.get('leash', 'en').text
    umb = Concept.get('umbrella', 'en').text
    cat = Concept.get('cat', 'en').text
    #pets = [dog, cat]

    #temple = Concept.get('temple', 'en').text
   # hindu = Concept.get('hindu', 'en').text
   # asia = Concept.get('asia', 'en').text
   # nepal = Concept.get('Nepal', 'en').text
   # spain = Concept.get('Spain', 'en').text
   #test = [temple, asia, nepal]

    #----------Load Tensor
    tensor = LoadTensor().load()
示例#13
0
def concept_lookup(concept, lang):
    return Concept.get_raw(concept, lang)
示例#14
0
        #Checks to see if the pickle file already exists
        if (os.path.exists(self.file_name) == False):
            #If not, reloads pickle
            self.reload()

        print "Loading Tensor"
        fl = file(self.file_name, 'rb')
        tensor = pickle.load(fl)
        fl.close()
        return tensor


if __name__ == '__main__':
    #--------Some example concepts to use
    #print "Getting test concepts"
    dog = Concept.get('dog', 'en').text
    leash = Concept.get('leash', 'en').text
    umb = Concept.get('umbrella', 'en').text
    cat = Concept.get('cat', 'en').text
    #pets = [dog, cat]

    #temple = Concept.get('temple', 'en').text
    # hindu = Concept.get('hindu', 'en').text
    # asia = Concept.get('asia', 'en').text
    # nepal = Concept.get('Nepal', 'en').text
    # spain = Concept.get('Spain', 'en').text
    #test = [temple, asia, nepal]

    #----------Load Tensor
    tensor = LoadTensor().load()