[Diving into WWDC 2017] Natural Language Processing and your Apps

自然语言处理在 App 中的应用

WWDC 2017 session 208

前言

自然语言处理(NLP)是计算机科学领域与人工智能领域中的一个重要方向。它研究能实现人与计算机之间用自然语言进行有效通信的各种理论和方法。

本 Session 主要介绍 iOS 中 NLP 相关的 API 以及这些 API 的应用。键盘的输入、手写输入和语音输入这些对于应用的输入和应用的输出(文章、用户产生的内容)都可能会涉及自然语言处理。通过运用 NLP 相关的 API 可以从这些内容中提取出一些有意义的信息,使你的应用更加智能用户体验更好。

《[Diving into WWDC 2017] Natural Language Processing and your Apps》

这些开放给开发者的 NLP API 在苹果各个平台的系统应用中已经被广泛使用。

《[Diving into WWDC 2017] Natural Language Processing and your Apps》

NLP API 的功能和结构

《[Diving into WWDC 2017] Natural Language Processing and your Apps》

这些 API 是基于语言学以及机器学习来实现,内部由以下功能模块组成:

  • 语言识别(Language Identification): 利用机器学习的手段去识别语言

《[Diving into WWDC 2017] Natural Language Processing and your Apps》

  • 分词(Tokenization): 将段落或句子分解为有意义的词。分词可以按照不同的规模来处理,可以分解为段落、句子或单独的一个单词。由于中文与其他的语种不同没有空格识别起来单独会更大。

《[Diving into WWDC 2017] Natural Language Processing and your Apps》

  • 词性标注(Part of speech): 标注一句话中每个单词的词性。

《[Diving into WWDC 2017] Natural Language Processing and your Apps》

  • 词形还原(Lemmatization): 将不同形式的词转换为词根形式。

《[Diving into WWDC 2017] Natural Language Processing and your Apps》

  • 实体识别(Named Entity Recognition): 从文本中识别出如人名、地名、组织等实体

《[Diving into WWDC 2017] Natural Language Processing and your Apps》

如何使用 NLP API?

NSLinguisticTagger NS_CLASS_AVAILABLE(10_7, 5_0)

位于 Fondation 中用来处理文本的对象,可以实现分段、分词、词性标注和还原的功能。将一个文本对象赋值给一个这个对象的实力来进行标注,在指定一个标注的模式(分词、词性标注、实体识别等)之后,方法会给你返回对应的标注。

新特性

  • NSLinguisticTagger

标注单元(Tagging units)

之前能只针对单词级别的分析,新的 API 可以在词、句、段落和文章级别来进行分析操作。

public enum NSLinguisticTaggerUnit : Int {  
    case word
    case sentence 
    case paragraph 
    case document
}

针对不同的 tagschme 提供了 API 可以进行查询 Units and schemes

class func availableTagSchemes (for unit: NSLinguisticTaggerUnit, language: String) -> [NSLinguisticTagScheme]  
  • 语种识别(dominantLanguage)
  • Swift 4 : named types for tags and tagSchemes
  • 更高的性能
  • 更高的准确率
  • 更多语种的支持

我们为什么要使用 NLP API?

  • 全 Apple 平台都支持(mac、pad、phone、watch、tv)且保持一致性
  • 安全性
  • 离线机器学习
  • 用户数据保存在本地
  • 性能
  • 多线程高度优化
  • 速度显著提升
  • 语种的支持
  • 准确性保持业界领先水平

调试技巧

未识别的标注

  • 模型没有更新 (模型和语种的键盘绑定)
  • 如果可能尽量明确指定语种

Demo

  • 语种识别
import Foundation  
let tagger = NSLinguisticTagger(tagSchemes: [.language], options: 0)  
tagger.string = "Die Kleinen haben friedlich zusammen gespielt."  
let language = tagger.dominantLanguage  
  • 分词
import Foundation  
let tagger = NSLinguisticTagger(tagSchemes: [.tokenType], options: 0)  
let text = "NSLinguisticTagger provides text processing APIs.\n NSLinguisticTagger 是苹果的 字处  平台。"  
tagger.string = text  
let range = NSRange(location: 0, length: text.utf16.count)  
let options: NSLinguisticTagger.Options = [.omitPunctuation, .omitWhitespace]  
tagger.enumerateTags(in: range, unit: .word, scheme: .tokenType, options: options) {  
tag, tokenRange, stop in  
    let token = (text as NSString).substring(with: tokenRange)
// Do something with each token 
}
  • 词性还原
import Foundation  
let tagger = NSLinguisticTagger(tagSchemes:[.lemma], options: 0)  
let text = "Great hikes make great pics! Wonderful afternoon in Marin County."  
tagger.string = text  
let range = NSRange(location:0, length: text.utf16.count)  
let options: NSLinguisticTagger.Options = [.omitPunctuation, .omitWhitespace]  
tagger.enumerateTags(in: range, unit: .word, scheme: .lemma, options: options) {  
tag, tokenRange, stop in  
    if let lemma = tag?.rawValue {
        // Do something with each lemma
    } 
}
  • 实体识别
import Foundation  
let tagger = NSLinguisticTagger(tagSchemes: [.nameType], options: 0)  
let text = "Tim Cook is the CEO of Apple Inc. which is located in Cupertino, California" tagger.string = text  
let range = NSRange(location:0, length: text.utf16.count)  
let options: NSLinguisticTagger.Options = [.omitPunctuation, .omitWhitespace, .joinNames] let tags: [NSLinguisticTag] = [.personalName, .placeName, .organizationName]  
tagger.enumerateTags(in: range, unit: .word, scheme: .nameType, options: options) {  
tag, tokenRange, stop in  
    if let tag = tag, tags.contains(tag) {
        let name = (text as NSString).substring(with: tokenRange)
    } 
}

总结

这个 Session 主要介绍了新自然语言处理 API 的优化,新 API 提供了更多模式的支持,在原有 API 的基础上速度更快、性能更高、支持语种更多。借助这套 API 可以使用最小的成本使你的 iOS 应用具备自然语言处理的能力。使你的应用拥有更丰富的功能和更好的交互体验。

参考链接

WWDC session

https://developer.apple.com/videos/play/wwdc2017/208/

Apple documents

https://developer.apple.com/documentation/foundation/nslinguistictagger

Tutorial

http://nshipster.com/nslinguistictagger/
https://objccn.io/issue-7-6/

    原文作者:字节跳动技术博客
    原文地址: https://techblog.toutiao.com/2017/07/05/session208/
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