Debian Codegraph @

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

raw symbols SCIP
1 {-
2 - Language/Ding/Enrich/Grammar.hs - Enrich grammar annotation
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 - Enrich a line's grammar annotations and infer such from the presence of
24 - inflected forms.
25 -
26 - This includes inferral from and transferral of grammar annotations, see
27 - `Language.Ding.Enrich.Grammar.Infer' and
28 - `Language.Ding.Enrich.Grammar.Transfer', respectively,
29 - and inferral of grammar information (POS: verb) from the presence of
30 - inflected forms, see `Language.Ding.Enrich.Grammar.InflectedForms`.
31 -
32 - Note: This module works on lines. It could equally well work on entries,
33 - as `Language.Ding.Enrich.Grammar.Transfer' does. This is purely done to
34 - be in line with other enrichment modules.
35 -}
36 module Language.Ding.Enrich.Grammar (enrichGrammar) where
37
38 import Data.NatLang.Grammar (GrammarInfo)
39 import Language.Ding.Syntax
40 import Language.Ding.Enrich.Grammar.InflectedForms (enrichFromInflectedForms)
41 import Language.Ding.Enrich.Grammar.Infer
42 ( expand
43 , joinVerbAnnots
44 , joinPronounAnnots
45 )
46 import Language.Ding.Enrich.Grammar.Transfer (transfer)
47
48
49 -- Notes:
50 -- * The order of enrichment steps is deliberately chosen.
51 -- * First, identify verbs from the presence of inflected forms; the
52 -- inflected forms constituting the input of this step do not change
53 -- during the whole grammar enrichment process.
54 -- * Do inferral (expand) next and hence before the remainder, to allow
55 -- e.g. `POS Noun' as derived from `SingulareTantum' to be transfered.
56 -- * Do join*Annots last, to account for any new duplicates.
57 -- * All functions operate on GramLexCategory, the rest of GrammarInfo
58 -- (Collocate) is of no importance.
59 -- * It might be better to separate GramLexCategory and Collocate into
60 -- two distinct lists in the ASTs (TODO?).
61 -- * enrichFromInflectedForms only needs to apply to the english side.
62 -- * It is, however, applied to both sides -- for reasons of simplicity.
63 -- * The side's languages are unknown here.
64 -- ? TODO?: Remove duplicates from the initial list of annotations on a unit?
65 -- ? TODO?: Testing: Quickcheck.
66
67
68 -- | Enrich a line's grammar annotations.
69 enrichGrammar :: Line -> Line
70 enrichGrammar (Line es) = Line $ map enrichEntry es
71
72 enrichEntry :: Entry -> Entry
73 enrichEntry = modEntryGrammar (joinVerbAnnots . joinPronounAnnots)
74 . transfer
75 . modEntryUnit
76 ( modUnitGrammar expand
77 . enrichFromInflectedForms
78 )
79
80
81 -- | Modify the units in an entry.
82 modEntryUnit :: (Unit -> Unit) -> Entry -> Entry
83 modEntryUnit f (Entry g h) =
84 Entry (modGroupUnit f g) (modGroupUnit f h)
85
86 modGroupUnit :: (Unit -> Unit) -> Group -> Group
87 modGroupUnit f (Group us) = Group $ map f us
88
89 -- | Modify the grammar annotations of all units in an entry.
90 modEntryGrammar :: ([GrammarInfo] -> [GrammarInfo]) -> Entry -> Entry
91 modEntryGrammar f e = modEntryUnit (modUnitGrammar f) e
92
93 modUnitGrammar :: ([GrammarInfo] -> [GrammarInfo]) -> Unit -> Unit
94 modUnitGrammar f u = u { unitGrammar = f $ unitGrammar u }
95
96
97 -- vi: ft=haskell ts=2 sw=2 et