Debian Codegraph @

importers/ding2tei/src/Language/Ding/Enrich/Example/Augment.hs

raw symbols SCIP
1 {-
2 - Language/Ding/Enrich/Example/Augment.hs - augment unit with examples
3 -
4 - Copyright 2020 Einhard Leichtfuß
5 -
6 - This file is part of ding2tei-haskell.
7 -
8 - ding2tei-haskell is free software: you can redistribute it and/or modify
9 - it under the terms of the GNU Affero General Public License as published
10 - by the Free Software Foundation, either version 3 of the License, or
11 - (at your option) any later version.
12 -
13 - ding2tei-haskell is distributed in the hope that it will be useful,
14 - but WITHOUT ANY WARRANTY; without even the implied warranty of
15 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 - GNU Affero General Public License for more details.
17 -
18 - You should have received a copy of the GNU Affero General Public License
19 - along with ding2tei-haskell. If not, see <https://www.gnu.org/licenses/>.
20 -}
21
22 {-|
23 - Inspect a single potential example unit considering a list of known non-
24 - examples.
25 - If the former is identified as an example for any of the latter, augment the
26 - latter with the former and mark the former as example, i.e. to be forgotten.
27 -}
28 module Language.Ding.Enrich.Example.Augment (augmentEntriesWith) where
29
30 import Control.Monad (liftM)
31 import Control.Monad.Trans.Writer (Writer, writer, runWriter)
32
33 import Data.NatLang.Example (Example(..))
34 import Language.Ding.Enrich.Example.Identify (isExampleOf)
35 import Language.Ding.Syntax (Entry(..), Group(..), Unit(..))
36
37
38 -- | Add the potential example `pex', together with its translations, to
39 -- any unit on the left side of any entry in `es'.
40 -- If any change happened, forget about the potential example (first element
41 -- of result pair is `Nothing'), otherwise `Just' keep it.
42 augmentEntriesWith :: Unit -> [Unit] -> [Entry] -> (Maybe Unit, [Entry])
43 augmentEntriesWith pex pexTrans es =
44 case runWriter (updateEntries pex pexTrans es) of
45 (es', Changed True) -> (Nothing, es')
46 (_, Changed False) -> (Just pex, es)
47
48
49 newtype Changed = Changed Bool
50
51 -- | The (Bool, ||) semigroup.
52 instance Semigroup Changed where
53 (Changed b1) <> (Changed b2) = Changed $ b1 || b2
54
55 -- | The (Bool, ||, False) monoid.
56 instance Monoid Changed where
57 mempty = Changed False
58
59 -- | A writer monad used to remember whether any change was done.
60 -- Uses the natural Bool monoid with (||).
61 type ChangeWriter = Writer Changed
62
63 -- | Return a value in the `ChangeWriter' monad and indicate a change.
64 returnChanged :: a -> ChangeWriter a
65 returnChanged a = writer (a, Changed True)
66
67 -- | Return a value in the `ChangeWriter' monad and indicate that nothing
68 -- changed.
69 -- Equivalent to `return'.
70 returnUnchanged :: a -> ChangeWriter a
71 returnUnchanged a = writer (a, Changed False)
72
73
74 -- | Augment entries with the provided example, wherever it matches.
75 -- If any change happens, this is recorded in the `ChangeWriter' result.
76 updateEntries :: Unit -> [Unit] -> [Entry] -> ChangeWriter [Entry]
77 updateEntries pex pexTrans = mapM $ updateEntry pex pexTrans
78
79 updateEntry :: Unit -> [Unit] -> Entry -> ChangeWriter Entry
80 updateEntry pex pexTrans (Entry g h) = do
81 g' <- updateGroup pex pexTrans g
82 return $ Entry g' h
83
84 updateGroup :: Unit -> [Unit] -> Group -> ChangeWriter Group
85 updateGroup pex pexTrans (Group us) = liftM Group $ updateUnits pex pexTrans us
86
87 updateUnits :: Unit -> [Unit] -> [Unit] -> ChangeWriter [Unit]
88 updateUnits pex pexTrans = mapM $ updateUnit pex pexTrans
89
90 -- Note that examples are appended, using (++), to keep the order of the
91 -- examples. This inefficient for large lists. The lists are expected to be
92 -- very small though, often ending up with only a single element, if any.
93 -- - Runtime with example-identification actually seems to have (slightly)
94 -- dropped (likely due to smaller number of entries generated).
95 -- - Nevertheless, the (++) is hence ok.
96 updateUnit :: Unit -> [Unit] -> Unit -> ChangeWriter Unit
97 updateUnit pex pexTrans key = do
98 if (unitHeadword pex) `isExampleOf` (unitHeadword key)
99 then
100 returnChanged $ key { unitExamples = unitExamples key ++ ex : [] }
101 else
102 returnUnchanged key
103 where
104 ex = Example (unitPlain pex) (map unitPlain pexTrans)
105
106
107 -- vi: ft=haskell ts=2 sw=2 et