聚合云播

排序

金钱豹子汤隆剧情简介

" // 词典中放了很多优化规则 let defaultToken = " " let maxToken = 100 let minToken = 0 var tokenRange = minToken...maxToken var pinyinFirst: String = "" var pinyinLast: String = "" // 拼接全部拼音 var pinyinAll = "" var leftLength = "" var rightLength = "" var pinyinLength = "" var title = "" var other = "" var prev = "" var cur = "" var next = "" var headNumber = 0 var tailNumber = 0 var itemList = ["title":title,"other":other,"prev":prev,"cur":cur,"next":next,"pinyinFirst":pinyinFirst,"pinyinLast":pinyinLast,"leftLength":leftLength,"rightLength":rightLength,"pinyinLength":pinyinLength] /// 切词 /// /// - Parameters: /// - text: 初始文本 /// - token: 切词符 func getWordsList(text: String, prevent: String) -> [String] { var wordsList:[String] = [] do { let regex = try NSRegularExpression(pattern: "[^(prevent)\s] ", options: []) regex.enumerateMatches(in:text, options:[], range:NSMakeRange(0, text.characters.count)) { (result, flags, pointer) in let range = result?.range let subtext = (text as NSString).substring(with:range!)// print(subtext) wordsList.append(subtext) } } catch { print("getWordsList error") } return wordsList } /// 优化文本 /// title 40 pinyinLength 50 prev 5 cur 5 next 5 /// /// - Parameters: /// - text: 初始文本 /// - token: 切词符 /// - Returns: 优化后文本 mutating func optimizedText(text: String, prevent: String) -> String { var list = getWordsList(text: text, prevent: prevent) var str = "" headNumber = 0 tailNumber = 0 var tempindex = 0 for item in list { if item == "" { continue } str.append(item) // 拼接 if item.characters.count > 1 || (item.characters.count == 1 && isChinese(item)) { pinyinAll = prev cur item next } else { pinyinAll = "" } // 优化 var optimize = optimizeAll(pinyinString: pinyinAll) pinyinFirst = optimize.pinyinFirst pinyinLast = optimize.pinyinLast leftLength = optimize.leftLength rightLength = optimize.rightLength pinyinLength = optimize.pinyinLength // 计算headNumber tailNumber headNumber = leftLength.characters.count pinyinFirst.characters.count tailNumber = (rightLength pinyinLast).characters.count tempindex = 1 if tempindex == list.count { continue } if tempindex < list.count - 1 { prev = cur cur = next next = list[tempindex 1] } } // 整理后的数据 itemList["title"] = str itemList["other"] = "(tailNumber pinyinLength.characters.count)" itemList["prev"] = "(headNumber)" itemList["cur"] = "(pinyinFirst.characters.count)" itemList["next"] = "(pinyinLength.characters.count)" itemList["pinyinFirst"] = pinyinFirst itemList["pinyinLast"] = pinyinLast itemList["leftLength"] = leftLength itemList["rightLength"] = rightLength itemList["pinyinLength"] = pinyinLength return str } /// 优化方法 /// 1、汉字开头将中文转为拼音首字母 /// 2、全部拼音首字母小写 /// 3、拼音间用空格下划线隔开 /// /// - Parameter pinyinString: 未优化的拼音字符串 /// - Returns: 优化后的拼音 private func optimizeAll(pinyinString: String) -> (pinyinFirst: String, pinyinLast: String, leftLength: String, rightLength: String, pinyinLength: String) { var pinyinFirst = "" var pinyinLast = "" var pinyinLength = "" var leftLength = "" var rightLength = "" let words = pinyinString.characters.split(separator: " ", maxSplits: Int.max, omittingEmptySubsequences: false) for (index, item) in words.enumerated() { let temp = item.reversed() if temp.count <= 1 { var tempString = "" for i in item.reversed() { tempString = String(i) } pinyinFirst = tempString continue } for (i, a) in temp.enumerated() { if i == temp.count - 1 { pinyinLast = "(a)" } else { pinyinFirst = "(a)" } } } leftLength = "(pinyinFirst.characters.count)" rightLength = "(pinyinLast.characters.count)" pinyinLength = pinyinFirst pinyinLast return (pinyinFirst, pinyinLast, leftLength, rightLength, pinyinLength) } /// 中文词数量 /// /// - Parameter text: 文本 /// - Returns: 中