Objective-C:将字符串数组与用户输入的字符串进行比较,然后返回适当的值?
这是一个关于使用 Objective C 进行 iOS 编程的问题。
我有一个字符串“csvContent”的 NSMutableArray,它是从包含问题、答案和关键字的伪数据库的 CSV 文件中解析出来的。 CSV 文件的内容如下:
ID#、“这是我要问的问题?”、“[问题、关键词、单词]”、“这是您问题的答案”。
这些问题以及相关的关键字和答案大约有 2,000 个,我已成功地将它们逐行解析到数组中,以便每个元素包含您在上面看到的示例中的所有内容。
我的问题是,如果我想让用户在 UITextField 中提出问题,然后比较 UserQuestion 并在字符串数组中找到最相似的问题,然后返回其答案,那么最好的方法是什么?我查看了有关 Levenshtein 距离的文档,并认为这将是一个不错的选择,但不知道如何准确实现它并让它迭代我的整个 CSVContent 数组。我并不是在寻找确切的代码,但理想的答案将包含一些关于如何实现此目的的伪代码或方法。
总结一下:
字符串数组 CSVContent,外观:[id,"question",("question keywords"),"answer"]。
我有一个 UITextField,我可以在其中将用户输入的问题解析为字符串 UserQuestion。
我想使用快速比较算法(Levenshtein?)将 UserQuestion 与 CSVContent 中的元素进行比较,并找到适当的问题和相关答案,然后返回答案。
This is a question about iOS programming with Objective C.
I have an NSMutableArray of strings "csvContent", that were parsed from a CSV file that contained a pseudo-database of questions, answers, and keywords. The contents of the CSV file were as follows:
ID#, "Here is the question I am asking?", "[question, key, words]", "This is the answer to your question."
There are about 2,000 of these questions and related keywords and answers, and I have successfully parsed them into the array, line by line so that each element contains everything in the example you see above.
My question is that if I want to have a user ask a question in a UITextField and then compare the UserQuestion and find the most similar question in my array of strings and then return its answer, what would be the best way to go about doing so? I've looked through documentation about Levenshtein distance and think that that would be a good option, but don't know how to exactly implement it and have it iterate through my entire CSVContent array. I'm not looking for exact code, but an ideal answer would contain some pseudocode or methodology on how to go about this.
To summarize:
Array of strings, CSVContent, of appearance: [id,"question",("question keywords"),"answer"].
I have a UITextField where I can parse a user's entered question into a string UserQuestion.
I want to use a fast comparison algorithm (Levenshtein?) to compare UserQuestion to the elements inside CSVContent and find the appropriate question and related answer, then return the answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当用户点击“搜索”按钮时,将 textField.text 传递给此方法:
您还需要实现此方法:
When user hits the Search button, pass textField.text to this method:
You'll need to implement this method also: