繁体   English   中英

Ruby阻止评论和个人资料问题

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM