Objective C 中的智能句子比较

发布于 2024-12-12 18:47:44 字数 1385 浏览 1 评论 0原文

我需要在 Objective C 中进行智能句子比较。

详细信息如下所述。

我有一根主弦。我将其命名为“OriginalString”

                   NSString *OriginalString = @""HOW DID STUDENT EMILY DO TODAY";

然后我有很多字符串,我需要与原始字符串进行比较。它们

             NSString *CompareString1 = @"HOW DID EMILY DO "
             NSString *CompareSting  = @"HOW DID YOUR FATHER"
             NSString *CompareString2 = @"WHAT IS YOUR EMILY"
             NSString *CompareString3 = @"THIS IS THE FAST DO ONE"
             NSString *CompareString4 = @"THIS SEARCH EMILY EXAMPLE DO"

来自这5个CompareStrings,我需要获取相对于原始字符串最高的字符串“HOW DID EMILY DO” (艾米丽学生今天表现如何)。

让我告诉你一件事。

如果我在 CompareStrings 组中有一个附加字符串作为“HOW DID STUDENT DO TODAY”,

那么现在我的 CompareStrings 是

             NSString *CompareString1 = @"HOW DID EMILY DO "
             NSString *CompareSting  = @"HOW DID YOUR FATHER"
             NSString *CompareString2 = @"WHAT IS YOUR EMILY"
             NSString *CompareString3 = @"THIS IS THE FAST DO ONE"
             NSString *CompareString4 = @"THIS SEARCH EMILY EXAMPLE DO"
             NSString *CompareString4 = @"HOW DID STUDENT DO TODAY"

来自这 6 个 CompareStrings,我需要获取字符串“HOW DID STUDENT DO TODAY”,该字符串相对于原始字符串(学生艾米丽今天做了什么)

我查看了 NSPredicate“比较和类似”方法。然而,我没有找到任何方法来完成这个过程,

感谢任何关于弄清楚这个概念的帮助。

谢谢。

I need to do the intelligence Sentence comparison in Objective C.

The Detail's are explained below.

I have one main string. I named it as "OriginalString"

                   NSString *OriginalString = @""HOW DID STUDENT EMILY DO TODAY";

Then I have many strings, that i need to compare with the Original String.They are

             NSString *CompareString1 = @"HOW DID EMILY DO "
             NSString *CompareSting  = @"HOW DID YOUR FATHER"
             NSString *CompareString2 = @"WHAT IS YOUR EMILY"
             NSString *CompareString3 = @"THIS IS THE FAST DO ONE"
             NSString *CompareString4 = @"THIS SEARCH EMILY EXAMPLE DO"

From these 5 CompareStrings ,i need to get the string "HOW DID EMILY DO" which is highest relative to the Original string(HOW DID STUDENT EMILY DO TODAY) .

Let me tell you one thing.

If i have an additional string in the group of CompareStrings As "HOW DID STUDENT DO TODAY"

So now i have the CompareStrings are

             NSString *CompareString1 = @"HOW DID EMILY DO "
             NSString *CompareSting  = @"HOW DID YOUR FATHER"
             NSString *CompareString2 = @"WHAT IS YOUR EMILY"
             NSString *CompareString3 = @"THIS IS THE FAST DO ONE"
             NSString *CompareString4 = @"THIS SEARCH EMILY EXAMPLE DO"
             NSString *CompareString4 = @"HOW DID STUDENT DO TODAY"

From these 6 CompareStrings ,i need to get the string "HOW DID STUDENT DO TODAY" which is highest relative to the Original string (HOW DID STUDENT EMILY DO TODAY)

I have looked the NSPredicate "comparison and like" methods. However i didn't found any way to do the process

Any help on figuring out this concept is appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

2024-12-19 18:47:44

自然语言解析是一个困难的话题,通过简单的字符串比较方法不太可能获得好的结果。我建议寻找一个在更高级别上运行的工具包。 Lion 和 iOS 5 通过 NSLinguisticTagger 类

Natural language parsing is a difficult topic, and it's unlikely you'll get good results with a simple string comparison approach. I would suggest looking for a toolkit that operates at a higher level. Lion and iOS 5 include a small level of support for this with the NSLinguisticTagger class.

内心荒芜 2024-12-19 18:47:44

听起来像是计算机科学作业。虽然它非常草率和低效,但它会起作用。我确信这里有人有更好的想法来提高性能。

将要搜索的短语分解为单个单词的数组:

NSArray *myWords = [myString componentsSeparatedByString:@" "];

现在 myWords 是每个单词的数组(没有空格)。然后,构建一个查找每个单词的谓词:

int found = 0;
for (NSString* word in myWords) {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[c] %@", word];
    BOOL ok = [predicate evaluateWithObject:originalString];
    if (ok) {
      ++found;
    }
}

完成后,您将得到一个 found,您可以对每个不同的短语进行比较。最大的那个与最多的单词匹配。

在这种情况下,您最终会像这样进行计数:

HOW DID STUDENT EMILY DO TODAY

  • “HOW DID EMILY DO”4
  • “HOW DID YOUR FATHER”2
  • “你的艾米丽是什么”1
  • “这是最快的做法”1
  • “这个搜索艾米丽的例子” 2
  • “学生今天表现如何”5

Sounds like comp-sci homework. It's sloppy and inefficient as hell, but it will work. I'm sure somebody on here has a better idea to improve performance.

Break down your phrases that you want to search for into arrays of individual words:

NSArray *myWords = [myString componentsSeparatedByString:@" "];

Now myWords is an array of each word (no spaces). Then, build a predicate looking for each of the words:

int found = 0;
for (NSString* word in myWords) {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[c] %@", word];
    BOOL ok = [predicate evaluateWithObject:originalString];
    if (ok) {
      ++found;
    }
}

When you are done, you'll end up with a found you can compare for each of the different phrases. The one that is greatest matched the most words.

In this case, you'd end up counting like this:

HOW DID STUDENT EMILY DO TODAY

  • "HOW DID EMILY DO " 4
  • "HOW DID YOUR FATHER" 2
  • "WHAT IS YOUR EMILY" 1
  • "THIS IS THE FAST DO ONE" 1
  • "THIS SEARCH EMILY EXAMPLE DO" 2
  • "HOW DID STUDENT DO TODAY" 5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文