所以我有这个shell脚本:
echo "Enter text to be classified, hit return to run classification."
read text
if [ `echo "$text" | sed -r 's/ +/ /g' | bin/stupidfilter data/c_rbf` = "1.000000" ]
then
echo "Text is not likely to be stupid."
fi
if [ `echo "$text" | sed -r 's/ +/ /g' | bin/stupidfilter data/c_rbf` = "0.000000" ]
then
echo "Text is likely to be stupid."
fi我想用python写它。我该怎么做呢?
(如您所见,它使用库http://stupidfilter.org/stupidfilter-0.2-1.tar.gz)
发布于 2009-06-23 01:36:01
要像shell脚本一样执行此操作:
import subprocess
text = raw_input("Enter text to be classified: ")
p1 = subprocess.Popen('bin/stupidfilter', 'data/c_trbf')
stupid = float(p1.communicate(text)[0])
if stupid:
print "Text is likely to be stupid"
else:
print "Text is not likely to be stupid"发布于 2009-06-23 00:56:00
您可以清楚地将这些命令作为子shell运行,并读取返回值,就像在shell脚本中一样,然后在Python中处理结果。
这比加载C函数更简单。
如果您真的想从stupidfilter库中加载一个函数,那么首先查看其他人是否已经这样做了。如果你找不到有这种能力的人,请阅读manual --如何从Python语言调用到C语言。
使用其他人已经做过的事情仍然更简单。
https://stackoverflow.com/questions/1030114
复制相似问题