2013-02-05 3 views
0

나는 레이크 작업이 매우 간단하고 예상대로 작동합니다.Regexp 내에서 해시 사용 - Regexp를 정수로 변환 할 수 없음

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 

이제 좀 더 복잡한 것을 얻으려고하지만 똑같은 아이디어를 사용하면 작동하지 않습니다. 아래의 코드를 참조하십시오 : 내림차순 "가져 오기 세부 사항을" 작업 : 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 

내가 레이크를 실행하려고하면 나는 다음과 같은 오류가

을! Regexp를 정수로 변환 할 수 없습니다

내가 이해하지 못하는 부분은 (배열을 실행하는 Regexp 내의 해시를 사용하는) 동일한 코드 부분이 레이크 작업 : qualify에서 올바르게 작동한다는 것입니다.

아이디어가 있으십니까?

감사합니다. combined이 문자열이 아닌 경우

+0

나는 모든 코드를 읽지 않을 것입니다. – sawa

+1

'결합 된'유형을 확인할 수 있습니까? 어쩌면 결합 = (content + title) .to_s – Blacksad

+0

감사합니다. @Blacksad! 이게 효과가있는 것처럼 보입니다. 그런 간단한 것, 나는 나 자신을 차고있다. 나는 왜 더 작은 레이크가 아닌, 더 복잡한 레이크가 아닌지 궁금해하고 있습니다.하지만 제가 얻을 수있는 것을 취할 것입니다. –

답변

2

결합 = (내용 + 제목), 루비 ...

당신이 배열에 액세스하고, 따라서 정규식 정수있을 것으로 기대된다 생각 combined[regex]에서

.to_s

관련 문제