This commit is contained in:
narawat lamaiin
2025-03-22 09:41:39 +07:00
parent cb4d01c612
commit 840b0e6205
2 changed files with 18 additions and 22 deletions

View File

@@ -771,35 +771,30 @@ end
"""
detect_keyword(keywords::AbstractVector{String}, text::String) -> Vector{Union{Nothing, String}}
detect_keyword(keywords::AbstractVector{String}, text::String) -> Dict{String, Integer}
Detects if multiple keywords exist in the text in different case variations (lowercase, uppercase first letter, or all uppercase).
Detects and counts occurrences of multiple keywords in the text in different case variations (lowercase, uppercase first letter, or all uppercase).
# Arguments:
- `keywords::AbstractVector{String}` Vector of keywords to search for
- `text::String` The text to search in
# Returns:
- `Vector{Union{Nothing, String}}` Returns a vector containing the matched keyword variations if found, otherwise nothing for each keyword
- `Dict{String, Integer}` Returns a dictionary mapping each keyword to its count in the text (0 if not found)
# Examples:
```jldoctest
julia> detect_keyword(["test", "error", "case"], "This is a Test case with ERRORS case")
2-element Vector{Union{Nothing, String}}:
"Test"
"ERRORS"
nothing
Dict{String, Integer}("test" => 1, "error" => 1, "case" => 2)
julia> detect_keyword(["warning", "missing"], "Warning: data is complete")
2-element Vector{Union{Nothing, String}}:
"Warning"
nothing
```
Dict{String, Integer}("warning" => 1, "missing" => 0)
# Signature
"""
function detect_keyword(keywords::T, text::String)::Union{Nothing, Dict} where {T<:AbstractVector}
kw = Dict{String, Any}()
function detect_keyword(keywords::T, text::String)::Dict{String, Integer} where {T<:AbstractVector}
kw = Dict{String, Integer}()
splittext = string.(split(text, " "))
# use for loop and detect_keyword function to get the exact variation of each keyword in the text then push to kw list
for keyword in keywords
@@ -808,11 +803,13 @@ function detect_keyword(keywords::T, text::String)::Union{Nothing, Dict} where {
if total != 0
kw[keyword] = total
else
kw[keyword] = nothing
kw[keyword] = 0
end
end
return kw
end
"""
detect_keyword(keyword::String, text::String) -> Union{Nothing, String}