Skip to content

Commit bc85078

Browse files
committed
added functions
1 parent 25be032 commit bc85078

14 files changed

+88
-109
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Readability"
22
uuid = "4455ec5f-b558-4ef1-b6d8-c3694046c382"
33
authors = ["Ceco E. Maples <[email protected]>"]
4-
version = "0.2.3"
4+
version = "0.3.0"
55

66
[compat]
77
julia = "1"

app/backend/Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ Readability = "4455ec5f-b558-4ef1-b6d8-c3694046c382"
66
[compat]
77
Oxygen = "1"
88
HTTP = "1"
9-
Readability = "0.2"
9+
Readability = "0.3"
1010
julia = "1"

docs/Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ Readability = "4455ec5f-b558-4ef1-b6d8-c3694046c382"
66
[compat]
77
Documenter = "1"
88
DocumenterCitations = "1.3"
9-
Readability = "0.2"
9+
Readability = "0.3"
1010
julia = "1"

src/Readability.jl

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
module Readability
22

33
export reading_time, speaking_time
4-
export characters, sentences, syllables,
5-
words, complex_words, polysyllabic_words, difficult_words,
6-
lines, paragraphs
4+
export characters, characters_per_word
5+
export syllables, syllables_per_word
6+
export words, words_per_sentence
7+
export sentences, sentences_per_paragraph
8+
export lines, paragraphs
79
export ARI
810
export ColemanLiau
9-
export DaleChall
10-
export FleschKincaidGradeLevel, FleschReadingEaseScore
11-
export GunningFog
12-
export SMOG
13-
export Spache
11+
export DaleChall, Spache, difficult_words
12+
export FleschKincaidGradeLevel, FleschReadingEase
13+
export GunningFog, complex_words
14+
export SMOG, polysyllabic_words
15+
16+
include("counts.jl")
17+
include("time.jl")
1418

1519
include("ari.jl") # Automatic Readability Index
1620
include("coleman-liau.jl")
17-
include("counts.jl")
1821
include("dale-chall.jl")
1922
include("fkgl.jl") # Flesch-Kincaid Grade Level
2023
include("fres.jl") # Flesch Reading Ease Score
2124
include("gunning_fog.jl")
2225
include("smog.jl") # Simple Measure of Gobbledygook
2326
include("spache.jl")
24-
include("time.jl")
2527

2628
end

src/ari.jl

+1-10
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,5 @@
44
Returns the Automated Readability Index (ARI) of `text`.
55
"""
66
function ARI(text::String)
7-
total_characters::Int = characters(text)
8-
total_words::Int = words(text)
9-
total_sentences::Int = sentences(text)
10-
11-
characters_per_word::Float64 = total_characters / total_words
12-
words_per_sentence::Float64 = total_words / total_sentences
13-
14-
grade::Float64 = 4.71 * characters_per_word + 0.5 * words_per_sentence - 21.43
15-
grade_rounded::Int = Base.ceil(Int, grade)
16-
return grade_rounded
7+
return Base.ceil(Int, 4.71 * characters_per_word(text) + 0.5 * words_per_sentence(text) - 21.43)
178
end

src/coleman-liau.jl

+3-8
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@
44
Returns the Coleman-Liau index of `text`.
55
"""
66
function ColemanLiau(text::String)
7-
total_characters::Int = characters(text)
8-
total_words::Int = words(text)
9-
total_sentences::Int = sentences(text)
7+
characters_per_100_words::Float64 = characters(text) / words(text) * 100
8+
sentences_per_100_words::Float64 = sentences(text) / words(text) * 100
109

11-
characters_per_100_words::Float64 = total_characters / total_words * 100
12-
sentences_per_100_words::Float64 = total_sentences / total_words * 100
13-
14-
CLI::Float64 = 0.0588 * characters_per_100_words - 0.296 * sentences_per_100_words - 15.8
15-
return CLI
10+
return 0.0588 * characters_per_100_words - 0.296 * sentences_per_100_words - 15.8
1611
end

src/counts.jl

