在Regexp中使用哈希 - 无法将Regexp转换为整数

时间:2013-02-05 16:12:52

标签: ruby regex hash rake

我有一个非常简单的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_clean => :环境做

       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! 无法将Regexp转换为整数

我不明白的部分是代码的相同部分(使用Regexp中的哈希来运行数组)在rake任务上运行得很好:qualify。

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

  

组合=(内容+标题).to_s

combined[regex]中,如果combined不是字符串,Ruby认为你正在访问一个数组,因此期望正则表达式是一个整数......