ios - 如何使用 Swift 4 将字符串拆分为英语和非英语?

标签 ios swift string

我有一个包含英语和阿拉伯语的字符串。我正在使用 API,这就是我无法在其中设置指标的原因。

我想要得到的是:阿拉伯语和英语分成两部分。这是一个示例字符串:

"بِاسْمِكَ رَبِّي وَضَعْتُ جَنْبِي، وَبِكَ أَرْفَعُهُ، فَإِنْ أَمْسَكْتَ نَفْسِي فَارْحَمْهَا، وَإِنْ أَرْسَلْتَهَا فَاحْفَظْهَا، بِمَا تَحْفَظُ بِهِ عِبَادَكَ الصَّالِحِينَ.Bismika rabbee wadaAAtu janbee wabika arfaAAuh, fa-in amsakta nafsee farhamha, wa-in arsaltaha fahfathha bima tahfathu bihi AAibadakas-saliheen. In Your name my Lord, I lie down and in Your name I rise, so if You should take my soul then have mercy upon it, and if You should return my soul then protect it in the manner You do so with Your righteous servants.",

我找不到如何将它分成两部分,我将阿拉伯语和英语分成两个不同的部分。

我想要的:

所以可以有任何语言,我的问题是只取出英语或阿拉伯语并在各自的字段中显示它们。

我怎样才能实现它?

最佳答案

您可以使用 Natural Language Tagger ,即使两个脚本混合在一起也能工作:

import NaturalLanguage

let str = "¿como? بداية start وسط middle начать средний конец نهاية end. 從中間開始. "

let tagger = NLTagger(tagSchemes: [.script])

tagger.string = str

var index = str.startIndex
var dictionary = [String: String]()
var lastScript = "other"


while index < str.endIndex {
    let res = tagger.tag(at: index, unit: .word, scheme: .script)
    let range = res.1

    let script = res.0?.rawValue

    switch script {
    case .some(let s):
        lastScript = s
        dictionary[s, default: ""] += dictionary["other", default: ""] + str[range]
        dictionary.removeValue(forKey: "other")
    default:
        dictionary[lastScript, default: ""] += str[range]
    }

    index = range.upperBound
}

print(dictionary)

如果你愿意,打印结果:

for entry in dictionary {
    print(entry.key, ":", entry.value)
}

产量:

Hant : 從中間開始. 
Cyrl : начать средний конец 
Arab : بداية وسط نهاية 
Latn : ¿como? start middle end.

这仍然不完美,因为语言标注器只检查单词中最多字母属于哪个脚本。例如,在您正在处理的字符串中,标注器会将 الصوّالِحِينو.Bismika 视为一个词。为了克服这个问题,我们可以使用两个指针并遍历原始字符串并分别检查 words 的脚本。单词被定义为连续的字母:

let str = "بِاسْمِكَ رَبِّي وَضَعْتُ جَنْبِي، وَبِكَ أَرْفَعُهُ، فَإِنْ أَمْسَكْتَ نَفْسِي فَارْحَمْهَا، وَإِنْ أَرْسَلْتَهَا فَاحْفَظْهَا، بِمَا تَحْفَظُ بِهِ عِبَادَكَ الصَّالِحِينَ.Bismika rabbee wadaAAtu janbee wabika arfaAAuh, fa-in amsakta nafsee farhamha, wa-in arsaltaha fahfathha bima tahfathu bihi AAibadakas-saliheen. In Your name my Lord, I lie down and in Your name I rise, so if You should take my soul then have mercy upon it, and if You should return my soul then protect it in the manner You do so with Your righteous servants."

let tagger = NLTagger(tagSchemes: [.script])
var i = str.startIndex
var dictionary = [String: String]()
var lastScript = "glyphs"

while i < str.endIndex {
    var j = i
    while j < str.endIndex,
        CharacterSet.letters.inverted.isSuperset(of: CharacterSet(charactersIn: String(str[j]))) {
        j = str.index(after: j)
    }
    if i != j { dictionary[lastScript, default: ""] += str[i..<j] }
    if j < str.endIndex { i = j } else { break }

    while j < str.endIndex,
        CharacterSet.letters.isSuperset(of: CharacterSet(charactersIn: String(str[j]))) {
        j = str.index(after: j)
    }

    let tempo = String(str[i..<j])
    tagger.string = tempo
    let res = tagger.tag(at: tempo.startIndex, unit: .word, scheme: .script)

    if let s = res.0?.rawValue {
        lastScript = s
        dictionary[s, default: ""] += dictionary["glyphs", default: ""] + tempo
        dictionary.removeValue(forKey: "glyphs")
    }
    else { dictionary["other", default: ""] += tempo }

    i = j
}

关于ios - 如何使用 Swift 4 将字符串拆分为英语和非英语?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55242906/

相关文章:

swift - 如果用 "copy constructor"初始化,Data 的 `freeWhenDone:false` 是否复制其引用的字节?

c - C 中将字符串传递给函数参数

swift - 使用模块 'CALayer' 作为类型

ios - 在 Swift 中反向搜索电话号码?

c++ - 将 Unicode 字符串作为字符循环

java - UTF-8 字符串和二进制数据的问题

ios - 在具有特定约束的 ScrollView 中显示图像(使用自动布局)

iphone - 查找字符串中字符的索引

ios - 从计划中的数组中删除适当的 CCSprite

ios - 如何只为 topLeft 和 bottomLeft 角设置圆角?