new-words

view new-words.sh @ 12:4bbe553c1ee2

Удаление суффиксов в английских словах.

+ функция для поиска похожих слов similarity,
но она пока, хотя и присутствует в коде,
но не используется
author Igor Chubin <igor@chub.in>
date Sat Apr 03 00:27:00 2010 +0300 (2010-04-03)
parents 34d0332f238c
children 975b549364f2
line source
1 #!/bin/sh
2 cat <<HELP > /dev/null
4 Поддержка нескольких языков:
6 new-words -l lang URL
8 Например, для немецких текстов:
10 new-words -l de URL
12 Или, предварительно создав соответствующую ссылку:
14 de-words URL
16 HELP
18 WORK_DIR=~/.new-words/
19 TEMP1=`mktemp /tmp/news-words-XXXXXXXXXX`
20 TEMP2=`mktemp /tmp/news-words-XXXXXXXXXX`
21 export ORIGINAL_TEXT=`mktemp /tmp/news-words-XXXXXXXXXX-orig`
22 editor=${EDITOR:-vim}
24 LANGUAGE=en
25 my_name="`echo $0 | sed s@.*/@@ | sed s/-.*// `"
26 if echo "$1" | grep -q http://...wikipedia.org/wiki/
27 then
28 LANGUAGE="`echo $1 | sed s@http://@@ | sed s@.wikipedia.*@@`"
29 fi
30 [ "${my_name}" = "new" ] || LANGUAGE="$my_name"
31 if [ "$1" = "-l" ]
32 then
33 LANGUAGE="$2"
34 VOCABULARY="$LANGUAGE".txt
35 shift 2
36 fi
37 VOCABULARY=${LANGUAGE}.txt
38 NOTES_FILE=notes-${LANGUAGE}.txt
40 get_words()
41 {
42 tr ' ' '\n' | sed 's/--/ /g' \
43 | sed "s/'/__APOSTROPHE__/g" \
44 | tr '—·-' '-----' \
45 | tr '*\r,.:#@()+=—<>$;"?!|·[]^%&' ' ' \
46 | tr ' ' '\n' \
47 | grep -x '[[:alpha:]'"'"'-]*' \
48 | tee "$1" \
49 | grep_v_english_perl \
50 | sort | uniq -c | awk '{if ($2!="") print;}' | sort -rn
51 }
53 add_stat()
54 {
55 before="$1"
56 after=${before}2
57 cat > "$after"
58 total="`wc -w $1 | awk '{print $1}'`"
59 total_unknown="`cat $after|awk '{s=s+$1}END{print s}'`"
60 total_known="`echo $total-$total_unknown|bc`"
61 percentage="`echo '100*('$total-$total_unknown')'/$total | bc -l | sed 's/\\.\(.\).*/.\1/'`"
62 echo "# $LANGUAGE, $percentage, <$total_known/$total>"
63 cat "$after"
64 rm $after
65 }
67 grep_v_english()
68 {
69 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
70 eval $(cat $VOCABULARY | tr -d "'" | xargs -n10 echo | tr ' ' '|' | sed 's/^/egrep -xv "RRRRRRR|/' | sed 's/$/"/' | tr '\n' '|')cat
71 }
73 grep_v_english_perl()
74 {
75 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
76 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
77 open(VOC, $ENV{VOCABULARY})
78 or die "Can't open VOCABULARY";
79 while (<VOC>){
80 chomp;
81 #s/'//g;
82 $voc{$_}="1";
83 }
84 while(<>) {
85 chomp;
86 if (not defined($voc{$_})) { print "$_\n"; }
87 }
88 PERL_SCRIPT
89 [ -e "$VOCABULARY" ] || touch "$VOCABULARY"
90 export VOCABULARY
91 perl $PERL_SCRIPT_TEMP_NAME
92 rm $PERL_SCRIPT_TEMP_NAME
93 }
95 group_words()
96 {
97 if [ "$LANGUAGE" != "en" ]
98 then
99 cat
100 return
101 fi
102 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-group-words-XXXXXXXX`
103 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
104 #!/usr/bin/perl
106 eval {
107 # http://stackoverflow.com/questions/251694/how-can-i-check-if-i-have-a-perl-module-before-using-it
108 require String::Similarity;
109 String::Similarity->import();
110 };
111 unless($@)
112 {
113 our $HAVE_String_Similarity=1;
114 }
116 sub similar($$){
117 my $a=shift;
118 my $b=shift;
119 if ($HAVE_String_Similarity) {
120 return $Similarity{"$a $b"};
121 }
122 else {
123 return 0;
124 }
125 }
127 sub normalize($)
128 {
129 $_=lc(shift);
131 s/s$//;
132 s/ed$//;
133 s/en$//;
134 s/er$//;
135 s/est$//;
136 s/ing$//;
138 s/ism$//; s/ist$//; s/ful$//; s/able$//; s/ably$//;
139 s/ation$//; s/ness$//; s/ally$//;
140 s/ment$//; s/ify$//; s/ity$//; s/fy$//; s/ly$//;
141 s/ise$//; s/ize$//;
143 s/e$//;
144 return $_;
145 }
148 sub compare($$)
149 {
150 my $a=shift;
151 my $b=shift;
152 $a =~ s/^\s*//;
153 $b =~ s/^\s*//;
154 my ($a1, $a2)= split /\s+/,$a,2;
155 my ($b1, $b2)= split /\s+/,$b,2;
157 my $cmp = $group_weight{normalize($a2)} <=> $group_weight{normalize($b2)};
159 if ($cmp) {
160 return $cmp;
161 }
162 else {
163 if (normalize($a2) ne normalize($b2)) {
164 return normalize($a2) cmp normalize($b2);
165 }
166 else {
167 return $a1 <=> $b1;
168 }
169 }
170 }
172 @lines=<>;
173 for $L (@lines) {
174 chomp($L);
175 $l=$L;
176 $l =~ s/^\s*//;
177 my ($a, $b)=split(/\s+/,$l,2);
178 $group_weight{normalize($b)}+=$a;
179 }
180 @lines2 = sort { compare($b,$a) } @lines;
181 for $l (@lines2) {
182 print "$l\n";
183 }
184 PERL_SCRIPT
185 perl $PERL_SCRIPT_TEMP_NAME
186 rm $PERL_SCRIPT_TEMP_NAME
187 }
189 text_from_url()
190 {
191 lynx -dump "$1" | perl -p -e 's@http://[a-zA-Z&_.:/0-9%?=,#+()\[\]~-]*@@'
192 }
194 add_marks()
195 {
196 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
197 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
198 $file = $ARGV[0];
199 our $dict;
200 if (open(NOTES, $ENV{NOTES_FILE})) {
201 while(<NOTES>) {
202 chomp;
203 s/^\s+//;
204 my ($a,$b)=split /\s+/,$_,2;
205 $dict{$a}=$b;
206 }
207 }
208 if (open(F, $file)) {
209 @lines=<F>;
210 close(F);
212 if (open(F, ">$file")) {
213 for (@lines) {
214 m/\s+\S+\s+(\S+)/;
215 $name=$1;
216 if (not /^#/ and defined($dict{$name})) {
217 chomp;
218 $mark=$dict{$name};
219 $space=" "x(30-length($_));
220 print F "$_$space$mark\n";
221 }
222 else {
223 print F "$_";
224 }
225 }
226 close(F);
227 }
228 }
229 PERL_SCRIPT
230 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
231 export NOTES_FILE
232 perl $PERL_SCRIPT_TEMP_NAME "$1"
233 rm $PERL_SCRIPT_TEMP_NAME
234 }
236 remove_marks()
237 {
238 PERL_SCRIPT_TEMP_NAME=`mktemp /tmp/perl-grep-v-english-XXXXXXXX`
239 cat <<'PERL_SCRIPT' > $PERL_SCRIPT_TEMP_NAME
240 $file = $ARGV[0];
241 our %dict;
242 if (open(F, $file)) {
243 @lines=<F>;
244 close(F);
246 if (open(F, ">$file")) {
247 for (@lines) {
248 chomp;
249 if (not /^#/ and m/(\s+)(\S+)(\s+)(\S+)(\s+)(.*)/) {
250 my $name=$4;
251 my $comment=$6;
252 $dict{$name}=$comment;
253 print F "$1$2$3$4\n";
254 }
255 else {
256 print F "$_\n";
257 }
258 }
259 }
260 }
261 if (open(NOTES, $ENV{NOTES_FILE})) {
262 @lines=<NOTES>;
263 close(NOTES);
265 if (open(NOTES, ">".$ENV{NOTES_FILE})) {
266 for (@lines) {
267 chomp;
268 s/^\s+//;
269 my ($a,$b)=split /\s+/,$_,2;
270 if (not defined($dict{$a}) || ($dict{$a} eq $b)) {
271 print NOTES "$_\n";
272 if (defined($dict{$a})) { unset($dict{$a}); }
273 }
274 }
275 for (keys %dict) {
276 $mark=$dict{$_};
277 $space=" "x(30-length($_));
278 print NOTES "$_$space$mark\n";
279 }
280 }
281 }
282 PERL_SCRIPT
283 [ -e "$NOTES_FILE" ] || touch "$NOTES_FILE"
284 export NOTES_FILE
285 perl $PERL_SCRIPT_TEMP_NAME "$1"
286 rm $PERL_SCRIPT_TEMP_NAME
287 }
289 mkdir -p $WORK_DIR
290 oldpwd="$PWD"
291 cd $WORK_DIR
292 if echo "$1" | grep -q http:
293 then
294 text_from_url "$1"
295 elif [ "$#" != 0 ]
296 then
297 cat "$oldpwd/$1"
298 else
299 cat
300 fi \
301 | tee $ORIGINAL_TEXT \
302 | get_words ${TEMP1}-full \
303 | group_words \
304 | add_stat ${TEMP1}-full \
305 | tee "$TEMP1" > "$TEMP2"
307 add_marks "$TEMP2"
308 if [ "$editor" = vim ]
309 then
310 vim -c 'set keywordprg='"$LANGUAGE" -c 'set iskeyword=@,48-57,/,.,-,_,+,,,#,$,%,~,=' "$TEMP2" < /dev/tty > /dev/tty
311 else
312 echo 2
313 $editor "$TEMP2"
314 fi
315 remove_marks "$TEMP2"
317 diff "$TEMP1" "$TEMP2" | awk '{print $3}' | sort -u >> "$VOCABULARY"
318 rm -f "$TEMP1" "$TEMP2" "${TEMP1}-full" "$ORIGINAL_TEXT"