|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace TextAnalysis\Taggers; |
| 5 | + |
| 6 | +use RuntimeException; |
| 7 | +use TextAnalysis\Filters\PunctuationFilter; |
| 8 | +use TextAnalysis\Tokenizers\WhitespaceTokenizer; |
| 9 | + |
| 10 | +/** |
| 11 | + * Abstract class for the Stanford NerTagger and PosTagger to extend |
| 12 | + * @author yooper |
| 13 | + */ |
| 14 | +abstract class StanfordAbstract |
| 15 | +{ |
| 16 | + /** |
| 17 | + * |
| 18 | + * @var array options passed the java vm |
| 19 | + */ |
| 20 | + protected $javaOptions = []; |
| 21 | + |
| 22 | + /** |
| 23 | + * |
| 24 | + * @var string Path to the classifier you want to use |
| 25 | + */ |
| 26 | + protected $classifierPath = null; |
| 27 | + |
| 28 | + /** |
| 29 | + * Path to the stanford NER jar |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + protected $jarPath = null; |
| 33 | + |
| 34 | + /** |
| 35 | + * Place for storing the tokenized words to |
| 36 | + * @var string |
| 37 | + */ |
| 38 | + protected $tmpFilePath = null; |
| 39 | + |
| 40 | + /** |
| 41 | + * The separators between tokens |
| 42 | + * @var string |
| 43 | + */ |
| 44 | + protected $separator = null; |
| 45 | + |
| 46 | + /** |
| 47 | + * |
| 48 | + * @var string Output from proc_open |
| 49 | + */ |
| 50 | + protected $output; |
| 51 | + |
| 52 | + /** |
| 53 | + * |
| 54 | + * @var string Errors from proc_open |
| 55 | + */ |
| 56 | + protected $errors; |
| 57 | + |
| 58 | + |
| 59 | + /** |
| 60 | + * |
| 61 | + * @param string $jarPath |
| 62 | + * @param string $classifierPath |
| 63 | + * @param string $javaOptions |
| 64 | + */ |
| 65 | + public function __construct($jarPath, $classifierPath, $javaOptions = ['-mx700m'], $separator = '/') |
| 66 | + { |
| 67 | + $this->jarPath = $jarPath; |
| 68 | + $this->classifierPath = $classifierPath; |
| 69 | + $this->javaOptions = $javaOptions; |
| 70 | + $this->separator = $separator; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * |
| 75 | + * @return string |
| 76 | + */ |
| 77 | + public function getTmpFilePath() |
| 78 | + { |
| 79 | + return $this->tmpFilePath; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @throws \RuntimeException |
| 84 | + * @param array $tokens Use a tokenizer |
| 85 | + */ |
| 86 | + public function tag(array $tokens) |
| 87 | + { |
| 88 | + $this->verify(); |
| 89 | + //write tokens to temp file |
| 90 | + file_put_contents($this->getTmpFilePath(), implode($this->getSeparator(), $tokens)); |
| 91 | + $this->exec(); |
| 92 | + |
| 93 | + return $this->getParsedOutput(); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Requires that class output var be populated. Punctuation may cause issues |
| 98 | + * @return array |
| 99 | + */ |
| 100 | + abstract protected function getParsedOutput(); |
| 101 | + |
| 102 | + |
| 103 | + /** |
| 104 | + * Separator used between tokens |
| 105 | + * @return string default is / |
| 106 | + */ |
| 107 | + public function getSeparator() |
| 108 | + { |
| 109 | + return $this->separator; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * verifies required files exist |
| 114 | + * @throws \RuntimeException |
| 115 | + */ |
| 116 | + protected function verify() |
| 117 | + { |
| 118 | + if(!is_file($this->getJarPath())) { |
| 119 | + throw new RuntimeException("Jar not found {$this->getJarPath()}"); |
| 120 | + } |
| 121 | + |
| 122 | + if(!is_file($this->getClassifierPath())) { |
| 123 | + throw new RuntimeException("Classifier not found {$this->getClassifierPath()}"); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Returns the path to the classifier |
| 129 | + * @return string |
| 130 | + */ |
| 131 | + public function getClassifierPath() |
| 132 | + { |
| 133 | + return $this->classifierPath; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Returns the path to the ner jar |
| 138 | + * @return string |
| 139 | + */ |
| 140 | + public function getJarPath() |
| 141 | + { |
| 142 | + return $this->jarPath; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * |
| 147 | + * @return array |
| 148 | + */ |
| 149 | + public function getJavaOptions() |
| 150 | + { |
| 151 | + return $this->javaOptions; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * |
| 156 | + * @return string |
| 157 | + */ |
| 158 | + public function getPathToJava() |
| 159 | + { |
| 160 | + if(getenv('JAVA_HOME')) { |
| 161 | + return getenv('JAVA_HOME'); |
| 162 | + } else { |
| 163 | + return 'java'; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * @return string Returns the cli that is passed to proc_open |
| 169 | + */ |
| 170 | + abstract public function getCommand(); |
| 171 | + |
| 172 | + /** |
| 173 | + * @return string Return based on the OS used |
| 174 | + */ |
| 175 | + protected function getPathSeparator() |
| 176 | + { |
| 177 | + if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
| 178 | + return ';'; |
| 179 | + } else { |
| 180 | + return ':'; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Calls the stanford jar file |
| 186 | + * @throws RuntimeException |
| 187 | + */ |
| 188 | + protected function exec() |
| 189 | + { |
| 190 | + $descriptors = [ |
| 191 | + 0 => ["pipe", "r"], // stdin is a pipe that the child will read from |
| 192 | + 1 => ["pipe", "w"], // stdout is a pipe that the child will write to |
| 193 | + 2 => ["pipe", "w"] // stderr is a file to write to |
| 194 | + ]; |
| 195 | + |
| 196 | + $process = proc_open($this->getCommand(), $descriptors, $pipes, dirname($this->getJarPath()), []); |
| 197 | + |
| 198 | + if (is_resource($process)) { |
| 199 | + fclose($pipes[0]); // close stdin pipe |
| 200 | + $this->output = stream_get_contents($pipes[1]); |
| 201 | + $this->errors = stream_get_contents($pipes[2]); |
| 202 | + fclose($pipes[2]); |
| 203 | + fclose($pipes[1]); |
| 204 | + if(proc_close($process) === -1) { |
| 205 | + throw new RuntimeException($this->errors); |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + public function __destruct() |
| 211 | + { |
| 212 | + unset($this->classifierPath); |
| 213 | + unset($this->errors); |
| 214 | + unset($this->jarPath); |
| 215 | + unset($this->javaOptions); |
| 216 | + unset($this->output); |
| 217 | + unset($this->separator); |
| 218 | + if(file_exists($this->tmpFilePath)) { |
| 219 | + unlink($this->tmpFilePath); |
| 220 | + } |
| 221 | + unset($this->tmpFilePath); |
| 222 | + } |
| 223 | +} |
0 commit comments