Skip to content

Commit

Permalink
Improve position dimension, update time+money rules
Browse files Browse the repository at this point in the history
  • Loading branch information
scroskey-clinc committed Aug 3, 2020
1 parent 778904f commit f815191
Show file tree
Hide file tree
Showing 45 changed files with 12,845 additions and 14,056 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.stack-work/
log/
.idea/
dist-newstyle/cache/compiler
4 changes: 2 additions & 2 deletions Duckling/AmountOfMoney/EN/Rules.hs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ ruleCent :: Rule
ruleCent = Rule
{ name = "cent"
, pattern =
[ regex "cents?|penn(y|ies)|pence|sens?"
[ regex "(cent|coin|dubloon|pence|sen)s?|penn(y|ies)"
]
, prod = \_ -> Just . Token AmountOfMoney $ currencyOnly Cent
}
Expand All @@ -123,7 +123,7 @@ ruleBucks :: Rule
ruleBucks = Rule
{ name = "bucks"
, pattern =
[ regex "bucks?"
[ regex "(buck|stack|greenback|clam|bill|bone|ace|single|smacker(oonie|oo)?|jawn)s?"
]
, prod = \_ -> Just . Token AmountOfMoney $ currencyOnly Unnamed
}
Expand Down
51 changes: 28 additions & 23 deletions Duckling/Position/EN/Rules.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import Duckling.Dimensions.Types
import Duckling.Numeral.Helpers (parseInt)
import Duckling.Position.Types (PositionData(..))
import Duckling.Numeral.Types (NumeralData(..))
import Duckling.Ordinal.Types (OrdinalData(..))
import Duckling.Ordinal.Helpers
import Duckling.Position.Helpers
import Duckling.Regex.Types
Expand All @@ -41,8 +42,8 @@ ruleOrdFromStart = Rule
, regex "(from |relative to |on |in |at |to |of )?(the )?(beginning|start|first|top|left)"
]
, prod = \tokens -> case tokens of
(Token Ordinal od:_:_) ->
Just . Token Position $ position (TOrdinal.value od - 1) 1 TPosition.Start
(Token Ordinal OrdinalData{TOrdinal.value = o}:_:_) ->
Just . Token Position $ position (o - 1) 1 TPosition.Start
_ -> Nothing
}

Expand All @@ -54,8 +55,8 @@ ruleOrdFromEnd = Rule
, regex "(from |relative to |on |in |at |to |of )?(the )?(right|bottom|end|last)"
]
, prod = \tokens -> case tokens of
(Token Ordinal od:_:_) ->
Just . Token Position $ position (- TOrdinal.value od) 1 TPosition.End
(Token Ordinal OrdinalData{TOrdinal.value = o}:_:_) ->
Just . Token Position $ position (- o) 1 TPosition.End
_ -> Nothing
}

Expand All @@ -80,8 +81,8 @@ ruleNumFromEnd = Rule
, regex "(from |relative to |on |in |at |to |of )?(the )?(right|bottom|end|last)"
]
, prod = \tokens -> case tokens of
(Token Numeral NumeralData{TNumeral.value = v}:_:_) ->
Just . Token Position $ position (- 1) (floor v) TPosition.End
(Token Numeral NumeralData{TNumeral.value = v}:_:_) ->
Just . Token Position $ position (- (floor v)) (floor v) TPosition.End
_ -> Nothing
}

Expand All @@ -94,8 +95,9 @@ ruleOrdNumFromStart = Rule
, regex "(from |relative to |on |in |at |to |of )?(the )?(beginning|start|first|top|left(hand)?)"
]
, prod = \tokens -> case tokens of
(Token Ordinal od:Token Numeral NumeralData{TNumeral.value = v}:_:_) ->
Just . Token Position $ position (TOrdinal.value od - 1) (floor v) TPosition.Start
(Token Ordinal OrdinalData{TOrdinal.value = o}:Token Numeral NumeralData{TNumeral.value = v}:_:_) -> do
start <- ordMulStart o v
Just . Token Position $ position start (floor v) TPosition.Start
_ -> Nothing
}

