Filter out Specified Token Depending on Tag Type










0















I am trying to filter out specific tokens based on their tags. When I run my code I get this as the output. I want to only retrieve the adjectives and have that outputted. Is there an easy way to do this?



Hello: NSLinguisticTag(_rawValue: Interjection)
World: NSLinguisticTag(_rawValue: Noun)
this: NSLinguisticTag(_rawValue: Determiner)
is: NSLinguisticTag(_rawValue: Verb)
my: NSLinguisticTag(_rawValue: Determiner)
main: NSLinguisticTag(_rawValue: Adjective)
goal: NSLinguisticTag(_rawValue: Noun)


tokenizeText(inputtedText: "Hello World this is my main goal, to take these words and figure out the adjectives, verbs and nouns")










share|improve this question




























    0















    I am trying to filter out specific tokens based on their tags. When I run my code I get this as the output. I want to only retrieve the adjectives and have that outputted. Is there an easy way to do this?



    Hello: NSLinguisticTag(_rawValue: Interjection)
    World: NSLinguisticTag(_rawValue: Noun)
    this: NSLinguisticTag(_rawValue: Determiner)
    is: NSLinguisticTag(_rawValue: Verb)
    my: NSLinguisticTag(_rawValue: Determiner)
    main: NSLinguisticTag(_rawValue: Adjective)
    goal: NSLinguisticTag(_rawValue: Noun)


    tokenizeText(inputtedText: "Hello World this is my main goal, to take these words and figure out the adjectives, verbs and nouns")










    share|improve this question


























      0












      0








      0








      I am trying to filter out specific tokens based on their tags. When I run my code I get this as the output. I want to only retrieve the adjectives and have that outputted. Is there an easy way to do this?



      Hello: NSLinguisticTag(_rawValue: Interjection)
      World: NSLinguisticTag(_rawValue: Noun)
      this: NSLinguisticTag(_rawValue: Determiner)
      is: NSLinguisticTag(_rawValue: Verb)
      my: NSLinguisticTag(_rawValue: Determiner)
      main: NSLinguisticTag(_rawValue: Adjective)
      goal: NSLinguisticTag(_rawValue: Noun)


      tokenizeText(inputtedText: "Hello World this is my main goal, to take these words and figure out the adjectives, verbs and nouns")










      share|improve this question
















      I am trying to filter out specific tokens based on their tags. When I run my code I get this as the output. I want to only retrieve the adjectives and have that outputted. Is there an easy way to do this?



      Hello: NSLinguisticTag(_rawValue: Interjection)
      World: NSLinguisticTag(_rawValue: Noun)
      this: NSLinguisticTag(_rawValue: Determiner)
      is: NSLinguisticTag(_rawValue: Verb)
      my: NSLinguisticTag(_rawValue: Determiner)
      main: NSLinguisticTag(_rawValue: Adjective)
      goal: NSLinguisticTag(_rawValue: Noun)


      tokenizeText(inputtedText: "Hello World this is my main goal, to take these words and figure out the adjectives, verbs and nouns")







      ios swift macos swift4 nslinguistictagger






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 10:38









      joern

      21.2k66683




      21.2k66683










      asked Nov 14 '18 at 8:43









      JazzinJazzin

      83




      83






















          1 Answer
          1






          active

          oldest

          votes


















          0














          You can simply check if a tag is of type .adjective in the enumerateTags closure and only continue if it is:



          let sentence = "The yellow cat hunts the little gray mouse around the block"
          let options: NSLinguisticTagger.Options = [.omitWhitespace, .omitPunctuation, .joinNames]
          let schemes = NSLinguisticTagger.availableTagSchemes(forLanguage: "en")
          let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))
          tagger.string = sentence
          tagger.enumerateTags(in: NSRange(location: 0, length: sentence.count), scheme: .nameTypeOrLexicalClass, options: options) (tag, tokenRange, _, _) in
          guard tag == .adjective, let adjectiveRange = Range(tokenRange, in: sentence) else return
          let adjectiveToken = sentence[adjectiveRange]
          print(adjectiveToken)



          This prints out:




          yellow

          little

          gray




          EDIT



          If you want the tokens of more than one tag type you could store the tokens in a dictionary with the tags as keys:



          let sentence = "The yellow cat hunts the little gray mouse around the block"
          let options: NSLinguisticTagger.Options = [.omitWhitespace, .omitPunctuation, .joinNames]
          let schemes = NSLinguisticTagger.availableTagSchemes(forLanguage: "en")
          let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))
          tagger.string = sentence
          var tokens: [NSLinguisticTag: [String]] = [:]
          tagger.enumerateTags(in: NSRange(location: 0, length: sentence.count), scheme: .nameTypeOrLexicalClass, options: options) (tag, tokenRange, _, _) in
          guard let tag = tag, let range = Range(tokenRange, in: sentence) else return
          let token = String(sentence[range])
          if tokens[tag] != nil
          tokens[tag]!.append(token)
          else
          tokens[tag] = [token]


          print(tokens[.adjective])
          print(tokens[.noun])


          Which prints out:




          Optional(["yellow", "little", "gray"])

          Optional(["cat", "mouse",
          "block"])




          EDIT#2



          If you want to be able to remove certain tags from a text you could write an extension like this:



          extension NSLinguisticTagger 
          func eliminate(unwantedTags: [NSLinguisticTag], from text: String, options: NSLinguisticTagger.Options) -> String
          string = text
          var textWithoutUnwantedTags = ""
          enumerateTags(in: NSRange(location: 0, length: text.utf16.count), scheme: .nameTypeOrLexicalClass, options: options) (tag, tokenRange, _, _) in
          guard
          let tag = tag,
          !unwantedTags.contains(tag),
          let range = Range(tokenRange, in: text)
          else return
          let token = String(text[range])
          textWithoutUnwantedTags += " (token)"


          return textWithoutUnwantedTags.trimmingCharacters(in: .whitespaces)




          Then you can use it like this:



          let sentence = "The yellow cat hunts the little gray mouse around the block"
          let options: NSLinguisticTagger.Options = [.omitWhitespace, .omitPunctuation, .joinNames]
          let schemes = NSLinguisticTagger.availableTagSchemes(forLanguage: "en")
          let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))

          let sentenceWithoutAdjectives = tagger.eliminate(unwantedTags: [.adjective], from: sentence, options: options)
          print(sentenceWithoutAdjectives)


          Which prints out:




          The cat hunts the mouse around the block







          share|improve this answer

























          • So what happens if I want to add nouns verbs etc? Isn’t there a way to add the text into a map and pull out the text accordingly? Or would I just have to do that for all tags I want to pull out ?

            – Jazzin
            Nov 14 '18 at 11:46











          • Unfortunatly you cannot apply a filter or map directly to the tags because of the block based implementation of NSLingusticTagger. But you could enumerate once and store the tokens according to the tags. Please have a look at the edited answer for an example.

            – joern
            Nov 14 '18 at 12:42











          • Apologies for all the questions. Basically my goal is to print out the sentence Eliminating certain token associated with tags. So hypothetically with the sentence you had, if I wanted to take out the adjectives, the sentence would print everything but yellow little gray.

            – Jazzin
            Nov 14 '18 at 16:08











          • No problem. Have a look at my second edit ;-)

            – joern
            Nov 15 '18 at 8:04











          • That’s exactly it. If I want to exclude nouns too, can I pass nouns in under the unwanted tag parameter?

            – Jazzin
            Nov 15 '18 at 8:39










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296067%2ffilter-out-specified-token-depending-on-tag-type%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You can simply check if a tag is of type .adjective in the enumerateTags closure and only continue if it is:



          let sentence = "The yellow cat hunts the little gray mouse around the block"
          let options: NSLinguisticTagger.Options = [.omitWhitespace, .omitPunctuation, .joinNames]
          let schemes = NSLinguisticTagger.availableTagSchemes(forLanguage: "en")
          let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))
          tagger.string = sentence
          tagger.enumerateTags(in: NSRange(location: 0, length: sentence.count), scheme: .nameTypeOrLexicalClass, options: options) (tag, tokenRange, _, _) in
          guard tag == .adjective, let adjectiveRange = Range(tokenRange, in: sentence) else return
          let adjectiveToken = sentence[adjectiveRange]
          print(adjectiveToken)



          This prints out:




          yellow

          little

          gray




          EDIT



          If you want the tokens of more than one tag type you could store the tokens in a dictionary with the tags as keys:



          let sentence = "The yellow cat hunts the little gray mouse around the block"
          let options: NSLinguisticTagger.Options = [.omitWhitespace, .omitPunctuation, .joinNames]
          let schemes = NSLinguisticTagger.availableTagSchemes(forLanguage: "en")
          let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))
          tagger.string = sentence
          var tokens: [NSLinguisticTag: [String]] = [:]
          tagger.enumerateTags(in: NSRange(location: 0, length: sentence.count), scheme: .nameTypeOrLexicalClass, options: options) (tag, tokenRange, _, _) in
          guard let tag = tag, let range = Range(tokenRange, in: sentence) else return
          let token = String(sentence[range])
          if tokens[tag] != nil
          tokens[tag]!.append(token)
          else
          tokens[tag] = [token]


          print(tokens[.adjective])
          print(tokens[.noun])


          Which prints out:




          Optional(["yellow", "little", "gray"])

          Optional(["cat", "mouse",
          "block"])




          EDIT#2



          If you want to be able to remove certain tags from a text you could write an extension like this:



          extension NSLinguisticTagger 
          func eliminate(unwantedTags: [NSLinguisticTag], from text: String, options: NSLinguisticTagger.Options) -> String
          string = text
          var textWithoutUnwantedTags = ""
          enumerateTags(in: NSRange(location: 0, length: text.utf16.count), scheme: .nameTypeOrLexicalClass, options: options) (tag, tokenRange, _, _) in
          guard
          let tag = tag,
          !unwantedTags.contains(tag),
          let range = Range(tokenRange, in: text)
          else return
          let token = String(text[range])
          textWithoutUnwantedTags += " (token)"


          return textWithoutUnwantedTags.trimmingCharacters(in: .whitespaces)




          Then you can use it like this:



          let sentence = "The yellow cat hunts the little gray mouse around the block"
          let options: NSLinguisticTagger.Options = [.omitWhitespace, .omitPunctuation, .joinNames]
          let schemes = NSLinguisticTagger.availableTagSchemes(forLanguage: "en")
          let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))

          let sentenceWithoutAdjectives = tagger.eliminate(unwantedTags: [.adjective], from: sentence, options: options)
          print(sentenceWithoutAdjectives)


          Which prints out:




          The cat hunts the mouse around the block







          share|improve this answer

























          • So what happens if I want to add nouns verbs etc? Isn’t there a way to add the text into a map and pull out the text accordingly? Or would I just have to do that for all tags I want to pull out ?

            – Jazzin
            Nov 14 '18 at 11:46











          • Unfortunatly you cannot apply a filter or map directly to the tags because of the block based implementation of NSLingusticTagger. But you could enumerate once and store the tokens according to the tags. Please have a look at the edited answer for an example.

            – joern
            Nov 14 '18 at 12:42











          • Apologies for all the questions. Basically my goal is to print out the sentence Eliminating certain token associated with tags. So hypothetically with the sentence you had, if I wanted to take out the adjectives, the sentence would print everything but yellow little gray.

            – Jazzin
            Nov 14 '18 at 16:08











          • No problem. Have a look at my second edit ;-)

            – joern
            Nov 15 '18 at 8:04











          • That’s exactly it. If I want to exclude nouns too, can I pass nouns in under the unwanted tag parameter?

            – Jazzin
            Nov 15 '18 at 8:39















          0














          You can simply check if a tag is of type .adjective in the enumerateTags closure and only continue if it is:



          let sentence = "The yellow cat hunts the little gray mouse around the block"
          let options: NSLinguisticTagger.Options = [.omitWhitespace, .omitPunctuation, .joinNames]
          let schemes = NSLinguisticTagger.availableTagSchemes(forLanguage: "en")
          let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))
          tagger.string = sentence
          tagger.enumerateTags(in: NSRange(location: 0, length: sentence.count), scheme: .nameTypeOrLexicalClass, options: options) (tag, tokenRange, _, _) in
          guard tag == .adjective, let adjectiveRange = Range(tokenRange, in: sentence) else return
          let adjectiveToken = sentence[adjectiveRange]
          print(adjectiveToken)



          This prints out:




          yellow

          little

          gray




          EDIT



          If you want the tokens of more than one tag type you could store the tokens in a dictionary with the tags as keys:



          let sentence = "The yellow cat hunts the little gray mouse around the block"
          let options: NSLinguisticTagger.Options = [.omitWhitespace, .omitPunctuation, .joinNames]
          let schemes = NSLinguisticTagger.availableTagSchemes(forLanguage: "en")
          let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))
          tagger.string = sentence
          var tokens: [NSLinguisticTag: [String]] = [:]
          tagger.enumerateTags(in: NSRange(location: 0, length: sentence.count), scheme: .nameTypeOrLexicalClass, options: options) (tag, tokenRange, _, _) in
          guard let tag = tag, let range = Range(tokenRange, in: sentence) else return
          let token = String(sentence[range])
          if tokens[tag] != nil
          tokens[tag]!.append(token)
          else
          tokens[tag] = [token]


          print(tokens[.adjective])
          print(tokens[.noun])


          Which prints out:




          Optional(["yellow", "little", "gray"])

          Optional(["cat", "mouse",
          "block"])




          EDIT#2



          If you want to be able to remove certain tags from a text you could write an extension like this:



          extension NSLinguisticTagger 
          func eliminate(unwantedTags: [NSLinguisticTag], from text: String, options: NSLinguisticTagger.Options) -> String
          string = text
          var textWithoutUnwantedTags = ""
          enumerateTags(in: NSRange(location: 0, length: text.utf16.count), scheme: .nameTypeOrLexicalClass, options: options) (tag, tokenRange, _, _) in
          guard
          let tag = tag,
          !unwantedTags.contains(tag),
          let range = Range(tokenRange, in: text)
          else return
          let token = String(text[range])
          textWithoutUnwantedTags += " (token)"


          return textWithoutUnwantedTags.trimmingCharacters(in: .whitespaces)




          Then you can use it like this:



          let sentence = "The yellow cat hunts the little gray mouse around the block"
          let options: NSLinguisticTagger.Options = [.omitWhitespace, .omitPunctuation, .joinNames]
          let schemes = NSLinguisticTagger.availableTagSchemes(forLanguage: "en")
          let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))

          let sentenceWithoutAdjectives = tagger.eliminate(unwantedTags: [.adjective], from: sentence, options: options)
          print(sentenceWithoutAdjectives)


          Which prints out:




          The cat hunts the mouse around the block







          share|improve this answer

























          • So what happens if I want to add nouns verbs etc? Isn’t there a way to add the text into a map and pull out the text accordingly? Or would I just have to do that for all tags I want to pull out ?

            – Jazzin
            Nov 14 '18 at 11:46











          • Unfortunatly you cannot apply a filter or map directly to the tags because of the block based implementation of NSLingusticTagger. But you could enumerate once and store the tokens according to the tags. Please have a look at the edited answer for an example.

            – joern
            Nov 14 '18 at 12:42











          • Apologies for all the questions. Basically my goal is to print out the sentence Eliminating certain token associated with tags. So hypothetically with the sentence you had, if I wanted to take out the adjectives, the sentence would print everything but yellow little gray.

            – Jazzin
            Nov 14 '18 at 16:08











          • No problem. Have a look at my second edit ;-)

            – joern
            Nov 15 '18 at 8:04











          • That’s exactly it. If I want to exclude nouns too, can I pass nouns in under the unwanted tag parameter?

            – Jazzin
            Nov 15 '18 at 8:39













          0












          0








          0







          You can simply check if a tag is of type .adjective in the enumerateTags closure and only continue if it is:



          let sentence = "The yellow cat hunts the little gray mouse around the block"
          let options: NSLinguisticTagger.Options = [.omitWhitespace, .omitPunctuation, .joinNames]
          let schemes = NSLinguisticTagger.availableTagSchemes(forLanguage: "en")
          let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))
          tagger.string = sentence
          tagger.enumerateTags(in: NSRange(location: 0, length: sentence.count), scheme: .nameTypeOrLexicalClass, options: options) (tag, tokenRange, _, _) in
          guard tag == .adjective, let adjectiveRange = Range(tokenRange, in: sentence) else return
          let adjectiveToken = sentence[adjectiveRange]
          print(adjectiveToken)



          This prints out:




          yellow

          little

          gray




          EDIT



          If you want the tokens of more than one tag type you could store the tokens in a dictionary with the tags as keys:



          let sentence = "The yellow cat hunts the little gray mouse around the block"
          let options: NSLinguisticTagger.Options = [.omitWhitespace, .omitPunctuation, .joinNames]
          let schemes = NSLinguisticTagger.availableTagSchemes(forLanguage: "en")
          let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))
          tagger.string = sentence
          var tokens: [NSLinguisticTag: [String]] = [:]
          tagger.enumerateTags(in: NSRange(location: 0, length: sentence.count), scheme: .nameTypeOrLexicalClass, options: options) (tag, tokenRange, _, _) in
          guard let tag = tag, let range = Range(tokenRange, in: sentence) else return
          let token = String(sentence[range])
          if tokens[tag] != nil
          tokens[tag]!.append(token)
          else
          tokens[tag] = [token]


          print(tokens[.adjective])
          print(tokens[.noun])


          Which prints out:




          Optional(["yellow", "little", "gray"])

          Optional(["cat", "mouse",
          "block"])




          EDIT#2



          If you want to be able to remove certain tags from a text you could write an extension like this:



          extension NSLinguisticTagger 
          func eliminate(unwantedTags: [NSLinguisticTag], from text: String, options: NSLinguisticTagger.Options) -> String
          string = text
          var textWithoutUnwantedTags = ""
          enumerateTags(in: NSRange(location: 0, length: text.utf16.count), scheme: .nameTypeOrLexicalClass, options: options) (tag, tokenRange, _, _) in
          guard
          let tag = tag,
          !unwantedTags.contains(tag),
          let range = Range(tokenRange, in: text)
          else return
          let token = String(text[range])
          textWithoutUnwantedTags += " (token)"


          return textWithoutUnwantedTags.trimmingCharacters(in: .whitespaces)




          Then you can use it like this:



          let sentence = "The yellow cat hunts the little gray mouse around the block"
          let options: NSLinguisticTagger.Options = [.omitWhitespace, .omitPunctuation, .joinNames]
          let schemes = NSLinguisticTagger.availableTagSchemes(forLanguage: "en")
          let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))

          let sentenceWithoutAdjectives = tagger.eliminate(unwantedTags: [.adjective], from: sentence, options: options)
          print(sentenceWithoutAdjectives)


          Which prints out:




          The cat hunts the mouse around the block







          share|improve this answer















          You can simply check if a tag is of type .adjective in the enumerateTags closure and only continue if it is:



          let sentence = "The yellow cat hunts the little gray mouse around the block"
          let options: NSLinguisticTagger.Options = [.omitWhitespace, .omitPunctuation, .joinNames]
          let schemes = NSLinguisticTagger.availableTagSchemes(forLanguage: "en")
          let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))
          tagger.string = sentence
          tagger.enumerateTags(in: NSRange(location: 0, length: sentence.count), scheme: .nameTypeOrLexicalClass, options: options) (tag, tokenRange, _, _) in
          guard tag == .adjective, let adjectiveRange = Range(tokenRange, in: sentence) else return
          let adjectiveToken = sentence[adjectiveRange]
          print(adjectiveToken)



          This prints out:




          yellow

          little

          gray




          EDIT



          If you want the tokens of more than one tag type you could store the tokens in a dictionary with the tags as keys:



          let sentence = "The yellow cat hunts the little gray mouse around the block"
          let options: NSLinguisticTagger.Options = [.omitWhitespace, .omitPunctuation, .joinNames]
          let schemes = NSLinguisticTagger.availableTagSchemes(forLanguage: "en")
          let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))
          tagger.string = sentence
          var tokens: [NSLinguisticTag: [String]] = [:]
          tagger.enumerateTags(in: NSRange(location: 0, length: sentence.count), scheme: .nameTypeOrLexicalClass, options: options) (tag, tokenRange, _, _) in
          guard let tag = tag, let range = Range(tokenRange, in: sentence) else return
          let token = String(sentence[range])
          if tokens[tag] != nil
          tokens[tag]!.append(token)
          else
          tokens[tag] = [token]


          print(tokens[.adjective])
          print(tokens[.noun])


          Which prints out:




          Optional(["yellow", "little", "gray"])

          Optional(["cat", "mouse",
          "block"])




          EDIT#2



          If you want to be able to remove certain tags from a text you could write an extension like this:



          extension NSLinguisticTagger 
          func eliminate(unwantedTags: [NSLinguisticTag], from text: String, options: NSLinguisticTagger.Options) -> String
          string = text
          var textWithoutUnwantedTags = ""
          enumerateTags(in: NSRange(location: 0, length: text.utf16.count), scheme: .nameTypeOrLexicalClass, options: options) (tag, tokenRange, _, _) in
          guard
          let tag = tag,
          !unwantedTags.contains(tag),
          let range = Range(tokenRange, in: text)
          else return
          let token = String(text[range])
          textWithoutUnwantedTags += " (token)"


          return textWithoutUnwantedTags.trimmingCharacters(in: .whitespaces)




          Then you can use it like this:



          let sentence = "The yellow cat hunts the little gray mouse around the block"
          let options: NSLinguisticTagger.Options = [.omitWhitespace, .omitPunctuation, .joinNames]
          let schemes = NSLinguisticTagger.availableTagSchemes(forLanguage: "en")
          let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))

          let sentenceWithoutAdjectives = tagger.eliminate(unwantedTags: [.adjective], from: sentence, options: options)
          print(sentenceWithoutAdjectives)


          Which prints out:




          The cat hunts the mouse around the block








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 8:03

























          answered Nov 14 '18 at 10:38









          joernjoern

          21.2k66683




          21.2k66683












          • So what happens if I want to add nouns verbs etc? Isn’t there a way to add the text into a map and pull out the text accordingly? Or would I just have to do that for all tags I want to pull out ?

            – Jazzin
            Nov 14 '18 at 11:46











          • Unfortunatly you cannot apply a filter or map directly to the tags because of the block based implementation of NSLingusticTagger. But you could enumerate once and store the tokens according to the tags. Please have a look at the edited answer for an example.

            – joern
            Nov 14 '18 at 12:42











          • Apologies for all the questions. Basically my goal is to print out the sentence Eliminating certain token associated with tags. So hypothetically with the sentence you had, if I wanted to take out the adjectives, the sentence would print everything but yellow little gray.

            – Jazzin
            Nov 14 '18 at 16:08











          • No problem. Have a look at my second edit ;-)

            – joern
            Nov 15 '18 at 8:04











          • That’s exactly it. If I want to exclude nouns too, can I pass nouns in under the unwanted tag parameter?

            – Jazzin
            Nov 15 '18 at 8:39

















          • So what happens if I want to add nouns verbs etc? Isn’t there a way to add the text into a map and pull out the text accordingly? Or would I just have to do that for all tags I want to pull out ?

            – Jazzin
            Nov 14 '18 at 11:46











          • Unfortunatly you cannot apply a filter or map directly to the tags because of the block based implementation of NSLingusticTagger. But you could enumerate once and store the tokens according to the tags. Please have a look at the edited answer for an example.

            – joern
            Nov 14 '18 at 12:42











          • Apologies for all the questions. Basically my goal is to print out the sentence Eliminating certain token associated with tags. So hypothetically with the sentence you had, if I wanted to take out the adjectives, the sentence would print everything but yellow little gray.

            – Jazzin
            Nov 14 '18 at 16:08











          • No problem. Have a look at my second edit ;-)

            – joern
            Nov 15 '18 at 8:04











          • That’s exactly it. If I want to exclude nouns too, can I pass nouns in under the unwanted tag parameter?

            – Jazzin
            Nov 15 '18 at 8:39
















          So what happens if I want to add nouns verbs etc? Isn’t there a way to add the text into a map and pull out the text accordingly? Or would I just have to do that for all tags I want to pull out ?

          – Jazzin
          Nov 14 '18 at 11:46





          So what happens if I want to add nouns verbs etc? Isn’t there a way to add the text into a map and pull out the text accordingly? Or would I just have to do that for all tags I want to pull out ?

          – Jazzin
          Nov 14 '18 at 11:46













          Unfortunatly you cannot apply a filter or map directly to the tags because of the block based implementation of NSLingusticTagger. But you could enumerate once and store the tokens according to the tags. Please have a look at the edited answer for an example.

          – joern
          Nov 14 '18 at 12:42





          Unfortunatly you cannot apply a filter or map directly to the tags because of the block based implementation of NSLingusticTagger. But you could enumerate once and store the tokens according to the tags. Please have a look at the edited answer for an example.

          – joern
          Nov 14 '18 at 12:42













          Apologies for all the questions. Basically my goal is to print out the sentence Eliminating certain token associated with tags. So hypothetically with the sentence you had, if I wanted to take out the adjectives, the sentence would print everything but yellow little gray.

          – Jazzin
          Nov 14 '18 at 16:08





          Apologies for all the questions. Basically my goal is to print out the sentence Eliminating certain token associated with tags. So hypothetically with the sentence you had, if I wanted to take out the adjectives, the sentence would print everything but yellow little gray.

          – Jazzin
          Nov 14 '18 at 16:08













          No problem. Have a look at my second edit ;-)

          – joern
          Nov 15 '18 at 8:04





          No problem. Have a look at my second edit ;-)

          – joern
          Nov 15 '18 at 8:04













          That’s exactly it. If I want to exclude nouns too, can I pass nouns in under the unwanted tag parameter?

          – Jazzin
          Nov 15 '18 at 8:39





          That’s exactly it. If I want to exclude nouns too, can I pass nouns in under the unwanted tag parameter?

          – Jazzin
          Nov 15 '18 at 8:39



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296067%2ffilter-out-specified-token-depending-on-tag-type%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Syphilis

          Darth Vader #20