1

I'm trying to port a Python program to Ruby but I'm completely ignorant about Python.

Could you give me any advice?

I want to run sampletrain method. However, I dont understand why features=self.getfeatures(item) is available. getfeatures is just an instance variable, isn't it? It seem to be used as a method.

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')
3
  • 1
    Here's a guess: perhaps the second argument passed in during initialization is a a function; though it's assigned to a property during initialization, it can be invoked later on with parens. (This is similar to JavaScript, but not Python.) Commented Nov 15, 2011 at 13:53
  • @ThiefMaster and steenslag :) just to work.. thanks your nice comments. Commented Nov 15, 2011 at 14:09
  • @Phrogz 'similar to JS' is easy to understand for me. thanks Commented Nov 15, 2011 at 14:14

3 Answers 3

5

In Python, because the brackets for a method call aren't optional it is possible to distinguish between a reference to a method and an invocation of a method. i.e.

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()

vs.

x = example() # calls example and assigns the return value to x

Because the brackets for a method call are optional in Ruby you need to use some extra code e.g. x = method(:example) and x.call to achieve the same thing.

Sign up to request clarification or add additional context in comments.

1 Comment

You should be able to use either the style I've given above or write initialize to accept a block as suggested by tokland depending on whether you're likely to want to provide a named method or an anonymous block for the getfeatures functionality. If you need any more details, just let me know.
3

The idiomatic way to send behaviours in Ruby (since getfeatures in your code is clearly a callable) is to use blocks:

class Classifier
  def initialize(filename = nil, &getfeatures)
    @getfeatures = getfeatures
    ...
  end

  def train(item, cat)
    features = @getfeatures.call(item)
    ...
  end

  ...
end

Classifier.new("my_filename") do |item|
  # use item to build the features (an enumerable, array probably) and return them
end

Comments

2

If you're translating from Python, you'll have to learn Python so you're not "completely ignorant" about it. No short cuts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.