From cb4d01c6128fa5e35f344e46b514dda6f020d259 Mon Sep 17 00:00:00 2001 From: tonaerospace Date: Thu, 20 Mar 2025 16:05:39 +0700 Subject: [PATCH] update --- Manifest.toml | 4 +-- src/util.jl | 62 ++++++++++++++++++++++++++++++++++++++ test/.vscode/settings.json | 3 ++ test/Manifest.toml | 41 +++++++++++++++++++++++++ test/Project.toml | 2 ++ test/etc.jl | 7 +++++ test/runtest.jl | 44 --------------------------- test/runtests.jl | 23 ++++++++++++++ 8 files changed, 140 insertions(+), 46 deletions(-) create mode 100644 test/.vscode/settings.json create mode 100644 test/Manifest.toml create mode 100644 test/Project.toml create mode 100644 test/etc.jl delete mode 100644 test/runtest.jl create mode 100644 test/runtests.jl diff --git a/Manifest.toml b/Manifest.toml index a74bf74..c29e4fe 100644 --- a/Manifest.toml +++ b/Manifest.toml @@ -1,6 +1,6 @@ # This file is machine-generated - editing it directly is not advised -julia_version = "1.11.2" +julia_version = "1.11.4" manifest_format = "2.0" project_hash = "75c6a269a13b222c106479d2177b05facfa23f74" @@ -310,7 +310,7 @@ version = "0.3.27+1" [[deps.OpenLibm_jll]] deps = ["Artifacts", "Libdl"] uuid = "05823500-19ac-5b8b-9628-191a04bc5112" -version = "0.8.1+2" +version = "0.8.1+4" [[deps.OpenSpecFun_jll]] deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Pkg"] diff --git a/src/util.jl b/src/util.jl index 8021f85..5e67e11 100644 --- a/src/util.jl +++ b/src/util.jl @@ -770,6 +770,49 @@ function extract_triple_backtick_text(input::String)::Vector{String} end +""" + detect_keyword(keywords::AbstractVector{String}, text::String) -> Vector{Union{Nothing, String}} + +Detects if multiple keywords exist 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 + +# 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 + + julia> detect_keyword(["warning", "missing"], "Warning: data is complete") + 2-element Vector{Union{Nothing, String}}: + "Warning" + nothing + ``` + +# Signature +""" +function detect_keyword(keywords::T, text::String)::Union{Nothing, Dict} where {T<:AbstractVector} + kw = Dict{String, Any}() + 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 + ws = detect_keyword.(keyword, splittext) + total = sum(issomething.(ws)) + if total != 0 + kw[keyword] = total + else + kw[keyword] = nothing + end + end + return kw +end """ detect_keyword(keyword::String, text::String) -> Union{Nothing, String} @@ -1075,8 +1118,27 @@ function convertCamelSnakeKebabCase(text::T, tocase::Symbol)::String where {T<:A end +""" Check if a value is not `nothing`. +# Arguments +- `x`: The value to check +# Returns +- `Bool`: `true` if `x` is not `nothing`, `false` otherwise + +# Examples +```jldoctest +julia> issomething(1) +true +julia> issomething(nothing) +false +julia> issomething("test") +true +```` +""" +function issomething(x) +return x === nothing ? false : true +end diff --git a/test/.vscode/settings.json b/test/.vscode/settings.json new file mode 100644 index 0000000..ca4f214 --- /dev/null +++ b/test/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "julia.environmentPath": "/appfolder/app/dev/GeneralUtils/test" +} \ No newline at end of file diff --git a/test/Manifest.toml b/test/Manifest.toml new file mode 100644 index 0000000..83f035b --- /dev/null +++ b/test/Manifest.toml @@ -0,0 +1,41 @@ +# This file is machine-generated - editing it directly is not advised + +julia_version = "1.11.4" +manifest_format = "2.0" +project_hash = "71d91126b5a1fb1020e1098d9d492de2a4438fd2" + +[[deps.Base64]] +uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" +version = "1.11.0" + +[[deps.InteractiveUtils]] +deps = ["Markdown"] +uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" +version = "1.11.0" + +[[deps.Logging]] +uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" +version = "1.11.0" + +[[deps.Markdown]] +deps = ["Base64"] +uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" +version = "1.11.0" + +[[deps.Random]] +deps = ["SHA"] +uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +version = "1.11.0" + +[[deps.SHA]] +uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" +version = "0.7.0" + +[[deps.Serialization]] +uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" +version = "1.11.0" + +[[deps.Test]] +deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] +uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +version = "1.11.0" diff --git a/test/Project.toml b/test/Project.toml new file mode 100644 index 0000000..0c36332 --- /dev/null +++ b/test/Project.toml @@ -0,0 +1,2 @@ +[deps] +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/test/etc.jl b/test/etc.jl new file mode 100644 index 0000000..4227812 --- /dev/null +++ b/test/etc.jl @@ -0,0 +1,7 @@ +python -> pandas -> dataframe -> csv + + + +julia -> DataFrames -> dataframe -> csv + + dict -> dataframe -> csv \ No newline at end of file diff --git a/test/runtest.jl b/test/runtest.jl deleted file mode 100644 index 2a844ed..0000000 --- a/test/runtest.jl +++ /dev/null @@ -1,44 +0,0 @@ -using Revise -using GeneralUtils, MQTTClient, JSON3 - -mqttMsgReceiveTopic = ["/receivetopic_1", "/receivetopic_2"] -mqttMsgReceiveChannel = (ch1=Channel(8), ch2=Channel(32)) -keepaliveChannel = Channel(8) -function onMsgCallback(topic, payload) - jobj = JSON3.read(String(payload)) - incomingMqttMsg = copy(jobj) # convert json object into julia dictionary recursively - - if occursin("topic_1", topic) - put!(mqttMsgReceiveChannel[:ch1], incomingMqttMsg) - elseif occursin("topic_2", topic) - put!(mqttMsgReceiveChannel[:ch2], incomingMqttMsg) - elseif occursin("keepalive", topic) - put!(keepaliveChannel, incomingMqttMsg) - else - println("undefined condition ", @__FILE__, " ", @__LINE__) - end - end -mqttInstance = GeneralUtils.mqttClientInstance_v2( - "mqtt.yiem.cc", - mqttMsgReceiveTopic, - mqttMsgReceiveChannel, - keepaliveChannel, - onMsgCallback - ) - - -_ = GeneralUtils.checkMqttConnection!(mqttInstance) - - -println("GeneralUtils test done") - - - - - - - - - - - diff --git a/test/runtests.jl b/test/runtests.jl new file mode 100644 index 0000000..c00177a --- /dev/null +++ b/test/runtests.jl @@ -0,0 +1,23 @@ +using Test +using GeneralUtils + +@testset "detect_keyword tests" begin + @test GeneralUtils.detect_keyword(["test"], "this is a test string") == Dict("test" => 1) + @test GeneralUtils.detect_keyword(["hello", "world"], "hello world") == Dict("hello" => 1, "world" => 1) + @test GeneralUtils.detect_keyword(["missing"], "no keyword here") == Dict("missing" => nothing) + @test GeneralUtils.detect_keyword(["a", "b"], "a a b b b") == Dict("a" => 2, "b" => 3) + @test GeneralUtils.detect_keyword(String[], "empty keywords") == Dict{String, Any}() + @test GeneralUtils.detect_keyword(["keyword"], "") == Dict("keyword" => nothing) + @test GeneralUtils.detect_keyword(["case"], "CASE case Case cAsE") == Dict("case" => 4) + mixed_results = GeneralUtils.detect_keyword(["found", "notfound"], "found found found") + @test mixed_results["found"] == 3 + @test mixed_results["notfound"] === nothing + + special_chars = GeneralUtils.detect_keyword(["test!"], "test! test? test.") + @test special_chars["test!"] == 1 +end + + + + +