gpt4 book ai didi

ios - 如何通过标题的第一个字母创建字典但忽略 "the"或 "a/an"等文章

转载 作者:行者123 更新时间:2023-11-28 12:14:02 25 4
gpt4 key购买 nike

我想按 TableView 中媒体项的标题对数据进行排序,但我想按第一个“实际”字母对数据进行排序,而不是像“the”或“an”这样的常见冠词

  • 一首民谣
  • 红 mustache 海贼之歌
  • 黑 mustache 海贼之歌
  • 钢球

我尝试了以下解决方案:

  extension String {

func firstLetter() -> Character{
var tmp = self.lowercased()
if tmp.hasPrefix("the "){
tmp = String(tmp.characters.dropFirst(4))
}else if tmp.hasPrefix("a "){
tmp = String(tmp.characters.dropFirst(2))
}else if tmp.hasPrefix("an "){
tmp = String(tmp.characters.dropFirst(3))
}
let hmm = "aąbcćdeęfghijklmnoópqrsśtuvwxyzżź0123456789"
let letters = Array(hmm.characters)
for index in characters.indices{
if letters.contains(tmp[index]){
return tmp[index]
}
}
return "_"
}

func firstSpecial() -> Bool {
let characterset = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
if prefix(1).rangeOfCharacter(from: characterset.inverted) != nil {
return true
}else{
return false
}
}

func firstNumber() -> Bool {
if lowercased().hasPrefix("the "){
return Int(dropFirst(4).prefix(1)) != nil
}else if lowercased().hasPrefix("a "){
return Int(dropFirst(2).prefix(1)) != nil
}else if lowercased().hasPrefix("an "){
return Int(dropFirst(3).prefix(1)) != nil
}else{
return Int(prefix(1)) != nil
}
}
}

songs = [MPMediaItem]()
result = [String: [MPMediaItem]]()
indexes = [String]()

func setup(){
var numbers = false
var special = false
songs = musicQuery.shared.songs
for song in songs {
var key = ""
if song.title!.firstNumber() {
print(song.title)
key = "#"
if result[key] != nil {
result[key]?.append(song)
}else{
result[key] = []
result[key]?.append(song)
numbers = true
}
}else if !(song.title?.firstSpecial())! {
key = String(describing: song.title?.firstLetter()).uppercased()
if result[key] != nil {
result[key]?.append(song)
}else{
result.updateValue([song], forKey: key)
indexes.append(key)
}
}else{
print(song.title)
key = "?"
if result[key] != nil {
result[key]?.append(song)
}else{
result[key] = []
result[key]?.append(song)
special = true
}
}
}
indexes = indexes.sorted { $0.compare($1) == ComparisonResult.orderedAscending }
if numbers {
indexes.append("#")
}
if special {
indexes.append("?")
}
}

但它远非最佳,需要很长时间才能完成并且省略了一些条目

最佳答案

简化的解决方案,只需在排序时替换那些您不想包含的文章。

    let unsortedList = ["Ballad of El Red Beard Pirate","A Ballad","Ballad of An Red Beard Pirate","The Ballad of Black Beard Pirate","Balls of steel"];

func removeLeadingArticle(string: String) -> String {
let articles = ["The ", "A ", "of "," An"];
var changedStr = string;
for (_,article) in articles.enumerated() {
changedStr = changedStr.replacingOccurrences(of: article, with: "");
}
print("changed string \(changedStr)");
return changedStr;
}

let sortedList = unsortedList.sorted { (firstStr, secondStr) -> Bool in
let title1 = removeLeadingArticle(string: firstStr);
let title2 = removeLeadingArticle(string: secondStr);
return title1.localizedCaseInsensitiveCompare(title2) == ComparisonResult.orderedAscending
}

print("sorted list \(sortedList)");

输出------------

changed string Ballad
changed string Ballad El Red Beard Pirate
changed string Ballad Red Beard Pirate
changed string Ballad El Red Beard Pirate
changed string Ballad Black Beard Pirate
changed string Ballad Red Beard Pirate
changed string Ballad Black Beard Pirate
changed string Ballad El Red Beard Pirate
changed string Ballad Black Beard Pirate
changed string Ballad
changed string Balls steel
changed string Ballad Red Beard Pirate
sorted list ["A Ballad", "The Ballad of Black Beard Pirate", "Ballad of El Red Beard Pirate", "Ballad of An Red Beard Pirate", "Balls of steel"]

获取以忽略“A”、“An”、“The”的前导字符为键的字典。

    let unsortedList = ["A Ballad","Ballad of An Red Beard Pirate","The Ballad of Black Beard Pirate","All Balls of steel","Red Riding Hood","The Earth"];

let articles = ["The","A","An"];

var dictionary:Dictionary = Dictionary<String,String>();

for objStr in unsortedList {

let article = objStr.components(separatedBy: " ").first!;

print("article: \(article)");

if articles.contains(article) {

if objStr.components(separatedBy: " ").count > 1 {
let secondStr = objStr.components(separatedBy: " ")[1];
dictionary["\(secondStr.first!)"] = objStr;
}
}else {
dictionary["\(article.first!)"] = objStr;
}
}


print("dictionary:- \(dictionary)");

输出 ------

文章:A文章:民谣文章:文章:全部文章:红色文章:

dictionary:- ["R": "Red Riding Hood", "B": "The Ballad of Black Beard Pirate", "A": "All Balls of steel", "E": "The Earth"]

关于ios - 如何通过标题的第一个字母创建字典但忽略 "the"或 "a/an"等文章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47455364/

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