Translation from Python

Hi. I’m not sure if I can ask this question here. If it’s not good,
please let me know.

I’m trying to read a book whose programming language is Python.
I prefer Ruby, so I need to translate the codes in the book into Ruby.

A part of the Python code is the following:

#--------------------
def getwordcounts(url):

Parse the feed

d=feedparser.parse(url)
wc={} #HERE!

Loop over all the entries

for e in d.entries:
if ‘summary’ in e: summary=e.summary
else: summary=e.description

# Extract a list of words
words=getwords(e.title+' '+summary)
for word in words:           #HERE!
  wc.setdefault(word,0)
  wc[word]+=1                 #HERE!

return d.feed.title,wc
#--------------------

It looks like counting the number of words that appear in the given url.
I’m pretty vague about “#HERE!” parts.

“wc” will be an array containing the words picked up…?
and there are “word” and “words” in the code but “word” does not appear
defined although it’s used locally.
and finally, I do not understand “wc[word]+=1” at all.

Could anyone help me out to write it in Ruby?

Thanks in advance.

soichi

wc = {} #Creates a new empty dictionnary. A ruby Hash

for word in words # Look over every word in words… words.split(’
").each do |word| …would probably be a ruby equivalent.

wc[word] += 1 # Since the default value of entries in the hash is zero,
this was set in the line right before, you now set the value of the
“word” to 1. Exact same thing in ruby.

hash = {}
hash.default=0
words.split(’ ').each { |word| hash[word] += 1 }

I don’t know what the feddparser returns so I can’t really help for the
rest…

Always remember that while python has sane syntax (compared to
perl and php), its inner logic does not work in a 1:1
translation into ruby code.

So you must adapt it to fit the ruby model. But usually this is
trivial and straight forward.

The first thing you should do is use the same method name but
adapt it to ruby.

Something like:
def getwordcounts(url):

Would then become:

def getwordcounts(url)
end

Next thing is to understand your data structures.

I do not know python really but something like this here:

wc[word]+=1

Seems to update the dictionary, at the keyword word position,
by + 1.

With hash.default = you can specify a default value for
your keys in a hash, in ruby.

Whenever you see a loop from python, like “for x in y”, the
ruby way is usually to use .each instead on an enumerator.

array.each do |line|
# do something here
end

Or shorter, rather than do/end, to use the {} block syntax.

“I’m pretty vague about “#HERE!” parts.”

I don’t know what the #HERE! means either. But it is clearly
a comment. Perhaps the one who wrote so wanted to jump from
#HERE! to #HERE! ?

““wc” will be an array containing the words picked up…?”

I believe wc typically is short for “word count”.

And with wc[word]+=1 you count the word, kind of. :smiley:

For ruby, I tend to use something like this to count the words
in a string:

class String

=====================================

=== word_count

Counts word frequencies. Returns a hash.

Usage examples:

x = %{ Dogs cat? cat? cat dog dog dog cat dog eagle dog dogs

}.word_count

=> { “cat”=>2, “dogs”=>2, “eagle”=>1, “dog”=>5, “cat?”=>2 }

=====================================

def word_count
hash = Hash.new(0)
downcase.scan(/\w+??/) { |word| hash[word] += 1 }
return hash
end

end

(Of course you can also just use a method instead of extending class
String,
and pass the string to that method as argument.)

“there are “word” and “words” in the code but “word” does not
appear defined although it’s used locally”

But it looks as if word is defined:

for word in words:

See? The word variable appears there. In ruby, this would become
something like this here:

array.each { |word| }

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs