Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
29
Task/Inverted-index/Ruby/inverted-index-1.rb
Normal file
29
Task/Inverted-index/Ruby/inverted-index-1.rb
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
if File.exist? "index.dat"
|
||||
@data = Marshal.load open("index.dat")
|
||||
else
|
||||
@data = {}
|
||||
end
|
||||
|
||||
# Let's give the string class the ability to tokenize itsself into lowercase
|
||||
# words with no punctuation.
|
||||
class String
|
||||
def index_sanitize
|
||||
self.split.collect do |token|
|
||||
token.downcase.gsub(/\W/, '')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Just implementing a simple inverted index here.
|
||||
ARGV.each do |filename|
|
||||
open filename do |file|
|
||||
file.read.index_sanitize.each do |word|
|
||||
@data[word] ||= []
|
||||
@data[word] << filename unless @data[word].include? filename
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
open("index.dat", "w") do |index|
|
||||
index.write Marshal.dump(@data)
|
||||
end
|
||||
22
Task/Inverted-index/Ruby/inverted-index-2.rb
Normal file
22
Task/Inverted-index/Ruby/inverted-index-2.rb
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
if File.exist? "index.dat"
|
||||
@data = Marshal.load open("index.dat")
|
||||
else
|
||||
raise "The index data file could not be located."
|
||||
end
|
||||
|
||||
class String
|
||||
def index_sanitize
|
||||
self.split.collect do |token|
|
||||
token.downcase.gsub(/\W/, '')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Take anything passed in on the command line in any form and break it
|
||||
# down the same way we did when making the index.
|
||||
ARGV.join(' ').index_sanitize.each do |word|
|
||||
@result ||= @data[word]
|
||||
@result &= @data[word]
|
||||
end
|
||||
|
||||
p @result
|
||||
Loading…
Add table
Add a link
Reference in a new issue