Example #1
0
def test_path_inequality():
    alice = Node(name="Alice")
    bob = Node(name="Bob")
    carol = Node(name="Carol")
    dave = Node(name="Dave")
    path_1 = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave)
    path_2 = Path(alice, "KNOWS", carol, Rev("KNOWS"), dave)
    assert path_1 != path_2
Example #2
0
def test_path_addition():
    alice = Node(name="Alice")
    bob = Node(name="Bob")
    carol = Node(name="Carol")
    assert alice + alice == Path(alice)
    assert alice + "KNOWS" == Path(alice, "KNOWS", None)
    assert alice + "KNOWS" + bob == Path(alice, "KNOWS", bob)
    assert alice + Rev("KNOWS") + bob == Path(alice, Rev("KNOWS"), bob)
    assert ((alice + "KNOWS" + bob) + (bob + "KNOWS" + carol) == Path(
        alice, "KNOWS", bob, "KNOWS", carol))
Example #3
0
def test_changing_type_of_unbound_rev_mirrors_to_pair_rel():
    rev = Rev("KNOWS")
    assert rev.pair is None
    rel = -rev
    assert rev.pair is rel
    assert rel.pair is rev
    assert rev.type == "KNOWS"
    assert rel.type == "KNOWS"
    rev.type = "LIKES"
    assert rev.type == "LIKES"
    assert rel.type == "LIKES"
def createRelationshipWithProperties():
    print("Start - Creating Relationships")
    # Authenticate the user using py2neo.authentication
    # Ensure that you change the password 'sumit' as per your database configuration.
    py2neo.authenticate("localhost:7474", "neo4j", "sumit")
    # Connect to Graph and get the instance of Graph
    graph = Graph("http://localhost:7474/db/data/")
    # Create Node with Properties
    amy = Node("FEMALE", name="Amy")
    # Create one more Node with Properties
    kristine = Node("FEMALE", name="Kristine")
    # Create one more Node with Properties
    sheryl = Node("FEMALE", name="Sheryl")

    #Define an Object of relationship which depicts the relationship between Amy and Kristine
    #We have also defined the properties of the relationship - "since=2005"
    #By Default the direction of relationships is left to right, i.e. the -->
    kristine_amy = Relationship(kristine, "FRIEND", amy, since=2005)

    #This relationship is exactly same as the earlier one but here we are using "Rev"
    #"py2neo.Rev = It is used to define the reverse relationship (<--) between given nodes
    amy_sheryl = Relationship(amy, Rev("FRIEND"), sheryl, since=2001)

    #Finally use graph Object and Create Nodes and Relationship
    #When we create Relationship between, then Nodes are also created.
    resultNodes = graph.create(kristine_amy, amy_sheryl)
    #Print the results (relationships)
    print("Relationship Created - ", resultNodes)
Example #5
0
def test_graph(graph):
    alice = Node(name="Alice")
    bob = Node(name="Bob")
    carol = Node(name="Carol")
    dave = Node(name="Dave")
    path, = graph.create(Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave))
    assert path.graph == Graph("http://localhost:7474/db/data/")
Example #6
0
def createPaths():
    #Connect to Neo4j Database
    graph = connectGraph()
    #Let us define few Nodes
    bradley, matthew, lisa = Node(name="Bradley"), Node(name="Matthew"), Node(
        name="Lisa")
    #Connect these Node and form a Path
    path_1 = Path(bradley, "Knows", matthew, Rev("Knows"), lisa)
    #Let us create this Path on the server
    graph.create(path_1)
    #Let us create some more Nodes
    john, annie, ripley = Node(name="John"), Node(name="Annie"), Node(
        name="Ripley")
    #Define another Path for these New Nodes
    path_2 = Path(john, "Knows", annie, "Knows", ripley)
    #Now, we will join path_1 and path_2 using function append(), and it will give us a new path
    path_3 = path_1.append("Knows", path_2)
    #Let us Create this new path in the server
    resultPath = graph.create(path_3)

    #Now we will print and see the results on the Console
    print("Print Raw Data")
    print("Nodes in the Path-1 = ", resultPath[0].nodes)
    print("Relationships in the Path-1 = ", resultPath[0].relationships)

    print("Print - All Relationships")
    for rels in resultPath[0].relationships:
        print(rels)
Example #7
0
def test_service_root_on_bound_path(graph):
    alice = Node(name="Alice")
    bob = Node(name="Bob")
    carol = Node(name="Carol")
    dave = Node(name="Dave")
    path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave)
    graph.create(path)
    assert path.service_root == ServiceRoot("http://localhost:7474/")
    path[0].unbind()
    assert path.service_root == ServiceRoot("http://localhost:7474/")
