{"id":6208,"date":"2024-07-02T10:23:39","date_gmt":"2024-07-02T02:23:39","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6208"},"modified":"2024-07-02T10:23:39","modified_gmt":"2024-07-02T02:23:39","slug":"ios-nlp-ner-%e4%bb%bb%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6208","title":{"rendered":"iOS NLP NER \u4efb\u52a1"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 iOS \u4e2d\u5b9e\u73b0\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u4e2d\u7684\u547d\u540d\u5b9e\u4f53\u8bc6\u522b\uff08NER\uff09\u4efb\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u4f7f\u7528 Core ML \u4e0e\u81ea\u5b9a\u4e49\u6a21\u578b<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Core ML \u662f\u82f9\u679c\u63d0\u4f9b\u7684\u673a\u5668\u5b66\u4e60\u6846\u67b6\uff0c\u652f\u6301\u5728 iOS \u5e94\u7528\u4e2d\u96c6\u6210\u81ea\u5b9a\u4e49\u673a\u5668\u5b66\u4e60\u6a21\u578b\u3002\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8bad\u7ec3\u6a21\u578b<\/strong>\uff1a\u5728 Python \u4e2d\u4f7f\u7528\u5e93\u5982 SpaCy\u3001NLTK \u6216 Transformers \u6765\u8bad\u7ec3\u4e00\u4e2a NER \u6a21\u578b\u3002<\/li>\n\n\n\n<li><strong>\u8f6c\u6362\u6a21\u578b<\/strong>\uff1a\u4f7f\u7528 Core ML Tools \u5c06\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u8f6c\u6362\u4e3a Core ML \u683c\u5f0f\uff08<code>.mlmodel<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u96c6\u6210\u6a21\u578b<\/strong>\uff1a\u5c06\u8f6c\u6362\u540e\u7684\u6a21\u578b\u5bfc\u5165\u5230 Xcode \u9879\u76ee\u4e2d\uff0c\u5e76\u4f7f\u7528 Core ML \u7684 API \u8fdb\u884c\u63a8\u7406\u3002<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import CoreML\n\n\/\/ \u52a0\u8f7d\u6a21\u578b\nlet model = try! NERModel(configuration: .init())\n\n\/\/ \u8f93\u5165\u6587\u672c\nlet input = NERModelInput(text: \"John Doe lives in New York\")\n\n\/\/ \u83b7\u53d6\u9884\u6d4b\u7ed3\u679c\nlet output = try! model.prediction(input: input)\nprint(output.entities)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u4f7f\u7528 NLP \u6846\u67b6<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u82f9\u679c\u5728 iOS 13 \u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\u63d0\u4f9b\u4e86\u65b0\u7684 Natural Language \u6846\u67b6\uff0c\u53ef\u4ee5\u7528\u4e8e\u7b80\u5355\u7684 NLP \u4efb\u52a1\u3002\u867d\u7136\u8be5\u6846\u67b6\u5185\u7f6e\u7684 NER \u529f\u80fd\u6709\u9650\uff0c\u4f46\u53ef\u4ee5\u7528\u4f5c\u7b80\u5355\u4efb\u52a1\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import NaturalLanguage\n\nlet text = \"John Doe lives in New York.\"\nlet tagger = NLTagger(tagSchemes: &#91;.nameType])\ntagger.string = text\n\nlet options: NLTagger.Options = &#91;.omitPunctuation, .omitWhitespace, .joinNames]\nlet tags: &#91;NLTag] = &#91;.personalName, .placeName, .organizationName]\n\ntagger.enumerateTags(in: text.startIndex..&lt;text.endIndex, unit: .word, scheme: .nameType, options: options) { tag, tokenRange in\n    if let tag = tag, tags.contains(tag) {\n        print(\"\\(text&#91;tokenRange]): \\(tag.rawValue)\")\n    }\n    return true\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728 iOS \u4e0a\u5b9e\u73b0 NER\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 TensorFlow Lite \u6216 ONNX Runtime \u53ef\u4ee5\u52a0\u8f7d\u5e76\u8fd0\u884c\u9884\u8bad\u7ec3\u7684 NER \u6a21\u578b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528 TensorFlow Lite\uff1a<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8f6c\u6362\u6a21\u578b<\/strong>\uff1a\u5c06\u9884\u8bad\u7ec3\u7684\u6a21\u578b\u8f6c\u6362\u4e3a TensorFlow Lite \u683c\u5f0f\uff08<code>.tflite<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u96c6\u6210 TensorFlow Lite<\/strong>\uff1a\u5728 iOS \u9879\u76ee\u4e2d\u96c6\u6210 TensorFlow Lite\u3002<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import TensorFlowLite\n\n\/\/ \u52a0\u8f7d\u6a21\u578b\nlet modelPath = Bundle.main.path(forResource: \"model\", ofType: \"tflite\")!\nlet interpreter = try Interpreter(modelPath: modelPath)\n\n\/\/ \u8f93\u5165\u6570\u636e\nlet input = \"John Doe lives in New York.\"\nlet inputTensor = try interpreter.input(at: 0)\n\n\/\/ \u8bbe\u7f6e\u8f93\u5165\u6570\u636e\nlet data = input.data(using: .utf8)!\ntry inputTensor.copy(data)\n\n\/\/ \u8fd0\u884c\u6a21\u578b\ntry interpreter.invoke()\n\n\/\/ \u83b7\u53d6\u8f93\u51fa\nlet outputTensor = try interpreter.output(at: 0)\nlet outputData = Data(outputTensor.data)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u4e91\u670d\u52a1 API<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528\u5916\u90e8\u7684 NLP \u670d\u52a1\u63d0\u4f9b\u7684 API\uff0c\u4f8b\u5982 Google Cloud NLP\u3001Microsoft Azure Text Analytics \u6216 IBM Watson\uff0c\u53ef\u4ee5\u907f\u514d\u5728\u8bbe\u5907\u4e0a\u8fd0\u884c\u590d\u6742\u7684\u6a21\u578b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import Foundation\n\nlet text = \"John Doe lives in New York.\"\nlet apiKey = \"YOUR_API_KEY\"\nlet url = URL(string: \"https:\/\/api.your-nlp-service.com\/ner\")!\n\nvar request = URLRequest(url: url)\nrequest.httpMethod = \"POST\"\nrequest.addValue(\"application\/json\", forHTTPHeaderField: \"Content-Type\")\nrequest.addValue(\"Bearer \\(apiKey)\", forHTTPHeaderField: \"Authorization\")\n\nlet body: &#91;String: Any] = &#91;\"text\": text]\nrequest.httpBody = try? JSONSerialization.data(withJSONObject: body)\n\nlet task = URLSession.shared.dataTask(with: request) { data, response, error in\n    if let data = data {\n        let jsonResponse = try? JSONSerialization.jsonObject(with: data, options: &#91;])\n        print(jsonResponse)\n    }\n}\ntask.resume()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u9700\u6c42\u548c\u8d44\u6e90\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u6765\u5728 iOS \u4e2d\u5b9e\u73b0 NER \u4efb\u52a1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 iOS \u4e2d\u5b9e\u73b0\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u4e2d\u7684\u547d\u540d\u5b9e\u4f53\u8bc6\u522b\uff08NER\uff09\u4efb\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a 1. \u4f7f\u7528 C&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6208\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[66,98,97],"class_list":["post-6208","post","type-post","status-publish","format-standard","hentry","category-5","tag-ios","tag-ner","tag-nlp"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6208"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6208\/revisions"}],"predecessor-version":[{"id":6209,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6208\/revisions\/6209"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}