_binary From 49643f80ba71ec30f554eddcd6ef7705a2b89d12 Mon Sep 17 00:00:00 2001
From: diesal9 <charles.bocage@sjsu.edu>
Date: Sat, 30 May 2015 21:29:36 -0700
Subject: [PATCH] I modified the summarizer.php file to have its weight
 variables in a place so that they can be modified from outside of the class
 for testing.  I also updated the summarizer_test.php file to modify those
 same weight variables directly instead of creating  a new file and updated
 the comments and variable names within the test method.

---
 lib/summarizers/summarizer.php |  31 +++++--
 tests/summarizer_test.php      | 183 ++++++++++-------------------------------
 2 files changed, 68 insertions(+), 146 deletions(-)

diff --git a/lib/summarizers/summarizer.php b/lib/summarizers/summarizer.php
index a3fd1a0..e1072a8 100644
--- a/lib/summarizers/summarizer.php
+++ b/lib/summarizers/summarizer.php
@@ -41,6 +41,26 @@ if(!defined('BASE_DIR')) {echo "BAD REQUEST"; exit();}
 class Summarizer
 {
     /**
+     * The value to represent the weight for class one tags.
+     */
+    static $CLASS_ONE_WEIGHT = 0;
+    /**
+     * The value to represent the weight for class two tags.
+     */
+    static $CLASS_TWO_WEIGHT = 0;
+    /**
+     * The value to represent the weight for class three tags.
+     */
+    static $CLASS_THREE_WEIGHT = 0;
+    /**
+     * The value to represent the weight for class four tags.
+     */
+    static $CLASS_FOUR_WEIGHT = 0;
+    /**
+     * The value to represent the weight for class five tags.
+     */
+    static $CLASS_FIVE_WEIGHT = 0;
+    /**
      * This function will return additional weights if the term is in
      * certain tags of the document.  Each tag is grouped into 6 categories:
      * A
@@ -74,11 +94,11 @@ class Summarizer
             "<ol.*?>(.*?" . $term . ".*?)<\/ol>|" .
             "<ul.*?>(.*?" . $term . ".*?)<\/ul>/iu";
         $class_five_regex = "/(?s)<title.*?>(.*?" . $term . ".*?)<\/title>/iu";
-        $class_one_weight = 0;
-        $class_two_weight = 0;
-        $class_three_weight = 0;
-        $class_four_weight = 0;
-        $class_five_weight = 0;
+        $class_one_weight = self::$CLASS_ONE_WEIGHT;
+        $class_two_weight = self::$CLASS_TWO_WEIGHT;
+        $class_three_weight = self::$CLASS_THREE_WEIGHT;
+        $class_four_weight = self::$CLASS_FOUR_WEIGHT;
+        $class_five_weight = self::$CLASS_FIVE_WEIGHT;
         $class_one_matches = 0;
         $class_two_matches = 0;
         $class_three_matches = 0;
@@ -138,4 +158,3 @@ class Summarizer
         return $result;
     }
 }
-
diff --git a/tests/summarizer_test.php b/tests/summarizer_test.php
index c5374be..bdc8d34 100644
--- a/tests/summarizer_test.php
+++ b/tests/summarizer_test.php
@@ -34,170 +34,73 @@ if(!defined('BASE_DIR')) {echo "BAD REQUEST"; exit();}
  */
 require_once BASE_DIR.'lib/unit_test.php';
 /**
+ * Load the base summarizer
+ */
+require_once BASE_DIR."lib/summarizers/summarizer.php";
+/**
  * Code used to test the summarizer algorithms.
  *
- * @author Chris Pollett
+ * @author Charles Bocage
  * @package seek_quarry\test
  */
 class SummarizerTest extends UnitTest
 {
-    static $summarizer_code_file_contents_snapshot = "";
-    static $summarizer_code_file_name;
     /**
      * Each test we set up a new Summarizer object
      */
     function setUp()
     {
-       /* self::$summarizer_code_file_name = BASE_DIR .
-            "lib/summarizers/summarizer.php";
-        self::$summarizer_code_file_contents_snapshot =
-            file_get_contents(self::$summarizer_code_file_name);
-        $this->modifyWeights();*/
-        /**
-         * Load the summarizer here because it gets modified in the test
-         * setup
-         */
-       // require_once self::$summarizer_code_file_name;
-       // $this->test_objects['FILE1'] = new Summarizer;
+        Summarizer::$CLASS_ONE_WEIGHT = 1;
+        Summarizer::$CLASS_TWO_WEIGHT = 1;
+        Summarizer::$CLASS_THREE_WEIGHT = 1;
+        Summarizer::$CLASS_FOUR_WEIGHT = 1;
+        Summarizer::$CLASS_FIVE_WEIGHT = 1;
     }
     /**
      * Nothing done for unit test tear done
      */
     function tearDown()
     {
-       /* file_put_contents(self::$summarizer_code_file_name,
-            self::$summarizer_code_file_contents_snapshot);*/
     }
     /**
+     * This function reads each line from the $terms_and_html_tags file. The
+     * $terms_and_html_tags file contains a list of a term to search for and a
+     * sample html tag separated by a pipe.  It parses each line from the
+     * $terms_and_html_tags file to and sends the arguments to the
+     * additionalWeights method.  Then it compares the value returned to its
+     * corresponding value in the $return_values_to_compare file to make sure
+     * they match.
      *
+     * $return_values_to_compare is a list of corresponding values used for
+     * comparing results
+     * $terms_and_html_tags is a list of terms and the sample html tags to
+     * search matches
      */
-    function additionalWeightTestCase()
+    function getAdditionalWeightTestCase()
     {
-       // $summarizer_dir = BASE_DIR.'/tests/test_files/summarizer';
-       // $test_words = file("$summarizer_dir/summarizer_input.txt");
-        //Stem word set from snowball for comparing results
-       // $stem_words = file("$summarizer_dir/summarizer_result.txt");
-        /**
-         * check if function gets the correct additional weight with the words
-         * in $test_words by comparing results with stem words in $stem_words
-         */
-      /*  for($i = 0; $i < count($test_words); $i++) {
-            $word = preg_split('/\|+/u', trim($test_words[$i]));
-            $stem = trim($stem_words[$i]);
-            $weighted_word = $this->test_objects['FILE1']->
-                getAdditionalWeight($word[0], $word[1]);
-            if($stem != $word_stem) {
-                $clean_word = htmlentities($word[1], ENT_QUOTES, "UTF-8");
-                echo "Calculating the weight of \"$word[0]\" in ".
-                    "$clean_word to $weighted_word should be $stem\n";
-                $this->tearDown();
+        $summarizer_dir = BASE_DIR.'/tests/test_files/summarizer';
+        $terms_and_html_tags = file("$summarizer_dir/summarizer_input.txt");
+        $return_values_to_compare =
+            file("$summarizer_dir/summarizer_result.txt");
+        for($i = 0; $i < count($terms_and_html_tags); $i++) {
+            $term_and_html_tag = preg_split('/\|+/u',
+                trim($terms_and_html_tags[$i]));
+            $return_value_to_compare = trim($return_values_to_compare[$i]);
+            $additional_weight_return_value =
+                Summarizer::getAdditionalWeight($term_and_html_tag[0],
+                $term_and_html_tag[1]);
+            if($return_value_to_compare != $additional_weight_return_value) {
+                $clean_word = htmlentities($term_and_html_tag[1], ENT_QUOTES,
+                    "UTF-8");
+                echo "Calculating the weight of \"$term_and_html_tag[0]\" in ".
+                    "$clean_word to $additional_weight_return_value should be".
+                    " $return_value_to_compare\n";
                 exit();
             }
-            $this->assertEqual($word_stem,
-                    $stem,"function getAdditionalWeight correctly weights
-                    \"$word[0]\" in $word[1] to $stem");
-        }*/
-    }
-    /**
-     * Modify the summarizer to have all of the weights set to 1 in order
-     * to get consitent weight for the unit tests
-     */
-    function modifyWeights()
-    {
-       /* $summarizer_code_file_contents = "";
-        $weights_to_try = 1;
-        $class_one_weight_code_to_modify = '$class_one_weight = ';
-        $class_two_weight_code_to_modify = '$class_two_weight = ';
-        $class_three_weight_code_to_modify = '$class_three_weight = ';
-        $class_four_weight_code_to_modify = '$class_four_weight = ';
-        $class_five_weight_code_to_modify = '$class_five_weight = ';
-        $file = fopen(self::$summarizer_code_file_name, "r");
-        while(!feof($file)){
-            $line = fgets($file);
-            if ((strpos(mb_strtolower($line),
-                mb_strtolower($class_one_weight_code_to_modify)) === false) &&
-                (strpos(mb_strtolower($line),
-                mb_strtolower($class_two_weight_code_to_modify)) === false) &&
-                (strpos(mb_strtolower($line),
-                mb_strtolower($class_three_weight_code_to_modify)) === false) &&
-                (strpos(mb_strtolower($line),
-                mb_strtolower($class_four_weight_code_to_modify)) === false) &&
-                (strpos(mb_strtolower($line),
-                mb_strtolower($class_five_weight_code_to_modify)) === false)) {
-                if ($summarizer_code_file_contents == "") {
-                    $summarizer_code_file_contents = $line;
-                } else {
-                    $summarizer_code_file_contents =
-                        $summarizer_code_file_contents . $line;
-                }
-            } else if (strpos(mb_strtolower($line),
-                mb_strtolower($class_one_weight_code_to_modify)) !== false) {
-                if ($summarizer_code_file_contents == "") {
-                    $summarizer_code_file_contents = "        " .
-                        $class_one_weight_code_to_modify
-                        . $weights_to_try . ";\r\n";
-                } else {
-                    $summarizer_code_file_contents =
-                        $summarizer_code_file_contents . "        " .
-                        $class_one_weight_code_to_modify . $weights_to_try .
-                        ";\r\n";
-                }
-            } else if (strpos(mb_strtolower($line),
-                mb_strtolower($class_two_weight_code_to_modify)) !== false) {
-                if ($summarizer_code_file_contents == "") {
-                    $summarizer_code_file_contents = "        " .
-                        $class_two_weight_code_to_modify
-                        . $weights_to_try . ";\r\n";
-                } else {
-                    $summarizer_code_file_contents =
-                        $summarizer_code_file_contents . "        " .
-                        $class_two_weight_code_to_modify . $weights_to_try .
-                        ";\r\n";
-                }
-            } else if (strpos(mb_strtolower($line),
-                mb_strtolower($class_three_weight_code_to_modify)) !== false) {
-                if ($summarizer_code_file_contents == "") {
-                    $summarizer_code_file_contents = "        " .
-                        $class_three_weight_code_to_modify
-                        . $weights_to_try . ";\r\n";
-                } else {
-                    $summarizer_code_file_contents =
-                        $summarizer_code_file_contents . "        " .
-                        $class_three_weight_code_to_modify . $weights_to_try .
-                        ";\r\n";
-                }
-            } else if (strpos(mb_strtolower($line),
-                mb_strtolower($class_four_weight_code_to_modify)) !== false) {
-                if ($summarizer_code_file_contents == "") {
-                    $summarizer_code_file_contents = "        " .
-                        $class_four_weight_code_to_modify
-                        . $weights_to_try . ";\r\n";
-                } else {
-                    $summarizer_code_file_contents =
-                        $summarizer_code_file_contents . "        " .
-                        $class_four_weight_code_to_modify .
-                        $weights_to_try .
-                        ";\r\n";
-                }
-            } else if (strpos(mb_strtolower($line),
-                mb_strtolower($class_five_weight_code_to_modify)) !== false) {
-                if ($summarizer_code_file_contents == "") {
-                    $summarizer_code_file_contents = "        " .
-                        $class_five_weight_code_to_modify
-                        . $weights_to_try . ";\r\n";
-                } else {
-                    $summarizer_code_file_contents =
-                        $summarizer_code_file_contents . "        " .
-                        $class_five_weight_code_to_modify . $weights_to_try .
-                        ";\r\n";
-                }
-            }
+            $this->assertEqual($additional_weight_return_value,
+                $return_value_to_compare,"function getAdditionalWeight ".
+                "correctly weights\"$term_and_html_tag[0]\" in ".
+                "$term_and_html_tag[1] to $return_value_to_compare");
         }
-        fclose($file);
-        file_put_contents(self::$summarizer_code_file_name,
-            $summarizer_code_file_contents);
-        file_put_contents(self::$summarizer_code_file_name . "txt",
-            $summarizer_code_file_contents);*/
     }
 }
-
-- 
1.9.5.github.0

