Compare commits

..

2 Commits

2 changed files with 123 additions and 17 deletions

View File

@ -42,7 +42,7 @@ final class NodaTimeSplitter {
];
const MONTH_NAMES_HUNGARIAN = [
"01" => ['január', 'jan'],
"01" => ['január', 'januar', 'jan'],
"02" => ['február', 'feb'],
"03" => ['március', 'mar.', 'már.'],
"04" => ['április', 'apr.', 'ápr.'],
@ -58,6 +58,8 @@ final class NodaTimeSplitter {
const STRINGS_TO_CLEAN = [
"között" => "",
" рр." => "",
" рр" => "",
"nach Christus" => "",
"n. Christus" => "",
"nach Chr." => "",
@ -69,6 +71,7 @@ final class NodaTimeSplitter {
"BCE" => "v. Chr.",
"CE" => "",
"vor Christus" => "v. Chr.",
" до н. е." => "v. Chr.",
];
const STRINGS_TO_CLEAN_START = [
@ -133,6 +136,7 @@ final class NodaTimeSplitter {
];
private const REGEX_CENTURIES = '(\ |)(Jh\.|Jhd(|\.)|Jhdt(|\.)|Jahrhundert|sz|század|th century|ст|ст\.)';
private const REGEX_DECADES = '(s|er|er\ Jahre|(\-|\ )es\ évek|(\-|\ )as\ \évek|\ évek|\-es\ években|\-ті)';
/**
* Cleans input strings by trimming obsolete stuff.
@ -521,20 +525,25 @@ final class NodaTimeSplitter {
// Skip, if dates are too long and do not contain spaces (= no translatable names)
if (str_contains($datum, " ") === false && strlen($datum) > 12) return [];
$unparsed = trim(strtolower(str_replace($year, '', $datum)), ' ,.');
foreach (self::MONTH_NAMES_HUNGARIAN as $monthVal => $monthValidNames) {
if (self::stri_occurs($datum, $monthValidNames)) {
$monat = (string)$monthVal;
foreach ($monthValidNames as $name) {
$unparsed = str_replace($name, '', $unparsed);
}
break;
}
}
if (strlen($unparsed) > 5) {
return [];
}
if (empty($monat) and self::is_numeric((string)\substr($datum, 5, 2))) $monat = \substr($datum, 5, 2);
else if (empty($monat) and self::is_numeric((string)\substr($datum, 6, 2))) $monat = \substr($datum, 6, 2);
// Last four characters must contain at least one space or one dot
$lastChars = substr($datum, -4);
if (str_contains($lastChars, '.') === false && str_contains($lastChars, ' ') === false) return [];
$day = self::validateDateSubstr($datum, -2);
if (empty($day)) $day = self::validateDateSubstr($datum, -3, 2);
if (empty($day)) $day = self::validateDateSubstr($datum, -4, 2);
@ -550,6 +559,10 @@ final class NodaTimeSplitter {
}
}
if (!empty($monat) && empty($day) && preg_match('~[0-9]+~', substr($datum, -3))) {
return [];
}
if (!empty($monat) and !empty($day)) {
return [$year, $year, $monat, $day, '+', ""];
}
@ -851,14 +864,17 @@ final class NodaTimeSplitter {
}
}
if (\preg_match("/\ (\(vor|\(Vor|vor)$/", $datum)) {
if (($spacePos = \strpos($datum, " ")) === false) {
if (\preg_match("/\ (\(vor|\(Vor|vor|előtt)$/", $datum)) {
if (($spacePos = \strrpos($datum, " ")) === false) {
return [];
}
if ($output = self::attempt_splitting(\substr($datum, 0, $spacePos))) {
$output[0] = "?";
if (empty(trim($output[2], "0 .,"))) $output[1] = \strval(\intval($output[1]) - 1);
// If month and day are unknown, lower end year by one
if (empty(trim($output[2], "0 .,"))) {
$output[1] = \strval(\intval($output[1]) - 1);
}
$output[5] = "Vor";
return $output;
}
@ -932,6 +948,18 @@ final class NodaTimeSplitter {
return $output;
}
}
if (!empty(\preg_match("/^[0-9]{4}ig$/", $datum))) {
if ($output = self::attempt_splitting(\substr($datum, 0, 4))) {
$output[0] = "?";
return $output;
}
}
if (str_ends_with($datum, '-as évekig') || str_ends_with($datum, '-es évekig')) {
if ($output = self::attempt_splitting(\substr($datum, 0, -2))) {
$output[0] = "?";
return $output;
}
}
return [];
@ -1035,14 +1063,14 @@ final class NodaTimeSplitter {
$bcBceIndicator = '+';
// 20er Jahre
if (\preg_match("/^[0-9]0(er|er\ Jahre|\-es\ évek|\-as\ \évek)$/", $datum)) {
if (\preg_match("/^[0-9]0" . self::REGEX_DECADES . "$/", $datum)) {
$start = "19" . \substr($datum, 0, 2);
$ende = (string)(\intval($start) + 9);
return [$start, $ende, "00", "00", $bcBceIndicator, ""];
}
// 1920er Jahre
if (\preg_match("/^[0-9]{3}0(s|er|er\ Jahre|(\-|\ )es\ évek|(\-|\ )as\ \évek)$/", $datum)) {
if (\preg_match("/^[0-9]{3}0" . self::REGEX_DECADES . "$/", $datum)) {
$start = \substr($datum, 0, 4);
$ende = (string)(\intval($start) + 9);
return [$start, $ende, "00", "00", $bcBceIndicator, ""];
@ -1102,7 +1130,7 @@ final class NodaTimeSplitter {
// Hungarian year and month until month
// 2005.01.-02. => 2005.01.-2005.02.
if ($inputLength === 12 && (\preg_match("/^[0-9]{4}\.[0-1][0-9]\.\-[0-1][0-9]\.$/", $datum)) !== false) {
if ($inputLength === 12 && (\preg_match("/^[0-9]{4}\.[0-1][0-9]\.\-[0-1][0-9]\.$/", $datum))) {
$reconstituted = substr($datum, 0, 8) . '-';
$reconstituted .= substr($datum, 0, 4) . '.' . substr($datum, -3);
return $reconstituted;
@ -1111,7 +1139,7 @@ final class NodaTimeSplitter {
// Hungarian year and month until month without a dot after the first YYYY-MM
// 2005.01-02. => 2005.01.-2005.02.
if (in_array($inputLength, [10, 11], true) && (\preg_match("/^[0-9]{4}\.[0-1][0-9]\-[0-1][0-9](\.|)$/", $datum)) !== false) {
if (in_array($inputLength, [10, 11], true) && (\preg_match("/^[0-9]{4}\.[0-1][0-9]\-[0-1][0-9](\.|)$/", $datum))) {
$reconstituted = substr($datum, 0, 7) . '.-';
$reconstituted .= substr($datum, 0, 4) . '.' . substr(rtrim($datum, '.'), -2) . '.';
return $reconstituted;
@ -1120,7 +1148,7 @@ final class NodaTimeSplitter {
// Hungarian year and month until month
// 2005.01.01.-02.02. => 2005.01.01-2005.02.02.
// 2005.01.01-02.02 => 2005.01.01-2005.02.02.
if ($inputLength >= 16 && $inputLength <= 18 && (\preg_match("/^[0-9]{4}\.[0-1][0-9]\.[0-3][0-9](\.|)\-[0-1][0-9]\.[0-3][0-9](\.|)$/", $datum)) !== false) {
if ($inputLength >= 16 && $inputLength <= 18 && (\preg_match("/^[0-9]{4}\.[0-1][0-9]\.[0-3][0-9](\.|)\-[0-1][0-9]\.[0-3][0-9](\.|)$/", $datum))) {
$parts = explode('-', $datum);
if (count($parts) !== 2) return '';
$reconstituted = substr($datum, 0, 10) . '.-';
@ -1129,7 +1157,7 @@ final class NodaTimeSplitter {
}
// Hungarian; without trailing dots: YYYY.MM.DD-DD
if ($inputLength >= 13 && $inputLength <= 15 && (\preg_match("/^[0-9]{4}\.[0-1][0-9]\.[0-3][0-9](\.|)\-[0-3][0-9](\.|)$/", $datum)) !== false) {
if ($inputLength >= 13 && $inputLength <= 15 && \preg_match("/^[0-9]{4}\.[0-1][0-9]\.[0-3][0-9](\.|)\-[0-3][0-9](\.|)$/", $datum)) {
$parts = explode('-', $datum);
if (count($parts) !== 2) return '';
$reconstituted = substr($datum, 0, 10) . '.-';
@ -1154,8 +1182,8 @@ final class NodaTimeSplitter {
// If es évek / as évek is contained in the string (e.g. 1880-1990-es évek), there
// will be more than one hyphens
if (MD_STD::stri_contains_any($datum, ['-as évek', '-es évek'])) {
return strtr($datum, ['-as évek' => ' as évek', '-es évek' => ' es évek']);
if (MD_STD::stri_contains_any($datum, ['-as évek', '-es-évek', '-es évek'])) {
return strtr($datum, ['-as évek' => ' as évek', '-es-évek' => ' es évek', '-es évek' => ' es évek']);
}
// 1981. július-augusztus > 1981.07-08

View File

@ -500,6 +500,18 @@ final class NodaTimeSplitterTest extends TestCase {
self::assertEquals(NodaTimeSplitter::timePartsToTimeName($output), "02.01.2020");
self::assertEquals(NodaTimeSplitter::timePartsToCountingYear($output), 2020);
$output = NodaTimeSplitter::attempt_splitting("2020. januar. 2.");
self::assertEquals($output, [
0 => "2020",
1 => "2020",
2 => "01",
3 => "02",
4 => "+",
5 => "",
]);
self::assertEquals(NodaTimeSplitter::timePartsToTimeName($output), "02.01.2020");
self::assertEquals(NodaTimeSplitter::timePartsToCountingYear($output), 2020);
$output = NodaTimeSplitter::attempt_splitting("2020.01.2.");
self::assertEquals($output, [
0 => "2020",
@ -609,15 +621,27 @@ final class NodaTimeSplitterTest extends TestCase {
self::assertEquals(NodaTimeSplitter::timePartsToTimeName($output), "1920-1929");
self::assertEquals(NodaTimeSplitter::timePartsToCountingYear($output), 1925);
$output = NodaTimeSplitter::attempt_splitting("2020. Januar");
$output = NodaTimeSplitter::attempt_splitting("1920-es évekig");
self::assertEquals($output, [
0 => "?",
1 => "1929",
2 => "00",
3 => "00",
4 => "+",
5 => "",
]);
self::assertEquals(NodaTimeSplitter::timePartsToTimeName($output), "Bis 1929");
self::assertEquals(NodaTimeSplitter::timePartsToCountingYear($output), 1929);
$output = NodaTimeSplitter::attempt_splitting("2020. Januar");
self::assertEquals([
0 => "2020",
1 => "2020",
2 => "01",
3 => "00",
4 => "+",
5 => "",
]);
], $output);
self::assertEquals(NodaTimeSplitter::timePartsToTimeName($output), "Januar 2020");
self::assertEquals(NodaTimeSplitter::timePartsToCountingYear($output), 2020);
@ -729,6 +753,57 @@ final class NodaTimeSplitterTest extends TestCase {
self::assertEquals(NodaTimeSplitter::timePartsToTimeName($output), "Bis 1801 v. Chr.");
self::assertEquals(NodaTimeSplitter::timePartsToCountingYear($output), 1801);
/*
* TODO
$output = NodaTimeSplitter::attempt_splitting("1900 előtt");
self::assertEquals($output, [
0 => "?",
1 => "1899",
2 => "00",
3 => "00",
4 => "+",
5 => "",
]);
self::assertEquals(NodaTimeSplitter::timePartsToTimeName($output), "Vor 1900");
self::assertEquals(NodaTimeSplitter::timePartsToCountingYear($output), 1899);
*/
$output = NodaTimeSplitter::attempt_splitting("1900-ig");
self::assertEquals($output, [
0 => "?",
1 => "1900",
2 => "00",
3 => "00",
4 => "+",
5 => "",
]);
self::assertEquals(NodaTimeSplitter::timePartsToTimeName($output), "Bis 1900");
self::assertEquals(NodaTimeSplitter::timePartsToCountingYear($output), 1900);
$output = NodaTimeSplitter::attempt_splitting("1900ig");
self::assertEquals($output, [
0 => "?",
1 => "1900",
2 => "00",
3 => "00",
4 => "+",
5 => "",
]);
self::assertEquals(NodaTimeSplitter::timePartsToTimeName($output), "Bis 1900");
self::assertEquals(NodaTimeSplitter::timePartsToCountingYear($output), 1900);
$output = NodaTimeSplitter::attempt_splitting("2020. december előtt");
self::assertEquals($output, [
0 => "?",
1 => "2020",
2 => "12",
3 => "00",
4 => "+",
5 => "Vor",
]);
self::assertEquals(NodaTimeSplitter::timePartsToTimeName($output), "Vor Dezember 2020");
self::assertEquals(NodaTimeSplitter::timePartsToCountingYear($output), 2020);
}
/**
@ -881,6 +956,9 @@ final class NodaTimeSplitterTest extends TestCase {
$output = NodaTimeSplitter::attempt_splitting("1978. július7");
self::assertEmpty($output);
$output = NodaTimeSplitter::attempt_splitting("1978. július 7 elött");
self::assertEmpty($output);
$output = NodaTimeSplitter::attempt_splitting("Anfang September 1903");
self::assertEmpty($output);