简体   繁体   English

Objective-C:NSLinguisticTagger“纽约”vs“纽约”

[英]Objective-C: NSLinguisticTagger “new york” vs “New York”

I just started playing around with NSLinguisticTagger basing my code on this blog: NSLinguisticTagger @ NSHipster.com 我刚刚开始使用NSLinguisticTagger我的代码基于这个博客: NSLinguisticTagger @ NSHipster.com

NSLinguisticTaggerOptions options = NSLinguisticTaggerOmitWhitespace | NSLinguisticTaggerOmitPunctuation | NSLinguisticTaggerJoinNames;
NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes: [NSLinguisticTagger availableTagSchemesForLanguage:@"en"] options:options];
tagger.string = question;
[tagger enumerateTagsInRange:NSMakeRange(0, [question length]) scheme:NSLinguisticTagSchemeNameTypeOrLexicalClass options:options usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) {
NSString *token = [question substringWithRange:tokenRange];
NSLog(@"%@: %@", token, tag); }];

When I run this with question = @"Weekend in New York" , "New York" gets tagged as PlaceName which is great. 当我用question = @"Weekend in New York""New York"被标记为PlaceName ,这很棒。 But when I run this with question = @"Weekend in new york" , "new" gets tagged as "Adjective" and "york" gets tagged as PlaceName . 但当我用question = @"Weekend in new york""new"被标记为"Adjective""york"被标记为PlaceName Is there any way to get around this such that "New York" and "new york" both get tagged as PlaceName ? 有没有办法解决这个问题, "New York""new york"都被标记为PlaceName

I'm totally new to this linguistics thing. 我对这种语言学事物完全陌生。

Taking this topic a little further. 进一步讨论这个话题。 Correct capitalization of first name and last name is a requirement for the NSLinguisticTagger to identify names. 正确的名字和姓氏大写是 NSLinguisticTagger识别名称的要求

After several hours of frustration , I decided to create various tests with uppercase, lowercase and capitalized-case words. 经过几个小时的挫折 ,我决定用大写,小写和大写单词创建各种测试。

The NSLinguisticTagger had different results in almost all tests NSLinguisticTagger几乎在所有测试中都有不同的结果

When the NSLinguisticTagger parses a string in capitalized-case almost all nouns are tagged as personalName . 当NSLinguisticTagger以大写字母大小写解析字符串时,几乎所有名词都被标记为personalName wtf? 跆拳道?

It was very frustrating. 这非常令人沮丧。

The lesson I want to share is that the NSLinguistic tagger can guess at the tags it places on words, but in the end it is just a grammatical evaluation of words. 我要分享的教训是,NSLinguistic标记器可以猜测它放在单词上的标签,但最后它只是对单词的语法评估。 The evaluation depends on proper language constructs such as word placement and whether the word is capitalized or not. 评估取决于正确的语言结构,例如单词放置以及单词是否大写。

I am still finding it a useful class, but the moral of this post is to "Be Proper" . 我仍然认为这是一个有用的课程,但这篇文章的寓意是“适当”

When parsing text sometimes we programmers have a tendency to play with uppercasing and lowercasing to simplify our work. 在解析文本时,有时我们程序员倾向于使用大写和小写来简化我们的工作。 We can still do this, but just keep in mind that word casing does change the NSLinguisticTagger results . 我们仍然可以这样做,但请记住,单词大小写会改变NSLinguisticTagger结果

This has already been mentioned in the comments, but wanted to point this out anyway. 评论中已经提到过这一点,但无论如何都想指出这一点。 NSLinguisticTagger believes that "New York" and "new york" are different - because they are. NSLinguisticTagger认为“纽约”和“纽约”是不同的 - 因为它们是。 The capital N tells it that it's a proper noun. 首都N告诉它它是一个专有名词。 To my knowledge, there is nothing in NSLinguisticTagger that can change this behavior. 据我所知,NSLinguisticTagger中没有任何内容可以改变这种行为。

However, what you can do is rely on iOS autocorrect. 但是,您可以做的是依赖iOS自动更正。 Just make sure that the text field where the value is being entered has autocorrect enabled, and it should automatically correct "new york" to "New York", and similar occurrences. 只需确保输入值的文本字段已启用自动更正,并且应自动将“new york”更正为“纽约”,以及类似的事件。 If autocorrect doesn't catch this, then I would try to find some other library for linguistic analysis. 如果自动更正没有捕获到这一点,那么我会尝试找一些其他库进行语言分析。

Retroactive autocorrect is already included in iOS (to a certain extend), so that should be good enough to correct "new york" to "New York". 追溯自动更正已经包含在iOS中(在某种程度上),因此应该足以将“纽约”更正为“纽约”。 If you want to correct the whole sentence (ie "weekend in new york" to "Weekend in New York"), you would need to implement that functionality yourself. 如果你想纠正整个句子(即“纽约周末”到“纽约周末”),你需要自己实现这个功能。 This shouldn't be terribly difficult, as there are just a few simple grammar rules you must follow, and many things will be picked up by autocorrect. 这应该不是非常困难,因为您必须遵循一些简单的语法规则,并且许多事情将通过自动更正来获取。

Hope this helps, let me know if you need more information. 希望这有帮助,如果您需要更多信息,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Apple 的 New York 字体可以在 iOS 12 中使用吗? - Can Apple's New York font be used in iOS 12? IOS / Objective-C:用于识别命名实体的NSLinguisticTagger - IOS/Objective-C: NSLinguisticTagger for Recognizing Named Entities 如何将Google Place自动完成api结果限制为仅纽约? - How to restrict google place auto complete api result to New york only? 在Objective-C中设置新的Parse服务器 - Set a new Parse server in Objective-C 4.4中的新Objective-c文字 - New Objective-c Literals in 4.4 UITableView中的单元格到新视图Objective-C - cells in UITableView to new views Objective-C Objective-C类别和新的iVar - Objective-C Category and new iVar 是否有可能两个用户以“America/New_York”格式报告相同的时区名称,但一个正在观察 DST 而另一个则不是? - Is it possible that two users report the same timezone name in the format "America/New_York" but one is observing DST and other isn't? 我可以将iOS应用限制为美国特定州吗? 例如-应用程序应仅在纽约州下载 - Can I restrict iOS app to specific US state ? e.g - app should be download in NEW YORK state only 如何在objective-c中导航到新故事板文件上的视图控制器 - How to navigate to a view controller on a new storyboard file in objective-c
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM