Last active
February 6, 2023 07:42
-
-
Save yudukikun5120/ae5a837c908d1f547a88ed46e4ffdb52 to your computer and use it in GitHub Desktop.
『マルチメディアデータ工学:音声・動画像データベースの高速検索技術 (宝珍 輝尚)』31㌻のシグネチャファイルのRubyでの実装
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
KEYWORDS = | |
[ | |
%w[ribbon trouble sister vigorous], | |
%w[representative deny hypothesize] | |
] | |
.freeze | |
QUERY_KEYWORD = 'representative' | |
to_signature = ->(text) { text.unpack1('b*').rjust(100, '0') } | |
binominal_disjunction = ->(args) { args.map(&:to_i).reduce(:|) } | |
greater_bits = | |
lambda do |bits1, bits2| | |
bits1 | |
.chars | |
.zip(bits2.chars) | |
.all? { |a, b| a == '1' || b == '0' } | |
end | |
keyword_signatures = | |
KEYWORDS.map do |keywords| | |
keywords.map { |keyword| to_signature.call keyword } | |
end | |
text_signatures = | |
keyword_signatures.map do |signatures| | |
signatures.first.chars.zip(*signatures.drop(1).map(&:chars)) | |
.map(&binominal_disjunction) | |
.join | |
end | |
query_signature = | |
to_signature.call QUERY_KEYWORD | |
result = | |
text_signatures.filter { |sign| greater_bits.call(sign, query_signature) } | |
puts result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment