-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy paththimk.hs
93 lines (88 loc) · 3.63 KB
/
thimk.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
-- Copyright © 2010 Greg Weber and Bart Massey
-- [This program is licensed under the "3-clause ('new') BSD License"]
-- Please see the file COPYING in this distribution for license information.
import Text.SpellingSuggest
import System.Console.ParseArgs
data ArgIndex = ArgWord
| ArgDB
| ArgCoder
| ArgPrefilter
| ArgNoPrefilter
| ArgDict
| ArgChoices
deriving (Eq, Ord, Show)
main :: IO ()
main = do
-- Parse the arguments.
av <- parseArgsIO ArgsComplete argd
-- Go ahead and get the dictionary opened up.
let dictPath = getArg av ArgDict
let dbPath = getArg av ArgDB
dict <- openDictionary dbPath dictPath
-- Construct the search params.
let prefilter = prefilterate av $ dictionaryIsDB dict
let coder =
case getArg av ArgCoder of
Just codername ->
case findPhoneticCoder codername of
Just c -> c
Nothing -> usageError av ("unknown phonetic coder " ++ codername)
Nothing -> defaultPhoneticCoder
let choices = getRequiredArg av ArgChoices
let parms = SearchParams {
searchParamsFilter = prefilter,
searchParamsCoder = coder,
searchParamsChoices = choices }
let word = getRequiredArg av ArgWord
-- Do the search.
suggestions <- suggest parms dict word
putStr $ unlines suggestions
where
-- This logic for deciding when to prefilter is quite
-- complicated and probably unnecessary.
prefilterate av havedb =
case npfarg && pfarg of
True -> usageError av "prefilter schizophrenia"
False ->
case not npfarg && not havedb || pfarg of
True -> defaultWordFilter
False -> anyWordFilter
where
npfarg = gotArg av ArgNoPrefilter
pfarg = gotArg av ArgPrefilter
-- The command-line arguments.
argd = [ Arg { argIndex = ArgDict,
argName = Just "dictionary",
argAbbr = Just 'd',
argData = argDataOptional "path" ArgtypeString,
argDesc = "Dictionary file to search" },
Arg { argIndex = ArgDB,
argName = Just "pcdb",
argAbbr = Just 'p',
argData = argDataOptional "phonetic-code-db" ArgtypeString,
argDesc = "Phonetic code database to search" },
Arg { argIndex = ArgCoder,
argName = Just "coder",
argAbbr = Just 'c',
argData = argDataOptional "phonetic-coder" ArgtypeString,
argDesc = "Phonetic coder: one of soundex, phonix"},
Arg { argIndex = ArgChoices,
argName = Nothing,
argAbbr = Just 'n',
argData = argDataDefaulted "choices" ArgtypeInt 1,
argDesc = "Max number of choices to offer"},
Arg { argIndex = ArgPrefilter,
argName = Just "distance-prefilter",
argAbbr = Nothing,
argData = Nothing,
argDesc = "Discard wildly misspelled words early"},
Arg { argIndex = ArgNoPrefilter,
argName = Just "no-distance-prefilter",
argAbbr = Nothing,
argData = Nothing,
argDesc = "Do not discard wildly misspelled words early"},
Arg { argIndex = ArgWord,
argName = Nothing,
argAbbr = Nothing,
argData = argDataRequired "word" ArgtypeString,
argDesc = "Word to be looked up" } ]