-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexttodict
executable file
·299 lines (272 loc) · 9.07 KB
/
texttodict
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/bin/bash
version=0.1.2
############################################################################
## ##
## texttodict: A shell-script to create words dictionary from pdf file ##
## using pdftotext utility ##
## ##
## Author: Pavel Loskot, [email protected] ##
## ##
## Usage: "texttodict -help" ##
## ##
## License: GNU/GPL version 2 or later. No other warranty. ##
## ##
## Configuration file may appear in future versions. ##
## ##
## Developed and tested with GNU bash version 5.2.15 ##
## ##
############################################################################
trap "echo :exiting;exit 1" SIGINT SIGTERM
set -e
export LC_CTYPE="en_US.utf8"
help_file="$0.1"
this="${0/#*\/}"
# check BASH version used is at least 5.0
printf "5.0\n${BASH_VERSION%.*}" | sort -C || echo "Warning use BASH >= 5.0"
req_commands="pdftotext aspell" # external dependencies
# check dependencies on external commands
for c in $req_commands; do
hash "$c" 2>/dev/null || err_exit "$c required but not found"
done
pdftotext_cmd='/usr/bin/pdftotext -eol unix -enc UTF-8 -nodiag -nopgbrk -q'
aspell_dict="en_US" # aspell dictionary to use
aspell_cmd="aspell --master=$aspell_dict list \
--ignore 3 --ignore-case --ignore-accents --sug-mode normal"
# check aspell English dictionary is available
$aspell_cmd <<< "" 2> /dev/null ||
err_exit "English dictionary for aspell not found."
# message to stdout (for debuggin)
DM() { echo -e "$@" >&1; }
# messages to stdout
msg0() { echo -n ""; } # no messages
msg1() { echo -e "$@" >&1; } # stdout
[[ "$@" =~ -nomsg ]] && MSG=msg0 || MSG=msg1
# exit ok
ok_exit() { $MSG "$1" ; exit 0; }
# exit with error
err_exit() { echo "Error: $1" >&2; exit 1; }
# show help and quit
[[ "$1" == @(-h|-help) ]] && { cat "$help_file" | sed '1,/^#/d'; exit 0; }
# show version number
[[ "$1" == "-version" ]] &&
ok_exit "Text to word dictionary $this version $version"
# initial checks
[[ "$#" == "0" ]] && err_exit "Use $this -help for instructions."
[[ "$#" == "1" ]] && err_exit "Nothing to do. Try $this -help"
############################################################################
# #
# Functions #
# #
############################################################################
# build dictionary for aspell
# Usage: buildaspelldict filename [-l {lang}] [-o filename]
buildaspelldict() {
[[ -f "$1" ]] || err_exit "File $1 not found."
local fin="$1" lang fout
if [[ "$(/usr/bin/file $fin)" =~ (pdf|PDF) ]]; then
$pdftotext_cmd "$fin" "$fin.txt"
$MSG "++ Converted $fin to $fin.txt"
fin="$fin.txt"
fi
shift
while [[ ! -z "$@" ]]; do
case "$1" in
-l)
lang="$2"
shift 2;;
-o)
fout="$2"
shift 2;;
-nomsg)
shift;;
*)
err_exit "Unknown option $2 in buildaspelldict."
;;
esac
done
[[ -z "$lang" ]] && lang="$aspell_dict"
[[ -z "$fout" ]] && fout="${fin%.*}.tdict"
[[ "$fout" =~ /.+ ]] || fout="./$fout"
aspell --lang="$lang" create master "$fout" < "$fin"
}
# dump content of aspell dictionary
# Usage: dumpaspelldict -l {lang} -o {filename}
dumpaspelldict() {
local lang="$aspell_dict" fout
while [[ ! -z "$@" ]]; do
case "$1" in
-l)
lang="$2"
shift 2;;
-o)
fout="$2"
shift 2;;
-nomsg)
shift;;
*)
err_exit "Unknown option $2 in dumpaspelldict."
;;
esac
done
[[ -z "$fout" ]] && fout="$lang.tdict"
[[ -f "$fout" ]] && err_exit "File $fout already exists."
aspell -d "${lang}" dump master | aspell -l "${lang}" expand | \
tr -s '[:space:]' '\n' | LC_ALL=C sort -u > "$fout"
}
# obtain bag of words from input file
# Usage: bow inputfile OPTIONS
# -ef {filename} exclude stop words in given file
# -es {string} exclude space separated words in given string
# -ep {pat} removes words matching the pattern {pat}
# -w {nubmber} exclude words shorter than {number} of characters
# -{number} truncate the output to {number} of lines
# -gt|-eq|lt|-ge|-le {number} keep only words with given occurrences
# -o filename output filename
# -so sort output by number of occurrences
# -sa sort output alphanumerically
# -sar sort output alphanumerically in reverse order
# -sl sort by length of strings
# -slr sort by length of strings in reverse order
bow() {
[[ -f "$1" ]] || err_exit "File $1 not found."
local fin="$1" s=o
if [[ "$(/usr/bin/file $fin)" =~ (pdf|PDF) ]]; then
[[ -f "${fin%.*}.txt" ]] \
&& err_exit "File ${fin%.*}.txt already exists."
$pdftotext_cmd "$fin" "${fin%.*}.txt"
$MSG "++ Converted $fin to ${fin%.*}.txt"
fin="${fin%.*}.txt"
fi
shift
ww="$(cat "$fin" | tr -dc '[:print:]\n\r' | \
sed 's/[/+=<>{}|(),*-.;:0-9"?!]/ /g' | sed 's/[][]/ /g' | \
sed 's/ /\n/g' | sed '/^\s*$/d' | \
sed '/^[0-9A-Z \t]\+$/!s/.*/\L&/')"
ww="$(echo "$ww" | sort | uniq -c | sort -rnk1)"
while [[ ! -z "$@" ]]; do
case "$1" in
-ef)
[[ -f "$2" ]] || err_exit "File $2 not found."
ww0="$(comm -13 <(cat "$2" | sort) \
<(echo "$ww" | awk '{print $2}' | sort))"
ww="$(echo "$ww" | grep -i -w "$ww0")"
shift 2;;
-es)
ww0="$(echo "$2" | sed 's/ /|/g')"
ww="$(echo "$ww" | grep -vE "$ww0")"
shift 2;;
-w)
[[ "$2" =~ [0-9][0-9]* ]] \
|| err_exit "Parameter -w must be followed by a number."
ww="$(echo "$ww" | awk -v n="$2" 'length($2)>n{print $0}')"
shift 2;;
-ep)
[[ "${2::1}" == "-" ]] && err_exit "Missing pattern after -ep"
ww="$(echo "$ww" | grep -ve "$2")"
shift 2;;
-[0-9]*)
ww="$(echo "$ww" | head $1)"
shift;;
-gt)
ww="$(echo "$ww" | awk -v v="$2" '$1>v{print $0}')"
shift 2;;
-eq)
ww="$(echo "$ww" | awk -v v="$2" '$1==v{print $0}')"
shift 2;;
-lt)
ww="$(echo "$ww" | awk -v v="$2" '$1<v{print $0}')"
shift 2;;
-ge)
ww="$(echo "$ww" | awk -v v="$2" '$1>=v{print $0}')"
shift 2;;
-le)
ww="$(echo "$ww" | awk -v v="$2" '$1<=v{print $0}')"
shift 2;;
-o)
local fout="$2"
[[ -f "$fout" ]] && err_exit "Output file $fout exists."
shift 2;;
-s*)
s=${1:2}
shift;;
-nomsg)
shift;;
*)
err_exit "Unkonwn parameter $1 in bow() function."
;;
esac
done
[[ -z "$fout" ]] && fout="${fin%.*}.tdict"
[[ -f "$fout" ]] && err_exit "Output file $fout exists."
[[ -z "$s" ]] && s=a
# output
touch "$fout"
case "$s" in
o)
echo "$ww" | awk '{ print $2}' > "$fout" ;;
a)
echo "$ww" | awk '{ print $2}' | sort > "$fout" ;;
ar)
echo "$ww" | awk '{ print $2}' | sort -r > "$fout" ;;
l)
echo "$ww" | awk '{ print $2}' | \
awk '{ print length, $0 }' | sort -n | \
cut -d" " -f2- > "$fout";;
lr)
echo "$ww" | awk '{ print $2}' | \
awk '{ print length, $0 }' | sort -n -r | \
cut -d" " -f2- > "$fout";;
*)
err_exit "Unknown sort option $s in texttodict/bow."
;;
esac
}
############################################################################
# #
# Main loop of command processor #
# #
############################################################################
case "$1" in
-dict)
# Command: -dict build [-l lang] [-o filename] inputfile
# Call: buildaspelldict inputfile [-l {lang}] [-o filename]
case "$2" in
build)
shift 2
inf="${@: -1}"
[[ -f "$inf" ]] || err_exit "Input file $inf does not exist."
set -- "${@:1:$(($#-1))}"
$MSG "<< buildaspelldict $inf $@"
buildaspelldict "$inf" "$@"
[[ "$?" == "0" ]] && $MSG ">> buildaspelldict ok" \
|| $MSG "-- buildaspelldict??"
;;
dump)
shift 2
$MSG "<< dumpaspelldict $@"
dumpaspelldict "$@"
[[ "$?" == "0" ]] && $MSG ">> dumpaspelldict ok" \
|| $MSG "-- dumpaspelldict??"
;;
*)
err_exit "Unknown option $2 for -dict command."
;;
esac
;;
-bow)
# Command: -bow [OPTIONS] inputfile
# Call: bow inputfile [OPTIONS]
shift
inf="${@: -1}"
[[ -f "$inf" ]] || err_exit "Input file $inf does not exist."
set -- "${@:1:$(($#-1))}"
$MSG "<< bow $inf $@"
bow "$inf" "$@"
[[ "$?" == "0" ]] && $MSG ">> bow ok" \
|| $MSG "-- bow??"
;;
*)
err_exit "Unknown command $1"
;;
esac
[[ "$MSG" == "msg1" ]] && ok_exit ">> Processing terminated normally."