Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Linker where
- import GrammarValue
- import Data.Map
- import Data.List (find)
- import Data.Function (on)
- data Rule = Rule {fatherCriterion, childCriterion :: Criterion, ruleWeight :: Double}
- true = const True
- type Criterion = GValue -> Bool
- data Word = Word {wordStr :: String, wordPosition :: Int, wordGV :: GValue}
- instance Show Word where
- show (Word str pos _) = str ++ ":" ++ show pos
- instance Eq Word where
- (==) = (==) `on` wordPosition
- instance Ord Word where
- (<) = (<) `on` wordPosition
- type Sentence = [Word]
- data JointType = Space | Mark | LeftBracket | RightBracket | SentenceBorder
- data Joint = Joint {jointStr :: String, jointType :: JointType}
- data Direction = DirLeft | DirRight
- deriving Eq
- type Weight = Double
- data Link = Link {
- linkFather, linkChild :: Word,
- linkDirection :: Direction,
- linkWeight :: Weight
- }
- instance Show Link where
- show (Link f c d w) = "[" ++ (show $ wordPosition f) ++ (if d == DirRight then "->" else "<-") ++ (show $ wordPosition c) ++ ": " ++ show w ++ "]"
- type LinkKey = (PartOfSpeech, PartOfSpeech)
- type LinkDictionary = Map LinkKey [Rule]
- type LinkGraph = Map (Word, Word) Link
- possibleLink :: LinkDictionary -> Word -> Word -> Maybe Link
- possibleLink dic father child | father == child = Nothing
- | otherwise = do possibles <- Data.Map.lookup (posOf father, posOf child) dic
- match <- find (\rule -> fatherCriterion rule (wordGV father) && childCriterion rule (wordGV child)) possibles
- return $ Link father child DirRight (ruleWeight match)
- where posOf = (gvPOS . wordGV)
- possibleLinks :: LinkDictionary -> Sentence -> LinkGraph
- possibleLinks dic sentence = possibleLinks' dic empty sentence sentence
- possibleLinks' dic acc _ [] = acc
- possibleLinks' dic acc sent (w:ws) = possibleLinks' dic (foldr tryInsert acc sent) sent ws
- where tryInsert word graph = case possibleLink dic word w of
- Nothing -> graph
- Just link -> insert (word, w) link graph
- testLinks = fromList [((Verb, Noun), [Rule true true 1]), ((Noun, Noun), [Rule true true 1])]
- testSentence = [Word "mama" 1 (GValue Noun Sg Nom TenseUndef), Word "romy" 2 (GValue Noun Sg Gen TenseUndef), Word "myla" 3 (GValue Verb Sg CasusUndef Past)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement