Objective-C:将字符串数组与用户输入的字符串进行比较,然后返回适当的值?

发布于 2025-01-02 23:01:02 字数 753 浏览 1 评论 0原文

这是一个关于使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灰色世界里的红玫瑰 2025-01-09 23:01:02

当用户点击“搜索”按钮时,将 textField.text 传递给此方法:

 - (int)matchingIDForString:(NSString *)userSuppliedText {

      NSInteger bestLevDistanceSoFar = 9999999;
      int       indexOfMatch=-1;
      // having to search the entire array each time is, of course, scary.
      for ( int j=0; j<[myMutableArray count]; j++ ) {
         NSString *candidateAnswer = [myMutableArray objectAtIndex:j];
         // if candidateAnswer is a string, just use it.  Else extract the member you want...
         NSInteger *levDistance = [self myLevensteinDistanceMethod:candidateAnswer
                                                         forstring:userSuppliedText];
         if ( levDistance < bestLevDistanceSoFar ) {
            indexOfMatch = j;
            bestLevDistanceSoFar = levDistance;
         }
      }
      return indexOfMatch;   // called should test for <0 meaning no match
 }

您还需要实现此方法:

 - (NSInteger *)myLevensteinDistanceMethod:(NSString *)string1 forString:(NSString *)string2 {
     // calculate the lev distance, and return it as an NSInteger *.
 }

When user hits the Search button, pass textField.text to this method:

 - (int)matchingIDForString:(NSString *)userSuppliedText {

      NSInteger bestLevDistanceSoFar = 9999999;
      int       indexOfMatch=-1;
      // having to search the entire array each time is, of course, scary.
      for ( int j=0; j<[myMutableArray count]; j++ ) {
         NSString *candidateAnswer = [myMutableArray objectAtIndex:j];
         // if candidateAnswer is a string, just use it.  Else extract the member you want...
         NSInteger *levDistance = [self myLevensteinDistanceMethod:candidateAnswer
                                                         forstring:userSuppliedText];
         if ( levDistance < bestLevDistanceSoFar ) {
            indexOfMatch = j;
            bestLevDistanceSoFar = levDistance;
         }
      }
      return indexOfMatch;   // called should test for <0 meaning no match
 }

You'll need to implement this method also:

 - (NSInteger *)myLevensteinDistanceMethod:(NSString *)string1 forString:(NSString *)string2 {
     // calculate the lev distance, and return it as an NSInteger *.
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文