Skip to content
Permalink
75530368cf
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
executable file 48 lines (42 sloc) 1.89 KB
#!/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
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!")
exit() # ... exit interpreter
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("\nyou're now in interactive mode!")
println("type word_dicts in the command line to view your dictionary")
println("\tlooking up key values is the same syntax as indexing")
println("\te.x. word_dicts[\"$(files[1])\"] would give you the dictionary of word frequencies for $(files[1])")