2022-10-03 04:33:59 +03:00
|
|
|
-- Copyright (c) 2016-present, Facebook, Inc.
|
|
|
|
-- All rights reserved.
|
|
|
|
--
|
|
|
|
-- This source code is licensed under the BSD-style license found in the
|
|
|
|
-- LICENSE file in the root directory of this source tree.
|
2022-07-15 02:47:23 +03:00
|
|
|
{-# LANGUAGE DeriveFunctor #-}
|
|
|
|
|
|
|
|
module Duckling.Numeral.DE.NumParser (parseNumeral) where
|
|
|
|
|
|
|
|
import Control.Applicative
|
|
|
|
import Data.Char
|
|
|
|
import Data.Foldable
|
2022-10-03 04:33:59 +03:00
|
|
|
import Data.List
|
2022-07-15 02:47:23 +03:00
|
|
|
import Data.String
|
2022-10-03 04:33:59 +03:00
|
|
|
import Prelude
|
2022-07-15 02:47:23 +03:00
|
|
|
|
2022-10-03 04:33:59 +03:00
|
|
|
newtype Parser a = Parser {runParser :: String -> Maybe (a, String)}
|
|
|
|
deriving (Functor)
|
2022-07-15 02:47:23 +03:00
|
|
|
|
|
|
|
char :: Char -> Parser Char
|
|
|
|
char c = Parser p
|
|
|
|
where
|
|
|
|
p [] = Nothing
|
2022-10-03 04:33:59 +03:00
|
|
|
p (x : xs)
|
|
|
|
| x == c = Just (x, xs)
|
2022-07-15 02:47:23 +03:00
|
|
|
| otherwise = Nothing
|
|
|
|
|
|
|
|
instance Applicative Parser where
|
|
|
|
pure a = Parser (\s -> Just (a, s))
|
|
|
|
(Parser fp) <*> xp = Parser $ \s ->
|
|
|
|
case fp s of
|
2022-10-03 04:33:59 +03:00
|
|
|
Nothing -> Nothing
|
|
|
|
Just (f, s') -> runParser (f <$> xp) s'
|
2022-07-15 02:47:23 +03:00
|
|
|
|
|
|
|
instance Alternative Parser where
|
|
|
|
empty = Parser (const Nothing)
|
|
|
|
Parser p1 <|> Parser p2 = Parser $ liftA2 (<|>) p1 p2
|
|
|
|
|
|
|
|
type NumParser = Parser Integer
|
|
|
|
|
|
|
|
(.+.) :: NumParser -> NumParser -> NumParser
|
|
|
|
p .+. p' = (+) <$> p <*> p'
|
|
|
|
|
|
|
|
(.*.) :: NumParser -> NumParser -> NumParser
|
|
|
|
p .*. p' = (*) <$> p <*> p'
|
|
|
|
|
|
|
|
infixl 6 .+.
|
2022-10-03 04:33:59 +03:00
|
|
|
|
2022-07-15 02:47:23 +03:00
|
|
|
infixl 7 .*.
|
|
|
|
|
|
|
|
opt :: NumParser -> NumParser
|
|
|
|
opt p = p <|> Parser p'
|
|
|
|
where
|
|
|
|
p' s = Just (0, s)
|
|
|
|
|
2022-10-03 04:33:59 +03:00
|
|
|
data NumItem = NumItem
|
|
|
|
{ base :: NumParser
|
|
|
|
, plus10 :: NumParser
|
|
|
|
, times10 :: [NumParser]
|
|
|
|
}
|
2022-07-15 02:47:23 +03:00
|
|
|
|
|
|
|
defaultNumItem :: Integer -> String -> NumItem
|
2022-10-03 04:33:59 +03:00
|
|
|
defaultNumItem value form =
|
|
|
|
NumItem
|
|
|
|
{ base = p
|
|
|
|
, plus10 = p .+. ten
|
|
|
|
, times10 = [p .*. ty]
|
|
|
|
}
|
|
|
|
where
|
|
|
|
p = assign value form
|
2022-07-15 02:47:23 +03:00
|
|
|
|
|
|
|
type Assignment = Integer -> String -> NumParser
|
|
|
|
|
|
|
|
assign :: Assignment
|
|
|
|
assign value = foldr (\c p -> (1 <$ char c) .*. p) (pure value)
|
|
|
|
|
|
|
|
ten :: NumParser
|
|
|
|
ten = assign 10 "zehn"
|
|
|
|
|
|
|
|
ty :: NumParser
|
|
|
|
ty = assign 10 "zig"
|
|
|
|
|
|
|
|
hundred :: NumParser
|
|
|
|
hundred = assign 100 "hundert"
|
|
|
|
|
|
|
|
thousand :: NumParser
|
|
|
|
thousand = assign 1000 "tausend"
|
|
|
|
|
|
|
|
und :: NumParser
|
|
|
|
und = assign 0 "und"
|
|
|
|
|
|
|
|
one :: NumItem
|
2022-10-03 04:33:59 +03:00
|
|
|
one =
|
|
|
|
(defaultNumItem 1 "ein")
|
|
|
|
{ plus10 = assign 11 "elf"
|
|
|
|
, times10 = [ten]
|
|
|
|
}
|
2022-07-15 02:47:23 +03:00
|
|
|
|
|
|
|
two :: NumItem
|
2022-10-03 04:33:59 +03:00
|
|
|
two =
|
|
|
|
(defaultNumItem 2 "zwei")
|
|
|
|
{ plus10 = assign 12 "zwölf"
|
|
|
|
, times10 = [assign 20 "zwanzig"]
|
|
|
|
}
|
|
|
|
|
2022-07-15 02:47:23 +03:00
|
|
|
three :: NumItem
|
2022-10-03 04:33:59 +03:00
|
|
|
three =
|
|
|
|
(defaultNumItem 3 "drei")
|
|
|
|
{ times10 =
|
|
|
|
[ assign 30 "dreißig"
|
|
|
|
, assign 30 "dreissig"
|
|
|
|
]
|
|
|
|
}
|
2022-07-15 02:47:23 +03:00
|
|
|
|
|
|
|
four :: NumItem
|
|
|
|
four = defaultNumItem 4 "vier"
|
|
|
|
|
|
|
|
five :: NumItem
|
|
|
|
five = defaultNumItem 5 "fünf"
|
|
|
|
|
|
|
|
six :: NumItem
|
2022-10-03 04:33:59 +03:00
|
|
|
six =
|
|
|
|
(defaultNumItem 6 "sechs")
|
|
|
|
{ plus10 = assign 16 "sechzehn"
|
|
|
|
, times10 = [assign 60 "sechzig"]
|
|
|
|
}
|
2022-07-15 02:47:23 +03:00
|
|
|
|
|
|
|
seven :: NumItem
|
2022-10-03 04:33:59 +03:00
|
|
|
seven =
|
|
|
|
(defaultNumItem 7 "sieben")
|
|
|
|
{ plus10 = assign 17 "siebzehn"
|
|
|
|
, times10 = [assign 70 "siebzig"]
|
|
|
|
}
|
2022-07-15 02:47:23 +03:00
|
|
|
|
|
|
|
eight :: NumItem
|
|
|
|
eight = defaultNumItem 8 "acht"
|
|
|
|
|
|
|
|
nine :: NumItem
|
|
|
|
nine = defaultNumItem 9 "neun"
|
|
|
|
|
|
|
|
digitLexicon :: [NumItem]
|
|
|
|
digitLexicon = [one, two, three, four, five, six, seven, eight, nine]
|
|
|
|
|
|
|
|
from1to9 :: NumParser
|
|
|
|
from1to9 = foldr ((<|>) . base) empty digitLexicon
|
|
|
|
|
|
|
|
tensFrom20 :: NumParser
|
|
|
|
tensFrom20 = asum (concatMap times10 (tail digitLexicon))
|
|
|
|
|
|
|
|
from1to99 :: NumParser
|
2022-10-03 04:33:59 +03:00
|
|
|
from1to99 =
|
|
|
|
opt (from1to9 .+. und) .+. tensFrom20
|
|
|
|
<|> foldr ((<|>) . plus10) empty digitLexicon
|
|
|
|
<|> ten
|
|
|
|
<|> from1to9
|
2022-07-15 02:47:23 +03:00
|
|
|
|
|
|
|
from1to999 :: NumParser
|
|
|
|
from1to999 = opt (from1to9 .*. hundred .+. opt und) .+. opt from1to99
|
|
|
|
|
|
|
|
from1to999999 :: NumParser
|
|
|
|
from1to999999 = opt (from1to999 .*. thousand .+. opt und) .+. opt from1to999
|
|
|
|
|
|
|
|
from1to999999' :: NumParser
|
|
|
|
from1to999999' = Parser p
|
|
|
|
where
|
|
|
|
p s
|
2022-10-03 04:33:59 +03:00
|
|
|
| isPrefixOf "hundert" s || isPrefixOf "tausend" s =
|
|
|
|
runParser from1to999999 ("ein" ++ s)
|
|
|
|
| otherwise =
|
|
|
|
runParser from1to999999 s
|
2022-07-15 02:47:23 +03:00
|
|
|
|
|
|
|
fromYear1100to1999 :: NumParser
|
2022-10-03 04:33:59 +03:00
|
|
|
fromYear1100to1999 =
|
|
|
|
asum ((\n -> plus10 n .*. hundred) <$> digitLexicon)
|
|
|
|
.+. opt (opt und .+. from1to99)
|
2022-07-15 02:47:23 +03:00
|
|
|
|
|
|
|
allNumerals :: NumParser
|
2022-10-03 04:33:59 +03:00
|
|
|
allNumerals =
|
|
|
|
fromYear1100to1999
|
|
|
|
<|> from1to999999'
|
2022-07-15 02:47:23 +03:00
|
|
|
|
|
|
|
removeInflection :: (Integer, String) -> Maybe Integer
|
|
|
|
removeInflection (n, suffix)
|
|
|
|
| n `mod` 10 == 1 && suffix `elem` inflection = Just n
|
|
|
|
where
|
|
|
|
inflection = ["s", "e", "em", "en", "er", "es"]
|
|
|
|
removeInflection (n, "") = Just n
|
2022-10-03 04:33:59 +03:00
|
|
|
removeInflection _ = Nothing
|
2022-07-15 02:47:23 +03:00
|
|
|
|
|
|
|
parseNumeral :: String -> Maybe Integer
|
|
|
|
parseNumeral s = removeInflection =<< runParser allNumerals s
|