Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
sample_code/wff_dict.jl
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
26 lines (20 sloc)
944 Bytes
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
#!/usr/local/bin/julia | |
const DEF = "tests/one.txt" | |
const files = length(ARGS) == 0 ? (println("please provide at least one file name!\n > RUNNING $DEF"); [DEF]) : ARGS | |
const delims = [' ','\n','\t','.',',','(',')','?','/'] # single quotes: character literal | |
addword!(d,word) = haskey(d, word) ? d[word] += 1 : d[word] = 1 | |
function findwords(file) | |
d = Dict{String,Int}() | |
println("reading $(file)") | |
open(file) do f | |
map(word->addword!(d,word),split(readstring(f),delims)) # example using map function | |
# findwords(file) = [addword!(d,word) for word in split(readstring(f),delims)] | |
end | |
d # if no return statement last line is returned | |
end | |
word_dicts = Dict(file=>findwords(file) for file in files) # as dict list comprehension | |
function printwords(file) | |
println("\nword frequencies for $file") | |
[println("\t$kv") for kv in word_dicts[file]] # word_dicts is in global scope! | |
end | |
map(printwords,files) |