Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
modules
  • Loading branch information
kirk gardner committed Apr 3, 2018
1 parent f3ca692 commit 89136f3
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 7 deletions.
14 changes: 12 additions & 2 deletions README.md
Expand Up @@ -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

Expand All @@ -16,10 +22,10 @@ Verbose implementation of wff using dictionaries.
Implementation of wff using dictionaries with compact code.

## Types
<!--
### wff_types_verbose.jl

Verbose implementation of wff using types

-->
### wff_types.jl

Implementation of wff using types with compact code.
Expand All @@ -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
35 changes: 35 additions & 0 deletions 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
10 changes: 10 additions & 0 deletions 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/*
4 changes: 2 additions & 2 deletions 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

Expand Down
2 changes: 1 addition & 1 deletion wff_dict_verbose.jl
Expand Up @@ -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)!
Expand Down
9 changes: 9 additions & 0 deletions 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)
3 changes: 1 addition & 2 deletions 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)
Expand All @@ -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\"")
Expand Down

0 comments on commit 89136f3

Please sign in to comment.