Example #8
0
 def test_can_reverse_iterate_path_relationships(self, alice, bob, carol, dave):
     # given
     path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave)
     # when
     rels = list(reversed(path))
     # then
     assert rels == [
         Relationship(carol, "KNOWS", dave),
         Relationship(carol, "HATES", bob),
         Relationship(alice, "LOVES", bob),
     ]
Example #9
0
def test_rel_and_rev_hashes(graph):
    assert hash(Rel("KNOWS")) == hash(Rel("KNOWS"))
    assert hash(Rel("KNOWS")) == -hash(Rev("KNOWS"))
    assert hash(Rel("KNOWS", well=True,
                    since=1999)) == hash(Rel("KNOWS", since=1999, well=True))
    rel_1 = Node("KNOWS", since=1999)
    graph.create(rel_1)
    rel_2 = Node("KNOWS", since=1999)
    rel_2.bind(rel_1.uri)
    assert rel_1 is not rel_2
    assert hash(rel_1) == hash(rel_2)
Example #10
0
def test_path_in_several_ways():
    alice = Node(name="Alice")
    bob = Node(name="Bob")
    carol = Node(name="Carol")
    dave = Node(name="Dave")
    path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave)
    assert path.__bool__()
    assert path.__nonzero__()
    assert path[0] == Relationship(alice, "LOVES", bob)
    assert path[1] == Relationship(carol, "HATES", bob)
    assert path[2] == Relationship(carol, "KNOWS", dave)
    assert path[-1] == Relationship(carol, "KNOWS", dave)
    assert path[0:1] == Path(alice, "LOVES", bob)
    assert path[0:2] == Path(alice, "LOVES", bob, Rev("HATES"), carol)
    try:
        _ = path[7]
    except IndexError:
        assert True
    else:
        assert False
Example #11
0
def test_service_root_on_unbound_path():
    alice = Node(name="Alice")
    bob = Node(name="Bob")
    carol = Node(name="Carol")
    dave = Node(name="Dave")
    path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave)
    try:
        assert path.service_root == ServiceRoot("http://localhost:7474/")
    except BindError:
        assert True
    else:
        assert False
Example #12
0
 def test_can_make_new_path_from_path(self, alice, bob, carol, dave):
     # given
     path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave)
     # when
     new_path = Path(path)
     # then
     new_rels = list(new_path)
     assert new_rels == [
         Relationship(alice, "LOVES", bob),
         Relationship(carol, "HATES", bob),
         Relationship(carol, "KNOWS", dave),
     ]
Example #13
0
 def test_can_create_path_from_object(self, graph):
     # given
     nodes = graph.create({}, {}, {}, {})
     path = Path(nodes[0], "LOVES", nodes[1], Rev("HATES"), nodes[2],
                 "KNOWS", nodes[3])
     # when
     created, = graph.create(path)
     # then
     assert isinstance(created, Path)
     assert created.nodes == nodes
     assert created.rels[0].type == "LOVES"
     assert created.rels[1].type == "HATES"
     assert isinstance(created.rels[1], Rev)
     assert created.rels[2].type == "KNOWS"
    def createFriends(self, graph, people):
        print("Creating Relationships between People")

        path_1 = Path(people['bradley'], 'FRIEND', people['matthew'], 'FRIEND',
                      people['lisa'], 'FRIEND', people['john'])
        path_2 = path_1.prepend(people['lisa'], Rev('FRIEND'))
        path_3 = Path(people['annie'], 'FRIEND', people['ripley'], 'FRIEND',
                      people['lisa'])
        path_4 = Path(people['bradley'], 'TEACHES', people['matthew'])

        friendsPath = graph.create(path_2, path_3, path_4)

        print("Finished Creating Relationships between People")

        return friendsPath
    def createAndExecuteBatchRequest(self):
        # Authenticate the user using py2neo.authentication
        # Ensure that you change the password 'sumit' as per your database configuration.
        py2neo.authenticate("localhost:7474", "neo4j", "sumit")
        # Connect to Graph and get the instance of Graph
        graph = Graph("http://localhost:7474/db/data/")

        #Get the instance of Write Batch request
        batch = WriteBatch(graph)
        #Create a Node
        daisy = Node(name='Daisy')
        # Now invoke create method on batch. It also shows another way of creating the Node
        batch.create({'name': 'Hana'})
        batch.create(daisy)
        #Create a relationships between the Nodes
        batch.create((0, Rev('KNOWS'), 1))
        #Finally submit the Nodes
        batch.submit()
