Skip to content

Instantly share code, notes, and snippets.

@prashantvc
Created December 19, 2013 13:31
Show Gist options
  • Save prashantvc/8039121 to your computer and use it in GitHub Desktop.
Save prashantvc/8039121 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace LinguisticTagger
{
// The UIApplicationDelegate for the application. This class is responsible for launching the
// User Interface of the application, as well as listening (and optionally responding) to
// application events from iOS.
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
// class-level declarations
UIWindow window;
readonly string statement = "A quick movement of the enemy will jeopardise six gunboats";
readonly List<Tuple<string, string>> words = new List<Tuple<string, string>> ();
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
// create a new window instance based on the screen size
window = new UIWindow (UIScreen.MainScreen.Bounds);
window.MakeKeyAndVisible ();
TagWords ();
return true;
}
void TagWords ()
{
var options = NSLinguisticTaggerOptions.JoinNames
| NSLinguisticTaggerOptions.OmitWhitespace;
var tagger = new NSLinguisticTagger (
NSLinguisticTagger.GetAvailableTagSchemesForLanguage ("en"),
options);
tagger.AnalysisString = statement;
tagger.EnumerateTagsInRange (new NSRange (0, statement.Length), NSLinguisticTag.SchemeLexicalClass, options, TaggerEnumerator);
PrintResult ();
}
void TaggerEnumerator (NSString tag, NSRange tokenRange, NSRange sentenceRange, ref bool stop)
{
var word = statement.Substring (tokenRange.Location, tokenRange.Length);
words.Add (new Tuple<string, string> (tag, word));
}
void PrintResult ()
{
var items = from word in words
group word.Item2 by word.Item1 into g
select new { Tag = g.Key, Words = g.ToList ()};
foreach (var item in items) {
Console.WriteLine ("{0}: {1}", item.Tag, string.Join (",", item.Words));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment