From 25385c0798bb86248548bce41a370cf188861586 Mon Sep 17 00:00:00 2001 From: tonaerospace Date: Mon, 10 Mar 2025 09:13:45 +0700 Subject: [PATCH] update --- src/util.jl | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/util.jl b/src/util.jl index 4359d4c..4a38244 100644 --- a/src/util.jl +++ b/src/util.jl @@ -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