+56-25
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,47 @@
33
44
Returns the number of characters in `text`.
55
"""
6-
function characters(text::String)
7-
count::Int = length(text)
8-
return count
6+
function characters(text::String)::Int
7+
return Base.length(text)
8+
end
9+
10+
"""
11+
characters_per_word(text::String)
12+
13+
Returns the number of characters per word or the average word length in `text`.
14+
"""
15+
function characters_per_word(text::String)::Float64
16+
return characters(text) / words(text)
917
end
1018

1119
"""
1220
sentences(text::String)
1321
1422
Returns the number of sentences in `text`.
1523
"""
16-
function sentences(text::String)
17-
count::Int = length(split(text, ['.', '!', '?']))
18-
return count
24+
function sentences(text::String)::Int
25+
return Base.length(Base.split(text, ['.', '!', '?']))
26+
end
27+
28+
"""
29+
sentences_per_paragraph(text::String)
30+
31+
Returns the number of sentences per paragraph or the average paragraph length in `text`.
32+
"""
33+
function sentences_per_paragraph(text::String)::Float64
34+
return sentences(text) / paragraphs(text)
1935
end
2036

2137
"""
2238
syllables(text::String)
2339
2440
Returns the number of syllables in `text`.
2541
"""
26-
function syllables(text::String)
42+
function syllables(text::String)::Int
2743
vowels::String = "aeiou"
2844
count::Int = 0
2945
in_vowel_sequence::Bool = false
30-
text::String = lowercase(text)
46+
text::String = Base.lowercase(text)
3147
for char in text
3248
if char in vowels
3349
if !in_vowel_sequence
@@ -41,34 +57,49 @@ function syllables(text::String)
4157
return count
4258
end
4359

60+
"""
61+
syllables_per_word(text::String)
62+
63+
Returns the number of syllables per word or the average word length in `text`.
64+
"""
65+
function syllables_per_word(text::String)::Float64
66+
return syllables(text) / words(text)
67+
end
68+
4469
"""
4570
words(text::String)
4671
4772
Returns the number of words in `text`.
4873
"""
49-
function words(text::String)
50-
count::Int = length(split(text))
51-
return count
74+
function words(text::String)::Int
75+
return Base.length(Base.split(text))
76+
end
77+
78+
"""
79+
words_per_sentence(text::String)
80+
81+
Returns the number of words per sentence or the sentence length in `text`.
82+
"""
83+
function words_per_sentence(text::String)::Float64
84+
return words(text) / sentences(text)
5285
end
5386

5487
"""
5588
lines(text::String)
5689
5790
Returns the number of lines `text`.
5891
"""
59-
function lines(text::String)
60-
count::Int = length(split(text, "\n"))
61-
return count
92+
function lines(text::String)::Int
93+
return Base.length(Base.split(text, "\n"))
6294
end
6395

6496
"""
6597
paragraphs(text::String)
6698
6799
Returns the number of paragraphs in `text`.
68100
"""
69-
function paragraphs(text::String)
70-
count::Int = length(split(text, "\n\n"))
71-
return count
101+
function paragraphs(text::String)::Int
102+
return Base.length(Base.split(text, "\n\n"))
72103
end
73104

74105
# Gunning Fog
@@ -77,12 +108,12 @@ end
77108
78109
Returns the number of complex words (words with 3 or more syllables and not ending in "es", "ed", or "ing") in `text`.
79110
"""
80-
function complex_words(text::String)
81-
words::Vector{String} = split(text)
111+
function complex_words(text::String)::Int
112+
words::Vector{String} = Base.split(text)
82113
count::Int = 0
83114
for word in words
84115
syllable_count::Int = syllables(word)
85-
if syllable_count >= 3 && !any(x -> occursin(x, word), ['-', "es", "ed", "ing"])
116+
if syllable_count >= 3 && !Base.any(x -> Base.occursin(x, word), ['-', "es", "ed", "ing"])
86117
count += 1
87118
end
88119
end
@@ -95,8 +126,8 @@ end
95126
96127
Returns the number of words with 3 or more syllables in `text`.
97128
"""
98-
function polysyllabic_words(text::String)
99-
words::Vector{String} = split(text)
129+
function polysyllabic_words(text::String)::Int
130+
words::Vector{String} = Base.split(text)
100131
count::Int = 0
101132
for word in words
102133
syllable_count = syllables(word)
@@ -121,8 +152,8 @@ end
121152
Returns the number of words that are not in the specified `word_list` (either "dale-chall" or "spache") in `text`.
122153
"""
123154
function difficult_words(text::String, word_list::String)
124-
lower_text::String = lowercase(text)
125-
words::Vector{String} = split(lower_text)
155+
lower_text::String = Base.lowercase(text)
156+
words::Vector{String} = Base.split(lower_text)
126157
count::Int = 0
127158

128159
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)
144175
end
145176
end
146177
else
147-
error("word_list must be 'dale-chall' or 'spache'")
178+
Base.error("word_list must be 'dale-chall' or 'spache'")
148179
end
149180

150181
return count

src/dale-chall.jl

