这是关于使用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中的元素进行比较,找到相应的问题及相关答案,然后返回答案。
答案 0 :(得分:0)
当用户点击“搜索”按钮时,将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 *.
}