2

我正在尝试将句法解析集成到在 Visual Studio 2017 上用 Xamarin.iOS (4.03) 编写的 iOS 应用程序中。为了让我的脚湿透,我创建了最简单的应用程序。

  • 单页应用
  • 2个UITextView,一个叫SentenceInput,一个叫SentenceOutput
  • 带有关联 TouchUpInside() 方法的单个按钮执行快速而肮脏的工作。

问题:

词法标记识别全部关闭!

一些示例短语和结果(** 突出显示是我的这篇文章)

SentenceInput   | SentenceOutput
----------------|-----------------------------------
enter text here | Analysed text is 'enter text here'
                | **Adverb: enter**,here
                | Noun: text
----------------|-----------------------------------
draw a circle   | Analysed text is 'draw a circle'
                | Verb: draw
                | Determiner: a
                | Noun: circle
----------------|-----------------------------------
enter           | Analysed text is 'enter'
                | **OtherWord: enter**
----------------|-----------------------------------
enter text      | Analysed text is 'enter text'
                | **Adverb: enter**
                | Noun: text
----------------|-----------------------------------
draw circle     | Analysed text is 'draw circle'
                | **Adjective: draw**
                | Noun: circle
----------------|-----------------------------------
enter first door| Analysed text is 'enter first door on the right'
on the right    | **Adverb: enter**
                | Adjective: first,right
                | Noun: door
                | Preposition: on
                | Determiner: the

我没想到 ML 类型的质量,但这些是最简单的句子,它们是无可救药的,除非我做错了什么。

enter被检测为副词甚至其他词,但不正确地作为动词。draw一旦被正确检测为动词,然后突然变成形容词!

我什至没有约会过任何更复杂的东西 - 除了通常的“快速棕色狐狸”,有趣的是它没问题!

这是简单的基本代码

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using UIKit;
using Foundation;

namespace App
{
    public partial class ViewController : UIViewController
    {
        readonly List<Tuple<string, string>> words = new List<Tuple<string, string>>();

        public ViewController(IntPtr handle) : base(handle)
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            // Perform any additional setup after loading the view, typically from a nib.
        }

        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
            // Release any cached data, images, etc that aren't in use.
        }

        partial void UIButton219_TouchUpInside(UIButton sender)
        {
            words.Clear();

            var schemes = NSLinguisticTagger.GetAvailableTagSchemesForLanguage("en");

            var options = NSLinguisticTaggerOptions.OmitPunctuation | NSLinguisticTaggerOptions.OmitWhitespace;
            var tagger = new NSLinguisticTagger(schemes, options);

            var range = new NSRange(0, SentenceInput.Text.Length);

            tagger.AnalysisString = SentenceInput.Text;

            tagger.EnumerateTagsInRange(range, NSLinguisticTag.SchemeLexicalClass, options, TaggerEnumerator);

            var items = from word in words group word.Item2 by word.Item1 into g select new {Tag = g.Key, Words = g.ToList()};

            SentenceOutput.Text = $"Analysed text is '{SentenceInput.Text}'\n";
            foreach (var item in items)
            {
                var results = $"{item.Tag}: {string.Join(",", item.Words)}\n";
                Console.WriteLine(results);
                SentenceOutput.Text += results;
            }

        }

        private void TaggerEnumerator(NSString tag, NSRange tokenRange, NSRange sentenceRange, ref bool stop)
        {
            var word = SentenceInput.Text.Substring((int)tokenRange.Location, (int)tokenRange.Length);
            words.Add(new Tuple<string, string>(tag, word));
        }
    }
}

正如你所看到的,它对于这个演示来说非常简单。没有尝试优化或重构。

NSLinguisticTagger 真的这么糟糕吗?我做错了吗?

4

0 回答 0