-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
120 additions
and
116 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,33 @@ | ||
#TO DO | ||
# Doc string | ||
function maskedscore(m::Langmodel, temp_lm::DefaultDict, word, context) | ||
score(m, temp_lm, lookup(m.vocab, [word])[1], lookup(m.vocab, [context])[1]) | ||
""" | ||
$(TYPEDSIGNATURES) | ||
""" | ||
function maskedscore(m::Langmodel, temp_lm::DefaultDict, word, context)::Float64 | ||
score(m, temp_lm, lookup(m.vocab, [word])[begin], lookup(m.vocab, [context])[begin]) | ||
end | ||
|
||
function logscore(m::Langmodel, temp_lm::DefaultDict, word, context) | ||
""" | ||
$(TYPEDSIGNATURES) | ||
""" | ||
function logscore(m::Langmodel, temp_lm::DefaultDict, word, context)::Float64 | ||
log2(maskedscore(m, temp_lm, word, context)) | ||
end | ||
|
||
function entropy(m::Langmodel, lm::DefaultDict, text_ngram) | ||
local log_set=Float64[] | ||
for ngram in text_ngram | ||
""" | ||
$(TYPEDSIGNATURES) | ||
""" | ||
function entropy(m::Langmodel, lm::DefaultDict, text_ngram::AbstractVector)::Float64 | ||
n_sum = sum(text_ngram) do ngram | ||
ngram = split(ngram) | ||
push!(log_set, logscore(m, lm, ngram[end], join(ngram[1:end-1], " "))) | ||
#println(logscore(m,lm,ngram[end],ngram[1:end-1])) | ||
logscore(m, lm, ngram[end], join(ngram[begin:end-1], " ")) | ||
end | ||
return(sum(log_set)/length(log_set)) | ||
return n_sum/length(text_ngram) | ||
end | ||
|
||
function perplexity(m::Langmodel, lm::DefaultDict, text_ngram) | ||
return(2^(entropy(m, lm, text_ngram))) | ||
""" | ||
$(TYPEDSIGNATURES) | ||
""" | ||
function perplexity(m::Langmodel, lm::DefaultDict, text_ngram::AbstractVector)::Float64 | ||
return 2^(entropy(m, lm, text_ngram)) | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,18 @@ | ||
using DataStructures | ||
|
||
""" | ||
counter is used to make conditional distribution, which is used by score functions to | ||
calculate conditional frequency distribution | ||
$(TYPEDSIGNATURES) | ||
counter is used to make conditional distribution, which is used by score functions to | ||
calculate conditional frequency distribution | ||
""" | ||
function counter2(data, min::Integer, max::Integer) | ||
data = everygram(data, min_len=min, max_len=max) | ||
data = split.(data) | ||
temp_lm = DefaultDict{SubString{String}, Accumulator{String,Int64}}(counter(SubString{String})) | ||
for i in 1:length(data) | ||
history,word = data[i][1:end-1], data[i][end] | ||
temp_lm = DefaultDict{SubString{String},Accumulator{String,Int64}}(counter(SubString{String})) | ||
for i in eachindex(data) | ||
history, word = data[i][begin:end-1], data[i][end] | ||
temp_lm[join(history, " ")][word] += 1 | ||
end | ||
return temp_lm | ||
end | ||
|
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
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
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
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
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
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
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
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
Oops, something went wrong.