Class: Eiwaji::LexerWidget
- Inherits:
-
Qt::DockWidget
- Object
- Qt::DockWidget
- Eiwaji::LexerWidget
- Defined in:
- lib/lexer_widget.rb
Defined Under Namespace
Modules: Underscore
Constant Summary collapse
- POS_IGNORE =
parts-of-speech to not provide hyperlinks for
[Ve::PartOfSpeech::Symbol]
- HISTORY_STRING_LENGTH =
30- MAX_HISTORY_ITEMS =
50
Instance Method Summary collapse
- #event(event) ⇒ Object
- #historyItemChanged(index) ⇒ Object
- #historyNext ⇒ Object
- #historyPrev ⇒ Object
-
#initialize(parent) ⇒ LexerWidget
constructor
A new instance of LexerWidget.
- #lexText(text, append_to_history = false) ⇒ Object
-
#part_of_speech_colors ⇒ Object
each symbol represents the class name of a Ve::PartOfSpeech.
- #searchWord(ref) ⇒ Object
- #textBrowser ⇒ Object
- #wordClicked(url) ⇒ Object
-
#wordToHtml(word, index) ⇒ Object
converts a Ve::Word and its index in the search results to a String representation.
Constructor Details
#initialize(parent) ⇒ LexerWidget
Returns a new instance of LexerWidget.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/lexer_widget.rb', line 15 def initialize(parent) super(parent) @ui = Ui_LexerWidget.new @ui.setupUi(self) part_of_speech_colors connect(@ui.lexerTextBrowser, SIGNAL('anchorClicked(QUrl)'), self, SLOT('wordClicked(QUrl)')) @ui.historyBox.insertItem(0, "...") connect(@ui.historyBox, SIGNAL('currentIndexChanged(int)'), self, SLOT('historyItemChanged(int)')) connect(@ui., SIGNAL('clicked()'), self, SLOT('historyNext()')) connect(@ui., SIGNAL('clicked()'), self, SLOT('historyPrev()')) end |
Instance Method Details
#event(event) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/lexer_widget.rb', line 36 def event(event) # provide tooltips for word part-of-speech when mousing over hrefs if event.type == Qt::Event::ToolTip cursor = @ui.lexerTextBrowser.cursorForPosition(event.pos) cursor.select(Qt::TextCursor::WordUnderCursor) fragment = cursor.selection() # works in this case, since the input is clearly defined in wordToHtml hrefRegex = /<a\s+(?:[^>]*?\s+)?href="([^"]*)"/ resultsIndex = hrefRegex.match(fragment.toHtml) if not resultsIndex or resultsIndex.size != 2 Qt::ToolTip.hideText else word = @lexer_results[resultsIndex[1].to_i] Qt::ToolTip.showText(event.globalPos(), word.lemma + " " + word.part_of_speech.name) end end super(event) end |
#historyItemChanged(index) ⇒ Object
57 58 59 60 61 |
# File 'lib/lexer_widget.rb', line 57 def historyItemChanged(index) return if index == 0 text = @ui.historyBox.itemData(index).value lexText(text) end |
#historyNext ⇒ Object
70 71 72 73 74 |
# File 'lib/lexer_widget.rb', line 70 def historyNext index = @ui.historyBox.currentIndex - 1 index = 1 if index < 1 @ui.historyBox.setCurrentIndex(index) end |
#historyPrev ⇒ Object
63 64 65 66 67 68 |
# File 'lib/lexer_widget.rb', line 63 def historyPrev index = @ui.historyBox.currentIndex + 1 index += 1 if @ui.historyBox.currentIndex == 0 index = @ui.historyBox.count - 1 if index >= @ui.historyBox.count - 1 @ui.historyBox.setCurrentIndex(index) end |
#lexText(text, append_to_history = false) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/lexer_widget.rb', line 76 def lexText(text, append_to_history=false) text = text.force_encoding("UTF-8") if append_to_history @ui.historyBox.setCurrentIndex(0) truncated_text = text.size < HISTORY_STRING_LENGTH ? text : text[0..HISTORY_STRING_LENGTH-1] + "..." @ui.historyBox.insertItem(1, truncated_text, Qt::Variant.new(text)) @ui.historyBox.removeItem(MAX_HISTORY_ITEMS+1) if @ui.historyBox.count > MAX_HISTORY_ITEMS+1 end lines = text.split("\n") html_lines = [] @lexer_results = Hash.new i = 0 lines.each do |line| words = Ve.in(:ja).words(line) html_words = [] words.each do |word| @lexer_results[i] = word html_words << wordToHtml(word, i) i = i + 1 end html_lines << "<div style='margin-bottom: 20px'>" + html_words.join(' ') + "<\div>" end html = html_lines.join("") @ui.lexerTextBrowser.setText(html) end |
#part_of_speech_colors ⇒ Object
each symbol represents the class name of a Ve::PartOfSpeech
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/lexer_widget.rb', line 133 def part_of_speech_colors @part_of_speech_colors ||= { :verb => "#808080", :noun => "#000000", :proper_noun => "#0000FF", :symbol => "#000000", :postposition => "#FF0000", :pronoun => "#008080", :adjective => "#008000", :adverb => "#43C6DB", :conjunction => "#FFA500", :background => "#FFFFFF", :default => "#FF00FF" } end |
#searchWord(ref) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/lexer_widget.rb', line 112 def searchWord(ref) raise "No results from the lexer." if @lexer_results.empty? word = @lexer_results[ref] # Word lemma # since the lemmas of Words like "1話" become "*話", remove extra '*'s lemma = word.lemma.tr('*','') # raw token, drops "する" and other extra parts query = word.tokens[0][:lemma] query = lemma if query == "*" if Eiwaji::DEBUG p word, query, lemma end # search on the query and sort by the results most similar to the lemma parent.search(query, lemma) end |
#textBrowser ⇒ Object
32 33 34 |
# File 'lib/lexer_widget.rb', line 32 def textBrowser @ui.textBrowser end |
#wordClicked(url) ⇒ Object
108 109 110 |
# File 'lib/lexer_widget.rb', line 108 def wordClicked(url) searchWord(url.path.to_i) end |
#wordToHtml(word, index) ⇒ Object
converts a Ve::Word and its index in the search results to a String representation
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/lexer_widget.rb', line 150 def wordToHtml(word, index) raw = word.word pos = word.part_of_speech color = @part_of_speech_colors[pos.name.gsub(' ', '_').underscore.to_sym] || @part_of_speech_colors[:default] if POS_IGNORE.include? pos raw = "<font style='color: #{color}'>" + raw + "</font>" else raw = "<a href=\'#{index}\' style='color: #{color}'>" + raw + "</a>" end end |