Skip to content

Commit

Permalink
Fixed equal function of trees bug, added tests for equals and changed…
Browse files Browse the repository at this point in the history
… output file from serializer test, Also added something that I forgot to put into README
  • Loading branch information
quimpm committed Jun 18, 2021
1 parent b0741b4 commit 462d148
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 7 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,27 @@ And a pair as:
```
Where h is the destination and t the origin

Also, the function serialize() has an optional argument called **aditional_atributes**. You can pass a function that receives a Node object and outputs a {key : value} dictionary
that will represent the additional attributes that you would like to add to the xml tags that represent the Nodes. The key will be the attribute name and the value will be the value of the attribute.

```python
def my_additional_atributes(node):
return {
"date" : node.publishedAt
"sentiment" : sentiment_analysis(node.txt) #Imagine we have a function that does sentiment analysis from an input text
}

tree.serialize("outuput_file.xml", my_aditional_atributes)
```
The nodes will have the following form:

```xml
<arg author="Flash Man" author_id="UCeFi97LktRRtpCvi_vqEmfg" id="Ugh8N1Ch9gCr-HgCoAEC" likeCount="1145" date="12-12-2012" sentiment="NEGATIVE">
My dad is an expert dragon slayer. "eeer but I don't see any dragons around... " You're welcome.
</arg>

