| 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 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 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 |
|
| 91 |
type EntryStackState = State [Entry] |
| 92 |
|
| 93 |
|
| 94 |
push :: s -> State [s] () |
| 95 |
push x = modify (x:) |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 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 |
|
| 110 |
|
| 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 |