Skip to content

Commit

Permalink
improve doc
Browse files Browse the repository at this point in the history
  • Loading branch information
guo-yong-zhi committed May 10, 2024
1 parent 068ffc3 commit c213742
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 28 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ using WordCloud
textfile = pkgdir(WordCloud)*"/res/alice.txt"
maskfile = pkgdir(WordCloud)*"/res/alice_mask.png"
wc = wordcloud(
    open(textfile),
    stopwords_extra = ["said"],
    maxnum = 500,
    mask = maskfile,
    maskcolor = "#faeef8",
    outline = 4,
    linecolor = "purple",
    colors = :Set1_5,
    angles = (0, 90),
    fonts = "Tahoma",
    density = 0.55,
open(textfile),
stopwords_extra = ["said"],
maxnum = 500,
mask = maskfile,
maskcolor = "#faeef8",
outline = 4,
linecolor = "purple",
colors = :Set1_5,
angles = (0, 90),
fonts = "Tahoma",
density = 0.55,
spacing = 3,) |> generate!
paint(wc, "alice.png", ratio=0.5)
```
Expand Down
24 changes: 13 additions & 11 deletions src/textprocessing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,32 +67,32 @@ LEMMATIZERS = Dict(
)

"""
settokenizer!(lang::AbstractString, str_to_list_func)
settokenizer!(lang::AbstractString, str_to_list_func)
customize tokenizer for language `lang`
Customize tokenizer for language `lang`
"""
function settokenizer!(lang::AbstractString, str_to_list_func)
TOKENIZERS[StopWords.normcode(String(lang))] = str_to_list_func
end
"""
setstopwords!(lang::AbstractString, str_set)
setstopwords!(lang::AbstractString, str_set)
customize stopwords for language `lang`
Customize stopwords for language `lang`
"""
function setstopwords!(lang::AbstractString, str_set)
STOPWORDS[StopWords.normcode(String(lang))] = str_set
end
"""
setlemmatizer!(lang::AbstractString, str_to_str_func)
setlemmatizer!(lang::AbstractString, str_to_str_func)
customize lemmatizer for language `lang`
Customize lemmatizer for language `lang`
"""
function setlemmatizer!(lang::AbstractString, str_to_str_func)
LEMMATIZERS[StopWords.normcode(String(lang))] = str_to_str_func
end

@doc raw"""
countwords(text_or_counter; counter=Dict{String,Int}(), language=:auto, regexp=r"(?:\S[\s\S]*)?[^0-9_\W](?:[\s\S]*\S)?")
countwords(text_or_counter; counter=Dict{String,Int}(), language=:auto, regexp=r"(?:\S[\s\S]*)?[^0-9_\W](?:[\s\S]*\S)?")
Count words in text. And save results into `counter`.
`text_or_counter` can be a String, a Vector of Strings, an opend file (IO) or a Dict.
Expand Down Expand Up @@ -173,13 +173,15 @@ function _rescaleweights(dict, func=identity, p=0)
end

"""
rescaleweights(func=identity, p=0)
rescaleweights(func=identity, p=0)
This function takes word length into account. Therefore, the rescaled weights can be used as font size coefficients.
The function func(w::Real)->Real is used to remap the weight, expressed as weight = func(weight); `p` represents the exponent of the power mean.
The function `func(w::Real)->Real` is used to remap the weight, expressed as `weight = func(weight)`; `p` represents the exponent of the power mean.
We set `weight = powermean(1*fontsize, wordlength*fontsize) = ((fontsize^p + (wordlength*fontsize)^p)/2) ^ (1/p)`.
That is, `weight = fontsize * powermean(1, wordlength)`.
Overall, this gives us `fontsize = func(weight) / powermean(1, wordlength)`.
When p is -Inf, the power mean is the minimum value, resulting in fontsize=weight. When p is Inf, the power mean is the maximum value, resulting in fontsize=weight/wordlength.
When p is -Inf, the power mean is the minimum value, resulting in `fontsize=weight`.
When p is Inf, the power mean is the maximum value, resulting in `fontsize=weight/wordlength`.
When p is -1, the power mean is the harmonic mean. When p is 0, the power mean is the geometric mean, preserving the word area.
When p is 1, the power mean is the arithmetic mean. When p is 2, the power mean is the root mean square, preserving the diagonal length.
"""
Expand Down Expand Up @@ -207,7 +209,7 @@ Process the text, filter the words, and adjust the weights. Return a vector of w
## Positional Arguments
* text_or_counter: a string, a vector of words, an opened file (IO), a Dict{<:String, <:Real}, a Vector{Pair}, a Vector{Tuple}, or two Vectors.
## Optional Keyword Arguments
* language: language of the text, default is `:auto``.
* language: language of the text, default is `:auto`.
* stopwords: a set of words, default is `:auto` which means decided by language.
* stopwords_extra: an additional set of stopwords. By setting this while keeping the `stopwords` argument as `:auto`, the built-in stopword list will be preserved.
* minlength, maxlength: minimum and maximum length of a word to be included
Expand Down
7 changes: 3 additions & 4 deletions src/wc-helper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function volume_factor(img, args...)
end
"""
Load an image as a mask, recolor it, or resize it, among other options.
## examples
## Examples
* loadmask(open("res/heart.jpg"), 256, 256) # resize to 256*256
* loadmask("res/heart.jpg", ratio=0.3) # scaled by 0.3
* loadmask("res/heart.jpg", color="red", ratio=2) # set forecolor
Expand Down Expand Up @@ -120,7 +120,7 @@ function paintsvg(wc::WC, file, args...; kargs...)
end

"""
# examples
# Examples
* paint(wc::WC)
* paint(wc::WC, background=false) # without background
* paint(wc::WC, background=outline(wc.mask)) # use a different background
Expand Down Expand Up @@ -182,8 +182,7 @@ end
runexample(example=:random) = @time evalfile(pkgdir(WordCloud)*"/examples/$(example).jl")
showexample(example=:random) = read(pkgdir(WordCloud)*"/examples/$(example).jl", String)|>print
examples = [e[1:prevind(e, end, 3)] for e in basename.(readdir(pkgdir(WordCloud)*"/examples")) if endswith(e, ".jl")]
@doc "Available values: [" * join(":".*examples, ", ") * "]" runexample
@doc "Available values: [" * join(":".*examples, ", ") * "]" showexample
@doc "Available values: [" * join(":".*examples, ", ") * "]" runexample showexample
function runexamples(examples=examples)
println(length(examples), " examples: ", examples)
for (i,e) in enumerate(examples)
Expand Down
11 changes: 9 additions & 2 deletions src/wc-method.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ function layout!(wc::WC; style=:auto, rt=:auto, centralword=:auto, reorder=:auto
wc
end

"rescale!(wc::WC, ratio::Real)\nResize all words proportionally. Use a ratio < 1 to shrink the size, and a ratio > 1 to expand the size."
"""
rescale!(wc::WC, ratio::Real)
Resize all words proportionally. Use a ratio < 1 to shrink the size, and a ratio > 1 to expand the size.
"""
function rescale!(wc::WC, ratio::Real)
qts = wc.qtrees
centers = getcenter.(qts)
Expand Down Expand Up @@ -222,7 +226,9 @@ The styles supported are `:average`, `:main`, `:clipping`, `:blending`, and :res
* recolor!(wc, style=:blending, alpha=0.3) # The `alpha` parameter is optional
* recolor!(wc, style=:reset)
The effects of `:average`, `:main`, and `:clipping` are determined solely by the background. However, the effect of `:blending` is also influenced by the previous color of the word. Therefore, `:blending` can also be used in combination with other styles.
The effects of `:average`, `:main`, and `:clipping` are determined solely by the background.
However, the effect of `:blending` is also influenced by the previous color of the word.
Therefore, `:blending` can also be used in combination with other styles.
The results of `clipping` and `blending` cannot be exported as SVG files; PNG should be used instead.
"""
function recolor!(wc, args...; style=:average, kargs...)
Expand All @@ -241,6 +247,7 @@ function recolor!(wc, args...; style=:average, kargs...)
end
nothing
end

"""
# Positional Arguments
* wc: the word cloud object generated by the [`wordcloud`](@ref) function, which needs to be fitted.
Expand Down

0 comments on commit c213742

Please sign in to comment.