Expand All @@ -108,16 +110,17 @@ ruleOrdNumFromEnd = Rule
, regex "(from |relative to |on |in |at |to |of )?(the )?(right(hand)?|bottom|end|last)"
]
, prod = \tokens -> case tokens of
(Token Ordinal od:Token Numeral NumeralData{TNumeral.value = v}:_:_) ->
Just . Token Position $ position (- TOrdinal.value od) (floor v) TPosition.End
(Token Ordinal OrdinalData{TOrdinal.value = o}:Token Numeral NumeralData{TNumeral.value = v}:_:_) -> do
start <- ordMulEnd o v
Just . Token Position $ position start (floor v) TPosition.End
_ -> Nothing
}

ruleStartingNum :: Rule
ruleStartingNum = Rule
{ name = "starting <integer>"
, pattern =
[regex "(starting|left(most)?|top(most)?|first|beginning|highest)"
[regex "(starting|left(most)?|top(most)?|beginning|highest)"
, Predicate isNatural
]
, prod = \tokens -> case tokens of
Expand All @@ -134,8 +137,8 @@ ruleEndingNum = Rule
, Predicate isNatural
]
, prod = \tokens -> case tokens of
(_:Token Numeral NumeralData{TNumeral.value = v}:_) ->
Just . Token Position $ position (- 1) (floor v) TPosition.End
(_:Token Numeral NumeralData{TNumeral.value = v}:_) -> do
Just . Token Position $ position (- (floor v)) (floor v) TPosition.End
_ -> Nothing
}

Expand All @@ -147,8 +150,9 @@ ruleOrdNum = Rule
, Predicate isNatural
]
, prod = \tokens -> case tokens of
(Token Ordinal od:Token Numeral NumeralData{TNumeral.value = v}:_) ->
Just . Token Position $ position (TOrdinal.value od - 1) (floor v) TPosition.Start
(Token Ordinal OrdinalData{TOrdinal.value = o}:Token Numeral NumeralData{TNumeral.value = v}:_) -> do
start <- ordMulStart o v
Just . Token Position $ position start (floor v) TPosition.Start
_ -> Nothing
}

Expand Down Expand Up @@ -184,8 +188,8 @@ ruleOrdLast = Rule
, regex "to last"
]
, prod = \tokens -> case tokens of
(Token Ordinal od:_:_) ->
Just . Token Position $ position (- TOrdinal.value od) 1 TPosition.End
(Token Ordinal OrdinalData{TOrdinal.value = o}:_:_) ->
Just . Token Position $ position (- o) 1 TPosition.End
_ -> Nothing
}

Expand All @@ -198,8 +202,9 @@ ruleOrdLastNum = Rule
, Predicate isNatural
]
, prod = \tokens -> case tokens of
(Token Ordinal od:_:Token Numeral NumeralData{TNumeral.value = v}:_) ->
Just . Token Position $ position (- TOrdinal.value od) (floor v) TPosition.End
(Token Ordinal OrdinalData{TOrdinal.value = o}:_:Token Numeral NumeralData{TNumeral.value = v}:_) -> do
start <- ordMulEnd o v
Just . Token Position $ position start (floor v) TPosition.End
_ -> Nothing
}

Expand All @@ -212,7 +217,7 @@ ruleNumInMid = Rule
]
, prod = \tokens -> case tokens of
(Token Numeral NumeralData{TNumeral.value = v}:_:_) ->
Just . Token Position $ position 0 (floor v) TPosition.Middle
Just . Token Position $ position (- floor ((v - 1) / 2)) (floor v) TPosition.Middle
_ -> Nothing
}

Expand All @@ -225,7 +230,7 @@ ruleMidNum = Rule
]
, prod = \tokens -> case tokens of
(_:Token Numeral NumeralData{TNumeral.value = v}:_) ->
Just . Token Position $ position 0 (floor v) TPosition.Middle
Just . Token Position $ position (- floor ((v - 1) / 2)) (floor v) TPosition.Middle
_ -> Nothing
}

Expand All @@ -239,8 +244,8 @@ rulePositionOfMid = Rule
, prod = \tokens -> case tokens of
(Token Position PositionData{TPosition.index = i, TPosition.value = v, TPosition.anchor = a}:_:_)
-> case a of
TPosition.Start -> Just . Token Position $ position (- i - 1) v TPosition.Middle
TPosition.End -> Just . Token Position $ position (- i) v TPosition.Middle
TPosition.Start -> Just . Token Position $ position (- i - v) v TPosition.Middle
TPosition.End -> Just . Token Position $ position (- i - v + 1) v TPosition.Middle
_ -> Nothing
_ -> Nothing
}
Expand Down
8 changes: 8 additions & 0 deletions Duckling/Position/Helpers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module Duckling.Position.Helpers
, isNatural
, position
, isNotMiddle
, ordMulEnd
, ordMulStart
) where

