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