-
Notifications
You must be signed in to change notification settings - Fork 0
/
sf_check.sh
executable file
·182 lines (182 loc) · 3.58 KB
/
sf_check.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
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
#! /bin/sh
trap "rm /tmp/sf_check.*.$$.tmp; exit 1" INT TERM
#
# spam filter programs by [email protected]
#
#----------------------------------------------------------------------
#
# functions
#
function show_help () {
echo "Usage : $0 [-w|--white|-b|--black] [file ...]"
echo "Check file."
echo " -w, --white check white?"
echo " -b, --black check black?"
echo "return 0 when check is true."
echo "return 1 when check is false."
}
#----------------------------------------------------------------------
#
# main routin
#
if [ "X$1" = "X-h" -o "X$1" = "X--help" ]
then
show_help
exit 0
fi
#
if [ "X${SFDIR}" = "X" ]
then
SFDIR=${HOME}/.sf
fi
#
if [ "X${SFDB}" = "X" ]
then
SFDB=sf.db
fi
#
SFDB_PATH=${SFDIR}/${SFDB}
#
maxlength=20; export maxlength
tab=`echo -n -e '\t'`
upperKeisen=`echo -n -e '\xe2\x80\xbe'`
##zsp=`echo -n -e '\241\241'`
#
table=""; export table
file=""
#
while [ $# != 0 ]
do
case $1 in
-w|--white )
table="t_white"
;;
-b|--black )
table="t_black"
;;
* )
file="${file} $1"
;;
esac
shift
done
#
if [ "X${table}" = "X" ]
then
show_help
exit 0
fi
#
echo ".separator \"\t\"" >/tmp/sf_check.1.$$.tmp
echo "begin;" >>/tmp/sf_check.1.$$.tmp
echo "select tablenm,count from t_total where tablenm='t_white';" >>/tmp/sf_check.1.$$.tmp
echo "select tablenm,count from t_total where tablenm='t_black';" >>/tmp/sf_check.1.$$.tmp
echo "end;" >>/tmp/sf_check.1.$$.tmp
#
echo ".separator \"\t\"" >/tmp/sf_check.2.$$.tmp
echo "begin;" >>/tmp/sf_check.2.$$.tmp
#
for i in `cat ${file} |\
sed -e '/Content-Transfer-Encoding: *base64/,$d' |\
sed -e 's/<[^>]*>//g;s/<.*$//;s/^.*>//' |\
nkf -X -e |\
kakasi -w -ieuc -oeuc |\
nkf -E -w |\
tr "'" ' '|\
tr '"' ' '|\
tr '[:cntrl:]' ' ' |\
tr -s ' ' |\
tr ' ' '\n' |\
awk 'BEGIN{ \
maxlength = ENVIRON["maxlength"]; \
} \
{ \
if (length($0) > 0 && length($0) <= maxlength){ \
print; \
} \
}' |\
tr '[:cntrl:]' '\n' |\
sort |\
uniq -c |\
sed -e "s/^[ ${tab}]*//;s/[ ${tab}][ ${tab}]*/,/"`
do
count=`echo $i | cut -d, -f1`
term=`echo $i | cut -d, -f2-`
#
if [ "X${term}" = "X" ]
then
: # do nothing
else
echo "select t_white.term,t_white.count*${count},t_black.count*${count} from t_white left join t_black on t_white.term = t_black.term where t_white.term='${term}';" >>/tmp/sf_check.2.$$.tmp
echo "select t_black.term,t_white.count*${count},t_black.count*${count} from t_black left join t_white on t_white.term = t_black.term where t_black.term='${term}' and t_white.term is NULL;" >>/tmp/sf_check.2.$$.tmp
fi
done
#
echo "end;" >>/tmp/sf_check.2.$$.tmp
#
(
cat /tmp/sf_check.1.$$.tmp |\
sqlite3 ${SFDB_PATH}
#
cat /tmp/sf_check.2.$$.tmp |\
sqlite3 ${SFDB_PATH} |\
sort |\
uniq
) |\
awk 'BEGIN{
FS = "\t";
w_total = 0; \
b_total = 0; \
total_score = 0.0;
table = ENVIRON["table"]; \
}
{
if (NR < 3){
if ($1 == "t_white"){
w_total = $2;
}
if ($1 == "t_black"){
b_total = $2;
}
}
#
if ($2 == ""){
w_count = 0;
}else{
w_count = $2;
}
#
if ($3 == ""){
b_count = 0;
}else{
b_count = $3;
}
#
if (w_total == 0 || b_total == 0 || (w_count == 0 && b_count == 0)){
# do nothing
}else{
total_score += (((w_count / w_total) / ((b_count / b_total) + (w_count / w_total))) - 0.5);
}
}
END{
print total_score;
if (table == "t_white"){
if (total_score < 0.0){
stat = 1;
}else{
stat = 0;
}
}else{
if (total_score < 0.0){
stat = 0;
}else{
stat = 1;
}
}
exit stat;
}'
stat=$?
#
rm /tmp/sf_check.*.$$.tmp
#
exit ${stat}