我有一个非常简单的 rake 任务,并且可以按我的预期工作。
task :qualify => :environment do
require 'classifier'
# encoding: utf-8
test = Skirt.where(:asin => "B007O9MXF0")
w = %w{ rayon wool cotton polyester nylon spandex}
a = test.first.content
b = test.first.title
c = a + b
w.each do |w|
if c[/#{w}/]
c = w
else
c
end
end
good ={}
skirt = Skirt.where(:quality => "Good")
skirt.each do |f|
good[f.content] = [f.quality]
end
bad = {}
skirt = Skirt.where(:quality => "Bad")
skirt.each do |f|
bad[f.content] = [f.quality]
end
classifier = Classifier::Bayes.new('Good', 'Bad')
good.each {|good| classifier.train_good "Good"}
bad.each {|bad| classifier.train_bad "Bad"}
puts classifier.classify(a),
c,
test.first.color,
a+b
end
现在我试图让一些更复杂的东西,但是使用完全相同的想法并且它不起作用。参见下面的代码: desc "Import details" task :import_clean => :environment do
require 'sucker'
require 'mechanize'
require 'nokogiri'
require 'open-uri'
require 'carrierwave'
require 'rmagick'
require 'csv'
# encoding: utf-8
skirt = Skirt.where(:quality => "Good")
good = {}
skirt_type = ""
skirt.each do |f|
content_title = f.content + f.title
good[content_title] = [f.quality]
end
bad = {}
skirt = Skirt.where(:quality => "Bad")
skirt.each do |f|
content_title = f.content + f.title
bad[content_title] = [f.quality]
end
classifier = Classifier::Bayes.new('Good', 'Bad')
good.each {|good| classifier.train_good "Good"}
bad.each {|bad| classifier.train_bad "Bad"}
request = Sucker.new(
:associate_tag => 'thenewoutpro-20',
:key => 'AKIAJXNLXYCBU3NJAIJQ',
:secret => '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',
:locale => :us)
request << {
"Operation" => "ItemSearch",
"SearchIndex" => "Apparel",
"Keywords" => ["women", "skirt"],
"ResponseGroup" => ["BrowseNodes"] }
rep = request.get
url = "#{rep.find('MoreSearchResultsUrl')}"
new_url = url[2, url.length-4]
agent = Mechanize.new
agent.get(new_url)
i = (0..47)
i.each do |i|
b = "#{i}"
item = "#result_" + b
doc = agent.page.search(item)
link = doc.css("a")[0]["href"]
asin = link[-10,10]
request2 = Sucker.new(
:associate_tag => 'thenewoutpro-20',
:key => 'KEY',
:secret => 'SECRET',
:locale => :us)
request2 << {
"Operation" => "ItemLookup",
"IdType" => "ASIN",
"ItemId" => asin,
"ResponseGroup" => ["Large"] }
response = request2.get
images = response.find('LargeImage').first.to_a
image_new = images[0].to_s
image = image_new[8, image_new.length-9]
color_string = response.find('Color')
color = color_string[6, color_string.length]
brand_string = response.find('Manufacturer')
brand = brand_string[5, brand_string.length]
content = response.find('Content')
title = response.find('Title')
combined = content + title
f = %w{ polyester rayon nylon cotton silk chiffon wool knit jersey viscose corduroy velvet lace }
s = %w{ pencil A-line mini maxi long pleated panel }
p = %w{ pettite 'plus size' maternity }
f.each do |f|
if combined[/#{f}/]
fabric = f
else
fabric = ""
end
end
s.each do |s|
a = combined[/#{s}/]
if a > 0
skirt_type = s
else
skirt_type = ""
end
end
p.each do |p|
if combined[/#{p}/]
size = p
else
size = "Regular"
end
end
quality = classifier.classify(combined)
Bottom.create!(
:asin => asin,
:title => response.find('Title'),
:price => response.find('FormattedPrice'),
:manufacturer => brand,
:content => response.find('Content'),
:color => color,
:fabric => fabric,
:size => size,
:skirt_type => skirt_type,
:images => image,
:link => link)
end
end
end
当我尝试运行 rake 时出现以下错误:rake aborted!无法将正则表达式转换为整数
我不明白的部分是代码的同一部分(使用正则表达式中的哈希在数组上运行)在 rake 任务上运行良好:qualify。
有任何想法吗?
谢谢!