-
Notifications
You must be signed in to change notification settings - Fork 12
/
Main.hs
260 lines (156 loc) · 10 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
-- |
--
-- Module : Main
-- Copyright : Otakar Smrz 2005-2017
-- License : GPL
--
-- Maintainer : otakar-smrz users.sf.net
-- Stability : provisional
-- Portability : portable
--
-- "ElixirFM"
module Main where
import ElixirFM
import Encode.Arabic
import System.Environment
import System.Console.GetOpt
import System.IO
import Data.Char
import Data.List hiding (lookup)
import qualified Data.Map as Map
import Prelude hiding (lookup)
import Elixir.Version
data Opts = RunAction ([Opts] -> [String] -> IO ()) | TreesResolve
| ListsResolve
| FuzzyResolve
| QuickResolve
| DisplayUsage
| PrintVersion
options :: [OptDescr Opts]
options = [ Option [] ["resolve"] (NoArg (RunAction elixirResolve))
"run the 'resolve' mode",
Option [] ["inflect"] (NoArg (RunAction elixirInflect))
"run the 'inflect' mode",
Option [] ["derive"] (NoArg (RunAction elixirDerive))
"run the 'derive' mode",
Option [] ["lookup"] (NoArg (RunAction elixirLookup))
"run the 'lookup' mode\n\n",
Option [] ["merge"] (NoArg (RunAction elixirMerge))
"'merge' templates with roots\n\n",
Option [] ["lexicon"] (NoArg (RunAction elixirLexicon))
"dump the 'lexicon' data",
Option [] ["compose"] (NoArg (RunAction elixirCompose))
"dump the 'compose' data\n\n",
Option ['t'] ["trees"] (NoArg TreesResolve)
"resolve using MorphoTrees",
Option ['l'] ["lists"] (NoArg ListsResolve)
"resolve using MorphoLists",
Option ['f'] ["fuzzy"] (NoArg FuzzyResolve)
"resolve even fuzzy notation",
Option ['q'] ["quick"] (NoArg QuickResolve)
"resolve without tokenizing\n\n",
Option ['h'] ["help"] (NoArg DisplayUsage)
"program's usage and online references",
Option ['v'] ["version"] (NoArg PrintVersion)
"library version and build information" ]
copyleft = unlines ["ElixirFM (C) 2017-2005 Otakar Smrz and Viktor Bielicky",
" 2004 Markus Forsberg, 2002 Tim Buckwalter",
"GNU General Public License http://www.gnu.org/licenses/"]
synopsis = unlines [copyleft,
" http://github.com/otakar-smrz/elixir-fm/",
" http://elixir-fm.sf.net/",
" <otakar-smrz users.sf.net>",
"",
"elixir [--]MODE [--OPTIONS] [PARAMETERS]"]
main = do args <- getArgs
hSetBinaryMode stdin True
hSetBinaryMode stdout True
hSetBuffering stdin LineBuffering
hSetBuffering stdout LineBuffering
let mods = case args of [] -> []
x : xs -> case x of
'-' : '-' : y -> stop y ++ xs
'-' : _ -> args
_ -> stop x ++ xs
stop x = if any (isPrefixOf x) ["inflect", "derive"]
then ["--" ++ x, "--"]
else ["--" ++ x]
(opts, pars, errs) = getOpt Permute options mods
if null errs && not (null opts) then case head opts of
RunAction runs -> runs (tail opts) pars
PrintVersion -> tell (unlines [copyleft,
unwords ["ElixirFM",
show (pretty version)]])
DisplayUsage -> tell (usageInfo synopsis options)
_ -> warn (usageInfo synopsis options)
else warn (usageInfo synopsis options)
tell = hPutStr stdout
warn = hPutStr stderr
elixirResolve o p = interact (unlines . map (encode UTF . decode UCS . show . q . words . encode UCS . decode UTF) . rows)
where q x = case e of
"tex" -> r [ (e, Map.findWithDefault (defaults e) e z) | e <- x ]
where f' = if f then (alike, alike) else (fuzzy, fuzzy)
t' = if t then tokenize else (\ x -> [[x]])
y = (nub . filter (any isLetter)) x
z = (Map.fromList . zip y . map harmonize . resolveBy (fst f') (omitting (snd f') omits) . map t') y
"tim" -> r [ (e, Map.findWithDefault (defaults e) e z) | e <- x ]
where f' = if f then (alike, alike) else (fuzzy, fuzzy)
t' = if t then tokenize else (\ x -> [[x]])
y = (nub . filter (any ('_' /=)) . filter (any isArabic . encode UCS . decode Tim)) x
z = (Map.fromList . zip y . map harmonize . resolveBy (fst f') (omitting (snd f') omits) . map (t' . decode Tim)) y
_ -> r [ (e, Map.findWithDefault (defaults e) e z) | e <- w ]
where f' = if f then (alike, alike) else (fuzzy, fuzzy)
t' = if t then tokenize else (\ x -> [[x]])
w = (concat . map (groupBy category)) x
y = (nub . filter (any ('\x0640' /=)) . filter (any isArabic)) w
z = (Map.fromList . zip y . map harmonize . resolveBy (fst f') (omitting (snd f') omits) . map (t' . decode UCS)) y
r = case compare l s of
EQ -> singleline pretty
GT -> singleline pretty . map (\ (x, y) -> (x, morpholists y))
LT -> singleline pretty . map (\ (x, y) -> (x, morphotrees y))
l = [ () | ListsResolve <- o ]
s = [ () | TreesResolve <- o ]
f = null [ () | FuzzyResolve <- o ]
t = null [ () | QuickResolve <- o ]
e = if null p then "" else map toLower (head p)
elixirInflect o p = interact (unlines . map (show . q) . map cols . rows)
where q x = singleline id [ z | (y, b) <- c (unwords x), let t = lists d b, let u = enumerate y,
z <- [ f t u s | r <- regroup u, s <- emanate r ] ]
f x u = unwraps (\ (Nest r z) -> pretty [ (show y, inflect (Lexeme r e) x) | (y, e) <- zip u z ])
c x = [ (y, e z) | (y, z) <- reads x ] ++ [ (clips y, e z) | (y, z) <- reads x ]
e x = [ z | y <- words x, z <- convert y ]
d = [ z | y <- lists ["----------"] p, z <- convert y ]
elixirDerive o p = interact (unlines . map (show . q) . rows)
where q x = singleline id [ z | (y, b) <- c x, let t = lists d b, let u = enumerate y,
z <- [ f t u s | r <- regroup u, s <- emanate r ] ]
f x u = unwraps (\ (Nest r z) -> pretty [ (show y, derive (Lexeme r e) x) | (y, e) <- zip u z ])
c x = [ (y, e z) | (y, z) <- reads x ] ++ [ (clips y, e z) | (y, z) <- reads x ]
e x = [ z | y <- words x, z <- convert y ]
d = [ z | y <- lists ["----------"] p, z <- convert y ]
elixirLookup o p = interact (unlines . map (encode UTF . decode UCS . show . q . encode UCS . decode UTF) . rows)
where q x = if null y then empty else pretty (
if null c then if null r then if any isArabic y then (y, lookup (decode UCS y))
else case e of
"tex" -> (y, lookup y)
"tim" -> (y, lookup (decode Tim y))
_ -> (y, lookup (words y))
else if any isArabic (head r) then (head r, lookup (decode UCS (head r)))
else if null (head r) then (show "", lookup "")
else (show (unwords r), lookup (map (decode UCS) r))
else head c )
where c = [ (show y, [y]) | (y, _) <- reads x ] ++ [ (show y, [clips y]) | (y, _) <- reads x ]
r = unfoldr (\ x -> let y = reads x in if null y then Nothing else Just (head y)) x
y = unwords (words x)
e = if null p then "" else map toLower (head p)
elixirMerge o p = interact (unlines . map (show . q) . rows)
where q x = singleline id [ f (lists (lists [""] p) s) y | (y, s) <- c x ]
f x = unwraps (\ m@(Morphs _ _ _) -> text (show m) <>
align ( vcat [ joinText [show (unwords (words r)),
merge r m, twine r m] | r <- x ] ))
c x = [ (y, r s) | (y, s) <- reads x ]
r = unfoldr (\ x -> let y = reads x in if null y then Nothing else Just (head y))
elixirLexicon o p = interact (unlines . map (show . q) . rows)
where q x = vcat [ pretty z | y <- c x, z <- emanate y ]
c x = [ y | (y, _) <- reads x ] ++ [ clips y | (y, _) <- reads x ]
elixirCompose o p = (putDoc . generate d) lexicon
where d = [ z | y <- lists ["Q---------", "P[-]--------", "SP--------"] p, z <- convert y ]