protected function detectChunk($text)
 {
     $ngrams = $this->sort->sort($this->parser->get($text));
     $total = min($this->config->maxNGram(), count($ngrams));
     foreach ($this->data as $lang => $data) {
         $distance[] = array('lang' => $lang, 'score' => $this->distance->distance($data, $ngrams, $total));
     }
     usort($distance, function ($a, $b) {
         return $a['score'] > $b['score'] ? -1 : 1;
     });
     if ($distance[0]['score'] - $distance[1]['score'] <= $this->threshold) {
         /** First and second language candidates are similar, we return the
             whole structure */
         return $distance;
     }
     /* we found a candidate which is at least 2% better than the second
        candidate */
     return $distance[0]['lang'];
 }
Example #2
0
 /**
  * @param string $text
  * @param string[] $languages
  * @return array[] [ [ 'lang' => <string>, 'score' => <float> ], ... ]
  */
 protected function detectChunk($text, $languages = null)
 {
     $ngrams = $this->sort->sort($this->parser->get($text));
     $total = min($this->config->maxNGram(), count($ngrams));
     foreach ($this->data as $lang => $data) {
         if ($languages && !in_array($lang, $languages)) {
             continue;
         }
         $distance[] = array('lang' => $lang, 'score' => $this->distance->distance($data, $ngrams, $total));
     }
     usort($distance, function ($a, $b) {
         return $a['score'] > $b['score'] ? -1 : 1;
     });
     return $distance;
 }