我试图将一个Python程序移植到Ruby,但我对Python完全无知。如何将Python程序移植到Ruby
你能给我什么建议吗?我想运行sampletrain
方法。但是,我不明白为什么features=self.getfeatures(item)
可用。 getfeatures
只是一个实例变量,不是吗?它似乎被用作一种方法。
class classifier:
def __init__(self,getfeatures,filename=None):
# Counts of feature/category combinations
self.fc={}
# Counts of documents in each category
self.cc={}
self.getfeatures=getfeatures
def train(self,item,cat):
features=self.getfeatures(item)
# Increment the count for every feature with this category
for f in features:
self.incf(f,cat)
# Increment the count for this category
self.incc(cat)
self.con.commit()
def sampletrain(cl):
cl.train('Nobody owns the water.','good')
cl.train('the quick rabbit jumps fences','good')
cl.train('buy pharmaceuticals now','bad')
cl.train('make quick money at the online casino','bad')
cl.train('the quick brown fox jumps','good')
这是一个猜测:也许在初始化过程中传递的第二个参数是一个函数;尽管它在初始化期间被分配给一个属性,但可以稍后使用parens调用它。 (这与JavaScript类似,但不是Python。) – Phrogz
@ThiefMaster和steenslag :)只是为了工作..感谢你的好评。 – zono
@Phrogz'类似于JS'对我来说很容易理解。谢谢 – zono