| 1 |
|
| 2 |
|
| 3 |
module Plugin::Score |
| 4 |
extend self |
| 5 |
|
| 6 |
def score_by_score(model, target_note=model) |
| 7 |
@score_cache ||= TimeLimitedStorage.new(Array, Object, 60) |
| 8 |
@score_cache[[model, target_note]] ||= score_by_score_nocache(model, target_note).to_a.freeze |
| 9 |
end |
| 10 |
|
| 11 |
def score_by_score_nocache(model, target_note=model) |
| 12 |
_, _, available_score_list = Plugin.filtering(:score_filter, model, target_note, Set.new) |
| 13 |
selected_score = choose_best_score(available_score_list) |
| 14 |
if selected_score && !selected_score.all? { |s| s.is_a?(Plugin::Score::TextNote) } |
| 15 |
score_expand(selected_score, model) |
| 16 |
elsif target_note.is_a?(Plugin::Score::TextNote) |
| 17 |
[target_note] |
| 18 |
else |
| 19 |
score_by_score(model, Plugin::Score::TextNote.new(description: model.description)) |
| 20 |
end |
| 21 |
end |
| 22 |
|
| 23 |
べきScoreをひとつだけ返す。 |
| 24 |
合は nil を返す。複数該当する場合は、結果は不定。 |
| 25 |
def choose_best_score(score_list) |
| 26 |
selected = max_score_count(smallest_leading_text_size(score_list)) |
| 27 |
selected.first |
| 28 |
end |
| 29 |
|
| 30 |
eのdescriptionの文字数の合計が少ないもののみを列挙する。 |
| 31 |
def smallest_leading_text_size(score_list) |
| 32 |
min_all(score_list, &method(:leading_text_size)) |
| 33 |
end |
| 34 |
|
| 35 |
が一番多いもののみを列挙する。 |
| 36 |
def max_score_count(score_list) |
| 37 |
max_all(score_list, &:count) |
| 38 |
end |
| 39 |
|
| 40 |
private |
| 41 |
|
| 42 |
def score_expand(score, model) |
| 43 |
score.flat_map do |note| |
| 44 |
if note.is_a? Plugin::Score::TextNote |
| 45 |
score_by_score(model, note) |
| 46 |
else |
| 47 |
[note] |
| 48 |
end |
| 49 |
end |
| 50 |
end |
| 51 |
|
| 52 |
def leading_text_size(score) |
| 53 |
score.inject(0) do |index, note| |
| 54 |
if note.is_a?(Plugin::Score::TextNote) |
| 55 |
index + note.description.size |
| 56 |
else |
| 57 |
break index |
| 58 |
end |
| 59 |
end |
| 60 |
end |
| 61 |
|
| 62 |
def min_all(list, &proc) |
| 63 |
return list if list.size <= 1 |
| 64 |
order = Hash.new{|h,k| h[k] = proc.(k) } |
| 65 |
_, result = list.sort_by{|node| |
| 66 |
order[node] |
| 67 |
}.chunk{|node| |
| 68 |
order[node] |
| 69 |
}.first |
| 70 |
result |
| 71 |
end |
| 72 |
|
| 73 |
def max_all(list, &proc) |
| 74 |
min_all(list){|x| -proc.(x) } |
| 75 |
end |
| 76 |
end |