docstr: Fits a topic model to documents in <texts>.
  Example:
      tm = ktrain.text.get_topic_model(docs, n_topics=20,
                                      n_features=1000, min_df=2, max_df=0.95)
  Args:
      texts (list of str): list of texts
      n_topics (int): number of topics.
                      If None, n_topics = min{400, sqrt[# documents/2]})
      n_features (int):  maximum words to consider
      max_df (float): words in more than max_df proportion of docs discarded
      stop_words (str or list): either 'english' for built-in stop words or
                               a list of stop words to ignore
      max_iter (int): maximum iterations for 'lda'.  5 is default if using lda_mode='online'.
      lda_max_iter (int): alias for max_iter for backwards compatibility
                          If lda_mode='batch', this should be increased (e.g., 1500).
                          Ignored if model_type != 'lda'
      lda_mode (str):  one of {'online', 'batch'}. Ignored of model_type !='lda'
      token_pattern(str): regex pattern to use to tokenize documents.
                          If None, a default tokenizer will be used
      hyperparam_kwargs(dict): hyperparameters for LDA/NMF
                               Keys in this dict can be any of the following:
                                   alpha: alpha for LDA  default: 5./n_topics
                                   beta: beta for LDA.  default:0.01
                                   nmf_alpha_W: alpha for NMF alpha_W (default is 0.0)
                                   nmf_alpha_H: alpha for NMF alpha_H (default is 'same')
                                   l1_ratio: l1_ratio for NMF. default: 0
                                   ngram_range:  whether to consider bigrams, trigrams. default: (1,1)
  
  Returns:
      tuple: (model, vectorizer)
docstr_tok: ["Fits","a","topic","model","to","documents","in","<","texts",">",".","Example",":","tm","=","ktrain.text.get_topic_model","(","docs",",","n_topics=20",",","n_features=1000",",","min_df=2",",","max_df=0.95",")","Args",":","texts","(","list","of","str",")",":","list","of","texts","n_topics","(","int",")",":","number","of","topics",".","If","None",",","n_topics","=","min","{","400",",","sqrt","[","#","documents\/2","]","}",")","n_features","(","int",")",":","maximum","words","to","consider","max_df","(","float",")",":","words","in","more","than","max_df","proportion","of","docs","discarded","stop_words","(","str","or","list",")",":","either","'english","'","for","built-in","stop","words","or","a","list","of","stop","words","to","ignore","max_iter","(","int",")",":","maximum","iterations","for","'lda","'",".","5","is","default","if","using","lda_mode='online","'",".","lda_max_iter","(","int",")",":","alias","for","max_iter","for","backwards","compatibility","If","lda_mode='batch","'",",","this","should","be","increased","(","e.g.",",","1500",")",".","Ignored","if","model_type","!","=","'lda'","lda_mode","(","str",")",":","one","of","{","'online","'",",","'batch","'","}",".","Ignored","of","model_type","!","='lda'","token_pattern","(","str",")",":","regex","pattern","to","use","to","tokenize","documents",".","If","None",",","a","default","tokenizer","will","be","used","hyperparam_kwargs","(","dict",")",":","hyperparameters","for","LDA\/NMF","Keys","in","this","dict","can","be","any","of","the","following",":","alpha",":","alpha","for","LDA","default",":","5.\/n_topics","beta",":","beta","for","LDA",".","default:0.01","nmf_alpha_W",":","alpha","for","NMF","alpha_W","(","default","is","0.0",")","nmf_alpha_H",":","alpha","for","NMF","alpha_H","(","default","is","'same","'",")","l1_ratio",":","l1_ratio","for","NMF",".","default",":","0","ngram_range",":","whether","to","consider","bigrams",",","trigrams",".","default",":","(","1,1",")","Returns",":","tuple",":","(","model",",","vectorizer",")"]
code: def train(
      self,
      texts,
      model_type="lda",
      n_topics=None,
      n_features=10000,
      min_df=5,
      max_df=0.5,
      stop_words="english",
      max_iter=5,
      lda_max_iter=None,
      lda_mode="online",
      token_pattern=None,
      hyperparam_kwargs=None,
  ):
      """
      Fits a topic model to documents in <texts>.
      Example:
          tm = ktrain.text.get_topic_model(docs, n_topics=20,
                                          n_features=1000, min_df=2, max_df=0.95)
      Args:
          texts (list of str): list of texts
          n_topics (int): number of topics.
                          If None, n_topics = min{400, sqrt[# documents/2]})
          n_features (int):  maximum words to consider
          max_df (float): words in more than max_df proportion of docs discarded
          stop_words (str or list): either 'english' for built-in stop words or
                                   a list of stop words to ignore
          max_iter (int): maximum iterations for 'lda'.  5 is default if using lda_mode='online'.
          lda_max_iter (int): alias for max_iter for backwards compatibility
                              If lda_mode='batch', this should be increased (e.g., 1500).
                              Ignored if model_type != 'lda'
          lda_mode (str):  one of {'online', 'batch'}. Ignored of model_type !='lda'
          token_pattern(str): regex pattern to use to tokenize documents.
                              If None, a default tokenizer will be used
          hyperparam_kwargs(dict): hyperparameters for LDA/NMF
                                   Keys in this dict can be any of the following:
                                       alpha: alpha for LDA  default: 5./n_topics
                                       beta: beta for LDA.  default:0.01
                                       nmf_alpha_W: alpha for NMF alpha_W (default is 0.0)
                                       nmf_alpha_H: alpha for NMF alpha_H (default is 'same')
                                       l1_ratio: l1_ratio for NMF. default: 0
                                       ngram_range:  whether to consider bigrams, trigrams. default: (1,1)
  
      Returns:
          tuple: (model, vectorizer)
      """
      max_iter = lda_max_iter if lda_max_iter is not None else max_iter
      if hyperparam_kwargs is None:
          hyperparam_kwargs = {}
      alpha = hyperparam_kwargs.get("alpha", 5.0 / n_topics)
      nmf_alpha_W = hyperparam_kwargs.get("nmf_alpha_W", 0.0)
      nmf_alpha_H = hyperparam_kwargs.get("nmf_alpha_H", "same")
      beta = hyperparam_kwargs.get("beta", 0.01)
      l1_ratio = hyperparam_kwargs.get("l1_ratio", 0)
      ngram_range = hyperparam_kwargs.get("ngram_range", (1, 1))
  
      # adjust defaults based on language detected
      if texts is not None:
          lang = TU.detect_lang(texts)
          if lang != "en":
              stopwords = (
                  None if stop_words == "english" else stop_words
              )
              token_pattern = (
                  r"(?u)\b\w+\b"
                  if token_pattern is None
                  else token_pattern
              )
          if pp.is_nospace_lang(lang):
              text_list = []
              for t in texts:
                  text_list.append(" ".join(jieba.cut(t, HMM=False)))
              texts = text_list
          if self.verbose:
              print("lang: %s" % (lang))
  
      # preprocess texts
      if self.verbose:
          print("preprocessing texts...")
      if token_pattern is None:
          token_pattern = TU.DEFAULT_TOKEN_PATTERN
      # if token_pattern is None: token_pattern = r'(?u)\b\w\w+\b'
      vectorizer = CountVectorizer(
          max_df=max_df,
          min_df=min_df,
          max_features=n_features,
          stop_words=stop_words,
          token_pattern=token_pattern,
          ngram_range=ngram_range,
      )
  
      x_train = vectorizer.fit_transform(texts)
  
      # fit model
  
      if self.verbose:
          print("fitting model...")
      if model_type == "lda":
          model = LatentDirichletAllocation(
              n_components=n_topics,
              max_iter=max_iter,
              learning_method=lda_mode,
              learning_offset=50.0,
              doc_topic_prior=alpha,
              topic_word_prior=beta,
              verbose=self.verbose,
              random_state=0,
          )
      elif model_type == "nmf":
          model = NMF(
              n_components=n_topics,
              max_iter=max_iter,
              verbose=self.verbose,
              alpha_W=nmf_alpha_W,
              alpha_H=nmf_alpha_H,
              l1_ratio=l1_ratio,
              random_state=0,
          )
      else:
          raise ValueError("unknown model type:", str(model_type))
      model.fit(x_train)
  
      # save model and vectorizer and hyperparameter settings
      return (model, vectorizer)
  
imports: import time
  from ..None import utils
  from ..imports import *
  from .None import preprocessor
  from .None import textutils
label_desc: Use image node_id 2 for calling the TopicModel obj's underlying member method code with example usage: obj.train(texts, model_type, n_topics, n_features, min_df, max_df, stop_words, max_iter, lda_max_iter, lda_mode, token_pattern, hyperparam_kwargs) and returns: model,  vectorizer