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/modules/wff.jl
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
35 lines (32 sloc)
1.01 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
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 |