| 1 |
{- |
| 2 |
- Language/Ding/Partial/Unit.hs - partial units |
| 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 |
|
| 24 |
|
| 25 |
|
| 26 |
module Language.Ding.Partial.Unit |
| 27 |
( PartialUnit |
| 28 |
, fromToken |
| 29 |
, fromVerbToken |
| 30 |
, toUnit |
| 31 |
, plusToken |
| 32 |
, plusGramAnnot |
| 33 |
, plusUsageAnnot |
| 34 |
, plusAbbrevAnnot |
| 35 |
, plusInflAnnot |
| 36 |
, plusSuffix |
| 37 |
, plusRef |
| 38 |
) where |
| 39 |
|
| 40 |
import Data.List.NonEmpty (NonEmpty) |
| 41 |
import qualified Data.List.NonEmpty as NEList (toList) |
| 42 |
|
| 43 |
import Data.NatLang.Grammar |
| 44 |
( GrammarInfo(GramLexCategory) |
| 45 |
, GramLexCategory(PartOfSpeech) |
| 46 |
, PartOfSpeech(Verb) |
| 47 |
) |
| 48 |
import Data.NatLang.InflectedForms (InflectedForms) |
| 49 |
import Data.NatLang.Usage (Usage) |
| 50 |
import Language.Ding.Syntax (Unit(..)) |
| 51 |
import Language.Ding.Token (Token, tokenToString) |
| 52 |
|
| 53 |
|
| 54 |
-- Notes |
| 55 |
-- * A PartialUnit contains information on a prefix of a unit. |
| 56 |
-- * It can be "modified" by appending annotations and regular unit text. |
| 57 |
-- * See below on "Modification". |
| 58 |
-- * "Modification": Inefficiency |
| 59 |
-- * A PartialUnit is immutable, hence any operation that might look like |
| 60 |
-- an update actually creates a new PartialUnit. |
| 61 |
-- * Cost is proportional to the number of fields in PartialUnit. |
| 62 |
-- * The problem could potentially be avoided by changing PartialUnit to |
| 63 |
-- essentially be a list of a sum type of all potential elements. This |
| 64 |
-- list would then later be processed by means equivalent to `plus*'. |
| 65 |
-- * All `plus*' functions are meant to be used in infix-form. |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
data PartialUnit = PartialUnit |
| 71 |
{ headwordToks :: [Token] |
| 72 |
, plainToks :: [Token] |
| 73 |
, gramAnnots :: [NonEmpty GrammarInfo] |
| 74 |
, usageAnnots :: [NonEmpty Usage] |
| 75 |
, abbrevAnnots :: [NonEmpty String] |
| 76 |
, mInfl :: Maybe InflectedForms |
| 77 |
, suffixes :: [String] |
| 78 |
, references :: [String] |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
empty :: PartialUnit |
| 83 |
empty = PartialUnit [] [] [] [] [] Nothing [] [] |
| 84 |
|
| 85 |
|
| 86 |
fromToken :: Token -> PartialUnit |
| 87 |
fromToken = (empty `plusToken`) |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
fromVerbToken :: Token -> PartialUnit |
| 92 |
fromVerbToken t = |
| 93 |
fromToken t `plusGramAnnot` pure (GramLexCategory $ PartOfSpeech $ Verb []) |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
plusToken :: PartialUnit -> Token -> PartialUnit |
| 102 |
|
| 103 |
-- Use plainToks also for the headwordToks, because they contain literal |
| 104 |
-- ()-annots. |
| 105 |
plusToken pu t = empty |
| 106 |
{ headwordToks = toks' |
| 107 |
, plainToks = toks' |
| 108 |
} |
| 109 |
where toks' = t : plainToks pu |
| 110 |
|
| 111 |
plusGramAnnot :: PartialUnit -> NonEmpty GrammarInfo -> PartialUnit |
| 112 |
plusGramAnnot pu as = pu { gramAnnots = as : gramAnnots pu } |
| 113 |
|
| 114 |
plusUsageAnnot :: PartialUnit -> NonEmpty Usage -> PartialUnit |
| 115 |
plusUsageAnnot pu as = pu { usageAnnots = as : usageAnnots pu } |
| 116 |
|
| 117 |
-- Note: |
| 118 |
-- * One might consider keeping one of the abbreviations in the example |
| 119 |
-- version (i.e., as Token) to allow for abbreviations to be retained |
| 120 |
-- verbatim when infixes of an example unit. |
| 121 |
-- * Further analysis required (TODO). |
| 122 |
plusAbbrevAnnot :: PartialUnit -> NonEmpty String -> PartialUnit |
| 123 |
plusAbbrevAnnot pu as = pu { abbrevAnnots = as : abbrevAnnots pu } |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
plusInflAnnot :: PartialUnit -> InflectedForms -> PartialUnit |
| 128 |
plusInflAnnot pu infl = pu { mInfl = Just infl } |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
plusSuffix :: PartialUnit -> (String, Token) -> PartialUnit |
| 133 |
plusSuffix pu (s, t) = pu |
| 134 |
{ suffixes = s : suffixes pu |
| 135 |
, plainToks = t : plainToks pu |
| 136 |
} |
| 137 |
|
| 138 |
plusRef :: PartialUnit -> String -> PartialUnit |
| 139 |
plusRef pu ref = pu { references = ref : references pu } |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
toUnit :: [(String, Token)] -> PartialUnit -> Unit |
| 146 |
toUnit prefs pu = Unit |
| 147 |
{ unitHeadword = tokenToString $ mconcat $ reverse $ headwordToks pu |
| 148 |
, unitPlain = tokenToString $ mconcat $ |
| 149 |
prefToks ++ (reverse $ plainToks pu) |
| 150 |
, unitGrammar = concat $ reverse $ map NEList.toList $ gramAnnots pu |
| 151 |
, unitUsages = concat $ reverse $ map NEList.toList $ usageAnnots pu |
| 152 |
, unitPrefixes = prefStrings |
| 153 |
, unitSuffixes = reverse $ suffixes pu |
| 154 |
, unitAbbrevs = concat $ reverse $ map NEList.toList $ abbrevAnnots pu |
| 155 |
, unitInflected = mInfl pu |
| 156 |
, unitReferences = reverse $ references pu |
| 157 |
, unitExamples = [] -- populated during enrichment |
| 158 |
} |
| 159 |
where |
| 160 |
(prefStrings, prefToks) = unzip prefs |
| 161 |
|
| 162 |
|
| 163 |
-- vi: ft=haskell ts=2 sw=2 et |