/** * Converts a year in Japanese format into Western format. * * @param $year : kanji or hiragana era name followed by digits, or era name in romaji, space and digit. I.e. : 明治33, めいじ33, Meiji 33 * @return string|array : The year(s) in Western format. * @throws Exception */ public static function toWesternYear($year) { if (Analyzer::hasKanji($year)) { $key = 'kanji'; $eraName = Helper::extractKanji($year); $eraName = $eraName[0]; $eraValue = (int) Helper::subString($year, Analyzer::length($eraName), Analyzer::length($year)); } elseif (Analyzer::hasHiragana($year)) { $key = 'kana'; $eraName = Helper::extractHiragana($year); $eraName = $eraName[0]; $eraValue = (int) Helper::subString($year, Analyzer::length($eraName), Analyzer::length($year)); } else { $key = 'romaji'; $year = strtolower($year); $parts = explode(' ', $year); $eraName = $parts[0]; $eraValue = (int) $parts[1]; } if (empty($eraName) || empty($eraValue)) { throw new Exception('Invalid year ' . $year); } $max = count(self::$mapEras); $westernYears = array(); for ($i = 0; $i < $max; $i++) { $era = self::$mapEras[$i]; $overflown = false; if (strtolower($era[$key]) == $eraName) { $eraStart = $era['year']; $westernYear = $eraStart + $eraValue - 1; if ($i < $max - 1) { $nextEra = self::$mapEras[$i + 1]; $nextEraYear = $nextEra['year']; if ($westernYear > $nextEraYear) { $overflown = true; } } $westernYears[] = array('value' => $westernYear, 'overflown' => $overflown); } } $results = array(); foreach ($westernYears as $westernYear) { if (!$westernYear['overflown']) { $results[] = $westernYear['value']; } } if (empty($results)) { throw new Exception('Year ' . $year . ' is invalid'); } elseif (count($results) == 1) { return $results[0]; } else { return $results; } }
/** * Inflects radical for the given verb to the given form * * @param array $verb * @param $form * @return array */ public static function getRadicals(array $verb, $form) { $kanji = $verb['kanji']; $kana = $verb['kana']; $type = $verb['type']; if ($type == 'k') { switch ($form) { case self::NON_PAST_NEGATIVE: case self::PAST_NEGATIVE: case self::PASSIVE: case self::PASSIVE_SUSPENSIVE: case self::IMPERATIVE_POLITE_NEGATIVE: case self::IMPERATIVE_HARD: case self::FACTITIVE: case self::FACTITIVE_SHORTENED: case self::POTENTIAL_NEUTRAL: case self::POTENTIAL_POLITE: case self::VOLITIONAL_FAMILIAR: case self::NOT_LOOK_LIKE_POLITE: case self::NOT_LOOK_LIKE_NEUTRAL: $kanaRadical = 'こ'; break; default: $kanaRadical = 'き'; } } elseif ($type == 's-i') { $kanaRadical = ''; } else { $kanaRadical = Helper::subString($kana, 0, Analyzer::length($kana) - 1); } if (!empty($kanji)) { $kanjiRadical = Helper::subString($kanji, 0, Analyzer::length($kanji) - 1); } else { $kanjiRadical = null; } return array('kanji' => $kanjiRadical, 'kana' => $kanaRadical); }
public function testLenghtOfAnEmptyString() { $result = Analyzer::length(''); $this->assertEquals(0, $result); }
/** * Transliterate proper combinaisons of latin alphabet characters into * Sokuon (http://en.wikipedia.org/wiki/Sokuon) characters. * * @param string $str String to be transliterated. * @param array $parameters Sokuon character. * * @return string Transliterated string. */ protected function transliterateSokuon($str, $parameters) { $new_str = $str; $length = Analyzer::length($str); //No need to go further. if ($length < 2) { return $new_str; } $skip = array('a', 'i', 'u', 'e', 'o', 'n'); for ($i = 1; $i < $length; $i++) { $prev_char = substr($str, $i - 1, 1); if (!in_array($prev_char, $skip)) { // Don't forget Hepburn special case: ch > tch if ($prev_char === substr($str, $i, 1) || $prev_char === 't' && substr($str, $i, 2) === 'ch') { $new_str = substr_replace($str, $parameters['sokuon'], $i - 1, 1); } } } return $new_str; }