# HG changeset patch # User Nicholas Jillings # Date 1455730049 0 # Node ID d85e219a5eeb9fff5f88617eb91da76ad5a3ed4e # Parent 2bde74ae8396499ea4b57a7921130ecef2d4a6a0 PHP Comment Parser exports to XML, JSON and CSV notations. Included HTML file interface. diff -r 2bde74ae8396 -r d85e219a5eeb scripts/comment_parser.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/comment_parser.html Wed Feb 17 17:27:29 2016 +0000 @@ -0,0 +1,72 @@ + + + + + + +

WAET Test Results Analysis

+

Comment Extraction

+

All of the XMLs in the server 'saves/' directory are automatically parsed and downloaded, extracting only the comments. Simply select the comments you wish to extract below and your desired data format.

+
+
+ + + +
+ + \ No newline at end of file diff -r 2bde74ae8396 -r d85e219a5eeb scripts/comment_parser.php --- a/scripts/comment_parser.php Wed Feb 17 12:10:55 2016 +0000 +++ b/scripts/comment_parser.php Wed Feb 17 17:27:29 2016 +0000 @@ -1,15 +1,29 @@ id = $id; + $this->comments = array(); + } + function addComment($str) { + array_push($this->comments,$str); + } +} +class testPage { + function __construct($id) { + $this->id = $id; + $this->elements = array(); + } +} // XML Saves location - assumes it will be saves/ $saves = glob("../saves/*.xml"); -$comment_struct = new SimpleXMLElement(''); +$comment_struct = array(); if (is_array($saves)) { foreach($saves as $filename) { $xml_string = file_get_contents($filename, FILE_TEXT); $xml_object = simplexml_load_string($xml_string); - $test_struct = $comment_struct->addChild("test"); if ($xml_object == false) { echo "

FATAL

could not parse file ".$filename.": "; foreach(libxml_get_errors() as $error) { @@ -19,27 +33,113 @@ // Iterate over each audioHolder node foreach($xml_object->page as $pageInstance) { - $page_struct = $test_struct->addChild("page"); - // Get the page-id and attach - $page_struct->addAttribute("page-id",$pageInstance['id']); - + // Find the page in the comment_struct + $page_struct = null; + foreach($comment_struct as $comment_struct_page) + { + if ($pageInstance['id'] == $comment_struct_page->id) + { + $page_struct = $comment_struct_page; + break; + } + } + if ($page_struct == null) { + array_push($comment_struct,new testPage($pageInstance['id'])); + $page_struct = $comment_struct[count($comment_struct)-1]; + } // Get the audioelements of the page foreach($pageInstance->audioelement as $fragment) { - $fragment_struct = $page_struct->addChild("audioelement"); - // Get the element-id and attach - $page_struct->addAttribute("element-id",$fragment['id']); - $page_struct->addAttribute("presented-id",$fragment['presentedId']); - $page_struct->addAttribute("url",$fragment['url']); - // Append the comment data - echo "

Comment: ".$fragement->comment."

"; - $comment = $fragment_struct->addChild("comment"); + // Find the page in the comment_struct + $element_struct = null; + foreach($page_struct->elements as $page_struct_element) + { + if ($fragment['id'] == $page_struct_element->id) + { + $element_struct = $page_struct_element; + break; + } + } + if ($element_struct == null) { + array_push($page_struct->elements,new audioElement($fragment['id'])); + $element_struct = $page_struct->elements[count($page_struct->elements)-1]; + } + $element_struct->addComment($fragment->comment->response); } } } } // Now we have a sub containing all comment data - echo $comment_struct->asXML(); + switch($_GET['format']) { + case "XML": + // Convert to an XML + $doc_struct = new SimpleXMLElement(''); + foreach($comment_struct as $page_struct) + { + $doc_page = $doc_struct->addChild("page"); + $doc_page->addAttribute("id",$page_struct->id); + foreach($page_struct->elements as $element_struct) + { + $doc_element = $doc_page->addChild("audioelement"); + $doc_element->addAttribute("id",$element_struct->id); + foreach($element_struct->comments as $comment) + { + $doc_comment = $doc_element->addChild("comment",$comment); + } + } + } + echo $doc_struct->asXML(); + break; + case "JSON": + // Convert to JSON + $doc_string = '{ "pages": ['; + for($page_index = 0; $page_index < count($comment_struct); $page_index++ ) + { + $page_struct = $comment_struct[$page_index]; + $doc_page = '{"id": "'.$page_struct->id.'", "elements": ['; + for($element_index = 0; $element_index < count($page_struct->elements); $element_index++ ) + { + $element_struct = $page_struct->elements[$element_index]; + $doc_element = '{"id": "'.$element_struct->id.'", "comments": ['; + for($comment_index = 0; $comment_index < count($element_struct->comments); $comment_index++ ) + { + $doc_comment = '"'.$element_struct->comments[$comment_index].'"'; + if ($comment_index < count($element_struct->comments)-1) { + $doc_comment = $doc_comment.','; + } + $doc_element = $doc_element.$doc_comment; + } + $doc_element = $doc_element.']}'; + if ($element_index < count($page_struct->elements)-1) { + $doc_element = $doc_element.','; + } + $doc_page = $doc_page.$doc_element; + } + $doc_page = $doc_page.']}'; + if ($page_index < count($comment_struct)-1) { + $doc_page = $doc_page.','; + } + $doc_string = $doc_string.$doc_page; + } + $doc_string = $doc_string."]}"; + echo $doc_string; + break; + case "CSV": + // Conver to CSV + // The CSV has three columns: page, element, comment + $doc_string = "page,element,comment"."\r\n"; + foreach($comment_struct as $page_struct) + { + foreach($page_struct->elements as $element_struct) + { + foreach($element_struct->comments as $comment) + { + $doc_string = $doc_string.$page_struct->id.",".$element_struct->id.",".$comment."\r\n"; + } + } + } + echo $doc_string; + } } else { echo "FATAL - No saved XML files discovered"; }