Xamarin和NSLinguisticTagger - 错误或使用不正确?

时间:2017-09-03 08:24:27

标签: c# xamarin xamarin.ios nslinguistictagger

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

  • 单页应用
  • 2个UITextViews,一个名为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类型的质量,但这些是最简单的句子而且它们无可救药地关闭,除非我做错了。

输入被检测为副词甚至是其他词,但不能正确地作为动词。 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真的很糟糕吗?我做错了吗?

0 个答案:

没有答案