diff --git a/Project.toml b/Project.toml index b5d3dc5..c089feb 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Readability" uuid = "4455ec5f-b558-4ef1-b6d8-c3694046c382" authors = ["Ceco E. Maples "] -version = "0.2.3" +version = "0.3.0" [compat] julia = "1" diff --git a/app/backend/Project.toml b/app/backend/Project.toml index 49e3800..b1aa544 100644 --- a/app/backend/Project.toml +++ b/app/backend/Project.toml @@ -6,5 +6,5 @@ Readability = "4455ec5f-b558-4ef1-b6d8-c3694046c382" [compat] Oxygen = "1" HTTP = "1" -Readability = "0.2" +Readability = "0.3" julia = "1" \ No newline at end of file diff --git a/docs/Project.toml b/docs/Project.toml index f41098f..1148de7 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -6,5 +6,5 @@ Readability = "4455ec5f-b558-4ef1-b6d8-c3694046c382" [compat] Documenter = "1" DocumenterCitations = "1.3" -Readability = "0.2" +Readability = "0.3" julia = "1" \ No newline at end of file diff --git a/src/Readability.jl b/src/Readability.jl index 1fd3988..40a505e 100644 --- a/src/Readability.jl +++ b/src/Readability.jl @@ -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 diff --git a/src/ari.jl b/src/ari.jl index bf21848..0cd554f 100644 --- a/src/ari.jl +++ b/src/ari.jl @@ -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 diff --git a/src/coleman-liau.jl b/src/coleman-liau.jl index c7b7eae..e3905fa 100644 --- a/src/coleman-liau.jl +++ b/src/coleman-liau.jl @@ -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 diff --git a/src/counts.jl b/src/counts.jl index c36e179..72a7f88 100644 --- a/src/counts.jl +++ b/src/counts.jl @@ -3,9 +3,17 @@ 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 """ @@ -13,9 +21,17 @@ end 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 """ @@ -23,11 +39,11 @@ end 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 @@ -41,14 +57,31 @@ 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 """ @@ -56,9 +89,8 @@ end 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 """ @@ -66,9 +98,8 @@ end 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 @@ -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 @@ -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) @@ -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") @@ -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 diff --git a/src/dale-chall.jl b/src/dale-chall.jl index 0eca9c6..899fee7 100644 --- a/src/dale-chall.jl +++ b/src/dale-chall.jl @@ -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 diff --git a/src/fkgl.jl b/src/fkgl.jl index 935002a..973607d 100644 --- a/src/fkgl.jl +++ b/src/fkgl.jl @@ -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 diff --git a/src/fres.jl b/src/fres.jl index e8a48c1..e82484e 100644 --- a/src/fres.jl +++ b/src/fres.jl @@ -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 diff --git a/src/gunning_fog.jl b/src/gunning_fog.jl index f27388b..78030a1 100644 --- a/src/gunning_fog.jl +++ b/src/gunning_fog.jl @@ -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 diff --git a/src/smog.jl b/src/smog.jl index d270c07..b8a74d3 100644 --- a/src/smog.jl +++ b/src/smog.jl @@ -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 diff --git a/src/spache.jl b/src/spache.jl index 4b08a92..f5f38d4 100644 --- a/src/spache.jl +++ b/src/spache.jl @@ -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 diff --git a/src/time.jl b/src/time.jl index 2d0b8c2..06ead57 100644 --- a/src/time.jl +++ b/src/time.jl @@ -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 @@ -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