Debian Codegraph @

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

raw symbols SCIP
1 {-
2 - Language/Ding/Enrich/Example.hs - example identification and linking
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 - Identify examples in a line, remove them from it and annotate to the units
24 - they belong to.
25 -
26 - Examples are identified on the unit-level. That is, a group may contain
27 - both examples and non-examples. If it contains only non-examples, the
28 - whole entry (!) is taken off the line.
29 - As a side effect, an entry that did not contain any unit on the source side
30 - from the beginning, is also removed.
31 -
32 - Examples are only recognized when occuring right of the unit they exemplify.
33 -
34 - Note that the enrichment done by `inferExamples' only happens on the source
35 - (left) side of the dictionary, therefore the dictionary becomes directional.
36 - This is because example identification may have different consequences
37 - on both sides, in particular entries may be lost.
38 -
39 - If both directed outputs (deu-eng, eng-deu) are desired, this module's
40 - enrichment should happen separately (and therefore twice).
41 -
42 - Examples are built from a unit in a group on the left side and the set
43 - of corresponding translations, which are in the surrounding entry.
44 -
45 - Note that this enrichment should not be done on the TEI AST -- as might seem
46 - more natural -- because
47 - a) this causes unnecessary calculations on later identified examples,
48 - in particular,
49 - b) there would be IDs generated for examples, potentially causing
50 - unnecesarily high number suffixes in other (retained) entries' IDs
51 - (likely rare and not really important).
52 -}
53 module Language.Ding.Enrich.Example (inferExamples) where
54
55 import Control.Monad (when, liftM)
56 import Control.Monad.Trans.State (State, state, modify, get, evalState)
57
58 import Language.Ding.Enrich.Example.Augment (augmentEntriesWith)
59 import Language.Ding.Enrich.Example.Identify (isPotentialExample)
60 import Language.Ding.Syntax (Line(..), Entry(..), Group(..), Unit(..))
61
62 -- Notes:
63 -- * Only non-examples need to be used to search for examples.
64 -- * Examples are right of their exemplified units, so go from left to right.
65 -- * A unit is a known non-example iff it is not an example to all preceding
66 -- (non-example) units.
67 -- * Therefore, stepwise (left to right) consider units for being examples.
68 -- * Units in the same group may be considered in any order, they should
69 -- not be examples of one another.
70 -- * In any step test all previously identified non-examples for the new
71 -- unit exemplifying them.
72 -- * Two things need to happen.
73 -- * a) remove units (and possibly entries) when examples.
74 -- * b) add examples to the units they exemplify.
75 -- * This is achieved by accumulating all identified non-examples in a
76 -- state monad.
77 -- * The state's preceding non-example list is used for any new potential
78 -- example.
79 -- * Any newly identified non-example is pushed to the state.
80 -- * The entries in the state are reversed, since the state is used as a
81 -- stack.
82 -- * This is not a problem, their order does not matter when
83 -- identifying examples.
84 -- * The list needs to be reversed in the end though.
85
86
87 -- Note: State [Group] (or even State [Unit]) would suffice, at the expense of
88 -- readability / complexity of the code. Might be more efficient though.
89
90 -- | A stack state to accumulate the entries.
91 type EntryStackState = State [Entry]
92
93 -- | Push an element to a state's stack.
94 push :: s -> State [s] ()
95 push x = modify (x:)
96
97
98 -- | In a line, link any examples to the units (on the source side of the
99 -- dictionary) they are examples to and remove any found examples from
100 -- the line.
101 inferExamples :: Line -> Line
102
103 -- The entries are accumulated in reverse in the state monad, hence the need
104 -- for a reversal.
105 inferExamples (Line entries) =
106 Line $ reverse $ evalState (handleEntries entries >> get) []
107
108
109 -- | Perform the enrichment on a list of entries.
110 -- The result is pushed to the state's stack.
111 handleEntries :: [Entry] -> EntryStackState ()
112 handleEntries = mapM_ handleEntry
113
114 handleEntry :: Entry -> EntryStackState ()
115 handleEntry (Entry g gTrans) = do
116 g'@(Group us') <- handleGroup g gTrans
117 when (not $ null us') $ push $ Entry g' gTrans
118
119 handleGroup :: Group -> Group -> EntryStackState Group
120 handleGroup (Group us) (Group uTrans) = liftM Group $ handleUnits us uTrans
121
122 handleUnits :: [Unit] -> [Unit] -> EntryStackState [Unit]
123 handleUnits [] _ = return []
124 handleUnits (u:us) uTrans = do
125 mu' <- handleUnit u uTrans
126 us' <- handleUnits us uTrans
127 return $ maybe id (:) mu' us'
128
129 handleUnit :: Unit -> [Unit] -> EntryStackState (Maybe Unit)
130 handleUnit u uTrans =
131 if isPotentialExample (unitHeadword u)
132 then state $ augmentEntriesWith u uTrans
133 else return $ Just u
134
135
136 -- vi: ft=haskell ts=2 sw=2 et