Skip to content

Commit

Permalink
added functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cecoeco committed Apr 20, 2024
1 parent 25be032 commit bc85078
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 109 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Readability"
uuid = "4455ec5f-b558-4ef1-b6d8-c3694046c382"
authors = ["Ceco E. Maples <[email protected]>"]
version = "0.2.3"
version = "0.3.0"

[compat]
julia = "1"
Expand Down
2 changes: 1 addition & 1 deletion app/backend/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ Readability = "4455ec5f-b558-4ef1-b6d8-c3694046c382"
[compat]
Oxygen = "1"
HTTP = "1"
Readability = "0.2"
Readability = "0.3"
julia = "1"
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ Readability = "4455ec5f-b558-4ef1-b6d8-c3694046c382"
[compat]
Documenter = "1"
DocumenterCitations = "1.3"
Readability = "0.2"
Readability = "0.3"
julia = "1"
22 changes: 12 additions & 10 deletions src/Readability.jl
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
module Readability

export reading_time, speaking_time
export characters, sentences, syllables,
words, complex_words, polysyllabic_words, difficult_words,
lines, paragraphs
export characters, characters_per_word
export syllables, syllables_per_word
export words, words_per_sentence
export sentences, sentences_per_paragraph
export lines, paragraphs
export ARI
export ColemanLiau
export DaleChall
export FleschKincaidGradeLevel, FleschReadingEaseScore
export GunningFog
export SMOG
export Spache
export DaleChall, Spache, difficult_words
export FleschKincaidGradeLevel, FleschReadingEase
export GunningFog, complex_words
export SMOG, polysyllabic_words

include("counts.jl")
include("time.jl")

include("ari.jl") # Automatic Readability Index
include("coleman-liau.jl")
include("counts.jl")
include("dale-chall.jl")
include("fkgl.jl") # Flesch-Kincaid Grade Level
include("fres.jl") # Flesch Reading Ease Score
include("gunning_fog.jl")
include("smog.jl") # Simple Measure of Gobbledygook
include("spache.jl")
include("time.jl")

end
11 changes: 1 addition & 10 deletions src/ari.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,5 @@
Returns the Automated Readability Index (ARI) of `text`.
"""
function ARI(text::String)
total_characters::Int = characters(text)
total_words::Int = words(text)
total_sentences::Int = sentences(text)

characters_per_word::Float64 = total_characters / total_words
words_per_sentence::Float64 = total_words / total_sentences

grade::Float64 = 4.71 * characters_per_word + 0.5 * words_per_sentence - 21.43
grade_rounded::Int = Base.ceil(Int, grade)
return grade_rounded
return Base.ceil(Int, 4.71 * characters_per_word(text) + 0.5 * words_per_sentence(text) - 21.43)
end
11 changes: 3 additions & 8 deletions src/coleman-liau.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@
Returns the Coleman-Liau index of `text`.
"""
function ColemanLiau(text::String)
total_characters::Int = characters(text)
total_words::Int = words(text)
total_sentences::Int = sentences(text)
characters_per_100_words::Float64 = characters(text) / words(text) * 100
sentences_per_100_words::Float64 = sentences(text) / words(text) * 100

characters_per_100_words::Float64 = total_characters / total_words * 100
sentences_per_100_words::Float64 = total_sentences / total_words * 100

CLI::Float64 = 0.0588 * characters_per_100_words - 0.296 * sentences_per_100_words - 15.8
return CLI
return 0.0588 * characters_per_100_words - 0.296 * sentences_per_100_words - 15.8
end
81 changes: 56 additions & 25 deletions src/counts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,47 @@
Returns the number of characters in `text`.
"""
function characters(text::String)
count::Int = length(text)
return count
function characters(text::String)::Int
return Base.length(text)
end

"""
characters_per_word(text::String)
Returns the number of characters per word or the average word length in `text`.
"""
function characters_per_word(text::String)::Float64
return characters(text) / words(text)
end

"""
sentences(text::String)
Returns the number of sentences in `text`.
"""
function sentences(text::String)
count::Int = length(split(text, ['.', '!', '?']))
return count
function sentences(text::String)::Int
return Base.length(Base.split(text, ['.', '!', '?']))
end

"""
sentences_per_paragraph(text::String)
Returns the number of sentences per paragraph or the average paragraph length in `text`.
"""
function sentences_per_paragraph(text::String)::Float64
return sentences(text) / paragraphs(text)
end

"""
syllables(text::String)
Returns the number of syllables in `text`.
"""
function syllables(text::String)
function syllables(text::String)::Int
vowels::String = "aeiou"
count::Int = 0
in_vowel_sequence::Bool = false
text::String = lowercase(text)
text::String = Base.lowercase(text)
for char in text
if char in vowels
if !in_vowel_sequence
Expand All @@ -41,34 +57,49 @@ function syllables(text::String)
return count
end

"""
syllables_per_word(text::String)
Returns the number of syllables per word or the average word length in `text`.
"""
function syllables_per_word(text::String)::Float64
return syllables(text) / words(text)
end

"""
words(text::String)
Returns the number of words in `text`.
"""
function words(text::String)
count::Int = length(split(text))
return count
function words(text::String)::Int
return Base.length(Base.split(text))
end

"""
words_per_sentence(text::String)
Returns the number of words per sentence or the sentence length in `text`.
"""
function words_per_sentence(text::String)::Float64
return words(text) / sentences(text)
end

"""
lines(text::String)
Returns the number of lines `text`.
"""
function lines(text::String)
count::Int = length(split(text, "\n"))
return count
function lines(text::String)::Int
return Base.length(Base.split(text, "\n"))
end

"""
paragraphs(text::String)
Returns the number of paragraphs in `text`.
"""
function paragraphs(text::String)
count::Int = length(split(text, "\n\n"))
return count
function paragraphs(text::String)::Int
return Base.length(Base.split(text, "\n\n"))
end

# Gunning Fog
Expand All @@ -77,12 +108,12 @@ end
Returns the number of complex words (words with 3 or more syllables and not ending in "es", "ed", or "ing") in `text`.
"""
function complex_words(text::String)
words::Vector{String} = split(text)
function complex_words(text::String)::Int
words::Vector{String} = Base.split(text)
count::Int = 0
for word in words
syllable_count::Int = syllables(word)
if syllable_count >= 3 && !any(x -> occursin(x, word), ['-', "es", "ed", "ing"])
if syllable_count >= 3 && !Base.any(x -> Base.occursin(x, word), ['-', "es", "ed", "ing"])
count += 1
end
end
Expand All @@ -95,8 +126,8 @@ end
Returns the number of words with 3 or more syllables in `text`.
"""
function polysyllabic_words(text::String)
words::Vector{String} = split(text)
function polysyllabic_words(text::String)::Int
words::Vector{String} = Base.split(text)
count::Int = 0
for word in words
syllable_count = syllables(word)
Expand All @@ -121,8 +152,8 @@ end
Returns the number of words that are not in the specified `word_list` (either "dale-chall" or "spache") in `text`.
"""
function difficult_words(text::String, word_list::String)
lower_text::String = lowercase(text)
words::Vector{String} = split(lower_text)
lower_text::String = Base.lowercase(text)
words::Vector{String} = Base.split(lower_text)
count::Int = 0

dale_chall_txt::String = Base.joinpath(Base.dirname(Base.@__FILE__), "dale-chall_word_list.txt")
Expand All @@ -144,7 +175,7 @@ function difficult_words(text::String, word_list::String)
end
end
else
error("word_list must be 'dale-chall' or 'spache'")
Base.error("word_list must be 'dale-chall' or 'spache'")
end

return count
Expand Down
10 changes: 2 additions & 8 deletions src/dale-chall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@
Returns the Dale-Chall readability score of `text`.
"""
function DaleChall(text::String)
total_words::Int = words(text)
total_difficult_words::Int = difficult_words(text, "dale-chall")
total_sentences::Int = sentences(text)
percentage_of_difficult_words::Float64 = 100 * difficult_words(text, "dale-chall") / words(text)

percentage_of_difficult_words::Float64 = 100 * total_difficult_words / total_words
words_per_sentence::Float64 = total_words / total_sentences

dale_chall_score::Float64 = 64 - 0.95 * percentage_of_difficult_words - 0.69 * words_per_sentence
return dale_chall_score
return 64 - 0.95 * percentage_of_difficult_words - 0.69 * words_per_sentence(text)
end
12 changes: 2 additions & 10 deletions src/fkgl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
Returns the Flesch-Kincaid grade level of `text`.
"""
function FleschKincaidGradeLevel(text::String)
total_words::Int = words(text)
total_sentences::Int = sentences(text)
total_syllables::Int = syllables(text)

words_per_sentence::Float64 = total_words / total_sentences
syllables_per_word::Float64 = total_syllables / total_words

grade_level::Float64 = 0.39 * words_per_sentence + 11.8 * syllables_per_word - 15.59
return grade_level
function FleschKincaidGradeLevel(text::String)::Float64
return 0.39 * words_per_sentence(text) + 11.8 * syllables_per_word(text) - 15.59
end
12 changes: 2 additions & 10 deletions src/fres.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
Returns the Flesch Reading Ease Score of `text`.
"""
function FleschReadingEase(text::String)
total_words::Int = words(text)
total_sentences::Int = sentences(text)
total_syllables::Int = syllables(text)

words_per_sentence::Float64 = total_words / total_sentences
syllables_per_word::Float64 = total_syllables / total_words

reading_ease_score::Float64 = 206.835 - 1.015 * words_per_sentence - 84.6 * syllables_per_word
return reading_ease_score
function FleschReadingEase(text::String)::Float64
return 206.835 - 1.015 * words_per_sentence(text) - 84.6 * syllables_per_word(text)
end
10 changes: 2 additions & 8 deletions src/gunning_fog.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@
Returns the Gunning Fog index of `text`.
"""
function GunningFog(text::String)
total_words::Int = words(text)
total_sentences::Int = sentences(text)
total_complex_words::Int = complex_words(text)
percentage_of_complex_words::Float64 = 100 * complex_words(text) / words(text)

words_per_sentence::Float64 = total_words / total_sentences
percentage_of_complex_words::Float64 = 100 * total_complex_words / total_words

fog_index::Float64 = 0.4 * (words_per_sentence + percentage_of_complex_words)
return fog_index
return 0.4 * (words_per_sentence(text) + percentage_of_complex_words)
end
6 changes: 1 addition & 5 deletions src/smog.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,5 @@
Returns the SMOG index of `text`.
"""
function SMOG(text::String)
total_sentences::Int = sentences(text)
total_polysyllablic_words::Int = polysyllabic_words(text)

smog_index::Float64 = 1.0430 * Base.sqrt(total_polysyllablic_words * (30 / total_sentences)) + 3.1291
return smog_index
return 1.0430 * Base.sqrt(polysyllabic_words(text) * (30 / sentences(text))) + 3.1291
end
10 changes: 2 additions & 8 deletions src/spache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@
Returns the Spache readability score of `text`.
"""
function Spache(text::String)
total_words::Int = words(text)
total_difficult_words::Int = difficult_words(text, "spache")
total_sentences::Int = sentences(text)
percentage_of_difficult_words::Float64 = 100 * difficult_words(text, "spache") / words(text)

percentage_of_difficult_words::Float64 = 100 * total_difficult_words / total_words
words_per_sentence::Float64 = total_words / total_sentences

spache_score::Float64 = 0.121 * words_per_sentence + 0.082 * percentage_of_difficult_words + 0.659
return spache_score
return 0.121 * words_per_sentence(text) + 0.082 * percentage_of_difficult_words + 0.659
end
6 changes: 2 additions & 4 deletions src/time.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
Returns the reading time of `text` in seconds.
"""
function reading_time(text::String; wpm::Number=238)
total_words::Int = words(text)
seconds::Float64 = total_words / wpm * 60
seconds::Float64 = words(text) / wpm * 60
return seconds
end

Expand All @@ -15,7 +14,6 @@ end
Returns the speaking time of `text` in seconds.
"""
function speaking_time(text::String; wpm::Number=183)
total_words::Int = words(text)
seconds::Float64 = total_words / wpm * 60
seconds::Float64 = words(text) / wpm * 60
return seconds
end

0 comments on commit bc85078

Please sign in to comment.