| 1 |
{- |
| 2 |
- App/Ding2TEI.hs - translate the Ding to TEI (AST) |
| 3 |
- |
| 4 |
- Copyright 2020-2022 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 |
module App.Ding2TEI (ding2tei) where |
| 26 |
|
| 27 |
import Control.Monad (liftM) |
| 28 |
import Control.Monad.Trans.State (State, state, evalState) |
| 29 |
import Data.Foldable (toList) |
| 30 |
import Data.List.NonEmpty (NonEmpty((:|))) |
| 31 |
|
| 32 |
-- When laziness is not required, Data.Map.Strict is to be preferred over |
| 33 |
-- the default Data.Map.Lazy. |
| 34 |
-- - See: <https://hackage.haskell.org/package/containers-0.6.2.1/docs/Data-Map-Strict.html> |
| 35 |
import qualified Data.Map.Strict as Map |
| 36 |
import Data.Map.Strict (Map) |
| 37 |
|
| 38 |
import Data.NatLang.Dictionary (Dictionary(Dictionary), Body(Body)) |
| 39 |
import Data.NatLang.Usage (Usage( Usage )) |
| 40 |
import qualified Data.NatLang.Usage as Usage |
| 41 |
import Language.Ding.Syntax (Ding) |
| 42 |
import qualified Language.Ding.Syntax as Ding |
| 43 |
import Language.TEI.Syntax (TEI) |
| 44 |
import qualified Language.TEI.Syntax as TEI |
| 45 |
import qualified Language.TEI.Syntax.Reference as TRef |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
type IdMap = Map String Int |
| 50 |
|
| 51 |
type IdState = State IdMap |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
modifyLookup :: Ord k => (Maybe a -> a) -> k -> Map k a -> (a, Map k a) |
| 58 |
|
| 59 |
-- Note that (,) is an instance of Functor, where the first argument remains |
| 60 |
-- constant upon `fmap'. (This is why the following works; v' is passed |
| 61 |
-- through.) See also the documentation of `alterF'. |
| 62 |
modifyLookup f = Map.alterF $ \ v -> let v' = f v in (v', Just v') |
| 63 |
|
| 64 |
|
| 65 |
-- Notes: |
| 66 |
-- * Computation is wrapped in the IdState monad. |
| 67 |
-- * This is to allow for the distribution of unique identifiers, which |
| 68 |
-- should be of the form `HW.n'. where `HW' is the headword and `n' is |
| 69 |
-- a number unique for that headword--naturally chosen as the entry's |
| 70 |
-- rank in the left-to-right order of headwords of the same name. |
| 71 |
-- * The IdState monad in particular is a MonadFix, which does allow to |
| 72 |
-- not only distribute identifiers using a state, but also to access |
| 73 |
-- identifiers of "later" to be constructed entries. |
| 74 |
-- * In short, MonadFix allows for bidirectional flow of information, |
| 75 |
-- albeit the state is only transferred left-to-right. |
| 76 |
-- * We use this to provide entries that stem from the same line with |
| 77 |
-- references to one another--also to later ones. |
| 78 |
-- * Alternatively, one could have separated this in two steps |
| 79 |
-- (labeling, translating), requiring an intermediate datatype (or |
| 80 |
-- some Maybe values) and a whole new set of (trivial) AST traversing |
| 81 |
-- functions. |
| 82 |
-- * See also: doc/thesis.pdf |
| 83 |
-- * There may be several orthographically identical headwords in a line. |
| 84 |
-- * Concatenating reference lists all around (often the shorter to the longer |
| 85 |
-- list) is not very efficient. To keep the order of the references, |
| 86 |
-- something like this is required though. |
| 87 |
-- * Alternative: Use a queue or a difference list. |
| 88 |
-- * Note that the lists are usually quite small. |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
ding2tei :: Ding -> TEI |
| 98 |
ding2tei (Dictionary header srcLang tgtLang body) = |
| 99 |
let |
| 100 |
(Body dingLines) = body |
| 101 |
teiEntries = evalState (convLines dingLines) Map.empty |
| 102 |
in |
| 103 |
if null teiEntries |
| 104 |
then error "No real entry found." |
| 105 |
else Dictionary header srcLang tgtLang (Body teiEntries) |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
convLines :: [Ding.Line] -> IdState [TEI.Entry] |
| 111 |
convLines = liftM concat . mapM convLine |
| 112 |
|
| 113 |
|
| 114 |
convLine :: Ding.Line -> IdState [TEI.Entry] |
| 115 |
convLine (Ding.Line entries) = liftM snd $ convEntries entries [] |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
convEntries :: [Ding.Entry] |
| 125 |
-> [TRef.Ident] |
| 126 |
-> IdState ([TRef.Ident], [TEI.Entry]) |
| 127 |
|
| 128 |
-- Notes: |
| 129 |
-- * We want to provide `convEntry' with references (identifiers) from both |
| 130 |
-- left and right of it. |
| 131 |
-- * Typically, monadic computations would only allow to pass such |
| 132 |
-- information from left to right. |
| 133 |
-- * Monads being instances of MonadFix, however, effectively are stripped |
| 134 |
-- of this restriction. |
| 135 |
-- * MonadFix has mfix, which can--analogously to fix for |
| 136 |
-- cyclic let-expressions--be used to have "information flow |
| 137 |
-- backwards"; particularly there may also be apparent cyclic |
| 138 |
-- statements. |
| 139 |
-- * We obtain references from the left, which we pass both to |
| 140 |
-- convEntry and the recursive convEntries call. |
| 141 |
-- * convEntry additionally obtains the references from the right, |
| 142 |
-- as produced by the recursive convEntries call. |
| 143 |
-- * The recursive convEntries call additionally gets the reference |
| 144 |
-- resulting from the convEntry call--as it also stems from left of |
| 145 |
-- the recursively to be processed entries. |
| 146 |
-- * Note that the order of reference concatenation somewhat matters; |
| 147 |
-- we'd like to keep them in order. |
| 148 |
-- * We could have only passed around TEI entries, these contain the |
| 149 |
-- identifiers. |
| 150 |
convEntries [] _ = return ([], []) |
| 151 |
convEntries (de:des) refs = do |
| 152 |
rec |
| 153 |
(refs1, tes1) <- convEntry de (refs ++ refs2) |
| 154 |
(refs2, tes2) <- convEntries des (refs ++ refs1) |
| 155 |
return (refs1 ++ refs2, tes1 ++ tes2) |
| 156 |
|
| 157 |
|
| 158 |
-- Naming convention: |
| 159 |
-- * gRefs: references to other group's units within the same line |
| 160 |
-- * uRefs: references to other units in the same group |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
convEntry :: Ding.Entry |
| 171 |
-> [TRef.Ident] |
| 172 |
-> IdState ([TRef.Ident], [TEI.Entry]) |
| 173 |
|
| 174 |
convEntry (Ding.Entry (Ding.Group keyUnits) valGroup) gRefs = |
| 175 |
convUnits keyUnits [] |
| 176 |
where |
| 177 |
|
| 178 |
-- Note: |
| 179 |
-- * Functions are local (in a where block) here, to not need to pass around |
| 180 |
-- the translations and `gRefs', which are only needed in the |
| 181 |
-- leaf-function, convUnit. |
| 182 |
|
| 183 |
translations = groupToTranslations valGroup |
| 184 |
|
| 185 |
|
| 186 |
-- same group (all sharing a common translation group). |
| 187 |
-- |
| 188 |
-- Resulting TEi entries are linked to one another as synonyms. |
| 189 |
convUnits :: [Ding.Unit] |
| 190 |
-> [TRef.Ident] |
| 191 |
-> IdState ([TRef.Ident], [TEI.Entry]) |
| 192 |
|
| 193 |
-- Note: Analogous to convEntries. |
| 194 |
convUnits [] _ = return ([], []) |
| 195 |
convUnits (u:us) uRefs = do |
| 196 |
rec |
| 197 |
(uRef', e ) <- convUnit u (uRefs ++ uRefs') |
| 198 |
(uRefs', es) <- convUnits us (uRefs ++ uRef' : []) |
| 199 |
return (uRef' : uRefs', e : es) |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
-- `translations', annotating it with references to synonymous and related |
| 204 |
-- entries (the local argument referring to synonymous entries, stemming |
| 205 |
-- from the same Ding entry/group). |
| 206 |
convUnit :: Ding.Unit -> [TRef.Ident] -> IdState (TRef.Ident, TEI.Entry) |
| 207 |
convUnit u uRefs = do |
| 208 |
let headword = Ding.unitHeadword u |
| 209 |
n <- nextId headword |
| 210 |
let ident = TRef.Ident headword n |
| 211 |
return (ident, makeTEIEntry u ident uRefs gRefs translations) |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
nextId :: String -> IdState Int |
| 217 |
|
| 218 |
-- Note: |
| 219 |
-- * Non-existing entries in the map are considered of value 0. |
| 220 |
-- * The first provided number is 1. |
| 221 |
-- * Dictionary people seem to like 1-based counting. |
| 222 |
nextId str = state $ \ cmap -> modifyLookup (maybe 1 (+1)) str cmap |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
groupToTranslations :: Ding.Group -> [TEI.Translation] |
| 227 |
groupToTranslations (Ding.Group us) = map unitToTranslation us |
| 228 |
where |
| 229 |
unitToTranslation :: Ding.Unit -> TEI.Translation |
| 230 |
unitToTranslation u = TEI.Translation |
| 231 |
{ TEI.translationOrth = Ding.unitHeadword u |
| 232 |
, TEI.translationGrammar = Ding.unitGrammar u |
| 233 |
|
| 234 |
-- Note: |
| 235 |
-- * Parenthesis-enclosed prefixes are translated to |
| 236 |
-- `usg[@type="colloc"]'. |
| 237 |
, TEI.translationUsages = Ding.unitUsages u |
| 238 |
++ map prefixAnnotToUsage (Ding.unitPrefixes u) |
| 239 |
, TEI.translationAbbrevs = Ding.unitAbbrevs u |
| 240 |
, TEI.translationInflected = Ding.unitInflected u |
| 241 |
, TEI.translationNotes = Ding.unitSuffixes u |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
makeTEIEntry :: Ding.Unit |
| 247 |
-> TRef.Ident |
| 248 |
-> [TRef.Ident] |
| 249 |
-> [TRef.Ident] |
| 250 |
-> [TEI.Translation] |
| 251 |
-> TEI.Entry |
| 252 |
|
| 253 |
makeTEIEntry u ident uRefs gRefs translations = TEI.Entry |
| 254 |
{ TEI.entryIdent = ident |
| 255 |
|
| 256 |
, TEI.entryForm = TEI.Form |
| 257 |
{ TEI.formOrth = Ding.unitHeadword u |
| 258 |
, TEI.formAbbrevs = Ding.unitAbbrevs u |
| 259 |
, TEI.formInflected = Ding.unitInflected u |
| 260 |
} |
| 261 |
|
| 262 |
, TEI.entryGrammar = Ding.unitGrammar u |
| 263 |
|
| 264 |
, TEI.entrySenses = [ |
| 265 |
TEI.Sense |
| 266 |
{ TEI.senseGrammar = [] |
| 267 |
, TEI.senseUsages = Ding.unitUsages u |
| 268 |
++ map prefixAnnotToUsage (Ding.unitPrefixes u) |
| 269 |
, TEI.senseTranslations = translations |
| 270 |
, TEI.senseExamples = Ding.unitExamples u |
| 271 |
, TEI.senseReferences = |
| 272 |
toList (makeRefGroup TRef.Synonymy |
| 273 |
$ map makeLinkedReference uRefs |
| 274 |
) |
| 275 |
++ toList (makeRefGroup TRef.Related |
| 276 |
$ map makeLinkedReference gRefs |
| 277 |
++ map makeTildeReference (Ding.unitReferences u) |
| 278 |
) |
| 279 |
, TEI.senseNotes = Ding.unitSuffixes u |
| 280 |
} |
| 281 |
] |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
makeRefGroup :: TRef.RefType -> [TRef.Reference] -> Maybe TRef.ReferenceGroup |
| 287 |
makeRefGroup _ [] = Nothing |
| 288 |
makeRefGroup refType (ref:refs) = Just |
| 289 |
$ TRef.ReferenceGroup refType (ref :| refs) |
| 290 |
|
| 291 |
|
| 292 |
makeLinkedReference :: TRef.Ident -> TRef.Reference |
| 293 |
makeLinkedReference ident@(TRef.Ident hw _) = |
| 294 |
TRef.Reference (Just ident) hw |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
makeTildeReference :: String -> TRef.Reference |
| 300 |
makeTildeReference = TRef.Reference Nothing |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
prefixAnnotToUsage :: String -> Usage |
| 308 |
prefixAnnotToUsage pref = Usage Usage.Colloc pref |
| 309 |
|
| 310 |
|
| 311 |
-- vi: ft=haskell ts=2 sw=2 et |