0

私は非常に単純で、期待どおりに機能するレーキタスクを持っています。

 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! 正規表現を整数に変換できません

私が理解していない部分は、コードの同じ部分 (Regexp 内のハッシュを使用して配列を実行する) が rake タスク :qualify で問題なく機能していることです。

何か案は?

ありがとう!

4

1 に答える 1

2

結合 = (コンテンツ + タイトル).to_s

combined[regex]combinedが文字列でない場合、Ruby は配列にアクセスしていると見なし、正規表現が整数であると想定します...

于 2013-02-05T16:52:00.033 に答える