Guest User

Untitled

a guest
Jan 22nd, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.37 KB | None | 0 0
  1. require 'normalizer'
  2. require 'ruby-prof'
  3.  
  4. describe Normalizer do
  5. #before(:all) do
  6. # RubyProf.start
  7. #end
  8.  
  9. #after(:all) do
  10. # result = RubyProf.stop
  11. # printer = RubyProf::FlatPrinter.new(result)
  12. # printer.print(STDOUT, 0)
  13. #end
  14.  
  15. it "should return nil when trying to replace synonyms with a empty or nil value" do
  16. Normalizer.replace_synonyms("").should==""
  17. Normalizer.replace_synonyms(nil).should==nil
  18. end
  19.  
  20. it "replacing keywords in a term that has no synonyms should return the same keyword" do
  21. Normalizer.replace_synonyms("sea dog").should=="sea dog"
  22. end
  23.  
  24. it "should replace a single word keyword that translates into a two word synonym" do
  25. Normalizer.replace_synonyms("se").should=="special edition"
  26. Normalizer.replace_synonyms("se car").should=="special edition car"
  27. Normalizer.replace_synonyms("Chevy HD Truck").should=="Chevy heavy duty Truck"
  28. Normalizer.replace_synonyms("Ford WT").should=="Ford work truck"
  29. Normalizer.replace_synonyms("Ford Work Truck").should=="Ford wt"
  30. Normalizer.replace_synonyms("10'th ann editio n").should=="10'th anniversary edition"
  31. Normalizer.replace_synonyms("10'th anniversary edition").should=="10'th ann edition"
  32. Normalizer.replace_synonyms("includes tech pkg").should=="includes technology pkg"
  33. Normalizer.replace_synonyms("includes technology pkg").should=="includes tech pkg"
  34. end
  35.  
  36. it "should replace a single word keyword that translates into a two word synonym,without regard for case" do
  37. #slow
  38. Normalizer.replace_synonyms("SE").should=="special edition"
  39. end
  40.  
  41. it "should replace multiple terms in a keyword" do
  42. #slow
  43. Normalizer.replace_synonyms("This is a special edition car").should=="This is a se car"
  44. end
  45.  
  46. it "should replace single word synonyms that appear at end of keyword" do
  47. #slow
  48. Normalizer.replace_synonyms("Buy this limited").should=="Buy this ltd"
  49. end
  50.  
  51. it "should replace double word synonyms that appear at end of keyword" do
  52. #slow
  53. Normalizer.replace_synonyms("Buy this special edition").should=="Buy this se"
  54. end
  55.  
  56. it "should replace double word synonyms that appear at end of keyword" do
  57. #slow
  58. Normalizer.replace_synonyms("special edition car").should=="se car"
  59. end
  60.  
  61. it "should replace a double word keyword regardless of case" do
  62. #slow
  63. Normalizer.replace_synonyms("Special Edition car").should=="se car"
  64. end
  65.  
  66. it "should replace a double word that has a term that is also in a single word synonym" do
  67. #slow
  68. Normalizer.replace_synonyms("Limited Edition car").should=="le car"
  69. end
  70.  
  71. it "should return nil if keyword or terms are nil or empty" do
  72. #slow
  73. Normalizer.normalize(nil,["bar","foo","baz"]).should=={:match=>nil, :match_type=>:empty}
  74. Normalizer.normalize("",["bar","foo","baz"]).should=={:match=>nil, :match_type=>:empty}
  75. Normalizer.normalize("foo",nil).should=={:match=>nil, :match_type=>:empty}
  76. Normalizer.normalize("foo",[]).should=={:match=>nil, :match_type=>:empty}
  77. end
  78.  
  79. it "should return an exact match if an exact match is found" do
  80. Normalizer.normalize("foo",["bar","foo","baz"]).should=={:match=>"foo", :match_type=>:exact_match}
  81. end
  82.  
  83. it "should return an exact match if an exact match is found, even if the case is different" do
  84. Normalizer.normalize("FOo",["bar","FOO","baz"]).should=={:match=>"FOO", :match_type=>:exact_match}
  85. end
  86.  
  87. it "should return an exact match even if there is a difference of a dash in the keyword" do
  88. Normalizer.normalize("GS-R",["GS","GSR","GSR VTEC"]).should=={:match=>"GSR", :match_type=>:exact_match}
  89. end
  90.  
  91. it "should return an exact match even if there is a difference of a slash in the keyword" do
  92. Normalizer.normalize("GS/R",["GS","GSR","GSR VTEC"]).should=={:match=>"GSR", :match_type=>:exact_match}
  93. end
  94.  
  95. it "should return an exact match even if there is a difference of a dash in the keyword, without regard for case" do
  96. Normalizer.normalize("GS-R",["GS","GSR","GSR VTEC"]).should=={:match=>"GSR", :match_type=>:exact_match}
  97. Normalizer.normalize("gs-r",["GS","GSR","GSR VTEC"]).should=={:match=>"GSR", :match_type=>:exact_match}
  98. end
  99.  
  100. it "should return an exact match even if there is a difference of a dash in one of the terms" do
  101. Normalizer.normalize("GSR",["GS","GS-R","GSR VTEC"]).should=={:match=>"GS-R", :match_type=>:exact_match}
  102. end
  103.  
  104. it "should return the shortest term that that they keyword fits completely inside" do
  105. Normalizer.normalize("foo bar",["foo","bar","foo bar bellyhoo baz","foo bar baz"]).should=={:match=>"foo bar baz", :match_type=>:sub_match}
  106. end
  107.  
  108. it "should return the shortest term that the keyword fits completely inside of without regard for case" do
  109. Normalizer.normalize("foo BAR",["foo","bar","foo bar bellyhoo baz","foo bar baz"]).should=={:match=>"foo bar baz", :match_type=>:sub_match}
  110. end
  111.  
  112. it "should return the shortest term that the keyword fits completely inside a term, even if the ketword is separated by dashes" do
  113. Normalizer.normalize("foo-bar",["foo","bar","foo bar bellyhoo baz","foo bar baz"]).should=={:match=>"foo bar baz", :match_type=>:sub_match}
  114. end
  115.  
  116. it "should return the longest term that fits completely inside the keyword" do
  117. Normalizer.normalize("VR6 Luxury",["VR6", "TDI", "VR6 Sport", "VR6 Lux", "V8", "V6"]).should=={:match=>"VR6 Lux", :match_type=>:sub_match}
  118. end
  119.  
  120. it "should return the longest term that fits completely inside the keyword regardless of case" do
  121. Normalizer.normalize("Vr6 luxury",["VR6", "TDI", "VR6 Sport", "VR6 Lux", "V8", "V6"]).should=={:match=>"VR6 Lux", :match_type=>:sub_match}
  122. end
  123.  
  124. it "should return longest term that fits completely inside the keyword, even if the term is multiple words and is not contiguous inside the keyword" do
  125. Normalizer.normalize("HD Ext 143.5\" WB C6P LS",["LS","HD","HD LS"]).should=={:match=>"HD LS", :match_type=>:sub_match}
  126. end
  127.  
  128. it "should return longest term that fits completely inside the keyword, even if the term is multiple words and is not contiguous inside the keyword, without regard for case" do
  129. Normalizer.normalize("Hd Ext 143.5\" WB C6P lS",["LS","hD","HD ls"]).should=={:match=>"HD ls", :match_type=>:sub_match}
  130. end
  131.  
  132. it "should return longest term that fits completely inside the keyword, even if the term is multiple words and is not contiguous inside the keyword, without regard for dashes" do
  133. Normalizer.normalize("LTZ 4x4 Z71",["LTZ", "LTZ Z-71"]).should=={:match=>"LTZ Z-71", :match_type=>:sub_match}
  134. end
  135.  
  136. it "should return longest term that fits completely inside the keyword, even if the term is multiple words and is not contiguous inside the keyword, without regard for spaces" do
  137. Normalizer.normalize("LTZ 4x4 Z 71",["LTZ", "LTZ Z-71"]).should=={:match=>"LTZ Z-71", :match_type=>:sub_match}
  138. end
  139.  
  140. it "should return longest term that fits completely inside the keyword, even if the term is multiple words and is not contiguous inside the keyword, without regard for dashes" do
  141. Normalizer.normalize("LTZ Z-71",["LTZ", "LTZ 4x4 Z 71"]).should=={:match=>"LTZ 4x4 Z 71", :match_type=>:sub_match}
  142. end
  143.  
  144. it "should return the longest term that fits completely inside the keyword, even if there is a dash in the term or keyword" do
  145. Normalizer.normalize("VR6-Luxury",["VR6", "TDI", "VR6 Sport", "VR6 Sport Matic Lux", "VR6 Lux", "V8", "V6"]).should=={:match=>"VR6 Lux", :match_type=>:sub_match}
  146. Normalizer.normalize("VR6 Luxury",["VR6", "TDI", "VR6 Sport", "VR6 Sport Matic Lux", "VR6-Lux", "V8", "V6"]).should=={:match=>"VR6-Lux", :match_type=>:sub_match}
  147. end
  148.  
  149. it "should return closest match when no direct matches can be found" do
  150. Normalizer.normalize("Ferd",["BMW","Buick","Chevrolet","Ford"]).should=={:match=>"Ford", :match_type=>:closest_match}
  151. end
  152.  
  153. it "should check synonyms if an exact match cannot be made" do
  154. Normalizer.normalize("GSR Limited Edition",["GSR","GSR SE","GSR X","GSR LE"]).should=={:match=>"GSR LE", :match_type=>:exact_match}
  155. end
  156.  
  157. it "should not use synonyms if they do not result in a better match" do
  158. Normalizer.normalize("GSR Limited Edition",["GSR","GSR SE","GSR X","GSR LTD"]).should=={:match=>"GSR", :match_type=>:sub_match}
  159. end
  160.  
  161. it "should return the term that has the most common terms when the original match and synonym match are both sub matches" do
  162. Normalizer.normalize("GSR Limited Edition Deluxe",["GSR","GSR SE","GSR X","GSR LE"]).should=={:match=>"GSR LE", :match_type=>:sub_match}
  163. Normalizer.normalize("Limited Edition Deluxe GSR",["GSR","GSR SE","GSR X","GSR LTD","GSR LE"]).should=={:match=>"GSR LE", :match_type=>:sub_match}
  164. end
  165.  
  166. it "should use the closest term with synonyms" do
  167. Normalizer.normalize("3.7L Technology Pkg AWD",["Touring Pkg", "Tech Pkg", "Sport Pkg", "Tech/Entertainment Pkg", "Sport/Entertainment Pkg", "3.5", "Navigation", "3.7", "Advance"] ).should=={:match=>"Tech Pkg", :match_type=>:sub_match}
  168. end
  169.  
  170. it "should return exact match if all words in the keyword match a term, even if they are out of order." do
  171. Normalizer.normalize("CrewCab LT",["LT","Crew Cab","LT Crewcab"] ).should=={:match=>"LT Crewcab", :match_type=>:exact_match}
  172. Normalizer.normalize("Crew-Cab LT",["LT","Crew Cab","LT Crewcab"] ).should=={:match=>"LT Crewcab", :match_type=>:exact_match}
  173. Normalizer.normalize("Crew Cab LT",["LT","Crew Cab","LT Crew-cab"] ).should=={:match=>"LT Crew-cab", :match_type=>:exact_match}
  174. end
  175.  
  176. it "should return a sub match when there is an exact match on some terms and the others fit inside a term" do
  177. Normalizer.normalize("CrewCab LT1",["LT","Crew Cab","LT Crewcab"] ).should=={:match=>"LT Crewcab", :match_type=>:sub_match}
  178. Normalizer.normalize("Crew-Cab LT1",["LT","Crew Cab","LT Crewcab"] ).should=={:match=>"LT Crewcab", :match_type=>:sub_match}
  179. Normalizer.normalize("Crew Cab LT1",["LT","Crew Cab","LT Crew-cab"] ).should=={:match=>"LT Crew-cab", :match_type=>:sub_match}
  180. Normalizer.normalize("Crew Cab L-T1",["LT","Crew Cab","LT Crewcab"] ).should=={:match=>"LT Crewcab", :match_type=>:sub_match}
  181. Normalizer.normalize("Crew Cab L-T 1",["LT","Crew Cab","LT Crew cab"] ).should=={:match=>"LT Crew cab", :match_type=>:sub_match}
  182. end
  183.  
  184. it "should return the term that has the most matching tokens" do
  185. Normalizer.normalize("Outback Extreme LTD 30th Ann",["Outback", "L 25th Anniversary", "Outback Limited 30th Anniversary", "Outback Limited"] ).should=={:match=>"Outback Limited 30th Anniversary",:match_type=>:sub_match}
  186. end
  187.  
  188. it "Time Test" do
  189. Normalizer.normalize("Silverado-DRW 143.5 WB 2500 Ext C6P HD LS",["1500 Pickups", "1500 Sport Pickups", "1500 Work Pickups", "2500 Chassis-Cabs", "2500 Pickups", "3500 Chassis-Cabs", "3500 HD Chassis-Cabs", "3500 Pickups", "Aluminum Step Van", "APV Cargo", "Astro", "Avalanche", "Aveo", "Beretta", "Blazer", "C 3500 HD", "C/K 1500", "C/K 2500", "C/K 3500", "Camaro", "Camaro Police Pkg", "Caprice", "Cavalier", "CC4500", "CC5500", "CC6500", "CC6H042", "CC7500", "CC7H042", "CC7H064", "CC8500", "CC8500 Tandem", "Celebrity", "CF6B042", "CF7B042", "CF7B064", "Chevy Cargo Van", "Chevy Sportvan", "Chevy Van", "Classic", "Classic Chevy Van", "Classic G Commercial Cutaway", "Classic G RV Cutaway", "Classic Sportvan", "Cobalt", "Colorado", "Commercial Cutaway Van", "Commercial/RV Cutaway Van", "Corsica", "Corvette", "Cruze", "CT6500", "CT7500", "CT8500", "CT8500 Tandem", "Equinox", "Express Van", "G Commercial Cutaway", "G Hi-Cube", "G RV Cutaway", "Geo Metro", "Geo Spectrum", "Geo Tracker", "HHR", "Hi-Cube Van", "Impala", "Impala Police", "Impala SS", "K Blazer", "Lumina", "Lumina APV", "Malibu", "Malibu Classic", "Malibu Hybrid", "Malibu Maxx", "Metro", "Monte Carlo", "Motor Home Chassis", "New Tahoe", "Nova", "Prizm", "RV Cutaway Van", "S-10", "S-10 Blazer", "S/T Blazer", "S/T Pickup", "Silverado 1500", "Silverado 2500", "Silverado 3500", "Silverado SS", "Spectrum", "Sport Van", "Sprint", "SSR", "Steel Value", "Suburban", "Tahoe", "Tahoe Police", "Tracker", "TrailBlazer", "Traverse", "Uplander", "Venture", "W3500", "W3S042", "W3S042i", "W4500", "Express 1500", "Express 2500", "Express 3500", "Chevelle", "C40", "Bel Air", "Corvair", "Citation", "Aveo5", "1500", "2500", "Volt", "Silverado 1500 HD", "Silverado 2500 HD", "Silverado 3500 HD"] ).should=={:match=>"Silverado 2500 HD", :match_type=>:sub_match}
  190. end
  191. end
Add Comment
Please, Sign In to add comment