class_docstr: Greedy Averaged Perceptron tagger, as implemented by Matthew Honnibal.
  See more implementation details here:
  https://explosion.ai/blog/part-of-speech-pos-tagger-in-python
  
  >>> from nltk.tag.perceptron import PerceptronTagger
  
  Train the model
  
  >>> tagger = PerceptronTagger(load=False)
  
  >>> tagger.train([[('today','NN'),('is','VBZ'),('good','JJ'),('day','NN')],
  ... [('yes','NNS'),('it','PRP'),('beautiful','JJ')]])
  
  >>> tagger.tag(['today','is','a','beautiful','day'])
  [('today', 'NN'), ('is', 'PRP'), ('a', 'PRP'), ('beautiful', 'JJ'), ('day', 'NN')]
  
  Use the pretrain model (the default constructor)
  
  >>> pretrain = PerceptronTagger()
  
  >>> pretrain.tag('The quick brown fox jumps over the lazy dog'.split())
  [('The', 'DT'), ('quick', 'JJ'), ('brown', 'NN'), ('fox', 'NN'), ('jumps', 'VBZ'), ('over', 'IN'), ('the', 'DT'), ('lazy', 'JJ'), ('dog', 'NN')]
  
  >>> pretrain.tag("The red cat".split())
  [('The', 'DT'), ('red', 'JJ'), ('cat', 'NN')]
class_docstr_tok: ["Greedy","Averaged","Perceptron","tagger",",","as","implemented","by","Matthew","Honnibal",".","See","more","implementation","details","here",":","https",":","\/\/explosion.ai\/blog\/part-of-speech-pos-tagger-in-python",">",">",">","from","nltk.tag.perceptron","import","PerceptronTagger","Train","the","model",">",">",">","tagger","=","PerceptronTagger","(","load=False",")",">",">",">","tagger.train","(","[","[","(","'today","'",",","'NN","'",")",",","(","'is","'",",","'VBZ","'",")",",","(","'good","'",",","'JJ","'",")",",","(","'day","'",",","'NN","'",")","]",",","...","[","(","'yes","'",",","'NNS","'",")",",","(","'it","'",",","'PRP","'",")",",","(","'beautiful","'",",","'JJ","'",")","]","]",")",">",">",">","tagger.tag","(","[","'today","'",",","'is","'",",","'","a","'",",","'beautiful","'",",","'day","'","]",")","[","(","'today","'",",","'NN","'",")",",","(","'is","'",",","'PRP","'",")",",","(","'","a","'",",","'PRP","'",")",",","(","'beautiful","'",",","'JJ","'",")",",","(","'day","'",",","'NN","'",")","]","Use","the","pretrain","model","(","the","default","constructor",")",">",">",">","pretrain","=","PerceptronTagger","(",")",">",">",">","pretrain.tag","(","'The","quick","brown","fox","jumps","over","the","lazy","dog'.split","(",")",")","[","(","'The","'",",","'DT","'",")",",","(","'quick","'",",","'JJ","'",")",",","(","'brown","'",",","'NN","'",")",",","(","'fox","'",",","'NN","'",")",",","(","'jumps","'",",","'VBZ","'",")",",","(","'over","'",",","'IN","'",")",",","(","'the","'",",","'DT","'",")",",","(","'lazy","'",",","'JJ","'",")",",","(","'dog","'",",","'NN","'",")","]",">",">",">","pretrain.tag","(","``","The","red","cat","''",".split","(",")",")","[","(","'The","'",",","'DT","'",")",",","(","'red","'",",","'JJ","'",")",",","(","'cat","'",",","'NN","'",")","]"]
docstr: Train a model from sentences, and save it at ``save_loc``. ``nr_iter``
  controls the number of Perceptron training iterations.
  
  :param sentences: A list or iterator of sentences, where each sentence
      is a list of (words, tags) tuples.
  :param save_loc: If not ``None``, saves a pickled model in this location.
  :param nr_iter: Number of training iterations.
docstr_tok: ["Train","a","model","from","sentences",",","and","save","it","at","``","save_loc","``",".","``","nr_iter","``","controls","the","number","of","Perceptron","training","iterations",".",":","param","sentences",":","A","list","or","iterator","of","sentences",",","where","each","sentence","is","a","list","of","(","words",",","tags",")","tuples",".",":","param","save_loc",":","If","not","``","None","``",",","saves","a","pickled","model","in","this","location",".",":","param","nr_iter",":","Number","of","training","iterations","."]
code: def train(self, sentences, save_loc=None, nr_iter=5):
      """Train a model from sentences, and save it at ``save_loc``. ``nr_iter``
      controls the number of Perceptron training iterations.
  
      :param sentences: A list or iterator of sentences, where each sentence
          is a list of (words, tags) tuples.
      :param save_loc: If not ``None``, saves a pickled model in this location.
      :param nr_iter: Number of training iterations.
      """
      # We'd like to allow ``sentences`` to be either a list or an iterator,
      # the latter being especially important for a large training dataset.
      # Because ``self._make_tagdict(sentences)`` runs regardless, we make
      # it populate ``self._sentences`` (a list) with all the sentences.
      # This saves the overheard of just iterating through ``sentences`` to
      # get the list by ``sentences = list(sentences)``.
  
      self._sentences = (
          list()
      )  # to be populated by self._make_tagdict...
      self._make_tagdict(sentences)
      self.model.classes = self.classes
      for iter_ in range(nr_iter):
          c = 0
          n = 0
          for sentence in self._sentences:
              words, tags = zip(*sentence)
  
              prev, prev2 = self.START
              context = (
                  self.START
                  + [self.normalize(w) for w in words]
                  + self.END
              )
              for i, word in enumerate(words):
                  guess = self.tagdict.get(word)
                  if not guess:
                      feats = self._get_features(
                          i, word, context, prev, prev2
                      )
                      guess, _ = self.model.predict(feats)
                      self.model.update(tags[i], guess, feats)
                  prev2 = prev
                  prev = guess
                  c += guess == tags[i]
                  n += 1
          random.shuffle(self._sentences)
          logging.info(f"Iter {iter_}: {c}/{n}={_pc(c, n)}")
  
      # We don't need the training sentences anymore, and we don't want to
      # waste space on them when we pickle the trained tagger.
      self._sentences = None
  
      self.model.average_weights()
      # Pickle as a binary file
      if save_loc is not None:
          with open(save_loc, "wb") as fout:
              # changed protocol from -1 to 2 to make pickling Python 2 compatible
              pickle.dump(
                  (self.model.weights, self.tagdict, self.classes),
                  fout,
                  2,
              )
  
imports: import logging
  import pickle
  import random
  from collections import defaultdict
  from nltk import jsontags
  from nltk.data import find, load
  from nltk.tag.api import TaggerI
label_desc: Use image node_id 3 for calling the PerceptronTagger obj's underlying member method code with example usage: obj.train(sentences, save_loc, nr_iter) without return types