```

### Representing a tree

If you want to see a representation of the tree, you can do:
Expand Down
2 changes: 1 addition & 1 deletion youtube_discussion_tree_api/_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def _find_name_in_thread(self, possible_names):
def __eq__(self, o: object) -> bool:
if self.video_id != o.video_id:
return False
for i, node in self.nodes:
for i, node in enumerate(self.nodes):
if node != o.nodes[i]:
return False
return True
Expand Down
1 change: 0 additions & 1 deletion youtube_discussion_tree_api/tests/output.txt

This file was deleted.

2 changes: 1 addition & 1 deletion youtube_discussion_tree_api/tests/output.xml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<entailment-corpus num_edges="7" num_nodes="8"><argument-list><arg author="authorOfVideo1" author_id="author1" id="video1" score="10000000">Video of turtles</arg><arg author="Argumentative Parser" author_id="UCv3WX7u1Yjk6umXJiOdmtdg" id="UgznJ9jPP_p6uIF5Wfp4AaABAg" score="0">This is a demo to see how comments work</arg><arg author="Quim Pic&#243; Mora" author_id="UCUR1hhWuXlbXgLJ2gwHgzLQ" id="UgznJ9jPP_p6uIF5Wfp4AaABAg.9MvYEeiNOK-9MvYLPbgx68" score="0">Hola, soc el quim n&#250;mero 1</arg><arg author="Quim Pic&#243; Mora" author_id="UCt-NaKjttdPl5F_4t8mT_bA" id="UgznJ9jPP_p6uIF5Wfp4AaABAg.9MvYEeiNOK-9MvYRUvooiC" score="0">Epale, jo soc el quim n&#250;mero 2</arg><arg author="Quim Pic&#243; Mora" author_id="UCt-NaKjttdPl5F_4t8mT_bA" id="UgznJ9jPP_p6uIF5Wfp4AaABAg.9MvYEeiNOK-9MvYTJqtQ01" score="0">@Quim Pic&#243; Mora Hola quim</arg><arg author="Argumentative Parser" author_id="UCv3WX7u1Yjk6umXJiOdmtdg" id="UgznJ9jPP_p6uIF5Wfp4AaABAg.9MvYEeiNOK-9MvYcfNG7h0" score="0">@Quim Pic&#243; Mora Vaja, veig que youtube permet que dos tios es diguin igual i comentin als comentaris</arg><arg author="Argumentative Parser" author_id="UCv3WX7u1Yjk6umXJiOdmtdg" id="UgznJ9jPP_p6uIF5Wfp4AaABAg.9MvYEeiNOK-9Nnv216MjLV" score="0">holaaaaaaa</arg><arg author="Argumentative Parser" author_id="UCv3WX7u1Yjk6umXJiOdmtdg" id="Ugzrk4QCbElug58ycGp4AaABAg" score="0">Hotal</arg></argument-list><argument-pairs><pair id="1"><t id="UgznJ9jPP_p6uIF5Wfp4AaABAg" /><h id="video1" /></pair><pair id="2"><t id="UgznJ9jPP_p6uIF5Wfp4AaABAg.9MvYEeiNOK-9MvYLPbgx68" /><h id="UgznJ9jPP_p6uIF5Wfp4AaABAg" /></pair><pair id="3"><t id="UgznJ9jPP_p6uIF5Wfp4AaABAg.9MvYEeiNOK-9MvYRUvooiC" /><h id="UgznJ9jPP_p6uIF5Wfp4AaABAg" /></pair><pair id="4"><t id="UgznJ9jPP_p6uIF5Wfp4AaABAg.9MvYEeiNOK-9MvYTJqtQ01" /><h id="UgznJ9jPP_p6uIF5Wfp4AaABAg.9MvYEeiNOK-9MvYLPbgx68" /></pair><pair id="5"><t id="UgznJ9jPP_p6uIF5Wfp4AaABAg.9MvYEeiNOK-9MvYcfNG7h0" /><h id="UgznJ9jPP_p6uIF5Wfp4AaABAg.9MvYEeiNOK-9MvYTJqtQ01" /></pair><pair id="6"><t id="UgznJ9jPP_p6uIF5Wfp4AaABAg.9MvYEeiNOK-9Nnv216MjLV" /><h id="UgznJ9jPP_p6uIF5Wfp4AaABAg" /></pair><pair id="7"><t id="Ugzrk4QCbElug58ycGp4AaABAg" /><h id="video1" /></pair></argument-pairs></entailment-corpus>
<entailment-corpus num_edges="2" num_nodes="3"><argument-list><arg author="Ororo" author_id="author1" id="comment1" likeCount="10000000">Hello, I love turtles and dogs</arg><arg author="Horno Microondas" author_id="author2" id="comment2" likeCount="10000000">Cats are the best animals in the whole world</arg><arg author="Kekino" author_id="author3" id="comment3" likeCount="10000000">I'm more of a dogs person, they are so cute</arg></argument-list><argument-pairs><pair id="1"><t id="comment2" /><h id="comment1" /></pair><pair id="2"><t id="comment3" /><h id="comment1" /></pair></argument-pairs></entailment-corpus>
30 changes: 29 additions & 1 deletion youtube_discussion_tree_api/tests/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,32 @@ def test_find_name_in_thread(self):
"Quim Picó Mora" : "Tha one and only"
}
self.assertEqual("Quim Picó Mora", self.tree._find_name_in_thread(["Quim", "Quim Picó", "Quim Picó Mora"]))


def test_tree_equals(self):
with open("./youtube_discussion_tree_api/tests/comments.json", "r") as f:
comments = json.load(f)
tree1 = self.tree.make_tree(
Node (
id = "video1",
author_name = "authorOfVideo1",
author_id = "author1",
text = "Video of turtles",
like_count = 10000000,
parent_id = None,
published_at = "12-12-2012"
),
comments["items"]
)
tree2 = self.tree.make_tree(
Node (
id = "video1",
author_name = "authorOfVideo1",
author_id = "author1",
text = "Video of turtles",
like_count = 10000000,
parent_id = None,
published_at = "12-12-2012"
),
comments["items"]
)
self.assertTrue(tree1==tree2)
6 changes: 3 additions & 3 deletions youtube_discussion_tree_api/tests/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ def test_serialize_tree(self):
published_at = "12-12-2012"
)
]
_serialize_tree("./youtube_discussion_tree_api/tests/output.txt", nodes, None)
self.assertTrue(os.path.isfile("./youtube_discussion_tree_api/tests/output.txt"))
tree = ET.parse('./youtube_discussion_tree_api/tests/output.txt')
_serialize_tree("./youtube_discussion_tree_api/tests/output.xml", nodes, None)
self.assertTrue(os.path.isfile("./youtube_discussion_tree_api/tests/output.xml"))
tree = ET.parse('./youtube_discussion_tree_api/tests/output.xml')
self.assertEqual("entailment-corpus",tree.findall(".")[0].tag)
self.assertTrue(tree.find("./argument-list") != None)
self.assertTrue(tree.find("./argument-pairs") != None)
Expand Down

0 comments on commit 462d148

Please sign in to comment.