PageRenderTime 78ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/global_verb_parser.rb

https://bitbucket.org/kapilnakhwa/demo-teachme
Ruby | 296 lines | 268 code | 28 blank | 0 comment | 7 complexity | 5e8a8386ffa5e6b454113f4c2444dc52 MD5 | raw file
  1. class StructuredVerb
  2. attr_accessor :notes,:tiempos,:global_verb
  3. def initialize(global_verb)
  4. self.global_verb=global_verb
  5. @personas=[["yo"],["tú"],["vos"],["él","ella","usted"],["nosotros","-as"],["vosotros","-as"],["ellos","ellas","ustedes"]]
  6. @personas_labels=@personas.map {|cur_p| cur_p.join("/") }
  7. self.tiempos={}
  8. self.notes=[]
  9. end
  10. def parse_new()
  11. nkg=Nokogiri::HTML.fragment("<basic>"+self.global_verb.conjugations+"</basic>");
  12. mode=nil;
  13. imperative_person_count=1;
  14. imperative_r={};
  15. neg_imperative_r={};
  16. new_personas_labels=["inglés"].concat(@personas_labels[0..1]).concat(@personas_labels[3..-1]);
  17. new_imperative_labels=["inglés",@personas_labels[1],@personas_labels[5],@personas_labels[3],@personas_labels[6]]
  18. nkg.css("tr").each do |node|
  19. tds=node.css("td");
  20. if(tds.length==1) then
  21. mode=tds.first.inner_text
  22. mode="Normal" if(mode=="Indicativo")
  23. else
  24. if(mode=="Normal" || mode=="Subjuntivo") then
  25. if(tds.first.inner_text.strip.length>2) then
  26. r={};
  27. r[:label]="new_format";
  28. r[:cat]=mode;
  29. r[:name]=tds.first.inner_text.strip;
  30. r[:personas]={};
  31. cur_i=0;
  32. tds[1..-1].each do |cur_td|
  33. r[:personas][new_personas_labels[cur_i]]=cur_td.inner_text.strip
  34. cur_i+=1;
  35. end
  36. self.tiempos[r[:cat]]||=Array.new();
  37. self.tiempos[r[:cat]]<< r
  38. end
  39. elsif(mode=="Imperativo") then
  40. if(tds[0].inner_text!=tds[1].inner_text) then #strange unicode character?
  41. iperson=new_imperative_labels[imperative_person_count];
  42. imperative_person_count+=1;
  43. if(imperative_r.keys.length==0) then
  44. imperative_r[:label]=neg_imperative_r[:label]="new_format";
  45. imperative_r[:name]="Imperative afirmativo"
  46. neg_imperative_r[:name]="Imperative negativo"
  47. imperative_r[:cat]=neg_imperative_r[:cat]=mode
  48. eng_split=tds[1].inner_text.split(/!/u).map{|ei| ei+"!" }
  49. imperative_r[:personas]={new_imperative_labels[0]=>eng_split[0]}
  50. neg_imperative_r[:personas]={new_imperative_labels[0]=>eng_split[0]}
  51. end
  52. puts iperson
  53. imperative_r[:personas][iperson]=tds[2].inner_text
  54. neg_imperative_r[:personas][iperson]=tds[3].inner_text
  55. end
  56. elsif(mode=="Otras formas") then
  57. r={}
  58. r[:cat]=mode
  59. r[:name]=tds.first.inner_text.strip
  60. r[:label]="new_format";
  61. r[:personas]={
  62. new_personas_labels[0]=>tds[1].inner_text.strip,
  63. "single"=>tds[2].inner_text.strip
  64. }
  65. self.tiempos[r[:cat]]||=Array.new();
  66. self.tiempos[r[:cat]]<< r
  67. else
  68. puts "Shouldn't be in here with #{self.global_verb.id}"
  69. end
  70. end
  71. end
  72. self.tiempos["Imperativo"]=[imperative_r,neg_imperative_r]
  73. save_cat(self.tiempos["Normal"],self.global_verb);
  74. save_cat(self.tiempos["Subjuntivo"],self.global_verb);
  75. save_cat(self.tiempos["Imperativo"],self.global_verb);
  76. save_cat(self.tiempos["Otras formas"],self.global_verb);
  77. end
  78. def parse_old()
  79. nkg=Nokogiri::HTML.fragment("<basic>"+self.global_verb.conjugations+"</basic>");
  80. personas=@personas
  81. def parse_personas(node)
  82. personas_txt=[["yo"],["tú"],["**vos"],["él"],["nosotros","-as"],["vosotros","-as"],["ellos","ellas","ustedes"]]
  83. cur=[];
  84. single=true;
  85. last="";
  86. ans=[];
  87. node.children.each{|c|
  88. cont=c.content;
  89. if(!c.text?) then
  90. personas_txt.each_with_index{|pr,i| pr.each{|ppr| cur<< i if (!cont.index(ppr).nil?)}}
  91. single=false;
  92. else
  93. cur.each{|c|
  94. ans[c]=cont;
  95. }
  96. last=cont;
  97. cur=[];
  98. end
  99. }
  100. ans_map={};
  101. str=if(single) then
  102. ans_map["single"]=node.content
  103. else
  104. ans.each_with_index{|a,i| ans_map[@personas_labels[i]]=a.to_s }
  105. end
  106. return ans_map;
  107. end
  108. def make_tiempo(node)
  109. r={};
  110. node.css("p").each do |p|
  111. if p[:class].blank? then
  112. a=r[:txt] || "";
  113. r[:txt]=a+"\n\n"+p.inner_html.gsub('<p>',"\n").gsub("</p>","\n");#to_s;
  114. elsif (p[:class]=="cabTiempo0" || p[:class]=="cabTiempo1") then
  115. r[:name]=p.content;
  116. elsif p[:class]=="cabTiempoINT" then
  117. r[:label]=p.content;
  118. elsif p[:class]=="negrita" then
  119. if(!r[:label].nil?) then
  120. if(r[:personas].nil?) then
  121. r[:personas]=parse_personas(p);
  122. else
  123. if(r[:or].nil?) then
  124. r[:or]=p.inner_text;#content;
  125. else
  126. r[:or]+=p.inner_html.gsub("<br>"," - ").gsub("</span>",":").gsub(/<span.*?>/,'');
  127. end
  128. end
  129. else
  130. a=r[:txt] || "";
  131. r[:txt]=a+"\n\n"+p.inner_html.gsub('<p>',"\n").gsub("</p>","\n");
  132. end
  133. end
  134. end
  135. r[:cat]=
  136. if(r[:name].nil?) then
  137. "Before"
  138. else
  139. if(!r[:name].index("Subjuntivo").nil?) then
  140. "Subjuntivo"
  141. elsif (!r[:name].index("Imperativo").nil?) then
  142. "Imperativo"
  143. elsif (r[:personas].keys.length==0) then
  144. "Before"
  145. elsif(r[:personas].keys.length==1) then
  146. "Otras formas"
  147. else
  148. "Normal"
  149. end
  150. end
  151. return r;
  152. end
  153. cats=["Normal","Subjuntivo","Imperativo","Otras formas"];
  154. nkg.css("div").each do |node|
  155. t=make_tiempo(node);
  156. self.tiempos[t[:cat]]||=Array.new();
  157. self.tiempos[t[:cat]]<<t;
  158. end
  159. save_cat(tiempos["Before"],self.global_verb);
  160. save_cat(tiempos["Normal"],self.global_verb);
  161. save_cat(tiempos["Subjuntivo"],self.global_verb);
  162. save_cat(tiempos["Imperativo"],self.global_verb);
  163. save_cat(tiempos["Otras formas"],self.global_verb);
  164. end
  165. def save_cat(ar,gv)
  166. istr="";
  167. estr="";
  168. bstr="";
  169. if(!ar.nil?) then
  170. ar.each do |t|
  171. if(!t[:label].blank?) then
  172. vt=VerbTime.first(:conditions=>["category=? and name=?",t[:cat],t[:name]]);
  173. if(vt.nil?) then
  174. vt=VerbTime.create(:category=>t[:cat],:name=>t[:name],:explanation=>t[:label]);
  175. elsif(vt.explanation.blank?) then
  176. vt.explanation=t[:label];
  177. vt.save
  178. end
  179. t[:personas].each do |k,i|
  180. vp=VerbPerson.first(:conditions=>["spanish=?",k.strip]);
  181. if(vp.nil?) then
  182. vp=VerbPerson.create(:spanish=>k.strip);
  183. end
  184. cv=ConjugatedVerb.first(:conditions=>["alternate=0 and verb_time_id=? and verb_person_id=? and global_verb_id=?",vt.id,vp.id,gv.id]);
  185. if(cv.nil?) then
  186. cv=ConjugatedVerb.create(:verb_time=>vt,:verb_person=>vp,:global_verb=>gv,:alternate=>0,:conjugation=>i.strip);
  187. else
  188. cv.conjugation=i.strip
  189. cv.save
  190. end
  191. end
  192. if(!t[:or].blank?) then
  193. ors=t[:or].split(' - ');
  194. ors.each do |cur_or|
  195. tmp=cur_or.split(':');
  196. if( !tmp[-2].nil? &&
  197. !tmp[-1].nil? &&
  198. !(k_or=tmp[-2].strip).blank? &&
  199. !(i_or=tmp[-1].strip).blank? ) then
  200. vp_or=VerbPerson.first(:conditions=>["spanish=?",k_or]);
  201. if(vp_or.nil?) then
  202. vp_or=VerbPerson.create(:spanish=>k_or);
  203. end
  204. cv_or=ConjugatedVerb.first(:conditions=>["alternate=1 and verb_time_id=? and verb_person_id=? and global_verb_id=?",vt.id,vp_or.id,gv.id]);
  205. if(cv_or.nil?) then
  206. cv_or=ConjugatedVerb.create(:verb_time=>vt,:verb_person=>vp_or,:global_verb=>gv,:alternate=>1,:conjugation=>i_or);
  207. else
  208. cv_or.conjugation=i_or
  209. cv_or.save
  210. end
  211. end
  212. end
  213. end
  214. else
  215. bstr+=t[:txt].to_s;
  216. end
  217. end
  218. end
  219. if( bstr.length>0 ) then
  220. gv.notes=bstr;
  221. gv.save;
  222. end
  223. return bstr,istr,estr;
  224. end
  225. end