diff --git a/README.md b/README.md index cfff460..46d5ee6 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,12 @@ Included are a number of implementations of the word frequency functionality (wf An additional implementation which uses user defined modules is also provided. +Start with wff_dict_verbose.jl + chmod +x wff_dict_verbose.jl + ./wff_dict_verbose.jl tests/* +or + julia -i wff_dict_verbose.jl tests/* + ## Dictionaries ### wff_dict_verbose.jl @@ -16,10 +22,10 @@ Verbose implementation of wff using dictionaries. Implementation of wff using dictionaries with compact code. ## Types + ### wff_types.jl Implementation of wff using types with compact code. @@ -28,3 +34,7 @@ Implementation of wff using types with compact code. ### modules/wff.jl A module which provides functions necessary for implementing wff. + +### wff_mod.jl + +Implementation of WFF using modules.wff.jl diff --git a/modules/wff.jl b/modules/wff.jl new file mode 100644 index 0000000..5e80088 --- /dev/null +++ b/modules/wff.jl @@ -0,0 +1,35 @@ +module WFF + export wordlist + + const delims = [' ','\n','\t','.',',','(',')','?','/'] # single quotes: character literal + + function getstr(file) + f = open(file) + str = readstring(f) + close(f) + str + end + + type wordlist + file::String + words::Dict{String,Int} + string::String + addword::Function + print::Function + function wordlist(file::String) + this = new(file) + this.words = Dict{String,Int}() + this.string = getstr(this.file) + this.addword = function(word) + haskey(this.words, word) ? this.words[word] += 1 : this.words[word] = 1 + this.words[word] + end + this.print = function() + println("\nword frequencies for $(this.file)") + [println("\t$kv") for kv in this.words] # word_dicts is object scope! + end + map(word->this.addword(word),split(this.string,delims)) + this + end + end +end diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..5a1df38 --- /dev/null +++ b/test.sh @@ -0,0 +1,10 @@ +#!/bin/bash +echo +echo "testing wff_dict.jl" +./wff_dict.jl tests/* +echo +echo "testing wff_type.jl" +./wff_type.jl tests/* +echo +echo "testing wff_mod.jl" +./wff_mod.jl tests/* diff --git a/wff_dict.jl b/wff_dict.jl index 3c756c7..fc9e68a 100755 --- a/wff_dict.jl +++ b/wff_dict.jl @@ -1,7 +1,7 @@ -#!/usr/local/bin/julia -i +#!/usr/local/bin/julia files = length(ARGS) == 0 ? (println("please provide at least one file name!"); exit()) : ARGS -delims = [' ','\n','\t','.',',','(',')','?'] # single quotes: character literal +delims = [' ','\n','\t','.',',','(',')','?','/'] # single quotes: character literal addword!(d,word) = haskey(d, word) ? d[word] += 1 : d[word] = 1 diff --git a/wff_dict_verbose.jl b/wff_dict_verbose.jl index d264d1a..889c097 100755 --- a/wff_dict_verbose.jl +++ b/wff_dict_verbose.jl @@ -18,7 +18,7 @@ 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 +delims = [' ','\n','\t','.',',','(',')','?','/'] # single quotes: character literal for file in files println("reading $(file)") # string interpolation (parentheses not necessary)! diff --git a/wff_mod.jl b/wff_mod.jl new file mode 100755 index 0000000..67010c6 --- /dev/null +++ b/wff_mod.jl @@ -0,0 +1,9 @@ +#!/usr/local/bin/julia + +const files = length(ARGS) == 0 ? (println("please provide at least one file name!"); exit()) : ARGS +include("modules/wff.jl") +using WFF + +file_o = [wordlist(file) for file in files] + +map(x->x.print(),file_o) diff --git a/wff_type.jl b/wff_type.jl index 18ffb42..ca86830 100755 --- a/wff_type.jl +++ b/wff_type.jl @@ -1,7 +1,7 @@ #!/usr/local/bin/julia const files = length(ARGS) == 0 ? (println("please provide at least one file name!"); exit()) : ARGS -const delims = [' ','\n','\t','.',',','(',')','?'] # single quotes: character literal +const delims = [' ','\n','\t','.',',','(',')','?','/'] # single quotes: character literal function getstr(file) f = open(file) @@ -28,7 +28,6 @@ type wordlist end end - file_o = [wordlist(file) for file in files] for wl in file_o println("$(typeof(wl)) \"obj\"")