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