Debian Codegraph @

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

raw symbols SCIP
1 {-
2 - Language/Ding/Enrich/Grammar/Infer.hs - Enrich grammar annotation within
3 - a unit.
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 - From a set of grammar annotations on a unit, infer others to apply to the
25 - same unit.
26 -
27 - Note that this works on grammar elements which are common to the Ding and
28 - TEI, however this module is deliberately placed under `Language.Ding',
29 - because the enrichment depends on inference rules which are particular to
30 - the Ding. E.g. `{f}' implies that the corresponding unit is a noun, in the
31 - Ding. There may however, in general, very well be other parts of speach
32 - bearing a gender.
33 -}
34 module Language.Ding.Enrich.Grammar.Infer
35 ( expand
36 , joinVerbAnnots
37 , joinPronounAnnots
38 ) where
39
40 import Data.List (union)
41
42 import Data.NatLang.Grammar
43
44
45 -- | Partition a list according to a function that yields `Maybe' values.
46 -- Values that are `Nothing' under the function are put in the second
47 -- partition, for the others, their `Just' value under that function is put
48 -- into the first partition.
49 -- Such a function also exists in the `utility-ht' package.
50 partitionMaybe :: (a -> Maybe b) -> [a] -> ([b], [a])
51 partitionMaybe _ [] = ([], [])
52 partitionMaybe f (x:xs) =
53 case f x of
54 Just y -> (y:ys', xs')
55 Nothing -> (ys', x:xs')
56 where
57 (ys', xs') = partitionMaybe f xs
58
59
60 getVTypes :: GrammarInfo -> Maybe [VerbType]
61 getVTypes (GramLexCategory (PartOfSpeech (Verb vTypes))) = Just vTypes
62 getVTypes _ = Nothing
63
64 getPTypes :: GrammarInfo -> Maybe [PronounType]
65 getPTypes (GramLexCategory (PartOfSpeech (Pronoun pTypes))) = Just pTypes
66 getPTypes _ = Nothing
67
68
69 -- (TODO): Consider to do a `nub' somewhen.
70
71 -- | Merge several verb annotations in a list, if any, to a single one. The
72 -- resulting annotation replaces the first of the old annotations
73 joinVerbAnnots :: [GrammarInfo] -> [GrammarInfo]
74
75 -- Note: The location of any first verb annotation is kept.
76 joinVerbAnnots (GramLexCategory (PartOfSpeech (Verb vTypes)) : as)
77 = (GramLexCategory $ PartOfSpeech $ Verb vTypes') : as'
78 where
79 (vTypess', as') = partitionMaybe getVTypes as
80 vTypes' = foldr union [] (vTypes : vTypess')
81 joinVerbAnnots (a:as) = a : joinVerbAnnots as
82 joinVerbAnnots [] = []
83
84
85 -- | Merge several pronoun annotations in a list, if any, to a single one. The
86 -- resulting annotation replaces the first of the old annotations
87 joinPronounAnnots :: [GrammarInfo] -> [GrammarInfo]
88
89 -- Note: The location of any first pronoun annotation is kept.
90 joinPronounAnnots (GramLexCategory (PartOfSpeech (Pronoun pTypes)) : as)
91 = (GramLexCategory $ PartOfSpeech $ Pronoun pTypes') : as'
92 where
93 (pTypess', as') = partitionMaybe getPTypes as
94 pTypes' = foldr union [] (pTypes : pTypess')
95 joinPronounAnnots (a:as) = a : joinPronounAnnots as
96 joinPronounAnnots [] = []
97
98
99 isPlural :: GrammarInfo -> Bool
100 isPlural (GramLexCategory (Number (Plural _))) = True
101 isPlural _ = False
102
103
104 -- | From a list of (all) grammar annotations on some entity, infer others,
105 -- which are not explicitly specified in the Ding source.
106 --
107 -- For most annotations, this does not infer anything.
108 --
109 -- This function should not be applied after any grammar annotation is
110 -- removed (e.g., when relocating an annotation to a higher level).
111 --
112 -- If the argument does not contain duplicates (recommended), neither does
113 -- the result.
114 expand :: [GrammarInfo] -> [GrammarInfo]
115 expand as =
116 let as' = concatMap infer as
117
118 -- Notes:
119 -- * union first removes duplicates in as', then deletes any element from
120 -- it that also is in as (see implementation of union).
121 -- This is exactly what we want.
122 -- * For longer lists, this would be quite inefficient, but in particular
123 -- as' is expected to have few unique elements.
124 in as `union` as'
125
126 where
127
128 infer :: GrammarInfo -> [GrammarInfo]
129 infer (GramLexCategory gram) = map GramLexCategory $ inferGLC gram
130 infer _ = []
131
132 -- Infer from single annotations.
133 -- Note that {noun} implies {sing} iff {pl} is not annotated.
134 inferGLC :: GramLexCategory -> [GramLexCategory]
135 inferGLC (Gender _)
136 | any isPlural as = [PartOfSpeech Noun]
137 | otherwise = [PartOfSpeech Noun, Number $ Singular False]
138 inferGLC (Number (Singular True)) = [PartOfSpeech Noun]
139 inferGLC (Number (Plural True)) = [PartOfSpeech Noun]
140 inferGLC _ = []
141
142
143 -- vi: ft=haskell ts=2 sw=2 et