mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-04 08:42:50 -06:00
+ References to modules under src/compiler have been eliminated from the PGF library (under src/runtime/haskell). Only two functions had to be moved (from GF.Data.Utilities to PGF.Utilities) to make this possible, other apparent dependencies turned out to be vacuous. + In gf.cabal, the GF executable no longer directly depends on the PGF library source directory, but only on the exposed library modules. This means that there is less duplication in gf.cabal and that the 30 modules in the PGF library will no longer be compiled twice while building GF. To make this possible, additional PGF library modules have been exposed, even though they should probably be considered for internal use only. They could be collected in a PGF.Internal module, or marked as "unstable", to make this explicit. + Also, by using the -fwarn-unused-imports flag, ~220 redundant imports were found and removed, reducing the total number of imports by ~15%.
67 lines
2.4 KiB
Haskell
67 lines
2.4 KiB
Haskell
module PGF.Morphology(Lemma,Analysis,Morpho,
|
|
buildMorpho,isInMorpho,
|
|
lookupMorpho,fullFormLexicon,
|
|
morphoMissing,morphoKnown,morphoClassify,
|
|
missingWordMsg) where
|
|
|
|
import PGF.CId
|
|
import PGF.Data
|
|
|
|
import qualified Data.Map as Map
|
|
import qualified Data.Set as Set
|
|
import qualified Data.IntMap as IntMap
|
|
import Data.Array.IArray
|
|
--import Data.List (intersperse)
|
|
import Data.Char (isDigit) ----
|
|
|
|
-- these 4 definitions depend on the datastructure used
|
|
|
|
type Lemma = CId
|
|
type Analysis = String
|
|
|
|
newtype Morpho = Morpho (Map.Map String [(Lemma,Analysis)])
|
|
|
|
buildMorpho :: PGF -> Language -> Morpho
|
|
buildMorpho pgf lang = Morpho $
|
|
case Map.lookup lang (concretes pgf) of
|
|
Just pinfo -> collectWords pinfo
|
|
Nothing -> Map.empty
|
|
|
|
collectWords pinfo = Map.fromListWith (++)
|
|
[(t, [(fun,lbls ! l)]) | (CncCat s e lbls) <- Map.elems (cnccats pinfo)
|
|
, fid <- [s..e]
|
|
, PApply funid _ <- maybe [] Set.toList (IntMap.lookup fid (productions pinfo))
|
|
, let CncFun fun lins = cncfuns pinfo ! funid
|
|
, (l,seqid) <- assocs lins
|
|
, sym <- elems (sequences pinfo ! seqid)
|
|
, t <- sym2tokns sym]
|
|
where
|
|
sym2tokns (SymKS t) = [t]
|
|
sym2tokns (SymKP ts alts) = concat (map sym2tokns ts ++ [sym2tokns sym | (syms,ps) <- alts, sym <- syms])
|
|
sym2tokns _ = []
|
|
|
|
lookupMorpho :: Morpho -> String -> [(Lemma,Analysis)]
|
|
lookupMorpho (Morpho mo) s = maybe [] id $ Map.lookup s mo
|
|
|
|
isInMorpho :: Morpho -> String -> Bool
|
|
isInMorpho (Morpho mo) s = maybe False (const True) $ Map.lookup s mo
|
|
|
|
fullFormLexicon :: Morpho -> [(String,[(Lemma,Analysis)])]
|
|
fullFormLexicon (Morpho mo) = Map.toList mo
|
|
|
|
morphoMissing :: Morpho -> [String] -> [String]
|
|
morphoMissing = morphoClassify False
|
|
|
|
morphoKnown :: Morpho -> [String] -> [String]
|
|
morphoKnown = morphoClassify True
|
|
|
|
morphoClassify :: Bool -> Morpho -> [String] -> [String]
|
|
morphoClassify k mo ws = [w | w <- ws, k /= null (lookupMorpho mo w), notLiteral w] where
|
|
notLiteral w = not (all isDigit w) ---- should be defined somewhere
|
|
|
|
missingWordMsg :: Morpho -> [String] -> String
|
|
missingWordMsg morpho ws = case morphoMissing morpho ws of
|
|
[] -> ", but all words are known"
|
|
ws -> "; unknown words: " ++ unwords ws
|
|
|