+2-8
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@
44
Returns the Dale-Chall readability score of `text`.
55
"""
66
function DaleChall(text::String)
7-
total_words::Int = words(text)
8-
total_difficult_words::Int = difficult_words(text, "dale-chall")
9-
total_sentences::Int = sentences(text)
7+
percentage_of_difficult_words::Float64 = 100 * difficult_words(text, "dale-chall") / words(text)
108

11-
percentage_of_difficult_words::Float64 = 100 * total_difficult_words / total_words
12-
words_per_sentence::Float64 = total_words / total_sentences
13-
14-
dale_chall_score::Float64 = 64 - 0.95 * percentage_of_difficult_words - 0.69 * words_per_sentence
15-
return dale_chall_score
9+
return 64 - 0.95 * percentage_of_difficult_words - 0.69 * words_per_sentence(text)
1610
end

src/fkgl.jl

+2-10
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@
33
44
Returns the Flesch-Kincaid grade level of `text`.
55
"""
6-
function FleschKincaidGradeLevel(text::String)
7-
total_words::Int = words(text)
8-
total_sentences::Int = sentences(text)
9-
total_syllables::Int = syllables(text)
10-
11-
words_per_sentence::Float64 = total_words / total_sentences
12-
syllables_per_word::Float64 = total_syllables / total_words
13-
14-
grade_level::Float64 = 0.39 * words_per_sentence + 11.8 * syllables_per_word - 15.59
15-
return grade_level
6+
function FleschKincaidGradeLevel(text::String)::Float64
7+
return 0.39 * words_per_sentence(text) + 11.8 * syllables_per_word(text) - 15.59
168
end

src/fres.jl

+2-10
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@
33
44
Returns the Flesch Reading Ease Score of `text`.
55
"""
6-
function FleschReadingEase(text::String)
7-
total_words::Int = words(text)
8-
total_sentences::Int = sentences(text)
9-
total_syllables::Int = syllables(text)
10-
11-
words_per_sentence::Float64 = total_words / total_sentences
12-
syllables_per_word::Float64 = total_syllables / total_words
13-
14-
reading_ease_score::Float64 = 206.835 - 1.015 * words_per_sentence - 84.6 * syllables_per_word
15-
return reading_ease_score
6+
function FleschReadingEase(text::String)::Float64
7+
return 206.835 - 1.015 * words_per_sentence(text) - 84.6 * syllables_per_word(text)
168
end

src/gunning_fog.jl

+2-8
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@
44
Returns the Gunning Fog index of `text`.
55
"""
66
function GunningFog(text::String)
7-
total_words::Int = words(text)
8-
total_sentences::Int = sentences(text)
9-
total_complex_words::Int = complex_words(text)
7+
percentage_of_complex_words::Float64 = 100 * complex_words(text) / words(text)
108

11-
words_per_sentence::Float64 = total_words / total_sentences
12-
percentage_of_complex_words::Float64 = 100 * total_complex_words / total_words
13-
14-
fog_index::Float64 = 0.4 * (words_per_sentence + percentage_of_complex_words)
15-
return fog_index
9+
return 0.4 * (words_per_sentence(text) + percentage_of_complex_words)
1610
end

src/smog.jl

+1-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,5 @@
44
Returns the SMOG index of `text`.
55
"""
66
function SMOG(text::String)
7-
total_sentences::Int = sentences(text)
8-
total_polysyllablic_words::Int = polysyllabic_words(text)
9-
10-
smog_index::Float64 = 1.0430 * Base.sqrt(total_polysyllablic_words * (30 / total_sentences)) + 3.1291
11-
return smog_index
7+
return 1.0430 * Base.sqrt(polysyllabic_words(text) * (30 / sentences(text))) + 3.1291
128
end

src/spache.jl

+2-8
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@
44
Returns the Spache readability score of `text`.
55
"""
66
function Spache(text::String)
7-
total_words::Int = words(text)
8-
total_difficult_words::Int = difficult_words(text, "spache")
9-
total_sentences::Int = sentences(text)
7+
percentage_of_difficult_words::Float64 = 100 * difficult_words(text, "spache") / words(text)
108

11-
percentage_of_difficult_words::Float64 = 100 * total_difficult_words / total_words
12-
words_per_sentence::Float64 = total_words / total_sentences
13-
14-
spache_score::Float64 = 0.121 * words_per_sentence + 0.082 * percentage_of_difficult_words + 0.659
15-
return spache_score
9+
return 0.121 * words_per_sentence(text) + 0.082 * percentage_of_difficult_words + 0.659
1610
end

src/time.jl

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
Returns the reading time of `text` in seconds.
55
"""
66
function reading_time(text::String; wpm::Number=238)
7-
total_words::Int = words(text)
8-
seconds::Float64 = total_words / wpm * 60
7+
seconds::Float64 = words(text) / wpm * 60
98
return seconds
109
end
1110

@@ -15,7 +14,6 @@ end
1514
Returns the speaking time of `text` in seconds.
1615
"""
1716
function speaking_time(text::String; wpm::Number=183)
18-
total_words::Int = words(text)
19-
seconds::Float64 = total_words / wpm * 60
17+
seconds::Float64 = words(text) / wpm * 60
2018
return seconds
2119
end

0 commit comments

Comments
 (0)