diff --git a/core/src/main/scala/org/dbpedia/extraction/config/dataparser/DateTimeParserConfig.scala b/core/src/main/scala/org/dbpedia/extraction/config/dataparser/DateTimeParserConfig.scala index bdc7279a3..aae0a6186 100644 --- a/core/src/main/scala/org/dbpedia/extraction/config/dataparser/DateTimeParserConfig.scala +++ b/core/src/main/scala/org/dbpedia/extraction/config/dataparser/DateTimeParserConfig.scala @@ -31,6 +31,14 @@ object DateTimeParserConfig "1月"->1,"2月"->2,"3月"->3,"4月"->4,"5月"->5,"6月"->6,"7月"->7,"8月"->8,"9月"->9,"10月"->10,"11月"->11,"12月"->12, "1月"->1,"2月"->2,"3月"->3,"4月"->4,"5月"->5,"6月"->6,"7月"->7,"8月"->8,"9月"->9,"10月"->10,"11月"->11,"12月"->12), "mk" -> Map("јануари"->1,"февруари"->2,"март"->3,"април"->4,"мај"->5,"јуни"->6,"јули"->7,"август"->8,"септември"->9,"октомври"->10,"ноември"->11,"декември"->12), + // Nepali: the most frequent spellings in Nepali Wikipedia (each appears at least 50 times + // before a year in the 2026-12-01 dump), plus the English names, which infobox values + // use almost as often as the Nepali ones. Bikram Sambat months are not Gregorian months + // and are listed in nonGregorianDateMarkersMap instead. + "ne" -> Map("january"->1,"february"->2,"march"->3,"april"->4,"may"->5,"june"->6,"july"->7,"august"->8,"september"->9,"october"->10,"november"->11,"december"->12, + "जनवरी"->1,"फेब्रुअरी"->2,"फरवरी"->2,"मार्च"->3,"अप्रिल"->4,"अप्रैल"->4,"मे"->5,"मई"->5,"जुन"->6,"जून"->6,"जुलाई"->7, + "अगस्ट"->8,"अगस्त"->8,"अगष्ट"->8,"सेप्टेम्बर"->9,"सितम्बर"->9,"सितंबर"->9,"अक्टोबर"->10,"अक्टुबर"->10,"अक्टूबर"->10, + "नोभेम्बर"->11,"नोवेम्बर"->11,"नवम्बर"->11,"नवंबर"->11,"डिसेम्बर"->12,"दिसम्बर"->12,"दिसंबर"->12), "nl" -> Map("januari"->1,"februari"->2,"maart"->3,"april"->4,"mei"->5,"juni"->6,"juli"->7,"augustus"->8,"september"->9,"oktober"->10,"november"->11,"december"->12), "pl" -> Map("stycznia"->1,"lutego"->2,"marca"->3,"kwietnia"->4,"maja"->5,"czerwca"->6,"lipca"->7,"sierpnia"->8,"września"->9,"października"->10,"listopada"->11,"grudnia"->12, "styczeń"->1,"luty"->2,"marzec"->3,"kwiecień"->4,"maj"->5,"czerwiec"->6,"lipiec"->7,"sierpień"->8,"wrzesień"->9,"październik"->10,"listopad"->11,"grudzień"->12), @@ -48,6 +56,15 @@ object DateTimeParserConfig //set of wiki codes for which this parser can be applied val supportedLanguages = monthsMap.keySet + // Values that are dates in another calendar and must not be read as Gregorian dates. + // Nepali: Bikram Sambat (वि.सं. / बि.सं.) and its month names. Converting these dates + // needs a Bikram Sambat calendar table and is left to a separate parser. + val nonGregorianDateMarkersMap = Map( + "ne" -> ("""(? text}.headOption; - month <- monthProperty.children.collect{case TextNode(text, _, _) => text}.headOption; - day <- dayProperty.children.collect{case TextNode(text, _, _) => text}.headOption) + year <- yearProperty.children.collect{case TextNode(text, _, _) => DevanagariDigits.toAscii(text)}.headOption; + month <- monthProperty.children.collect{case TextNode(text, _, _) => DevanagariDigits.toAscii(text)}.headOption; + day <- dayProperty.children.collect{case TextNode(text, _, _) => DevanagariDigits.toAscii(text)}.headOption) { try { @@ -188,9 +189,17 @@ class DateTimeParser ( context : None } - private def findDate(input: String) : Option[Date] = + private def findDate(rawInput: String) : Option[Date] = { + // dates in another calendar (e.g. Bikram Sambat in Nepali) must not be read as Gregorian + for (markers <- nonGregorianDateMarkers; if markers.findFirstIn(rawInput).isDefined) + { + return None + } + + // the date regexes only match ASCII digits: convert Devanagari digits (०-९) first + val input = DevanagariDigits.toAscii(rawInput) // scan for Ethiopian (geez) calendar dates if(language == "am"){ diff --git a/core/src/main/scala/org/dbpedia/extraction/util/DevanagariDigits.scala b/core/src/main/scala/org/dbpedia/extraction/util/DevanagariDigits.scala new file mode 100644 index 000000000..8ff8e68dd --- /dev/null +++ b/core/src/main/scala/org/dbpedia/extraction/util/DevanagariDigits.scala @@ -0,0 +1,22 @@ +package org.dbpedia.extraction.util + +/** + * Devanagari digits (U+0966 to U+096F, ० to ९) are used for numbers and dates in + * Nepali, Hindi, Marathi and other Wikipedias. The date regexes in DateTimeParser + * only match ASCII digits, so such dates are converted to ASCII digits first. + */ +object DevanagariDigits +{ + private val Zero = '०' + private val Nine = '९' + + def containsDevanagariDigit(input: String): Boolean = + input.exists(c => c >= Zero && c <= Nine) + + /** Replaces every Devanagari digit with the ASCII digit of the same value. */ + def toAscii(input: String): String = + { + if (input == null || !containsDevanagariDigit(input)) input + else input.map(c => if (c >= Zero && c <= Nine) ('0' + (c - Zero)).toChar else c) + } +} diff --git a/core/src/test/scala/org/dbpedia/extraction/dataparser/DateTimeParserTest.scala b/core/src/test/scala/org/dbpedia/extraction/dataparser/DateTimeParserTest.scala index 301eca57c..1cb9d5448 100644 --- a/core/src/test/scala/org/dbpedia/extraction/dataparser/DateTimeParserTest.scala +++ b/core/src/test/scala/org/dbpedia/extraction/dataparser/DateTimeParserTest.scala @@ -736,6 +736,67 @@ class DateTimeParserTest extends FlatSpec with Matchers parse("am", "xsd:date", "ጳጉሜ 5 ፳፻") should equal (Some("2008-09-10")) } +// Nepali: Gregorian dates with Nepali month names and Devanagari digits +// (the first four strings are taken from Nepali Wikipedia article leads) +"DateTimeParser" should "parse Nepali date १२ जनवरी १९२७ and return 1927-01-12" in + { + parse("ne", "xsd:date", "१२ जनवरी १९२७") should equal (Some("1927-01-12")) + } +"DateTimeParser" should "parse Nepali date १४ सेप्टेम्बर १९५४ and return 1954-09-14" in + { + parse("ne", "xsd:date", "१४ सेप्टेम्बर १९५४") should equal (Some("1954-09-14")) + } +"DateTimeParser" should "parse Nepali date २ जुन २००४ and return 2004-06-02" in + { + parse("ne", "xsd:date", "२ जुन २००४") should equal (Some("2004-06-02")) + } +"DateTimeParser" should "parse Nepali date ११ नोभेम्बर १९६० and return 1960-11-11" in + { + parse("ne", "xsd:date", "११ नोभेम्बर १९६०") should equal (Some("1960-11-11")) + } +"DateTimeParser" should "parse Nepali spelling variant अगष्ट (१५ अगष्ट १९४७) and return 1947-08-15" in + { + parse("ne", "xsd:date", "१५ अगष्ट १९४७") should equal (Some("1947-08-15")) + } +"DateTimeParser" should "parse Nepali spelling variant मई (१ मई २०२०) and return 2020-05-01" in + { + parse("ne", "xsd:date", "१ मई २०२०") should equal (Some("2020-05-01")) + } +"DateTimeParser" should "parse a Nepali month name with ASCII digits (11 डिसेम्बर 1954)" in + { + parse("ne", "xsd:date", "11 डिसेम्बर 1954") should equal (Some("1954-12-11")) + } +"DateTimeParser" should "parse an English month name in Nepali (December 11, 1954)" in + { + parse("ne", "xsd:date", "December 11, 1954") should equal (Some("1954-12-11")) + } +"DateTimeParser" should "parse a Devanagari year in Nepali (१९५४) as gYear 1954" in + { + parse("ne", "xsd:gYear", "१९५४") should equal (Some("1954")) + } +// Nepali: Bikram Sambat dates must not be read as Gregorian dates (no conversion yet) +"DateTimeParser" should "not parse the Bikram Sambat date वि.सं. २०११ मङ्सिर २६" in + { + parse("ne", "xsd:date", "वि.सं. २०११ मङ्सिर २६") should equal (None) + } +"DateTimeParser" should "not parse the Bikram Sambat date २०७२ असोज ३" in + { + parse("ne", "xsd:date", "२०७२ असोज ३") should equal (None) + } +"DateTimeParser" should "not parse the Bikram Sambat year वि.सं. २०७२ as gYear 2072" in + { + parse("ne", "xsd:gYear", "वि.सं. २०७२") should equal (None) + } +// Devanagari digits are also converted for other languages +"DateTimeParser" should "parse Hindi date १५ अगस्त १९४७ and return 1947-08-15" in + { + parse("hi", "xsd:date", "१५ अगस्त १९४७") should equal (Some("1947-08-15")) + } +"DateTimeParser" should "parse Devanagari digits inside a date template ({{Birth date|१९५४|१२|११}})" in + { + parse("en", "xsd:date", "{{Birth date|१९५४|१२|११}}") should equal (Some("1954-12-11")) + } + private val wikiParser = WikiParser.getInstance() private def parse(language : String, datatype : String, input : String) : Option[String] = @@ -750,7 +811,7 @@ class DateTimeParserTest extends FlatSpec with Matchers val page = new WikiPage(WikiTitle.parse("TestPage", lang), input) wikiParser(page) match { - case Some(n) => dateParser.parse(n).map(_.toString) + case Some(n) => dateParser.parse(n).map(_.value.toString) case None => None } diff --git a/core/src/test/scala/org/dbpedia/extraction/util/DevanagariDigitsTest.scala b/core/src/test/scala/org/dbpedia/extraction/util/DevanagariDigitsTest.scala new file mode 100644 index 000000000..ee56b6b77 --- /dev/null +++ b/core/src/test/scala/org/dbpedia/extraction/util/DevanagariDigitsTest.scala @@ -0,0 +1,30 @@ +package org.dbpedia.extraction.util + +import org.scalatest.{FlatSpec, Matchers} +import org.scalatest.junit.JUnitRunner +import org.junit.runner.RunWith + +@RunWith(classOf[JUnitRunner]) +class DevanagariDigitsTest extends FlatSpec with Matchers +{ + "DevanagariDigits" should "convert all ten Devanagari digits" in + { + DevanagariDigits.toAscii("०१२३४५६७८९") should equal ("0123456789") + } + + it should "convert only the digits and keep the rest of the text" in + { + DevanagariDigits.toAscii("१२ जनवरी १९२७") should equal ("12 जनवरी 1927") + } + + it should "leave text without Devanagari digits unchanged" in + { + DevanagariDigits.toAscii("12 January 1927") should equal ("12 January 1927") + DevanagariDigits.containsDevanagariDigit("12 January 1927") should equal (false) + } + + it should "accept null" in + { + DevanagariDigits.toAscii(null) should equal (null) + } +}