Guest User

Untitled

a guest
Feb 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. def search( keyword )
  2. results_exact = Array.new
  3. results_start = Array.new
  4. results_wildcard = Array.new
  5. results = Array.new
  6. word = Struct.new( :kanji, :kana, :meaning )
  7. # seek to the start of the file
  8. @fd.rewind
  9. # Exact match search
  10. match_exact_line = /(#{keyword})/
  11. match_exact_kanji = /(.*)\s+\[(.*)\]\s+\/(.*)\//
  12. match_exact_kana = /(.*)\s+\/(.*)\//
  13. @fd.each do |line|
  14. if match_exact_line =~ line
  15. exact = word.new
  16. if line =~ match_exact_kanji
  17. exact.kanji = $1
  18. exact.kana = $2
  19. exact.meaning = $3
  20. end
  21. if line =~ match_exact_kana
  22. exact.kana = $1
  23. exact.meaning = $2
  24. end
  25. results_exact.push( exact )
  26. end
  27. end
  28. # seek to the start of the file
  29. @fd.rewind
  30. # Matches all entries with the keyword in it.
  31. match_wildcard_line = /#{keyword}/
  32. match_wildcard_kanji = /(.*)\s+\[(.*)\]\s+\/(.*)\//
  33. match_wildcard_kana = /(.*)\s+\/(.*)\//
  34. @fd.each do |line|
  35. if match_wildcard_line =~ line
  36. wildcard = word.new
  37. if line =~ match_wildcard_kanji
  38. wildcard.kanji = $1
  39. wildcard.kana = $2
  40. wildcard.meaning = $3
  41. end
  42. if line =~ match_wildcard_kana
  43. wildcard.kana = $1
  44. wildcard.meaning = $2
  45. end
  46. results_wildcard.push( wildcard )
  47. end
  48. end
  49. results.push( results_exact )
  50. results.push( results_wildcard )
  51. return results
  52. end
Add Comment
Please, Sign In to add comment