Class: Eiwaji::DictionaryTableModel
- Inherits:
-
Qt::AbstractTableModel
- Object
- Qt::AbstractTableModel
- Eiwaji::DictionaryTableModel
- Defined in:
- lib/dictionary_model.rb
Instance Method Summary collapse
- #calcSimilarity(word) ⇒ Object
- #calcSimilarityForAll ⇒ Object
- #columnCount(parent) ⇒ Object
- #data(index, role = Qt::DisplayRole) ⇒ Object
- #flags(index) ⇒ Object
- #headerData(section, orientation, role = Qt::DisplayRole) ⇒ Object
-
#initialize(parent, entries, lemma) ⇒ DictionaryTableModel
constructor
A new instance of DictionaryTableModel.
- #rowCount(parent) ⇒ Object
Constructor Details
#initialize(parent, entries, lemma) ⇒ DictionaryTableModel
Returns a new instance of DictionaryTableModel.
3 4 5 6 7 8 9 |
# File 'lib/dictionary_model.rb', line 3 def initialize(parent, entries, lemma) super(parent) @white = Text::WhiteSimilarity.new @entries = entries @lemma = lemma calcSimilarityForAll end |
Instance Method Details
#calcSimilarity(word) ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/dictionary_model.rb', line 98 def calcSimilarity(word) w = word.force_encoding("UTF-8") sim = @white.similarity(@lemma, w) if sim.nan? sim = (@lemma == w ? 1.0 : 0.0) end sim end |
#calcSimilarityForAll ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/dictionary_model.rb', line 11 def calcSimilarityForAll @sim = Array.new(@entries.size) @entries.each_with_index do |entry, i| similarity = 0.0 # if there are multiple kana/kanji readings, get the highest similarity out of each unless entry.kana.nil? if entry.kana.kind_of?(Array) similarity += entry.kana.inject(0.0) do |max, k| sim = calcSimilarity(k) [max, sim].max end else similarity += calcSimilarity(entry.kana) end end unless entry.kanji.nil? if entry.kanji.kind_of?(Array) similarity += entry.kanji.inject(0.0) do |max, k| sim = calcSimilarity(k) [max, sim].max end else similarity += calcSimilarity(entry.kanji) end end @sim[i] = similarity end end |
#columnCount(parent) ⇒ Object
41 42 43 |
# File 'lib/dictionary_model.rb', line 41 def columnCount(parent) 4 end |
#data(index, role = Qt::DisplayRole) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/dictionary_model.rb', line 57 def data(index, role=Qt::DisplayRole) invalid = Qt::Variant.new return invalid unless role == Qt::DisplayRole return invalid if @entries.empty? return invalid if @entries.size <= index.row return invalid unless index.isValid entry = @entries[index.row] v = case index.column when 0 # kanji if not entry.kanji.empty? if entry.kanji.kind_of?(Array) entry.kanji.map {|k| k.force_encoding("UTF-8") }.join(', ') else entry.kanji.force_encoding("UTF-8") end end when 1 # kana entry.kana.map {|k| k.force_encoding("UTF-8") }.join(', ') unless entry.kana.nil? when 2 # sense if entry.senses.size > 1 sense = entry.senses.map.with_index(1) do |s, i| pos = s.parts_of_speech.nil? ? "" : "(" + s.parts_of_speech.join(", ") + ") " glosses = s.glosses.join(", ") pos + glosses end sense.reverse.join(" \n ") else s = entry.senses.first pos = s.parts_of_speech.nil? ? "" : "(" + s.parts_of_speech.join(", ") + ") " glosses = s.glosses.join(", ") pos + glosses end when 3 # similarity @sim[index.row] else raise "invalid column #{index.column}" end || "" return Qt::Variant.new(v) end |
#flags(index) ⇒ Object
53 54 55 |
# File 'lib/dictionary_model.rb', line 53 def flags(index) Qt::ItemIsSelectable | Qt::ItemIsEnabled end |
#headerData(section, orientation, role = Qt::DisplayRole) ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/dictionary_model.rb', line 107 def headerData(section, orientation, role=Qt::DisplayRole) return Qt::Variant.new if role != Qt::DisplayRole v = case orientation when Qt::Horizontal ["Kanji","Kana","Meaning","Similarity"][section] else section.to_s end return Qt::Variant.new(v) end |
#rowCount(parent) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/dictionary_model.rb', line 45 def rowCount(parent) if parent.valid? return 0 else return @entries.size end end |