module NLP.Concraft.Morphosyntax
( Word (..)
, mapWord
, Sent
, Choice
, mapChoice
, Positive (unPositive)
, (<+>)
, mkPositive
, best
, known
) where
import Control.Arrow (first)
import Data.Ord (comparing)
import Data.List (maximumBy)
import qualified Data.Set as S
import qualified Data.Map as M
import qualified Data.Text as T
data Word t = Word {
orth :: T.Text
, tags :: S.Set t }
deriving (Show, Read, Eq, Ord)
mapWord :: Ord b => (a -> b) -> Word a -> Word b
mapWord f Word{..} = Word
{ orth = orth
, tags = S.fromList . map f . S.toList $ tags }
type Sent t = [Word t]
type Choice t = M.Map t (Positive Double)
newtype Positive a = Positive { unPositive :: a }
deriving (Show, Eq, Ord)
(<+>) :: Num a => Positive a -> Positive a -> Positive a
Positive x <+> Positive y = Positive (x + y)
mapChoice :: Ord b => (a -> b) -> Choice a -> Choice b
mapChoice f = M.fromListWith (<+>) . map (first f) . M.toList
mkPositive :: (Num a, Ord a) => a -> Positive a
mkPositive x
| x > 0 = Positive x
| otherwise = error "mkPositive: not a positive number"
best :: Choice t -> t
best c
| M.null c = error "best: null choice"
| otherwise = fst . maximumBy (comparing snd) $ M.toList c
known :: Word t -> Bool
known = not . S.null . tags