-data Input = Input
- { inputText :: Text
- , inputPhonetic :: [IPA.Syllable []]
- , inputMeaning :: Maybe ShortText
- }
-
-patterns :: Map PatKey PatNode
-patterns =
- [ PatKeyNext (PatContextChar 'a') :=
- [ PatKeyNext PatContextLexicalBorder :=
- PatEnd ["a" := "ə"]
- ]
- , PatKeyNext (PatContextChar 't') :=
- [ PatKeyNext (PatContextChar 'h') :=
- [ PatKeyNext (PatContextChar 'e') :=
- [ PatKeyNext (PatContextLexicalCategory Char.Space) :=
- PatEnd ["the" := "zi"]
- ]
- ]
- ]
- ]
-
-data State = State
- { stateInput :: LZ.Zipper Inp
- , stateBuffer :: [PatKey]
- , statePats :: Map PatKey PatNode
- , statePatReset :: Bool
- }
-
-parse :: Map PatKey PatNode -> Text -> [Inp]
-parse initPats input =
- loop
- State
- { stateInput = input & Text.unpack & fmap charToInp & LZ.fromList
- , stateBuffer = []
- , statePats = initPats
- , statePatReset = True
- }
- & stateInput
- & LZ.toList
- where
- charToInp :: Char -> Inp
- charToInp c =
- Inp
- { inpPats = [PatKeyNext (PatContextChar c)]
- , inpPronunciations = []
- }
- loop :: State -> State
- loop st =
- [ look (key, val) st
- | (key, val) <- st & statePats & Map.toList
- ]
- & catMaybes
- & headMaybe
- & fromMaybe (loop st{statePats = initPats})
- look :: (PatKey, PatNode) -> State -> Maybe State
- look kv@(key, val) st =
- case key of
- PatKeyPrev patPrev -> errorShow ("prev" :: Text)
- PatKeyNext patNext ->
- case patNext of
- PatContextLexicalBorder
- | stateInput st & LZ.endp -> match kv st
- -- PatContextChar c
- -- | Just inpNext <- stateInput st & LZ.safeCursor ->
- -- inpNext & inpPats &
- -- case of
- -- Nothing ->
- _ -> Nothing
- match kv@(key, val) st =
- case val of
- PatEnd pron ->
- Just
- st
- { statePats = initPats
- , stateBuffer = []
- , stateInput =
- stateInput st
- & LZ.insert
- Inp
- { inpPats = stateBuffer st & (key :) & List.reverse
- , inpPronunciations = pron
- }
- }
- PatNode pats ->
- Just
- st
- { statePats = pats
- , stateBuffer = key : stateBuffer st
- }