-
Notifications
You must be signed in to change notification settings - Fork 2
/
bayes.py
37 lines (29 loc) · 974 Bytes
/
bayes.py
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
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
from valence.bayes import get_classifier, space
from optparse import OptionParser
def doit(fi, fo):
classifier = get_classifier()
if classifier:
for para in fi:
words = space.split(para)
feats = dict([(word, True) for word in words])
print(classifier.classify(feats), file=fo)
def main():
parser = OptionParser(usage='Usage: %prog file')
parser.add_option('-s', '--set', action="store_true", default=False, dest="set", help='stdio has file list')
opts, args = parser.parse_args()
if not opts.set:
doit(sys.stdin, sys.stdout)
else:
for filename in sys.stdin:
f = filename.strip()
print(f, file=sys.stderr)
fi = open(f, "r")
fo = open(f + ".e", "w")
doit(fi, fo)
fi.close()
fo.close()
if __name__ == '__main__':
main()