Repository navigation
Add Nepali (ne) month names and Devanagari digit handling to DateTimeParser #864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| import java.util.logging.{Logger, Level} | ||
| import org.dbpedia.extraction.wikiparser._ | ||
| import org.dbpedia.extraction.config.dataparser.{DataParserConfig, DateTimeParserConfig} | ||
| import org.dbpedia.extraction.util.{Language, Date} | ||
| import org.dbpedia.extraction.util.{Language, Date, DevanagariDigits} | ||
| import org.dbpedia.extraction.mappings.Redirects | ||
| import scala.language.reflectiveCalls | ||
|
|
||
|
|
@@ -27,6 +27,7 @@ | |
| private val language = if(DateTimeParserConfig.supportedLanguages.contains(context.language.wikiCode)) context.language.wikiCode else "en" | ||
|
|
||
| private val months = DateTimeParserConfig.monthsMap.getOrElse(language, DateTimeParserConfig.monthsMap("en")) | ||
| private val nonGregorianDateMarkers = DateTimeParserConfig.nonGregorianDateMarkersMap.get(language).map(_.r) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nepali pages drop English Adding After this change, Nepali pages must keep the English template mapping for these templates until Nepali template support is added. Scope that mapping to the
|
||
| private val eraStr = DateTimeParserConfig.eraStrMap.getOrElse(language, DateTimeParserConfig.eraStrMap("en")) | ||
| private val cardinalityRegex = DateTimeParserConfig.cardinalityRegexMap.getOrElse(language, DateTimeParserConfig.cardinalityRegexMap("en")) | ||
| private val templates = DateTimeParserConfig.templateDateMap.getOrElse(language, Map()) | ||
|
|
@@ -150,9 +151,9 @@ | |
| for (yearProperty <- node.property(yearNum); | ||
| monthProperty <- node.property(monthNum); | ||
| dayProperty <- node.property(dayNum); | ||
| year <- yearProperty.children.collect{case TextNode(text, _, _) => 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 @@ | |
| 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check calendar markers before property splitting. For a nonfunctional date property, A value of Check the original property for calendar markers before splitting. Keep normal splitting for unmarked Gregorian date lists.
|
||
| { | ||
| 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"){ | ||
|
|
@@ -260,7 +269,7 @@ | |
| { | ||
| val eraIdentifier = getEraSign(era) | ||
| months.get(month.toLowerCase) match | ||
| { | ||
|
Check warning on line 272 in core/src/main/scala/org/dbpedia/extraction/dataparser/DateTimeParser.scala
|
||
| case Some(monthNumber) => return Some(new Date(Some((eraIdentifier+year).toInt), Some(monthNumber), Some(day.toInt), datatype)) | ||
| case None => | ||
| logger.log(Level.FINE, "Month with name '"+month+"' (language: "+language+") is unknown") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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") | ||
|
Check failure on line 22 in core/src/test/scala/org/dbpedia/extraction/util/DevanagariDigitsTest.scala
|
||
| DevanagariDigits.containsDevanagariDigit("12 January 1927") should equal (false) | ||
| } | ||
|
|
||
| it should "accept null" in | ||
| { | ||
| DevanagariDigits.toAscii(null) should equal (null) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recognize the calendar abbreviation after its year.
For
ne,xsd:gYear,"२०७२ वि.सं"misses this guard. The expression acceptsवि.संonly when digits follow it. Normalization then produces"2072 वि.सं", andcatchYearreturns Gregorian year2072instead ofNone.Allow the abbreviation without a final period before the existing Unicode boundary. Keep both
"वि.सं २०७२"and"२०७२ वि.सं"rejected.