Skip to content

Commit

Permalink
fixed errors in graph.get_or_create_assertion, added test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Speer committed Sep 30, 2011
1 parent 4355a10 commit c2411d9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
19 changes: 15 additions & 4 deletions conceptnet5/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ def _list_nodes_and_uris(self, input_list):
uris = []
nodes = []
for index, node_uri in enumerate(input_list):
if isinstance(node_uri,Node):
if isinstance(node_uri, Node):
uris.append(node_uri['uri'])
nodes.append(node_uri)
elif uri_is_safe(node_uri):
elif isinstance(node_uri, basestring):
uris.append(node_uri)
nodes.append(self.get_or_create_node(node_uri))
else:
Expand Down Expand Up @@ -219,6 +219,16 @@ def get_node(self, uri):
else:
assert False, "Got multiple results for URI %r" % uri

def find_nodes(self, pattern):
"""
Search for all nodes whose URIs match a given wildcard pattern,
using Lucene's wildcard syntax. Returns an iterator of the results.
See this document for Lucene's syntax:
http://lucene.apache.org/java/2_0_0/queryparsersyntax.html
"""
return self._node_index.query('uri', pattern)

def get_edges(self, source, target):
"""
Get edges between `source` and `target`, specified as IDs or nodes.
Expand Down Expand Up @@ -325,8 +335,9 @@ def get_or_create_assertion(self, relation, args, properties = {}):
if index == 0: invalid = 'relation'
else: invalid = 'argument ' + str(index)
raise TypeError("%s is an invalid type. " % invalid)
uri = self.make_assertion_uri(self, uris[0],uris[1:])
return self.get_node(uri) or self._create_assertion_from_components(self, uri, nodes[0],nodes[1:], properties)

uri = self.make_assertion_uri(uris[0], uris[1:])
return self.get_node(uri) or self._create_assertion_from_components(uri, nodes[0],nodes[1:], properties)

def get_or_create_conjunction(self, conjuncts):
conjuncts = [self._any_to_node(c) for c in conjuncts]
Expand Down
2 changes: 1 addition & 1 deletion pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ load-plugins=
# can either give multiple identifier separated by comma (,) or put this option
# multiple time (only on the command line, not in the configuration file where
# it should appear only once).
#disable=
disable=W0142


[REPORTS]
Expand Down
3 changes: 3 additions & 0 deletions test/test_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ def test_create_assertions_twice():
g = ConceptNetGraph('http://tortoise.csc.media.mit.edu/db/data')
a1 = g.get_or_create_node(u"/assertion/_/relation/IsA/_/concept/en/dog/_/concept/en/animal")
assert a1 == g.get_or_create_node(u"/assertion/_/relation/IsA/_/concept/en/dog/_/concept/en/animal")
assert a1 == g.get_or_create_assertion(u'/relation/IsA',
[u'/concept/en/dog', u'/concept/en/animal']
)

a2 = g.get_or_create_node(u"/assertion/_/relation/UsedFor/_/concept/zh_TW/枕頭/_/concept/zh_TW/睡覺")
assert a2 == g.get_or_create_node(u"/assertion/_/relation/UsedFor/_/concept/zh_TW/枕頭/_/concept/zh_TW/睡覺")
Expand Down

0 comments on commit c2411d9

Please sign in to comment.