我想尝试运行我的代码,即使遇到错误也是如此。我将说明我认为我在哪里得到以下错误以及错误是什么:
require 'rubygems'
require 'simple_oauth'
require 'cloudsight'
require 'rubygems'
require 'net/http'
require 'uri'
require 'json'
require 'open-uri'
require 'openssl'
require 'hpricot'
#ALCHEMY
file='C:\\Users\\ENTER USERNAME\\Desktop\\cloudsight.txt'
f = File.open(file, "r")
f.each_line {|line|
tstart = 'name"=>"'
tstop = '"'
term = line[/#{tstart}(.*?)#{tstop}/m, 1]
url = 'http://access.alchemyapi.com/calls'
service = '/text/TextGetRankedTaxonomy'
apikey = '?apikey=ENTER ALCHEMY API KEY'
thething = '&text='
termencoded = URI::encode(term)
fullurl = url + service + apikey + thething + termencoded
sleep 1
opener = open(fullurl, 'Accept-Encoding' => '') {|f| f.read }
#print opener
我想我现在得到了错误。
lstart = '<label>/'
lstop = '</label>'
label = opener[/#{lstart}(.*?)#{lstop}/m, 1]
sleep 1
cstart = '<score>'
cstop = '</score>'
confidence = opener[/#{cstart}(.*?)#{cstop}/m, 1]
#data = label + ',' + confidence + ',' + line
print label
print confidence
print "\n"
}
这是我似乎得到的错误:
C:/Ruby21/lib/ruby/2.1.0/uri/common.rb:304:in `escape': undefined method `gsub' for nil:NilClass (NoMethodError)
关于我如何传递错误/逃避它/或使字符串出现的任何想法?
答案 0 :(得分:0)
我猜错误来自这条线:
termencoded = URI::encode(term)
可能正在发生,因为term
是零。添加一些调试以查看在调用该行之前设置的term
。如果您在term
空白时没问题,可以将该行更改为:
termencoded = URI::encode(term.to_s)
你的程序是否可以接受我不能说。
我不会试图压制错误。有些东西不正常,你应该弄清楚它是什么以及如何处理它。
答案 1 :(得分:0)
我解决了这个问题。事实证明,在这个例子中,炼金术方面存在问题。分类搜索只是无法找到一个术语