如何将Python程序移植到Ruby

zon*_*ono 1 ruby python

我正在尝试将Python程序移植到Ruby,但我完全不了解Python.

你能给我一些建议吗?

我想运行sampletrain方法.但是,我不明白为什么features=self.getfeatures(item)可用.getfeatures只是一个实例变量,不是吗?它似乎被用作一种方法.

docclass.py:

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')
Run Code Online (Sandbox Code Playgroud)

mik*_*kej 5

在Python中,因为方法调用的括号不是可选的,所以可以区分对方法的引用和方法的调用.即

def example():
    pass

x = example # x is now a reference to the example 
            # method. no invocation takes place
            # but later the method can be called as
            # x()
Run Code Online (Sandbox Code Playgroud)

x = example() # calls example and assigns the return value to x
Run Code Online (Sandbox Code Playgroud)

因为方法调用的括号在Ruby中是可选的,所以你需要使用一些额外的代码,例如x = method(:example)x.call实现相同的功能.