| 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 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 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 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 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 |
|
| 72 |
|
| 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 |
|
| 86 |
|
| 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 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 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 |