Example #16
0
def createRelationshipWithProperties(query):
    # creating an object of class RootPostag
    rootPostag = RootPostag()

    # creating an object of class Relations
    rel = Relations()

    # Spliting the query/text into sentences
    sentences = query.split('ред')

    for sentence in sentences:
        # Spliting the sentence into words/tokens
        sentence = sentence.replace('\n', '').replace('\r', '')
        input_words = sentence.split(' ')

        # Getting root-words and corresponding pos-tags for the tokens/words
        # root_words is list which will store the corresponding root-words for each token
        # pos_tags is list which will store the corresponding pos-tags for each token
        root_words, pos_tags = rootPostag.rootWORD_and_posTAG(sentence)

        # headNode for each graph will be unique
        headNode = Node(
            'URL',
            url=
            'http://www.livehindustan.com/news/ncr/article1-aam-admi-party-leader-kumar-vishwas-says-will-take-decision-tonight-809231.html'
        )

        # List of relationships
        relationships = []

        # Making a list of dictionaries/nodes, where each dictionary/node will contain properties of the individual word/node
        nodes = []
        word_dic = []
        for i, j, k in zip(input_words, root_words, pos_tags):
            word_dic.append({"word": i, "root_word": j, "pos_tag": k})
            nodes.append(Node("UNL-Word", word=i, root_word=j, pos_tag=k))
            relationships.append(
                Relationship(nodes[-1], Rev('LINKED'), headNode))

        # Checking for unl relations between a pair of words in the sentence
        for i in range(0, len(word_dic) - 1):
            for j in range(i + 1, len(word_dic)):
                label1 = nodes[i]
                label2 = nodes[j]
                #label1 = Node(word_dic[i]['pos_tag'], word = word_dic[i]['word'], root_word = word_dic[i]['root_word'], pos_tag = word_dic[i]['pos_tag'])
                #label2 = Node(word_dic[j]['pos_tag'], word = word_dic[j]['word'], root_word = word_dic[j]['root_word'], pos_tag = word_dic[j]['pos_tag'])

                if rel.qua_relation(word_dic[i], word_dic[j]):
                    relationships.append(Relationship(label1, 'QUA', label2))
                if rel.qua_relation(word_dic[j], word_dic[i]):
                    relationships.append(
                        Relationship(label1, Rev('QUA'), label2))

                if rel.dur_relation(word_dic[i], word_dic[j]):
                    relationships.append(Relationship(label1, 'DUR', label2))
                if rel.dur_relation(word_dic[j], word_dic[i]):
                    relationships.append(
                        Relationship(label1, Rev('DUR'), label2))

                if rel.agt_relation(word_dic[i], word_dic[j]):
                    relationships.append(Relationship(label1, 'AGT', label2))
                if rel.agt_relation(word_dic[j], word_dic[i]):
                    relationships.append(
                        Relationship(label1, Rev('AGT'), label2))

                if rel.aoj_relation(word_dic[i], word_dic[j]):
                    relationships.append(Relationship(label1, 'AOJ', label2))
                if rel.aoj_relation(word_dic[j], word_dic[i]):
                    relationships.append(
                        Relationship(label1, Rev('AOJ'), label2))

                if rel.nam_relation(word_dic[i], word_dic[j]):
                    relationships.append(Relationship(label1, 'NAM', label2))
                if rel.nam_relation(word_dic[j], word_dic[i]):
                    relationships.append(
                        Relationship(label1, Rev('NAM'), label2))

                if rel.obj_relation(word_dic[i], word_dic[j]):
                    relationships.append(Relationship(label1, 'OBJ', label2))
                if rel.obj_relation(word_dic[j], word_dic[i]):
                    relationships.append(
                        Relationship(label1, Rev('OBJ'), label2))

        for r in relationships:
            resultNodes = graph.create(r)
Example #17
0
def test_can_apply_unary_absolute_to_rev():
    before = Rev("KNOWS", since=1999)
    after = abs(before)
    assert isinstance(after, Rel)
    assert after.type == before.type
    assert after.properties == before.properties
Example #18
0
def test_can_apply_unary_negative_to_rev():
    before = Rev("KNOWS", since=1999)
    after = -before
    assert isinstance(after, Rel)
    assert after.type == before.type
    assert after.properties == before.properties
Example #19
0
def test_can_delete_path(graph):
    path = Path({}, "LOVES", {}, Rev("HATES"), {}, "KNOWS", {})
    graph.create(path)
    assert path.exists
    graph.delete(path)
    assert not path.exists
Example #20
0
def test_unbound_rev_representation():
    likes = Rev("LIKES")
    assert repr(likes) == "<-[:LIKES]-"