[英]Ruby block comment and profile questions
我已經寫了Erik Demaine(MIT)docdist8.py的Ruby版本。 這可以在github上作為docdist-v3.rb獲得 。 我遇到了兩種奇怪的情況:
1)在函數inner_product中有一個塊注釋:
Inner product between two vectors, where vectors
are repeated as dictionaries of (word, freq) pairs.
Example : inner_product({"and":3, "of":2, "the":5},
{"and":4, "in":1, "of":1, "this":2}) = 14.0
如果我用= begin和= end包裹它,就沒有問題,但是如果我用三重雙引號“”“包裹,則會出現如下錯誤:
./docdist-v3.rb:71: syntax error, unexpected tIDENTIFIER, expecting kEND
Example : inner_product({"and":3, "of":2, "the":5},
^
./docdist-v3.rb:71: syntax error, unexpected tIDENTIFIER, expecting kEND
Example : inner_product({"and":3, "of":2, "the":5},
^
./docdist-v3.rb:72: syntax error, unexpected kIN, expecting kEND
... {"and":4, "in":1, "of":1, "this":2}) = 14.0
^
./docdist-v3.rb:72: syntax error, unexpected tIDENTIFIER, expecting kEND
... {"and":4, "in":1, "of":1, "this":2}) = 14.0
^
./docdist-v3.rb:72: syntax error, unexpected tIDENTIFIER, expecting kEND
..."and":4, "in":1, "of":1, "this":2}) = 14.0
^
是否有“ /”的規則/允許的條目與= begin和= end不同?
2)當我使用time命令運行程序時,它將在0.3秒內執行。 但是,如果我要求“配置文件”,則與之相比,它花費的時間會非常長-30秒。 因此,我根本沒有獲得正確的輸出。 原始Python版本似乎不是這種情況,在原始Python版本中,僅需花費很少的額外時間即可進行配置。 如何在Ruby中運行相同的配置文件?
注意:我用來運行Ruby程序的兩個文件是t2.bobsey.txt和t3.lewis.txt。 它們可在http://ocw.mit.edu/ans7870/6/6.006/s08/lecturenotes/dd_data.htm上找到。
1)塊注釋始終具有以下形式:
=begin
Comment
=end
您實際上正在創建一個從未使用過的字符串:
"""
Not a comment
"""
# => "\nNot a comment\n"
這就是為什么在添加另一個引號時會出錯,並且是語法高亮顯示它們為字符串的原因。
2)使用事件探查器速度較慢,但得到的結果相同:
ruby docdist-v3.rb t2.bobsey.txt t3.lewis.txt
File t2.bobsey.txt:262111 lines,49785 words,3354 distinct words
File t3.lewis.txt:1015474 lines,182355 words,8530 distinct words
The distance between the documents is: 0.574160 (radians)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.