gpt4 book ai didi

swift - 如何在另一个字符串中找到所有出现的字符串子集并将它们替换为其他内容? (例如 : Emote replacement)

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:52:05 26 4
gpt4 key购买 nike

我正在为我的应用创建聊天功能,我们有一组自定义表情符号。

如果用户在他们的评论中键入其中一个表情的快捷方式,我们需要能够检测到快捷方式按出现顺序并用适当的表情替换它们。

例如。

let emotes = [
"app" : "[]"
"happy" : ":)",
]

let userComment = "I am happy"

let expectedResult = "I am :)" // "app" was not replaced because "happy"'s "h" was found first, hence it was evaluated before "app"

考虑到同一个表情可以多次出现在一条评论中,是否有一种有效的算法可以实现这一点?

我知道有 range(of:) 可以找到子字符串的范围,但考虑到我们可能有几十个表情符号,使用它似乎非常低效。


更新:

请注意,表情“键”(快捷方式)可能会发生冲突,因此遍历表情字典不是解决问题的办法。我更改了示例代码以反射(reflect)这一点。

最佳答案

正则表达式可用于查找所有出现的任何字典键,例如

(app|happy)

单词边界模式 \b 可用于仅匹配完整的单词:

\b(app|happy)\b

将此方法与 Replace matching regex values in string with correct value from dictionary 中的代码相结合我们得到以下实现:

func replaceOccurrences(in string: String, fromDict dict: [String: String]) -> String {
var result = string

// A pattern matching any of the dictionary keys.
let pattern = "\\b(" + dict.keys.map { NSRegularExpression.escapedPattern(for: $0)}
.joined(separator: "|") + ")\\b"
let regex = try! NSRegularExpression(pattern: pattern)
let matches = regex.matches(in: string, range: NSRange(string.startIndex..., in: string))

// Iterate in reverse to avoid messing up the ranges as the keys are replaced with the values.
for match in matches.reversed() {
if let range = Range(match.range, in: string) {
// Extract key, look up replacement in dictionary, and replace in result string.
if let replacement = dict[String(string[range])] {
result.replaceSubrange(range, with: replacement)
}
}
}

return result
}

示例 1:

let emotes = [
"happy" : ":)",
"sad" : ":("
]

let userComment = "I am happy to meet you, but I am sad cos of this algorithm"
let result = replaceOccurrences(in: userComment, fromDict: emotes)
print(result) // I am :) to meet you, but I am :( cos of this algorithm

示例 2:

let emotes = [
"app" : "[]",
"happy" : ":)"
]

let userComment = "I am happy!"
let result = replaceOccurrences(in: userComment, fromDict: emotes)
print(result) // I am :)!

关于swift - 如何在另一个字符串中找到所有出现的字符串子集并将它们替换为其他内容? (例如 : Emote replacement),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56413038/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com