diff --git a/src/Foundation/Enums.cs b/src/Foundation/Enums.cs index e0ebad1150..0029f28212 100644 --- a/src/Foundation/Enums.cs +++ b/src/Foundation/Enums.cs @@ -125,4 +125,126 @@ namespace Foundation { Interactive = 2, Aggregate = 3, } + + public enum NSLinguisticTagScheme { + [Field ("NSLinguisticTagSchemeTokenType")] + Token, + + [Field ("NSLinguisticTagSchemeLexicalClass")] + LexicalClass, + + [Field ("NSLinguisticTagSchemeNameType")] + Name, + + [Field ("NSLinguisticTagSchemeNameTypeOrLexicalClass")] + NameOrLexicalClass, + + [Field ("NSLinguisticTagSchemeLemma")] + Lemma, + + [Field ("NSLinguisticTagSchemeLanguage")] + Language, + + [Field ("NSLinguisticTagSchemeScript")] + Script, + } + +#if !XAMCORE_4_0 + public enum NSLinguisticTagUnit { +#else + public enum NSLinguisticTag { +#endif + [Field ("NSLinguisticTagWord")] + Word, + + [Field ("NSLinguisticTagPunctuation")] + Punctuation, + + [Field ("NSLinguisticTagWhitespace")] + Whitespace, + + [Field ("NSLinguisticTagOther")] + Other, + + [Field ("NSLinguisticTagNoun")] + Noun, + + [Field ("NSLinguisticTagVerb")] + Verb, + + [Field ("NSLinguisticTagAdjective")] + Adjective, + + [Field ("NSLinguisticTagAdverb")] + Adverb, + + [Field ("NSLinguisticTagPronoun")] + Pronoun, + + [Field ("NSLinguisticTagDeterminer")] + Determiner, + + [Field ("NSLinguisticTagParticle")] + Particle, + + [Field ("NSLinguisticTagPreposition")] + Preposition, + + [Field ("NSLinguisticTagNumber")] + Number, + + [Field ("NSLinguisticTagConjunction")] + Conjunction, + + [Field ("NSLinguisticTagInterjection")] + Interjection, + + [Field ("NSLinguisticTagClassifier")] + Classifier, + + [Field ("NSLinguisticTagIdiom")] + Idiom, + + [Field ("NSLinguisticTagOtherWord")] + OtherWord, + + [Field ("NSLinguisticTagSentenceTerminator")] + Terminator, + + [Field ("NSLinguisticTagOpenQuote")] + OpenQuote, + + [Field ("NSLinguisticTagCloseQuote")] + CloseQuote, + + [Field ("NSLinguisticTagOpenParenthesis")] + OpenParenthesis, + + [Field ("NSLinguisticTagCloseParenthesis")] + CloseParenthesis, + + [Field ("NSLinguisticTagWordJoiner")] + WordJoiner, + + [Field ("NSLinguisticTagDash")] + Dash, + + [Field ("NSLinguisticTagOtherPunctuation")] + OtherPunctuation, + + [Field ("NSLinguisticTagParagraphBreak")] + ParagraphBreak, + + [Field ("NSLinguisticTagOtherWhitespace")] + OtherWhitespace, + + [Field ("NSLinguisticTagPersonalName")] + PersonalName, + + [Field ("NSLinguisticTagOrganizationName")] + OrganizationName, + + [Field ("NSLinguisticTagPlaceName")] + PlaceName, + } } diff --git a/src/foundation.cs b/src/foundation.cs index 0f0bec6de9..3c3298e0e3 100644 --- a/src/foundation.cs +++ b/src/foundation.cs @@ -4227,6 +4227,8 @@ namespace Foundation delegate void LinguisticTagEnumerator (string tag, NSRange tokenRange, bool stop); +#if !XAMCORE_4_0 + [Obsolete ("Use 'NSLinguisticTagUnit' enum instead.")] [Static] interface NSLinguisticTag { [Field ("NSLinguisticTagSchemeTokenType")] @@ -4343,7 +4345,8 @@ namespace Foundation [Field ("NSLinguisticTagOrganizationName")] NSString OrganizationName { get; } } - +#endif + [BaseType (typeof (NSObject))] // 'init' returns NIL so it's not usable evenif it does not throw an ObjC exception // funnily it was "added" in iOS 7 and header files says "do not invoke; not a valid initializer for this class" @@ -8194,6 +8197,35 @@ namespace Foundation } + delegate bool NSEnumerateLinguisticTagsEnumerator (NSString tag, NSRange tokenRange, NSRange sentenceRange, ref bool stop); + + [Category] + [BaseType (typeof(NSString))] + interface NSLinguisticAnalysis { +#if XAMCORE_4_0 + [return: BindAs (typeof (NSLinguisticTag []))] +#else + [return: BindAs (typeof (NSLinguisticTagUnit []))] +#endif + [EditorBrowsable (EditorBrowsableState.Advanced)] + [Export ("linguisticTagsInRange:scheme:options:orthography:tokenRanges:")] + NSString[] GetLinguisticTags (NSRange range, NSString scheme, NSLinguisticTaggerOptions options, [NullAllowed] NSOrthography orthography, [NullAllowed] out NSValue[] tokenRanges); + + [Wrap ("GetLinguisticTags (This, range, scheme.GetConstant (), options, orthography, out tokenRanges)")] +#if XAMCORE_4_0 + NSLinguisticTag[] GetLinguisticTags (NSRange range, NSLinguisticTagScheme scheme, NSLinguisticTaggerOptions options, [NullAllowed] NSOrthography orthography, [NullAllowed] out NSValue[] tokenRanges); +#else + NSLinguisticTagUnit[] GetLinguisticTags (NSRange range, NSLinguisticTagScheme scheme, NSLinguisticTaggerOptions options, [NullAllowed] NSOrthography orthography, [NullAllowed] out NSValue[] tokenRanges); +#endif + + [EditorBrowsable (EditorBrowsableState.Advanced)] + [Export ("enumerateLinguisticTagsInRange:scheme:options:orthography:usingBlock:")] + void EnumerateLinguisticTags (NSRange range, NSString scheme, NSLinguisticTaggerOptions options, [NullAllowed] NSOrthography orthography, NSEnumerateLinguisticTagsEnumerator handler); + + [Wrap ("EnumerateLinguisticTags (This, range, scheme.GetConstant (), options, orthography, handler)")] + void EnumerateLinguisticTags (NSRange range, NSLinguisticTagScheme scheme, NSLinguisticTaggerOptions options, [NullAllowed] NSOrthography orthography, NSEnumerateLinguisticTagsEnumerator handler); + } + // // We expose NSString versions of these methods because it could // avoid an extra lookup in cases where there is a large volume of diff --git a/tests/monotouch-test/Foundation/NSLinguisticAnalysisTest.cs b/tests/monotouch-test/Foundation/NSLinguisticAnalysisTest.cs new file mode 100644 index 0000000000..30356c97fa --- /dev/null +++ b/tests/monotouch-test/Foundation/NSLinguisticAnalysisTest.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +#if XAMCORE_2_0 +using Foundation; +using ObjCRuntime; +using Security; +#else +using MonoTouch.Foundation; +using MonoTouch.ObjCRuntime; +using MonoTouch.Security; +#endif +using NUnit.Framework; + +namespace monotouchtest +{ + [TestFixture] + [Preserve (AllMembers = true)] + public class NSLinguisticAnalysisTest + { + List words; + + [SetUp] + public void SetUp () + { + words = new List (); + } + + public bool Enumerator (NSString tag, NSRange tokenRange, NSRange sentenceRange, ref bool stop) + { + words.Add (tag); + stop = false; + return true; + } + + public bool StopEnumerator (NSString tag, NSRange tokenRange, NSRange sentenceRange, ref bool stop) + { + words.Add (tag); + stop = true; + return true; + } + + [Test] + public void EnumerateSubstringsInRangeTest () + { + var testString = new NSString ("Hello Hola Bonjour!"); + var range = new NSRange (0, testString.Length - 1); + testString.EnumerateLinguisticTags (range, NSLinguisticTagScheme.Token, NSLinguisticTaggerOptions.OmitWhitespace, null, Enumerator); + Assert.AreEqual (3, words.Count, "Word count"); +#if XAMCORE_4_0 + Assert.True (words.Contains (NSLinguisticTag.Word.GetConstant ()), "Token type."); +#else + Assert.True (words.Contains (NSLinguisticTagUnit.Word.GetConstant ()), "Token type."); +#endif + } + + [Test] + public void StopEnumerateSubstringsInRangeTest () + { + var testString = new NSString ("Hello Hola Bonjour!"); + var range = new NSRange (0, testString.Length - 1); + testString.EnumerateLinguisticTags (range, NSLinguisticTagScheme.Token, NSLinguisticTaggerOptions.OmitWhitespace, null, StopEnumerator); + Assert.AreEqual (1, words.Count, "Word count"); +#if XAMCORE_4_0 + Assert.True (words.Contains (NSLinguisticTag.Word.GetConstant ()), "Token type."); +#else + Assert.True (words.Contains (NSLinguisticTagUnit.Word.GetConstant ()), "Token type."); +#endif + } + + [Test] + public void GetLinguisticTagsTest () + { + var testString = new NSString ("Hello Hola Bonjour!"); + var range = new NSRange (0, testString.Length - 1); + NSValue[] tokenRanges; + var tags = testString.GetLinguisticTags (range, NSLinguisticTagScheme.NameOrLexicalClass, NSLinguisticTaggerOptions.OmitWhitespace, null, out tokenRanges); + Assert.AreEqual (3, tags.Length, "Tags Length"); + } + } +} \ No newline at end of file diff --git a/tests/xtro-sharpie/common-Foundation.ignore b/tests/xtro-sharpie/common-Foundation.ignore index 442c623dac..ab974b7a5e 100644 --- a/tests/xtro-sharpie/common-Foundation.ignore +++ b/tests/xtro-sharpie/common-Foundation.ignore @@ -9,10 +9,6 @@ ## untyped enums (only values, not type name) NSError.h !unknown-native-enum! NSUrlErrorCancelledReason bound -## NSLinguisticAnalysis category over NSString https://bugzilla.xamarin.com/show_bug.cgi?id=35009 -!missing-selector! NSString::linguisticTagsInRange:scheme:options:orthography:tokenRanges: not bound -!missing-selector! NSString::enumerateLinguisticTagsInRange:scheme:options:orthography:usingBlock: not bound - ## https://bugzilla.xamarin.com/show_bug.cgi?id=35021 !missing-selector! NSFileWrapper::initWithSerializedRepresentation: not bound