Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
35
Task/Inverted-index/CoffeeScript/inverted-index-1.coffee
Normal file
35
Task/Inverted-index/CoffeeScript/inverted-index-1.coffee
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
fs = require 'fs'
|
||||
|
||||
make_index = (fns) ->
|
||||
# words are indexed by filename and 1-based line numbers
|
||||
index = {}
|
||||
for fn in fns
|
||||
for line, line_num in fs.readFileSync(fn).toString().split '\n'
|
||||
words = get_words line
|
||||
for word in words
|
||||
word = mangle(word)
|
||||
index[word] ||= []
|
||||
index[word].push [fn, line_num+1]
|
||||
index
|
||||
|
||||
grep = (index, word) ->
|
||||
console.log "locations for '#{word}':"
|
||||
locations = index[mangle(word)] || []
|
||||
for location in locations
|
||||
[fn, line_num] = location
|
||||
console.log "#{fn}:#{line_num}"
|
||||
console.log "\n"
|
||||
|
||||
get_words = (line) ->
|
||||
words = line.replace(/\W/g, ' ').split ' '
|
||||
(word for word in words when word != '')
|
||||
|
||||
mangle = (word) ->
|
||||
# avoid conflicts with words like "constructor"
|
||||
'_' + word
|
||||
|
||||
do ->
|
||||
fns = (fn for fn in fs.readdirSync('.') when fn.match /\.coffee/)
|
||||
index = make_index(fns)
|
||||
grep index, 'make_index'
|
||||
grep index, 'sort'
|
||||
15
Task/Inverted-index/CoffeeScript/inverted-index-2.coffee
Normal file
15
Task/Inverted-index/CoffeeScript/inverted-index-2.coffee
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
> coffee inverted_index.coffee
|
||||
locations for 'make_index':
|
||||
inverted_index.coffee:3
|
||||
inverted_index.coffee:33
|
||||
inverted_index.coffee:34
|
||||
|
||||
|
||||
locations for 'sort':
|
||||
anagrams.coffee:8
|
||||
derangements.coffee:14
|
||||
heap.coffee:34
|
||||
heap.coffee:43
|
||||
huffman.coffee:81
|
||||
inverted_index.coffee:35
|
||||
knuth_sample.coffee:12
|
||||
Loading…
Add table
Add a link
Reference in a new issue