简体   繁体   English

Swift 3.0语音到文本:改变单词的颜色

[英]Swift 3.0 Speech to Text: Changing Color of Words

I'm trying to change the color of words in textfield set by spoken words (ex: happy, sad, angry, etc). 我试图通过口头语言改变文本字段中的单词颜色(例如:快乐,悲伤,愤怒等)。 It doesn't work if the word is spoken more than once. 如果不止一次说出这个词,它就不起作用。 For example, if I say, "I'm feeling happy because my cat is being nice to me. My brother is making me sad. I'm happy again." 例如,如果我说,“我感到高兴,因为我的猫对我很好。我哥哥让我伤心。我再次开心。” it will only change the color of the first 'happy' and I'm not exactly sure why. 它只会改变第一个'快乐'的颜色,我不确定为什么。

func setTextColor(text: String) -> NSMutableAttributedString {

    let string:NSMutableAttributedString = NSMutableAttributedString(string: text)
    let words:[String] = text.components(separatedBy:" ")

        for word in words {
            if emotionDictionary.keys.contains(word) {
                let range:NSRange = (string.string as NSString).range(of: word)
                string.addAttribute(NSForegroundColorAttributeName, value: emotionDictionary[word], range: range)
            }
        }
    return string

}

Thanks! 谢谢!

There are two problems with your code. 您的代码有两个问题。

The first problem is the punctuation in your example. 第一个问题是你的例子中的标点符号。 When you do: 当你这样做时:

text.components(separatedBy:" ")

The resulting array will look like: 结果数组将如下所示:

["I'm", "feeling", "happy", ..., "making", "me", "sad."]

The sad has a period in it and won't match what's in your emotion dictionary if the key is just "sad". 如果关键只是“悲伤”,那么悲伤就会有一段时间与你的情感词典中的内容不相符。

The second problem is with: 第二个问题是:

let range:NSRange = (string.string as NSString).range(of: word)

Since you have "happy" twice in your example this will only return the range of the first occurrence of happy, so only the first happy will be highlighted. 由于你在你的例子中有两次“快乐”,这只会返回第一次出现快乐的范围,因此只会突出显示第一次快乐。

The best path is to use a regular expression for each key in your emotion dictionary. 最好的方法是为情绪词典中的每个键使用正则表达式。 Then you can call regex.matches which will get you the ranges of all occurrences of happy or sad. 然后你可以调用regex.matches来获取所有出现的快乐或悲伤的范围。 You can then loop over that and set the color appropriately. 然后,您可以循环并适当地设置颜色。

This does the following and should work with your example: 这将执行以下操作并应与您的示例一起使用:

func setTextColor(text: String) -> NSMutableAttributedString {

    let string:NSMutableAttributedString = NSMutableAttributedString(string: text)

    for key in emotionDictionary.keys {
        do {
            let regex = try NSRegularExpression(pattern: key)
            let allMatches = regex.matches(in: text, options: [], range: NSMakeRange(0, string.length))
                                  .map { $0.range }
            for range in allMatches {
                string.addAttribute(NSForegroundColorAttributeName, value: emotionDictionary[key], range: range)
            }
        }
        catch { }
    }

    return string
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM