PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/spanish_verb_conjugation_controller.rb

https://bitbucket.org/kapilnakhwa/demo-teachme
Ruby | 164 lines | 160 code | 3 blank | 1 comment | 1 complexity | c2ccff4c5f0907c89a7ffb46c7490ce6 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. require "htmlentities";
  3. class SpanishVerbConjugationController < ApplicationController
  4. def make_multi
  5. return if(!@multiverb.nil?)
  6. pre_multiverb={
  7. "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&eacute;semos"],
  8. "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"],
  9. "=("=>["no", "te", "i", "am", "se", "nos", "os", "used", "to", "don't" ]
  10. }
  11. @multiverb={};
  12. pre_multiverb.each do |k,i|
  13. i.each {|w| @multiverb[w]=k}
  14. end
  15. end
  16. caches_action :index, { :cache_path => proc {|c| { :slug => params[:verb] }} }, { :expires_in => 3.days }
  17. def index
  18. @wide_layout = true
  19. @page_title = "Spanish Verb Conjugation"
  20. @page_heading = "Spanish Verb Conjugation Charts"
  21. @meta_description = "The Spanish Verb Conjugator displays the full conjugation of spanish verb infinitives along with example sentences."
  22. @meta_keywords = "spanish verb conjugation, spanish verb charts, infinitive, verb tenses"
  23. @audio = nil;
  24. v = params[:verb]
  25. if v && !v.eql?('')
  26. @status, @verb = VerbFinder.lookup_verb(v)
  27. if @verb && @verb.verb
  28. @page_title = "Spanish Verb Conjugation - #{@verb.verb}"
  29. @page_heading = "Spanish Verb Conjugator - #{@verb.verb}"
  30. @spanish_english_sentences = TranslatedSentence.ferret_find_by_phrase('spanish', @verb.verb, 10)
  31. media_hash = MediaMapping.find_media('spanish' => @verb.verb, 'match' => 'exact')
  32. @audio = media_hash['audio'] if media_hash
  33. end
  34. end
  35. @bad_input_message = "We suspect that <b>#{params[:verb]}</b> is not a proper Spanish verb infinitive. Verb infinitives " +
  36. "end in 'ar', 'er', 'ir', or 'se' and are just a single word. Examples include hablar, comer, " +
  37. "asistir, and vestirse. Please adjust your input and try again. If you are just looking to translate " +
  38. "a word, then you may want to use our <a href=\"/spanish_word_for/\" title=\"Spanish for\">Spanish/English Dictionary</a>"
  39. end
  40. # processes and maps input, and then redirects
  41. # to index
  42. def prep
  43. v = params[:verb]
  44. #override verb for html encoded param (mostly used in tests -- not intended for public consumption)
  45. if !params[:html_encoded_verb].nil?
  46. params_html_encoder = HTMLEntities.new
  47. v=params_html_encoder.decode(params[:html_encoded_verb])
  48. params[:verb]=v
  49. end
  50. if(!v || v.eql?(''))
  51. render(:layout=>"application",:text => '<p>You have not entered anything to search for.</p>')
  52. return
  53. end
  54. v.gsub!(/;/, '')
  55. self.make_multi
  56. # bypass this logic if verb routing has been cached
  57. cache = JmfCache.instance
  58. cache_key = "verb_conjugation_#{params[:verb]}"
  59. if cache.has_key?(cache_key)
  60. v2 = cache[cache_key]
  61. redirect_to(:controller => 'spanish_verb_conjugation', :action => 'index', :verb => v2)
  62. return
  63. end
  64. v_dc=de_utf(v).downcase;
  65. if(!(vn=@multiverb[v_dc]).nil?) then
  66. v=vn;
  67. end
  68. search_verb=search_verb_or_id=v;
  69. #might not be encoded properly, prepare variables for second looks
  70. html_encoder = HTMLEntities.new
  71. search_verb_html_named=html_encoder.encode(search_verb, :named)
  72. search_verb_html_numbered=html_encoder.encode(search_verb, :decimal)
  73. search_verb_html_hexed=html_encoder.encode(search_verb, :hexadecimal)
  74. has_encoding_differences=(search_verb!=search_verb_html_numbered)
  75. global_verb=nil;
  76. # check for search by record id
  77. if(search_verb_or_id.to_i.to_s==search_verb_or_id)
  78. global_verb=GlobalVerb.find(search_verb);
  79. if(global_verb.nil?) then
  80. render :layout=>"application",:text=>"Nothing found"
  81. return
  82. end
  83. else
  84. # search by verb, or by meaning
  85. global_verb=GlobalVerb.select('id, lookup_key, verb, meaning').where(['verb=?',search_verb]).first
  86. if has_encoding_differences && !global_verb.nil? and global_verb.verb!=search_verb
  87. #might not be encoded properly, give it another try
  88. global_verb=GlobalVerb.select('id, lookup_key, verb, meaning').where(['verb IN (?)',
  89. [search_verb_html_named,search_verb_html_numbered,search_verb_html_hexed]]).first
  90. end
  91. if global_verb.nil?
  92. global_verb=GlobalVerb.select('id, lookup_key, verb, meaning').where(['meaning=? OR meaning= ?',search_verb,'to '+search_verb]).first
  93. #check again if there are encoding differences
  94. if has_encoding_differences && !global_verb.nil? and global_verb.meaning!=search_verb and global_verb.meaning!='to '+search_verb
  95. global_verb=GlobalVerb.select('id, lookup_key, verb, meaning').where(['meaning IN (?)',
  96. [search_verb_html_named,search_verb_html_numbered,search_verb_html_hexed,
  97. 'to '+search_verb_html_named,'to '+search_verb_html_numbered,'to '+search_verb_html_hexed]]).first
  98. elsif has_encoding_differences && global_verb.nil?
  99. global_verb=GlobalVerb.select('id, lookup_key, verb, meaning').where(['meaning IN (?)',
  100. [search_verb_html_named,search_verb_html_numbered,search_verb_html_hexed,
  101. 'to '+search_verb_html_named,'to '+search_verb_html_numbered,'to '+search_verb_html_hexed]]).first
  102. end
  103. end
  104. end
  105. # if those checks fail, search by conjugation
  106. # note: ConjugatedVerb appears to use UTF8 text for conjugation, so no need to check against html encoded entities
  107. if(global_verb.nil?)
  108. search_conjugations=ConjugatedVerb.select("distinct global_verb_id, conjugation").where(["conjugation=?",search_verb]).all
  109. # single, exact match of conjugation found
  110. if(search_conjugations.length==1)
  111. global_verb=search_conjugations.first.global_verb
  112. else
  113. # no exact matches found, so do a broader search
  114. if(search_conjugations.length==0)
  115. search_conjugations=ConjugatedVerb.select("distinct global_verb_id, conjugation").where(["conjugation like ?","#{search_verb}%"]).limit(400).all
  116. if(search_conjugations.length==0)
  117. render :layout=>"application",:text=>"<p>Nothing found.</p>"
  118. return
  119. end
  120. end
  121. global_verb_ids=Hash[search_conjugations.map {|sc| [sc.global_verb_id,sc.conjugation] }]
  122. # single global verb found
  123. if(global_verb_ids.length==1)
  124. global_verb=GlobalVerb.select('id, lookup_key, verb').find(global_verb_ids.keys.first)
  125. else
  126. # display multiple verbs
  127. id_str = global_verb_ids.keys.join(', ') # "945, 231, 123, ..."
  128. verb_txt = GlobalVerb.select("id, lookup_key, verb").where("id IN (#{id_str})").collect {|gv|
  129. conj = global_verb_ids[gv.id];
  130. '<a href="'+gv.lookup_key.to_s.html_safe+'">(<b>'+gv.verb+'</b>=<i>'+conj+'</i>)</a>'
  131. }.sort.join(', ')
  132. options_txt = "<p>Some options that have matching conjugations are: #{verb_txt}</p>"
  133. render(:text => options_txt, :layout=>"application")
  134. return
  135. end
  136. end
  137. end
  138. v2=global_verb.lookup_key
  139. cache[cache_key]=v2 # store in cache
  140. redirect_to(:controller => 'spanish_verb_conjugation', :action => 'index', :verb => v2)
  141. return
  142. end
  143. def de_utf(s)
  144. s.gsub(/[ñÑ]/,"n").gsub(/[áÁ]/,"a").gsub(/[éÉ]/,"e").gsub(/[íÍ]/,"i").gsub(/[óÓ]/,"o").gsub(/[úÚ]/,'u');
  145. end
  146. end