13 Commits

Author SHA1 Message Date
ton
6fb1c6dbff Merge pull request 'WIP v0.2.1-dev' (#2) from v0.2.1-dev into main
Reviewed-on: #2
2025-01-25 07:39:23 +00:00
narawat lamaiin
00d2173f26 update 2025-01-25 14:21:45 +07:00
narawat lamaiin
43ead8b8d5 update 2025-01-25 13:31:15 +07:00
narawat lamaiin
2700e594e4 update 2025-01-20 18:20:30 +07:00
narawat lamaiin
b51059d73f update 2025-01-16 11:06:43 +07:00
narawat lamaiin
f3dc298ba7 update version number 2025-01-15 03:00:31 +07:00
ton
b942c2470a Merge pull request 'v0.1.1-dev' (#1) from v0.1.1-dev into main
Reviewed-on: #1
2025-01-14 01:05:38 +00:00
narawat lamaiin
496a6520c8 merge v0.1.1 2025-01-14 07:57:15 +07:00
narawat lamaiin
b0d80930c9 update 2025-01-14 07:52:22 +07:00
narawat lamaiin
95b3997f52 update 2025-01-11 16:57:32 +07:00
narawat lamaiin
6d7094028f update 2025-01-06 13:12:11 +07:00
narawat lamaiin
d8e1cbb94a correct countGivenWords() docstring 2025-01-06 06:19:28 +07:00
narawat lamaiin
44a27a0ba4 change version 2025-01-06 06:11:24 +07:00
3 changed files with 80 additions and 22 deletions

View File

@@ -1,7 +1,7 @@
name = "GeneralUtils" name = "GeneralUtils"
uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe" uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe"
authors = ["tonaerospace <tonaerospace.etc@gmail.com>"] authors = ["tonaerospace <tonaerospace.etc@gmail.com>"]
version = "0.1.0" version = "0.2.1"
[deps] [deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"

View File

@@ -117,7 +117,7 @@ end
julia> using Revise julia> using Revise
julia> using GeneralUtils, Dates, JSON3, UUIDs julia> using GeneralUtils, Dates, JSON3, UUIDs
julia> mqttMsgReceiveTopic = ["/receivetopic_1", "/receivetopic_2"] julia> mqttMsgReceiveTopic = ["/receivetopic_1", "/receivetopic_2"]
julia> mqttMsgReceiveChannel = (ch1=Channel(8), ch2=Channel(32)) julia> mqttMsgReceiveChannel = (ch1=Channel(8), ch2=Channel(32)) # single channel Ex. (ch1=Channel(8),)
julia> keepaliveChannel = Channel(8) julia> keepaliveChannel = Channel(8)
julia> function onMsgCallback(topic, payload) julia> function onMsgCallback(topic, payload)
jobj = JSON3.read(String(payload)) jobj = JSON3.read(String(payload))
@@ -496,7 +496,7 @@ function sendMqttMsg(outgoingMsg::Dict{Symbol, T})::NamedTuple where {T<:Any}
) )
response = sendMqttMsg(mqttInstance, outgoingMsg) response = sendMqttMsg(mqttInstance, outgoingMsg)
disconnect(mqttInstance.client) try disconnect(mqttInstance.client) catch end
return response return response
end end
@@ -576,7 +576,7 @@ function sendReceiveMqttMsg(outgoingMsg::Dict{Symbol, T};
) )
response = sendReceiveMqttMsg(mqttInstance, :ch1, outgoingMsg; timeout=timeout, maxattempt=maxattempt) response = sendReceiveMqttMsg(mqttInstance, :ch1, outgoingMsg; timeout=timeout, maxattempt=maxattempt)
disconnect(mqttInstance.client) try disconnect(mqttInstance.client) catch end
return response return response
end end

View File

