Debian Codegraph @

importers/ding2tei/src/Language/Ding/Enrich/Grammar/Transfer.hs

raw symbols SCIP
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 - Transfer grammar annotation between units in an entry.
25 - Units of the same and of the respective other language are treated the same.
26 - As of now, only part of speech annotation is considered transferable,
27 - further excluding verb types (transitivity, reflexivity).
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 -- | Transfer grammar annotations within an entry.
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 -- | From grammar information, extract any that is considered transferable to
77 -- units in the same entry (i.e., the same group and the translation group).
78 --
79 -- Duplicates may arise. These are not removed, because the result of this
80 -- application is to be merged with `union' anyways (union removes duplicates
81 -- off its second argument).
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 -- | Extract grammar information that shall be transfered to other units
95 -- in the same entry (i.e., the same group and the translation group).
96 --
97 -- Note: Handling in-language synonyms and translations the same way is not
98 -- natural, but works for a German-English dictionary with the present
99 -- grammar annotations. The grammatical case might form an exception,
100 -- but it is rarely annotated anyways (possibly even no occurence).
101 reduceGLC :: GramLexCategory -> Maybe GramLexCategory
102 reduceGLC (PartOfSpeech pos) = fmap PartOfSpeech $ reducePOS pos
103 reduceGLC _ = Nothing
104
105
106 -- | Retain all information except verb types (reflexivity & transitivity)
107 --
108 -- Notes:
109 -- * Proposition-annotations ({prp}) are often made seemingly incorrect.
110 -- * It is assumed preposition means adposition.
111 -- * See https://en.wikipedia.org/wiki/Preposition_and_postposition
112 -- * Otherwise, a preposition might be translated to a postposition.
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