Exemplo n.º 1
0
 /**
  * {@inheritDoc}
  */
 public function load($path)
 {
     $data = Yaml::parse($path);
     $storage = new Storage();
     if (!$data) {
         return $storage;
     }
     foreach ($data as $language => $alphabetInfo) {
         $chars = Unicode::ordStr($alphabetInfo['chars']);
         $commonCharCodes = array();
         if (!empty($alphabetInfo['common_chars'])) {
             $commonCharCodes = Unicode::ordStr($alphabetInfo['common_chars']);
         }
         $multipleCharsCodes = array();
         if (!empty($alphabetInfo['multiple_chars'])) {
             $multipleChars = explode(' ', $alphabetInfo['multiple_chars']);
             foreach ($multipleChars as $multipleChar) {
                 $multipleCharsCodes[] = Unicode::ordStr($multipleChar);
             }
         }
         $alphabet = new Alphabet($language, $chars, $commonCharCodes, $multipleCharsCodes);
         $storage->add($alphabet);
     }
     return $storage;
 }
 /**
  * Detect languages
  *
  * @param string $string
  *
  * @return Languages
  */
 public function detect($string)
 {
     // Remove special chars
     $codes = Unicode::ordStr($string);
     $languages = new Languages();
     foreach ($this->getVisitors() as $visitor) {
         $visitor->visit($string, $codes, $languages);
     }
     return $languages;
 }
Exemplo n.º 3
0
 /**
  * Fix section languages by alphabet
  *
  * @param string $alphabetsFile
  * @param string $sectionsFile
  *
  * @throws \InvalidArgumentException
  */
 public static function fixSectionLanguagesByAlphabet($alphabetsFile, $sectionsFile)
 {
     if (!is_file($alphabetsFile)) {
         throw new \InvalidArgumentException(sprintf('File "%s" not found.', $alphabetsFile));
     }
     if (!is_file($sectionsFile)) {
         throw new \InvalidArgumentException(sprintf('File "%s" not found.', $sectionsFile));
     }
     $sections = Yaml::parse($sectionsFile);
     $alphabets = Yaml::parse($alphabetsFile);
     if (!$sections || !$alphabets) {
         return;
     }
     foreach ($alphabets as $language => $alphabetInfo) {
         $alphabetChars = Unicode::ordStr($alphabetInfo['chars']);
         $availableSections = array();
         foreach ($alphabetChars as $char) {
             foreach ($sections as $section => $sectionInfo) {
                 list($diapMin, $diapMax) = $sectionInfo['diap'];
                 if ($char >= $diapMin && $char <= $diapMax) {
                     $availableSections[] = $section;
                 }
             }
         }
         if (count($availableSections)) {
             foreach ($availableSections as $availableSection) {
                 if (!is_array($sections[$availableSection]['languages'])) {
                     $sections[$availableSection]['languages'] = array();
                 }
                 if (!in_array($language, $sections[$availableSection]['languages'])) {
                     $sections[$availableSection]['languages'][] = $language;
                 }
             }
         }
     }
     file_put_contents($sectionsFile, Yaml::dump($sections));
 }