module DictionaryReport
DictionaryReport โ IRC cards and WordNet sense-line formatting. Public: plain_sense_line, style_sense_line, format_wn_output, clean_line, truncate_text Tests: test/lib/dictionary_report_test.rb
Constants
- IRC_ITALIC
- MAX_BODY_LINES
- MAX_LINE_LENGTH
- PLAIN_SENSE_LINE
- RAW_SENSE_LINE
- STYLE_TITLE
Public Instance Methods
Source
# File lib/dictionary_report.rb, line 25 def clean_line(line) line.to_s.gsub(/\t/, " ").gsub(/\s+/, " ").strip end
Source
# File lib/dictionary_report.rb, line 64 def extract_body_lines(lines) body = [] lines.each do |line| cleaned = clean_line(line) next if cleaned.empty? next if cleaned.match?(/\AOverview of /i) next if cleaned.match?(/\AThe (noun|verb|adj|adverb) /i) next if cleaned.match?(/\A\d+ of \d+ senses of /i) if cleaned.match?(/\A\d+\./) body << plain_sense_line(cleaned) elsif cleaned.start_with?("=>") || cleaned.start_with?("*>") body << cleaned.gsub(/\A=>\s*/, " ") elsif cleaned.match?(/\ASense \d+/i) || cleaned.match?(/\ASynonyms of /i) || cleaned.match?(/\AAntonyms of /i) || cleaned.match?(/\AHypernym/i) body << cleaned end end body end
Source
# File lib/dictionary_report.rb, line 95 def format_header(word, lines, search_label:, corrected_word: nil) title_word = if corrected_word && corrected_word.to_s != word.to_s "#{word} โ #{corrected_word}" else word.to_s.strip end title_bits = [title_word] overview = lines.find { |line| line.match?(/\AOverview of /i) } if overview pos = overview.match(/\AOverview of (\S+) /i)&.[](1) title_bits << pos if pos && !pos.empty? end title_bits << search_label unless search_label.to_s.strip.empty? title = title_bits.reject(&:empty?).join(" ยท ") IrcFormat.report_header(tag: "DICT", title: title, style: STYLE_TITLE) end
Source
# File lib/dictionary_report.rb, line 112 def format_no_results(word) header = IrcFormat.report_header(tag: "DICT", title: word.to_s.strip, style: STYLE_TITLE) footer = IrcFormat.report_footer("WordNet") [header, "No WordNet entry for that word.", footer].join("\n") end
Source
# File lib/dictionary_report.rb, line 118 def format_wn_output(raw, word:, search_label:, corrected_word: nil) lines = raw.to_s.lines.map(&:rstrip) return format_no_results(word) if no_match_output?(raw) body = extract_body_lines(lines) return format_no_results(word) if body.empty? truncated = false if body.length > MAX_BODY_LINES body = body.first(MAX_BODY_LINES) truncated = true end body = body.map do |line| plain = sense_line?(line) ? plain_sense_line(line) : line plain = truncate_text(plain) sense_line?(plain) ? style_formatted_sense_line(plain) : plain end body << "... more senses โ try a specific search flag" if truncated [ format_header(word, lines, search_label: search_label, corrected_word: corrected_word), *body, IrcFormat.report_footer(search_label, "WordNet") ].join("\n") end
Source
# File lib/dictionary_report.rb, line 18 def italic(text) value = text.to_s return value if value.empty? "#{IRC_ITALIC}#{value}#{IRC_ITALIC}" end
Source
# File lib/dictionary_report.rb, line 85 def no_match_output?(text) value = text.to_s return true if value.strip.empty? value.match?(/no (results|senses|data)/i) || value.match?(/can't find/i) || value.include?("Fatal error") || value.match?(/\A0 of \d+ senses\b/i) end
Source
# File lib/dictionary_report.rb, line 36 def plain_sense_line(line) cleaned = clean_line(line) match = cleaned.match(RAW_SENSE_LINE) return cleaned unless match number, words, gloss = match.captures gloss = truncate_text(gloss) "#{number}. #{words} โ #{gloss}" end
Source
# File lib/dictionary_report.rb, line 60 def sense_line?(line) clean_line(line).match?(PLAIN_SENSE_LINE) || clean_line(line).match?(RAW_SENSE_LINE) end
Source
# File lib/dictionary_report.rb, line 51 def style_formatted_sense_line(plain_line) cleaned = clean_line(plain_line) match = cleaned.match(PLAIN_SENSE_LINE) return cleaned unless match number, words, gloss = match.captures style_sense_line(number, words, gloss) end
Source
# File lib/dictionary_report.rb, line 46 def style_sense_line(number, words, gloss) truncated_gloss = truncate_text(gloss) "#{IrcFormat.bold("#{number}.")} #{IrcFormat.underline(words)} โ #{italic(truncated_gloss)}" end
Source
# File lib/dictionary_report.rb, line 29 def truncate_text(text, max_length = MAX_LINE_LENGTH) value = text.to_s.strip return value if value.length <= max_length "#{value[0, max_length - 3]}..." end