-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvrt-extract-lemgrams.sh
executable file
·104 lines (85 loc) · 2.29 KB
/
vrt-extract-lemgrams.sh
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
#! /bin/sh
# -*- coding: utf-8 -*-
export LC_ALL=C
progname=`basename $0`
progdir=`dirname $0`
usage_header="$progname [options] [input.vrt ...]
Extract lemgram frequency information from VRT input and output the
data in a TSV format suitable for importing to the lemgram_index
table of the Korp MySQL database."
optspecs='
corpus-id|corpus-name=CORPUS
Korp corpus identifier is CORPUS (required).
lemgram-field=FIELD_NUM "-1" lemgram_fieldnr
Extract lemgrams from the positional attribute number FIELD_NUM.
prefix-field=FIELD_NUM prefix_fieldnr
Extract prefixes from the positional attribute number FIELD_NUM.
suffix-field=FIELD_NUM suffix_fieldnr
Extract suffixes from the positional attribute number FIELD_NUM.
'
usage_footer='If FIELD_NUM is empty or not specified, do not extract the information
in question. If FIELD_NUM is negative, count from the end (the last
attribute is -1).'
. $progdir/korp-lib.sh
# Process options
eval "$optinfo_opt_handler"
if [ "x$corpus_id" = x ]; then
error "Please specify a corpus id with --corpus-name"
fi
extract_lemgrams () {
fieldnr=$1
if [ "x$fieldnr" = x ]; then
return
fi
if [ $fieldnr -lt 0 ]; then
fieldnr='(NF - '`expr $fieldnr + 1`')'
fi
fieldtype=$2
shift
shift
if [ "$#" != "0" ]; then
cat "$@"
else
cat
fi |
grep -Ev '^<' |
awk -F' ' '{print $'"$fieldnr"'}' |
tr '|' '\n' |
grep -Ev '^(.*:[0-9]+)?$' |
perl -ne 'chomp; print "$_\t'$fieldtype'\n"'
}
combine_lemgrams () {
corpname_u=`echo $corpus_id | sed 's/.*/\U&\E/'`
sort |
uniq -c |
vrt_decode_special_chars --no-xml-entities |
perl -e '
$token = $prev_token = "";
@freqs = ("0", "0", "0");
sub output {
printf "%s\t%s\t%s\t%s\t'$corpname_u'\n", $prev_token, @freqs;
}
while (<>) {
($freq, $token, $type) = /^\s*(\d+)\s+(.+?)\t(.+)$/;
if ($prev_token && $token ne $prev_token) {
output ();
@freqs = ("0", "0", "0");
}
$freqs[$type] = $freq;
$prev_token = $token;
}
output ();'
}
make_lemgrams_tsv () {
(
extract_lemgrams "$lemgram_fieldnr" 0 "$@"
extract_lemgrams "$prefix_fieldnr" 1 "$@"
extract_lemgrams "$suffix_fieldnr" 2 "$@"
) |
combine_lemgrams
}
if [ "$#" = 0 ]; then
cat > "$tmp_prefix.in"
set -- "$tmp_prefix.in"
fi
make_lemgrams_tsv "$@"