Advertisement
Yurry

Untitled

Mar 14th, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Linker where
  2.  
  3. import GrammarValue
  4. import Data.Map
  5. import Data.List (find)
  6. import Data.Function (on)
  7.  
  8. data Rule = Rule {fatherCriterion, childCriterion :: Criterion, ruleWeight :: Double}
  9. true = const True
  10.  
  11. type Criterion = GValue -> Bool
  12.  
  13. data Word = Word {wordStr :: String, wordPosition :: Int, wordGV :: GValue}
  14. instance Show Word where
  15.     show (Word str pos _) = str ++ ":" ++ show pos
  16.  
  17. instance Eq Word where
  18.     (==) = (==) `on` wordPosition
  19. instance Ord Word where
  20.     (<) = (<) `on` wordPosition
  21.  
  22. type Sentence = [Word]
  23.  
  24. data JointType = Space | Mark | LeftBracket | RightBracket | SentenceBorder
  25.  
  26. data Joint = Joint {jointStr :: String, jointType :: JointType}
  27.  
  28. data Direction = DirLeft | DirRight
  29.     deriving Eq
  30.    
  31. type Weight = Double
  32.  
  33. data Link = Link {
  34.     linkFather, linkChild :: Word,
  35.     linkDirection :: Direction,
  36.     linkWeight :: Weight
  37.     }
  38.  
  39. instance Show Link where
  40.     show (Link f c d w) = "[" ++ (show $ wordPosition f) ++ (if d == DirRight then "->" else "<-") ++ (show $ wordPosition c) ++ ": " ++ show w ++ "]"
  41.    
  42. type LinkKey = (PartOfSpeech, PartOfSpeech)
  43. type LinkDictionary = Map LinkKey [Rule]
  44.    
  45. type LinkGraph = Map (Word, Word) Link
  46.  
  47. possibleLink :: LinkDictionary -> Word -> Word -> Maybe Link
  48. possibleLink dic father child | father == child = Nothing
  49.                               | otherwise = do possibles <- Data.Map.lookup (posOf father, posOf child) dic
  50.                                                match <- find (\rule -> fatherCriterion rule (wordGV father) && childCriterion rule (wordGV child)) possibles
  51.                                                return $ Link father child DirRight (ruleWeight match)
  52.                                 where posOf = (gvPOS . wordGV)
  53.  
  54. possibleLinks :: LinkDictionary -> Sentence -> LinkGraph
  55. possibleLinks dic sentence = possibleLinks' dic empty sentence sentence
  56.  
  57. possibleLinks' dic acc _ [] = acc
  58. possibleLinks' dic acc sent (w:ws) = possibleLinks' dic (foldr tryInsert acc sent) sent ws
  59.                                      where tryInsert word graph = case possibleLink dic word w of
  60.                                                                        Nothing -> graph
  61.                                                                        Just link -> insert (word, w) link graph
  62.  
  63. testLinks = fromList [((Verb, Noun), [Rule true true 1]), ((Noun, Noun), [Rule true true 1])]
  64.  
  65. 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