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_verbose.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
54 lines (48 sloc)
2.13 KB
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 -i | |
# header not necessary, allows one to call | |
# ./wff1.jl ARGS... | |
# after chmod +x wff1.jl | |
# Alternatively call | |
# julia -i wff1.jl | |
# the -i denotes "interactive mode" similar to the cli in python. | |
# to just execute the code omit -i | |
5 | |
const DEF = "tests/one.txt" | |
if length(ARGS) > 0 # ARGS is an array of command line arguments as strings (separated by spaces) | |
files = ARGS[1:end] # indexing starts at 1! | |
else | |
println("please provide at least one file name!") | |
println("RUNNING $DEF") | |
files = [DEF] | |
end | |
# instantiate a dictionary of dictionaries | |
# one dictionary for each file provided, with the file name as key | |
word_dicts = Dict(file=>Dict{String, Int}() for file in files) # optional typing | |
delims = [' ','\n','\t','.',',','(',')','?','/'] # single quotes: character literal | |
for file in files | |
println("reading $(file)") # string interpolation (parentheses not necessary)! | |
open(file) do f # alternatively, f = open(file), just have to remember close(f) when done | |
s = readstring(f) # read entire contents as string | |
for word in split(s,delims) # split string by literals in array 'delims' | |
if haskey(word_dicts[file], word) # check if word has already been seen | |
word_dicts[file][word] += 1 # if so, increment counter | |
else | |
word_dicts[file][word] = 1 # otherwise add word as key with value 1 | |
end | |
end | |
end | |
end | |
# print word frequencies for each file | |
for file in files | |
println("\nword frequencies for $file") # linebreak (\n) interpolation | |
for kv in word_dicts[file] | |
println("\t$(kv)") # tab (\t) interpolation | |
end | |
end | |
println("\n --------------------------------------------------------------------") | |
println("\nyou are now in interactive mode!") | |
println("enter `word_dicts` in the command line to view your dictionary in REPL") | |
println("\tlooking up key values is the same syntax as indexing") | |
println(" e.g. `word_dicts[\"$(files[1])\"]` is the dictionary of word frequencies for `$(files[1])`") | |
println("\n --------------------------------------------------------------------\n") | |
println("enter `exit()` to ESCAPE\n") |