From 05ec5a5f9c8f10f3629fca28f161899581e4d32d Mon Sep 17 00:00:00 2001 From: Ninglin Du Date: Fri, 26 Jan 2024 20:42:11 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BF=AE=E5=A4=8D=E2=80=9C=E4=B8=80?= =?UTF-8?q?=E4=B9=9D=E4=BA=94=E5=85=AD=E5=B9=B4=E7=9A=84=E6=98=A8=E5=A4=A9?= =?UTF-8?q?=E2=80=9D=E6=8A=BD=E5=8F=96=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#209)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 精简相关规则 --- .../duckling/dimension/time/date/Rules.scala | 35 +++---------------- .../duckling/dimension/time/predicates.scala | 13 ++++--- .../duckling/dimension/time/Examples.scala | 1 + 3 files changed, 15 insertions(+), 34 deletions(-) diff --git a/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/date/Rules.scala b/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/date/Rules.scala index 124ed407..c3cd130e 100644 --- a/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/date/Rules.scala +++ b/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/date/Rules.scala @@ -146,9 +146,12 @@ trait Rules extends DimRules { val ruleYearNumericWithYearSymbol = Rule( name = "date - year (numeric with year symbol)", - pattern = List(seqYearOf1000to9999.predicate, "(年?版|年)".regex), + pattern = List(seqYearOf1000to9999or00to99.predicate, "(年?版|年)".regex), prod = tokens { - case token :: _ => getIntValue(token).map(i => Token(Date, year(i.toInt))) + case token :: _ => getIntValue(token).map(i => { + val y = if (i < 30) i + 2000 else if (i < 100) i + 1900 else i + Token(Date, year(y.toInt)) + }) } ) @@ -174,34 +177,6 @@ trait Rules extends DimRules { val singleNumberPredicate = singleNumeber.predicate - val ruleTwoDigitYear = Rule( - name = "date - year (like 九八年)", - pattern = List(singleNumberPredicate, singleNumberPredicate, "(年?版|年)".regex), - prod = tokens { - case t1 :: t2 :: _ => - val y = for { - thirdDigit <- getIntValue(t1) - fourthDigit <- getIntValue(t2) - } yield { - val lastTwo = thirdDigit * 10 + fourthDigit - if (thirdDigit >= 3) 1900 + lastTwo - else 2000 + lastTwo - } - y.map(i => Token(Date, year(i.toInt))) - } - ) - - val ruleTowDigitYear06 = Rule( - name = "date - year (like 06年)", - pattern = List(raw"\d{2}".regex, "(年?版|年)".regex), - prod = singleRegexMatch { - case s => - val lastTwo = Integer.parseInt(s) - val y = adjustYear(lastTwo, s).get - Token(Date, year(y)) - } - ) - val ruleMonthNumericWithMonthSymbol = Rule( name = "date: month (numeric with month symbol)", pattern = List(isIntegerBetween(1, 12).predicate, "月(份)?".regex), diff --git a/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/predicates.scala b/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/predicates.scala index 44e35f53..055d0a9e 100644 --- a/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/predicates.scala +++ b/duckling-fork-chinese/core/src/main/scala/com/xiaomi/duckling/dimension/time/predicates.scala @@ -114,10 +114,15 @@ object predicates { val singleNumeber: Predicate = isIntegerBetween(0, 9) - val seqYearOf1000to9999: Predicate = { - case Token(DigitSequence, DigitSequenceData(seq, zh, raw)) if seq.length == 4 => - val v = seq.toDouble - v > 1000 && v < 9999 + val seqYearOf1000to9999or00to99: Predicate = { + case Token(DigitSequence, DigitSequenceData(seq, zh, raw)) => + if (seq.length == 4) { + val v = seq.toDouble + v > 1000 && v < 9999 + } else if (seq.length == 2) { + val v = seq.toDouble + v >= -0.1 && v < 99.1 + } else false } val arabicSeqOf1950to2050: Predicate = { diff --git a/duckling-fork-chinese/learning/src/main/scala/com/xiaomi/duckling/dimension/time/Examples.scala b/duckling-fork-chinese/learning/src/main/scala/com/xiaomi/duckling/dimension/time/Examples.scala index 8dc4ed42..2066d345 100644 --- a/duckling-fork-chinese/learning/src/main/scala/com/xiaomi/duckling/dimension/time/Examples.scala +++ b/duckling-fork-chinese/learning/src/main/scala/com/xiaomi/duckling/dimension/time/Examples.scala @@ -41,6 +41,7 @@ object Examples extends DimExamples { (ymd(2013, 2, 14), List("后天", "后日")), (ymd(2013, 2, 10), List("前天", "前日")), (ymd(2012, 2, 12), List("去年的今天")), + (ymd(1956, 2, 11), List("一九五六年的昨天")), (ymd(2010, 2, 12), List("去年的前年的今天")), (ymd(2013, 2, 15), List("明天的后天")), (ymd(2013, 2, 28), List("这个月月底")),