| 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 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 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 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 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 |
|
| 52 |
instance Semigroup Changed where |
| 53 |
(Changed b1) <> (Changed b2) = Changed $ b1 || b2 |
| 54 |
|
| 55 |
|
| 56 |
instance Monoid Changed where |
| 57 |
mempty = Changed False |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
type ChangeWriter = Writer Changed |
| 62 |
|
| 63 |
|
| 64 |
returnChanged :: a -> ChangeWriter a |
| 65 |
returnChanged a = writer (a, Changed True) |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
returnUnchanged :: a -> ChangeWriter a |
| 71 |
returnUnchanged a = writer (a, Changed False) |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 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 |