/app/controllers/spanish_verb_conjugation_controller.rb
Ruby | 164 lines | 160 code | 3 blank | 1 comment | 1 complexity | c2ccff4c5f0907c89a7ffb46c7490ce6 MD5 | raw file
- # -*- coding: utf-8 -*-
- require "htmlentities";
- class SpanishVerbConjugationController < ApplicationController
-
-
- def make_multi
- return if(!@multiverb.nil?)
- pre_multiverb={
- "haber"=>["has", "he", "ha", "hemos", "han", "hayamos", "hayan", "hayais", "hubieramos", "hubieran", "hubierais", "habeis", "habiamos", "habian", "habiais", "habias", "habia", "hubiese", "hubiste", "hube", "hubo", "hubimos", "hubieron", "hubisteis", "habras", "habrias", "habre", "habra", "habremos", "habreis", "habriamos", "habrian", "habriais", "habran", "habria","hayas", "hubieras", "haya", "hubiera", "hubieses", "hubiesemos", "hubieseis", "hubiesen","would","have","habran", "had", "hubieres", "hubieremos", "hubieren", "hubiereis", "hubiere", "hubiésemos"],
- "ser"=>["eres", "soy", "somos", "son", "sois", "es", "sos", "fuesemos", "sido", "seas", "sea", "seamos", "seais", "sean", "fueramos", "fueran", "fuerais", "fueses", "fueseis", "fueras", "fuera", "fuese", "fuesen","will","was"],
- "=("=>["no", "te", "i", "am", "se", "nos", "os", "used", "to", "don't" ]
- }
- @multiverb={};
- pre_multiverb.each do |k,i|
- i.each {|w| @multiverb[w]=k}
- end
- end
- caches_action :index, { :cache_path => proc {|c| { :slug => params[:verb] }} }, { :expires_in => 3.days }
- def index
- @wide_layout = true
- @page_title = "Spanish Verb Conjugation"
- @page_heading = "Spanish Verb Conjugation Charts"
- @meta_description = "The Spanish Verb Conjugator displays the full conjugation of spanish verb infinitives along with example sentences."
- @meta_keywords = "spanish verb conjugation, spanish verb charts, infinitive, verb tenses"
- @audio = nil;
- v = params[:verb]
- if v && !v.eql?('')
- @status, @verb = VerbFinder.lookup_verb(v)
- if @verb && @verb.verb
- @page_title = "Spanish Verb Conjugation - #{@verb.verb}"
- @page_heading = "Spanish Verb Conjugator - #{@verb.verb}"
- @spanish_english_sentences = TranslatedSentence.ferret_find_by_phrase('spanish', @verb.verb, 10)
- media_hash = MediaMapping.find_media('spanish' => @verb.verb, 'match' => 'exact')
- @audio = media_hash['audio'] if media_hash
- end
- end
- @bad_input_message = "We suspect that <b>#{params[:verb]}</b> is not a proper Spanish verb infinitive. Verb infinitives " +
- "end in 'ar', 'er', 'ir', or 'se' and are just a single word. Examples include hablar, comer, " +
- "asistir, and vestirse. Please adjust your input and try again. If you are just looking to translate " +
- "a word, then you may want to use our <a href=\"/spanish_word_for/\" title=\"Spanish for\">Spanish/English Dictionary</a>"
- end
-
- # processes and maps input, and then redirects
- # to index
- def prep
- v = params[:verb]
- #override verb for html encoded param (mostly used in tests -- not intended for public consumption)
- if !params[:html_encoded_verb].nil?
- params_html_encoder = HTMLEntities.new
- v=params_html_encoder.decode(params[:html_encoded_verb])
- params[:verb]=v
- end
- if(!v || v.eql?(''))
- render(:layout=>"application",:text => '<p>You have not entered anything to search for.</p>')
- return
- end
- v.gsub!(/;/, '')
- self.make_multi
-
- # bypass this logic if verb routing has been cached
- cache = JmfCache.instance
- cache_key = "verb_conjugation_#{params[:verb]}"
- if cache.has_key?(cache_key)
- v2 = cache[cache_key]
- redirect_to(:controller => 'spanish_verb_conjugation', :action => 'index', :verb => v2)
- return
- end
-
- v_dc=de_utf(v).downcase;
- if(!(vn=@multiverb[v_dc]).nil?) then
- v=vn;
- end
- search_verb=search_verb_or_id=v;
- #might not be encoded properly, prepare variables for second looks
- html_encoder = HTMLEntities.new
- search_verb_html_named=html_encoder.encode(search_verb, :named)
- search_verb_html_numbered=html_encoder.encode(search_verb, :decimal)
- search_verb_html_hexed=html_encoder.encode(search_verb, :hexadecimal)
- has_encoding_differences=(search_verb!=search_verb_html_numbered)
-
- global_verb=nil;
- # check for search by record id
- if(search_verb_or_id.to_i.to_s==search_verb_or_id)
- global_verb=GlobalVerb.find(search_verb);
- if(global_verb.nil?) then
- render :layout=>"application",:text=>"Nothing found"
- return
- end
- else
- # search by verb, or by meaning
- global_verb=GlobalVerb.select('id, lookup_key, verb, meaning').where(['verb=?',search_verb]).first
- if has_encoding_differences && !global_verb.nil? and global_verb.verb!=search_verb
- #might not be encoded properly, give it another try
- global_verb=GlobalVerb.select('id, lookup_key, verb, meaning').where(['verb IN (?)',
- [search_verb_html_named,search_verb_html_numbered,search_verb_html_hexed]]).first
- end
- if global_verb.nil?
- global_verb=GlobalVerb.select('id, lookup_key, verb, meaning').where(['meaning=? OR meaning= ?',search_verb,'to '+search_verb]).first
- #check again if there are encoding differences
- if has_encoding_differences && !global_verb.nil? and global_verb.meaning!=search_verb and global_verb.meaning!='to '+search_verb
- global_verb=GlobalVerb.select('id, lookup_key, verb, meaning').where(['meaning IN (?)',
- [search_verb_html_named,search_verb_html_numbered,search_verb_html_hexed,
- 'to '+search_verb_html_named,'to '+search_verb_html_numbered,'to '+search_verb_html_hexed]]).first
- elsif has_encoding_differences && global_verb.nil?
- global_verb=GlobalVerb.select('id, lookup_key, verb, meaning').where(['meaning IN (?)',
- [search_verb_html_named,search_verb_html_numbered,search_verb_html_hexed,
- 'to '+search_verb_html_named,'to '+search_verb_html_numbered,'to '+search_verb_html_hexed]]).first
- end
- end
- end
-
- # if those checks fail, search by conjugation
- # note: ConjugatedVerb appears to use UTF8 text for conjugation, so no need to check against html encoded entities
- if(global_verb.nil?)
- search_conjugations=ConjugatedVerb.select("distinct global_verb_id, conjugation").where(["conjugation=?",search_verb]).all
- # single, exact match of conjugation found
- if(search_conjugations.length==1)
- global_verb=search_conjugations.first.global_verb
- else
- # no exact matches found, so do a broader search
- if(search_conjugations.length==0)
- search_conjugations=ConjugatedVerb.select("distinct global_verb_id, conjugation").where(["conjugation like ?","#{search_verb}%"]).limit(400).all
- if(search_conjugations.length==0)
- render :layout=>"application",:text=>"<p>Nothing found.</p>"
- return
- end
- end
- global_verb_ids=Hash[search_conjugations.map {|sc| [sc.global_verb_id,sc.conjugation] }]
- # single global verb found
- if(global_verb_ids.length==1)
- global_verb=GlobalVerb.select('id, lookup_key, verb').find(global_verb_ids.keys.first)
- else
- # display multiple verbs
- id_str = global_verb_ids.keys.join(', ') # "945, 231, 123, ..."
- verb_txt = GlobalVerb.select("id, lookup_key, verb").where("id IN (#{id_str})").collect {|gv|
- conj = global_verb_ids[gv.id];
- '<a href="'+gv.lookup_key.to_s.html_safe+'">(<b>'+gv.verb+'</b>=<i>'+conj+'</i>)</a>'
- }.sort.join(', ')
- options_txt = "<p>Some options that have matching conjugations are: #{verb_txt}</p>"
- render(:text => options_txt, :layout=>"application")
- return
- end
-
- end
- end
-
- v2=global_verb.lookup_key
- cache[cache_key]=v2 # store in cache
- redirect_to(:controller => 'spanish_verb_conjugation', :action => 'index', :verb => v2)
- return
- end
- def de_utf(s)
- s.gsub(/[ñÑ]/,"n").gsub(/[áÁ]/,"a").gsub(/[éÉ]/,"e").gsub(/[íÍ]/,"i").gsub(/[óÓ]/,"o").gsub(/[úÚ]/,'u');
- end
- end