Classification.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * Artificial Neural Network - Version 2.2
  4. *
  5. * For updates and changes visit the project page at http://ann.thwien.de/
  6. *
  7. *
  8. *
  9. * <b>LICENCE</b>
  10. *
  11. * The BSD 2-Clause License
  12. *
  13. * http://opensource.org/licenses/bsd-license.php
  14. *
  15. * Copyright (c) 2007 - 2012, Thomas Wien
  16. * All rights reserved.
  17. *
  18. * Redistribution and use in source and binary forms, with or without
  19. * modification, are permitted provided that the following conditions
  20. * are met:
  21. *
  22. * 1. Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. *
  25. * 2. Redistributions in binary form must reproduce the above copyright
  26. * notice, this list of conditions and the following disclaimer in the
  27. * documentation and/or other materials provided with the distribution.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  32. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  33. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  34. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  35. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  36. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  37. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  39. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGE.
  41. *
  42. * @author Thomas Wien <info_at_thwien_dot_de>
  43. * @version ANN Version 2.2 by Thomas Wien
  44. * @copyright Copyright (c) 2007-2012 by Thomas Wien
  45. * @package ANN
  46. */
  47. namespace ANN;
  48. /**
  49. * @package ANN
  50. * @access public
  51. */
  52. final class Classification extends Filesystem implements InterfaceLoadable
  53. {
  54. /**#@+
  55. * @ignore
  56. */
  57. /**
  58. * @var integer
  59. */
  60. protected $intMaxClassifiers;
  61. /**
  62. * @var array
  63. */
  64. protected $arrClassifiers = array();
  65. /**#@-*/
  66. /**
  67. * @param integer $intMaxClassifiers
  68. * @throws Exception
  69. */
  70. public function __construct($intMaxClassifiers)
  71. {
  72. if(!is_integer($intMaxClassifiers) || $intMaxClassifiers <= 0)
  73. throw new Exception('Constraints: $intMaxClassifiers should be a positive integer number');
  74. $this->intMaxClassifiers = $intMaxClassifiers;
  75. }
  76. /**
  77. * @param string $strValue
  78. * @throws Exception
  79. * @uses existsClassifier()
  80. */
  81. public function addClassifier($strValue)
  82. {
  83. if(count($this->arrClassifiers) == $this->intMaxClassifiers)
  84. throw new Exception('Maximal count of classifiers reached');
  85. if($this->existsClassifier($strValue))
  86. throw new Exception('Classifier "'. $strValue .'" does already exist');
  87. $this->arrClassifiers[] = $strValue;
  88. }
  89. /**
  90. * @param string $strValue
  91. * @return boolean
  92. */
  93. protected function existsClassifier($strValue)
  94. {
  95. foreach($this->arrClassifiers as $strClassifier)
  96. {
  97. if(strtolower($strClassifier) == strtolower($strValue))
  98. return TRUE;
  99. }
  100. return FALSE;
  101. }
  102. /**
  103. * @param string|array $mixedValues
  104. * @return array
  105. * @uses calculateOutputValues()
  106. * @throws Exception
  107. */
  108. public function getOutputValue($mixedValues)
  109. {
  110. if(!is_string($mixedValues) && !is_array($mixedValues))
  111. throw new Exception('$mixedValues should be either string or array');
  112. $arrValues = array();
  113. if(is_string($mixedValues))
  114. {
  115. $arrValues = array($mixedValues);
  116. }
  117. else
  118. {
  119. $arrValues = $mixedValues;
  120. }
  121. return $this->calculateOutputValues($arrValues);
  122. }
  123. /**
  124. * @param array $arrValues
  125. * @return array
  126. * @throws Exception
  127. */
  128. protected function calculateOutputValues($arrValues)
  129. {
  130. $arrReturn = array();
  131. $boolFound = FALSE;
  132. foreach($this->arrClassifiers as $intKey => $strClassifier)
  133. {
  134. $arrReturn[$intKey] = (in_array($strClassifier, $arrValues)) ? 1 : 0;
  135. if($arrReturn[$intKey] == 1)
  136. $boolFound = TRUE;
  137. }
  138. if(!$boolFound)
  139. throw new Exception('Classifier(s) "'. implode(', ', $arrValues) .'" not found');
  140. $intCountRemainingOutputs = $this->intMaxClassifiers - count($arrReturn);
  141. for($intIndex = 0; $intIndex < $intCountRemainingOutputs; $intIndex++)
  142. {
  143. $arrReturn[] = 0;
  144. }
  145. return $arrReturn;
  146. }
  147. /**
  148. * @param string|array $mixedValues
  149. * @return array
  150. * @uses getOutputValue()
  151. */
  152. public function __invoke($mixedValues)
  153. {
  154. return $this->getOutputValue($mixedValues);
  155. }
  156. /**
  157. * @param array $arrValues
  158. * @return array
  159. */
  160. public function getRealOutputValue($arrValues)
  161. {
  162. $arrReturn = array();
  163. foreach($this->arrClassifiers as $intKey => $strClassifier)
  164. {
  165. if($arrValues[$intKey] == 1)
  166. $arrReturn[] = $strClassifier;
  167. }
  168. return $arrReturn;
  169. }
  170. }