def main():
sys.stdout = codecs.getwriter('utf8')(sys.stdout.buffer)
parser = argparse.ArgumentParser(description='')
parser.add_argument('-i', '--input', help='Input file', required=True)
parser.add_argument('-b', '--binary',
help='Polarity classification, i.e., posivitive vs negative (default: posivitive/negative/neutral classification)',
action='store_true')
parser.add_argument('-t', '--test', help='Test file', required=True)
parser.add_argument('-o', '--output', help='Output filename prefix', required=True)
parser.add_argument('-c', '--c', help='C value for SVM', type=float, default=1.0)
parser.add_argument('-k', '--k', help='Number of features to keep', type=int, default=1000)
args = parser.parse_args()
data = read_semeval_classification(args.input, encoding='windows-1252')
if args.binary:
data = filter_polarity_classification(data)
analyzer = get_rich_analyzer(word_ngrams=[2, 3], char_ngrams=[4])
pipeline = Pipeline([
('vect', CountVectorizer(analyzer=analyzer)),
('tfidf', TfidfTransformer()),
('sel', SelectKBest(chi2, k=args.k)),
('clf', LinearSVC(C=args.c)),
])
pipeline.fit(data[0], data[1])
test = read_test_data(args.test, args.binary, encoding='windows-1252', topic=args.binary)
classifier = pipeline.fit(data[0], data[1])
y = classifier.predict(test[1])
if args.binary:
task = 'B'
else:
task = 'A'
with open('%sc%f-k%i-%s.output' % (args.output, args.c, args.k, task), 'w', encoding='utf8') as outfile:
if args.binary:
for id_, topic, label in zip(test[0], test[2], y):
print(id_, topic, label, sep='\t', file=outfile)
else:
for id_, label in zip(test[0], y):
print(id_, label, sep='\t', file=outfile)
semeval_classification.py 文件源码
python
阅读 18
收藏 0
点赞 0
评论 0
评论列表
文章目录