Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def search( keyword )
- results_exact = Array.new
- results_start = Array.new
- results_wildcard = Array.new
- results = Array.new
- word = Struct.new( :kanji, :kana, :meaning )
- # seek to the start of the file
- @fd.rewind
- # Exact match search
- match_exact_line = /(#{keyword})/
- match_exact_kanji = /(.*)\s+\[(.*)\]\s+\/(.*)\//
- match_exact_kana = /(.*)\s+\/(.*)\//
- @fd.each do |line|
- if match_exact_line =~ line
- exact = word.new
- if line =~ match_exact_kanji
- exact.kanji = $1
- exact.kana = $2
- exact.meaning = $3
- end
- if line =~ match_exact_kana
- exact.kana = $1
- exact.meaning = $2
- end
- results_exact.push( exact )
- end
- end
- # seek to the start of the file
- @fd.rewind
- # Matches all entries with the keyword in it.
- match_wildcard_line = /#{keyword}/
- match_wildcard_kanji = /(.*)\s+\[(.*)\]\s+\/(.*)\//
- match_wildcard_kana = /(.*)\s+\/(.*)\//
- @fd.each do |line|
- if match_wildcard_line =~ line
- wildcard = word.new
- if line =~ match_wildcard_kanji
- wildcard.kanji = $1
- wildcard.kana = $2
- wildcard.meaning = $3
- end
- if line =~ match_wildcard_kana
- wildcard.kana = $1
- wildcard.meaning = $2
- end
- results_wildcard.push( wildcard )
- end
- end
- results.push( results_exact )
- results.push( results_wildcard )
- return results
- end
Add Comment
Please, Sign In to add comment