| 1 |
{- |
| 2 |
- Language/Ding/Enrich/Grammar/Transfer.hs - Transfer grammar annotation |
| 3 |
- within an entry. |
| 4 |
- |
| 5 |
- Copyright 2020 Einhard Leichtfuß |
| 6 |
- |
| 7 |
- This file is part of ding2tei-haskell. |
| 8 |
- |
| 9 |
- ding2tei-haskell is free software: you can redistribute it and/or modify |
| 10 |
- it under the terms of the GNU Affero General Public License as published |
| 11 |
- by the Free Software Foundation, either version 3 of the License, or |
| 12 |
- (at your option) any later version. |
| 13 |
- |
| 14 |
- ding2tei-haskell is distributed in the hope that it will be useful, |
| 15 |
- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
- GNU Affero General Public License for more details. |
| 18 |
- |
| 19 |
- You should have received a copy of the GNU Affero General Public License |
| 20 |
- along with ding2tei-haskell. If not, see <https://www.gnu.org/licenses/>. |
| 21 |
-} |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
module Language.Ding.Enrich.Grammar.Transfer (transfer) where |
| 30 |
|
| 31 |
|
| 32 |
import Data.Maybe (mapMaybe) |
| 33 |
import Data.List (union) |
| 34 |
|
| 35 |
import Language.Ding.Syntax (Entry(..), Group(..), Unit, unitGrammar) |
| 36 |
import Data.NatLang.Grammar |
| 37 |
|
| 38 |
|
| 39 |
-- Note |
| 40 |
-- * The things in here could also be done in an attribute grammar (~Happy). |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
transfer :: Entry -> Entry |
| 45 |
transfer (Entry g h) = Entry g' h' |
| 46 |
where |
| 47 |
-- Notice how the below only works because of lazyness (and Haskell's |
| 48 |
-- automated fixed point calculation). |
| 49 |
-- (The below two function evaluations depend on results of one another.) |
| 50 |
(as1, g') = handleGroup as2 g |
| 51 |
(as2, h') = handleGroup as1 h |
| 52 |
|
| 53 |
|
| 54 |
handleGroup :: [GrammarInfo] -> Group -> ([GrammarInfo], Group) |
| 55 |
handleGroup as (Group us) = fmap Group $ handleUnits as us |
| 56 |
|
| 57 |
handleUnits :: [GrammarInfo] -> [Unit] -> ([GrammarInfo], [Unit]) |
| 58 |
handleUnits _ [] = ([], []) |
| 59 |
|
| 60 |
-- Note: |
| 61 |
-- * (++) may cause many duplicates here. Since the lists are short, it |
| 62 |
-- is however ok (possibly better) to remove the duplicates once in the end |
| 63 |
-- (at the leaves/units -> repeatedly). |
| 64 |
handleUnits as (u:us) = (as1 ++ as2, u':us') |
| 65 |
where |
| 66 |
-- Again, this works because of Haskell's lazyness. |
| 67 |
(as1, u') = handleUnit (as ++ as2) u |
| 68 |
(as2, us') = handleUnits (as ++ as1) us |
| 69 |
|
| 70 |
handleUnit :: [GrammarInfo] -> Unit -> ([GrammarInfo], Unit) |
| 71 |
handleUnit as u = (transferableGrammar uas, u { unitGrammar = uas `union` as }) |
| 72 |
where |
| 73 |
uas = unitGrammar u |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
transferableGrammar :: [GrammarInfo] -> [GrammarInfo] |
| 83 |
transferableGrammar = mapMaybe reduceGram |
| 84 |
|
| 85 |
|
| 86 |
-- Note: Collocations such as {+ Gen} are dropped. In many cases, co-occuring |
| 87 |
-- units share such collocations (without explicit annotation on each), |
| 88 |
-- but these cases are hard to identify. |
| 89 |
reduceGram :: GrammarInfo -> Maybe GrammarInfo |
| 90 |
reduceGram (GramLexCategory gram) = fmap GramLexCategory $ reduceGLC gram |
| 91 |
reduceGram _ = Nothing |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
reduceGLC :: GramLexCategory -> Maybe GramLexCategory |
| 102 |
reduceGLC (PartOfSpeech pos) = fmap PartOfSpeech $ reducePOS pos |
| 103 |
reduceGLC _ = Nothing |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
reducePOS :: PartOfSpeech -> Maybe PartOfSpeech |
| 114 |
reducePOS (Verb _) = Just $ Verb [] |
| 115 |
reducePOS pos = Just pos |
| 116 |
|
| 117 |
|
| 118 |
-- vi: ft=haskell ts=2 sw=2 et |