This commit is contained in:
2025-03-10 09:13:45 +07:00
parent fa33531d5f
commit 25385c0798

View File

@@ -278,14 +278,15 @@ OrderedCollections.OrderedDict{Any, Any}(:thought => "what to do",
# Signature # Signature
""" """
function textToDict(text::String, keywords::Vector{String}; function textToDict(text::String, detectKeywords::Vector{String};
rightmarker::Union{String, Nothing}=nothing, symbolkey::Bool=false, lowercasekey::Bool=false dictKey::Union{Vector{String}, Nothing}=nothing,
symbolkey::Bool=false, lowercasekey::Bool=false
)::OrderedDict )::OrderedDict
# make sure this function detect variation of a work e.g. agent, Agent, AGENT # make sure this function detect variation of a work e.g. agent, Agent, AGENT
kw = [] kw = []
# use for loop and detect_keyword function to get the exact variation of each keyword in the text then push to kw list # 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 for keyword in detectKeywords
detected = detect_keyword(keyword, text) detected = detect_keyword(keyword, text)
if detected !== nothing if detected !== nothing
push!(kw, detected) push!(kw, detected)
@@ -302,17 +303,18 @@ function textToDict(text::String, keywords::Vector{String};
end end
remainingtext = text remainingtext = text
dictKey_ = reverse(dictKey)
for keyword in reverse(kw) # process text from back to front
mkeyword = rightmarker !== nothing ? keyword * rightmarker : keyword for (i,keyword) in enumerate(reverse(kw))
# Find the position of the keyword in the text # Find the position of the keyword in the text
keywordidx = findlast(mkeyword, remainingtext) keywordidx = findlast(keyword, remainingtext)
dKey = dictKey_[i]
if keywordidx !== nothing if keywordidx !== nothing
substr = remainingtext[keywordidx[end]+1:end] substr = remainingtext[keywordidx[end]+1:end]
str = string(strip(substr)) # Removes both leading and trailing whitespace. str = string(strip(substr)) # Removes both leading and trailing whitespace.
_key = lowercasekey == true ? lowercase(keyword) : keyword _key = lowercasekey == true ? lowercase(dKey) : dKey
key = symbolkey == true ? Symbol(_key) : _key key = symbolkey == true ? Symbol(_key) : _key
od1[key] = str od1[key] = str
remainingtext = remainingtext[1:keywordidx[1]-1] remainingtext = remainingtext[1:keywordidx[1]-1]
@@ -321,12 +323,11 @@ function textToDict(text::String, keywords::Vector{String};
end end
end end
kw = lowercasekey == true ? lowercase.(kw) : kw
# correct the order # correct the order
for keyword in kw ks = reverse([i for i in keys(od1)])
key = symbolkey == true ? Symbol(keyword) : keyword for k in ks
od2[key] = od1[key] k = symbolkey == true ? Symbol(k) : k
od2[k] = od1[k]
end end
return od2 return od2