import Prelude
Expand All @@ -30,3 +32,9 @@ position i v a = PositionData {TPosition.value = v, TPosition.index = i, TPositi
isNotMiddle :: Predicate
isNotMiddle (Token Position PositionData {anchor = TPosition.Middle}) = False
isNotMiddle _ = False

ordMulEnd :: Int -> Double -> Maybe Int
ordMulEnd o v = Just (- (floor v) * o)

ordMulStart :: Int -> Double -> Maybe Int
ordMulStart o v = Just (((floor v) * o) - (floor v))
4 changes: 2 additions & 2 deletions Duckling/Position/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ instance Resolve PositionData where
instance ToJSON PositionData where
toJSON PositionData {value, index, anchor} = object
[ "type" .= ("value" :: Text)
, "value" .= value
, "index" .= index
, "span" .= value
, "offset" .= index
, "anchor" .= anchor
]
43 changes: 22 additions & 21 deletions Duckling/Ranking/Classifiers/AR_XX.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,25 @@ classifiers
koData =
ClassData{prior = -infinity, unseen = -1.3862943611198906,
likelihoods = HashMap.fromList [], n = 0}}),
("<integer> <unit-of-duration>",
Classifier{okData =
ClassData{prior = -infinity, unseen = -2.3978952727983707,
likelihoods = HashMap.fromList [], n = 0},
koData =
ClassData{prior = 0.0, unseen = -3.8501476017100584,
likelihoods =
HashMap.fromList
[("integer 10minute (grain)", -2.4423470353692043),
("year", -2.7300291078209855),
("integer 10year (grain)", -2.7300291078209855),
("integer (20..90)minute (grain)", -2.4423470353692043),
("month", -2.7300291078209855),
("integer (numeric)minute (grain)", -2.4423470353692043),
("integer (numeric)month (grain)", -2.7300291078209855),
("minute", -1.120591195386885),
("integer 21..99minute (grain)", -2.7300291078209855),
("integer (13..19)minute (grain)", -2.4423470353692043)],
n = 18}}),
("dual <unit-of-duration>",
Classifier{okData =
ClassData{prior = 0.0, unseen = -1.6094379124341003,
Expand Down Expand Up @@ -1553,12 +1572,13 @@ classifiers
("daymonth", -2.3513752571634776),
("single <unit-of-duration>in <named-month>",
-2.3513752571634776),
("<> <unit-of-duration>part of days", -2.3513752571634776),
("single <unit-of-duration>part of days", -2.3513752571634776),
("minutehour", -1.9459101490553135),
("single <unit-of-duration>month (integer)",
-2.3513752571634776),
("secondmonth", -1.9459101490553135)],
("secondmonth", -1.9459101490553135),
("<integer> <unit-of-duration>part of days",
-2.3513752571634776)],
n = 5}}),
("\1593\1610\1583 \1575\1604\1605\1610\1604\1575\1583",
Classifier{okData =
Expand Down Expand Up @@ -1914,25 +1934,6 @@ classifiers
("integer 3", -2.1972245773362196),
("integer 5", -2.1972245773362196)],
n = 14}}),
("<> <unit-of-duration>",
Classifier{okData =
ClassData{prior = -infinity, unseen = -2.3978952727983707,
likelihoods = HashMap.fromList [], n = 0},
koData =
ClassData{prior = 0.0, unseen = -3.8501476017100584,
likelihoods =
HashMap.fromList
[("integer 10minute (grain)", -2.4423470353692043),
("year", -2.7300291078209855),
("integer 10year (grain)", -2.7300291078209855),
("integer (20..90)minute (grain)", -2.4423470353692043),
("month", -2.7300291078209855),
("integer (numeric)minute (grain)", -2.4423470353692043),
("integer (numeric)month (grain)", -2.7300291078209855),
("minute", -1.120591195386885),
("integer 21..99minute (grain)", -2.7300291078209855),
("integer (13..19)minute (grain)", -2.4423470353692043)],
n = 18}}),
("single <unit-of-duration>",
Classifier{okData =
ClassData{prior = -4.2626798770413155,
Expand Down
Loading

0 comments on commit f815191

Please sign in to comment.