@@ -3,8 +3,9 @@ module util
export timedifference, showstracktrace, findHighestIndexKey, uuid4snakecase, replaceDictKeys, export timedifference, showstracktrace, findHighestIndexKey, uuid4snakecase, replaceDictKeys,
findMatchingDictKey, textToDict, randstring, randstrings, timeout, findMatchingDictKey, textToDict, randstring, randstrings, timeout,
dataframeToCSV, dfToVectorDict, disintegrate_vectorDict, getDataFrameValue, dfRowtoString, dataframeToCSV, dfToVectorDict, disintegrate_vectorDict, getDataFrameValue, dfRowtoString,
dfToString, dataframe_to_json_list, dict_to_string, extract_triple_backtick_text, dfToString, dataframe_to_json_list, dictToString, dictToString_noKey,
countGivenWords, remove_french_accents dictToString_numbering, extract_triple_backtick_text,
countGivenWords, remove_french_accents, detect_keyword, extractTextBetweenCharacter
using JSON3, DataStructures, Distributions, Random, Dates, UUIDs, MQTTClient, DataFrames using JSON3, DataStructures, Distributions, Random, Dates, UUIDs, MQTTClient, DataFrames
@@ -284,7 +285,10 @@ function textToDict(text::String, keywords::Vector{String};
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 keywords
push!(kw, detect_keyword(keyword, text)) detected = detect_keyword(keyword, text)
if detected !== nothing
push!(kw, detected)
end
end end
od1, od2 = od1, od2 =
@@ -476,7 +480,7 @@ end
function dfToVectorDict(df::DataFrame) function dfToVectorDict(df::DataFrame)
vec = [] vec = []
for row in eachrow(df) for row in eachrow(df)
d = Dict{String, Any}() d = DataStructures.OrderedDict{String, Any}()
for col in names(df) for col in names(df)
d[col] = row[col] d[col] = row[col]
end end
@@ -706,7 +710,15 @@ end
# Signature # Signature
""" """
function dict_to_string(od::T) where {T<:AbstractDict} function dictToString(od::T) where {T<:AbstractDict}
items = []
for (i, (key, value)) in enumerate(od)
push!(items, "$key: $value")
end
return join(items, ", ")
end
function dictToString_numbering(od::T) where {T<:AbstractDict}
items = [] items = []
for (i, (key, value)) in enumerate(od) for (i, (key, value)) in enumerate(od)
push!(items, "$i) $key: $value") push!(items, "$i) $key: $value")
@@ -714,6 +726,14 @@ function dict_to_string(od::T) where {T<:AbstractDict}
return join(items, ", ") return join(items, ", ")
end end
function dictToString_noKey(od::T) where {T<:AbstractDict}
items = []
for (i, (key, value)) in enumerate(od)
push!(items, "$value")
end
return join(items, ", ")
end
""" """
extract_triple_backtick_text(text::String) -> Vector{String} extract_triple_backtick_text(text::String) -> Vector{String}
@@ -727,7 +747,7 @@ Extracts text enclosed within triple backticks (```) from the given string.
- `Vector{String}`: A vector of strings, each representing a block of text enclosed within triple backticks found in the input string. - `Vector{String}`: A vector of strings, each representing a block of text enclosed within triple backticks found in the input string.
# Examples: # Examples:
```julia ```jldoctest
julia> extract_triple_backtick_text("Here is some text ```with a code block``` and more text.") julia> extract_triple_backtick_text("Here is some text ```with a code block``` and more text.")
1-element Vector{String}: 1-element Vector{String}:
"with a code block" "with a code block"
@@ -759,7 +779,7 @@ Detects if a keyword exists in the text in different case variations (lowercase,
- `Union{Nothing, String}`: Returns the matched keyword variation if found, otherwise returns nothing - `Union{Nothing, String}`: Returns the matched keyword variation if found, otherwise returns nothing
# Examples: # Examples:
```julia ```jldoctest
julia> detect_keyword("test", "This is a Test case") julia> detect_keyword("test", "This is a Test case")
"Test" "Test"
@@ -798,19 +818,19 @@ Count the occurrences of each word in the given list within the provided text.
- `words::Vector{String}`: A vector of words whose occurrences need to be counted. - `words::Vector{String}`: A vector of words whose occurrences need to be counted.
# Returns # Returns
- `Dict{String, Int}`: A dictionary where keys are the words from the `words` list and values are their respective counts in the `text`. - `Vector{Int64}`: Their respective counts in the `text`.
# Examples # Examples
```julia ```jldoctest
julia> countGivenWords("hello world hello", ["hello", "world"]) julia> GeneralUtils.countGivenWords("hello world hello", ["hello", "world"])
Dict{String,Int64} with 2 entries: 2-element Vector{Int64}:
"hello" => 2 2
"world" => 1 1
julia> countGivenWords("foo bar baz foo", ["foo", "qux"]) julia> GeneralUtils.countGivenWords("foo bar baz foo", ["foo", "qux"])
Dict{String,Int64} with 2 entries: 2-element Vector{Int64}:
"foo" => 2 2
"qux" => 0 0
``` ```
# Signature # Signature
@@ -843,7 +863,7 @@ Remove French accents from the given text.
- `String`: The input string with all French accents removed. - `String`: The input string with all French accents removed.
# Examples # Examples
```julia ```jldoctest
julia> remove_french_accents("Café") julia> remove_french_accents("Café")
"Cafe" "Cafe"
@@ -887,6 +907,44 @@ function remove_french_accents(text::AbstractString)::AbstractString
end end
"""
extractTextBetweenCharacters(text::String, start_char::Char, end_char::Char)::String
Extracts and returns the text that is enclosed between two specified characters within a given string.
# Arguments:
- `text::String`: The input string from which to extract the text.
- `startchar::Char`: The starting character that marks the beginning of the desired text.
- `endchar::Char`: The ending character that marks the end of the desired text.
# Returns:
- `String`: The substring enclosed between `start_char` and `end_char`.
# Examples:
```jldoctest
julia> text = "Hello [World]!"
julia> extracted_text = extractTextBetweenCharacter(text, '[', ']')
println(extracted_text) # Output: "World"
"""
function extractTextBetweenCharacter(text::String, startchar::Char, endchar::Char)
result = []
start_index = 0
in_braces = false
for (i, c) in enumerate(text)
if c == startchar
start_index = i + 1
in_braces = true
elseif c == endchar
if in_braces
push!(result, text[start_index:i-1])
in_braces = false
end
end
end
return result
end