Permalink
Browse files

Add task to 'localize' topics' questions

  • Loading branch information...
1 parent c75edb1 commit f69d0d66bf2ca3e3b072826c0c7285251fc97245 Ricardo Panaggio committed Nov 9, 2011
Showing with 22 additions and 0 deletions.
  1. +22 −0 lib/tasks/topics.rake
View
@@ -0,0 +1,22 @@
+require 'language_detector'
+
+LANG_PT_BR = 'pt'
+BRAZIL_LOCALE = 'BR'
+
+namespace :topics do
+ desc 'Move questions from given topic to another localized topic'
+ task :localize_questions, :topic_id, :needs => :environment do |t, args|
+ detector = LanguageDetector.new
+
+ orig = Topic.find_by_slug_or_id(args[:topic_id])
+ local = Topic.find_or_create_by_title("#{orig.title}#{BRAZIL_LOCALE}")
+
+ orig.questions.each do |q|
+ corpus = "#{q.title} #{q.body} #{q.answers.map(&:body).join(" ")}"
+ if detector.detect(corpus) == LANG_PT_BR
+ q.pull(:topic_ids => orig.id)
+ q.push(:topic_ids => local.id)
+ end
+ end
+ end
+end

0 comments on commit f69d0d6

Please sign in to comment.