This commit is contained in:
2025-03-20 16:05:39 +07:00
parent e6344f1a92
commit cb4d01c612
8 changed files with 140 additions and 46 deletions

View File

@@ -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"]

View File

@@ -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

3
test/.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,3 @@
{
"julia.environmentPath": "/appfolder/app/dev/GeneralUtils/test"
}

41
test/Manifest.toml Normal file
View File

@@ -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"

2
test/Project.toml Normal file
View File

@@ -0,0 +1,2 @@
[deps]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

7
test/etc.jl Normal file
View File

@@ -0,0 +1,7 @@
python -> pandas -> dataframe -> csv
julia -> DataFrames -> dataframe -> csv
dict -> dataframe -> csv

View File

@@ -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")

23
test/runtests.jl Normal file
View File

@@ -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