docstr: Reads Mongo collections.
  
  Each collection should contain one document/text per item.
  
  Each item should have the following format:
  {
  'Beskrivning': 'Abrahamsby. Gård i Gottröra sn, Långhundra hd, Stockholms län, nära Långsjön.',
  'Län':'Stockholms län',
  'Härad': 'Långhundra',
  'Församling': 'Gottröra',
  'Plats': 'Abrahamsby'
  }
  
  Args:
      query: Query, e.g. {'Län': 'Stockholms län'}
      host: Host, e.g. 'localhost',
      port: Port, e.g. 27017
      database: Database, e.g. 'rosenberg',
      collection: Collection, e.g. 'book',
      text_field: Text field, e.g. 'Beskrivning',
      categories_field: List of category fields, e.g ['Län', 'Härad', 'Tingslag', 'Församling', 'Plats'],
      max_tokens_per_doc: Takes at most this amount of tokens per document. If set to -1 all documents are taken as is.
      max_tokens_per_doc: If set, truncates each Sentence to a maximum number of Tokens
      max_chars_per_doc: If set, truncates each Sentence to a maximum number of chars
      tokenizer: Custom tokenizer to use (default SegtokTokenizer)
      in_memory: If True, keeps dataset as Sentences in memory, otherwise only keeps strings
      tag_type: The tag type to assign labels to.
  
  Returns: list of sentences
docstr_tok: ["Reads","Mongo","collections",".","Each","collection","should","contain","one","document\/text","per","item",".","Each","item","should","have","the","following","format",":","{","'Beskrivning","'",":","'Abrahamsby",".","G\u00e5rd","i","Gottr\u00f6ra","sn",",","L\u00e5nghundra","hd",",","Stockholms","l\u00e4n",",","n\u00e4ra","L\u00e5ngsj\u00f6n",".","'",",","'L\u00e4n","'",":","'Stockholms","l\u00e4n","'",",","'H\u00e4rad","'",":","'L\u00e5nghundra","'",",","'F\u00f6rsamling","'",":","'Gottr\u00f6ra","'",",","'Plats","'",":","'Abrahamsby'","}","Args",":","query",":","Query",",","e.g",".","{","'L\u00e4n","'",":","'Stockholms","l\u00e4n","'","}","host",":","Host",",","e.g",".","'localhost","'",",","port",":","Port",",","e.g",".","27017","database",":","Database",",","e.g",".","'rosenberg","'",",","collection",":","Collection",",","e.g",".","'book","'",",","text_field",":","Text","field",",","e.g",".","'Beskrivning","'",",","categories_field",":","List","of","category","fields",",","e.g","[","'L\u00e4n","'",",","'H\u00e4rad","'",",","'Tingslag","'",",","'F\u00f6rsamling","'",",","'Plats","'","]",",","max_tokens_per_doc",":","Takes","at","most","this","amount","of","tokens","per","document",".","If","set","to","-1","all","documents","are","taken","as","is",".","max_tokens_per_doc",":","If","set",",","truncates","each","Sentence","to","a","maximum","number","of","Tokens","max_chars_per_doc",":","If","set",",","truncates","each","Sentence","to","a","maximum","number","of","chars","tokenizer",":","Custom","tokenizer","to","use","(","default","SegtokTokenizer",")","in_memory",":","If","True",",","keeps","dataset","as","Sentences","in","memory",",","otherwise","only","keeps","strings","tag_type",":","The","tag","type","to","assign","labels","to",".","Returns",":","list","of","sentences"]
code: def __init__(
      self,
      query: str,
      host: str,
      port: int,
      database: str,
      collection: str,
      text_field: str,
      categories_field: Optional[List[str]] = None,
      max_tokens_per_doc: int = -1,
      max_chars_per_doc: int = -1,
      tokenizer: Tokenizer = SegtokTokenizer(),
      in_memory: bool = True,
      tag_type: str = "class",
  ) -> None:
      """Reads Mongo collections.
  
      Each collection should contain one document/text per item.
  
      Each item should have the following format:
      {
      'Beskrivning': 'Abrahamsby. Gård i Gottröra sn, Långhundra hd, Stockholms län, nära Långsjön.',
      'Län':'Stockholms län',
      'Härad': 'Långhundra',
      'Församling': 'Gottröra',
      'Plats': 'Abrahamsby'
      }
  
      Args:
          query: Query, e.g. {'Län': 'Stockholms län'}
          host: Host, e.g. 'localhost',
          port: Port, e.g. 27017
          database: Database, e.g. 'rosenberg',
          collection: Collection, e.g. 'book',
          text_field: Text field, e.g. 'Beskrivning',
          categories_field: List of category fields, e.g ['Län', 'Härad', 'Tingslag', 'Församling', 'Plats'],
          max_tokens_per_doc: Takes at most this amount of tokens per document. If set to -1 all documents are taken as is.
          max_tokens_per_doc: If set, truncates each Sentence to a maximum number of Tokens
          max_chars_per_doc: If set, truncates each Sentence to a maximum number of chars
          tokenizer: Custom tokenizer to use (default SegtokTokenizer)
          in_memory: If True, keeps dataset as Sentences in memory, otherwise only keeps strings
          tag_type: The tag type to assign labels to.
  
      Returns: list of sentences
      """
      # first, check if pymongo is installed
      try:
          import pymongo
      except ModuleNotFoundError:
          log.warning("-" * 100)
          log.warning(
              'ATTENTION! The library "pymongo" is not installed!'
          )
          log.warning(
              'To use MongoDataset, please first install with "pip install pymongo"'
          )
          log.warning("-" * 100)
  
      self.in_memory = in_memory
      self.tokenizer = tokenizer
  
      if self.in_memory:
          self.sentences = []
      else:
          self.indices = []
  
      self.total_sentence_count: int = 0
      self.max_chars_per_doc = max_chars_per_doc
      self.max_tokens_per_doc = max_tokens_per_doc
  
      self.__connection = pymongo.MongoClient(host, port)
      self.__cursor = self.__connection[database][collection]
  
      self.text = text_field
      self.categories = (
          categories_field if categories_field is not None else []
      )
      self.tag_type = tag_type
  
      start = 0
  
      if self.in_memory:
          for document in self.__cursor.find(
              filter=query, skip=start, limit=0
          ):
              sentence = self._parse_document_to_sentence(
                  document[self.text],
                  [
                      document[_] if _ in document else ""
                      for _ in self.categories
                  ],
                  tokenizer,
              )
              if sentence is not None and len(sentence.tokens) > 0:
                  self.sentences.append(sentence)
                  self.total_sentence_count += 1
      else:
          self.indices = self.__cursor.find().distinct("_id")
          self.total_sentence_count = self.__cursor.count_documents()
  
imports: import logging
  from abc import abstractmethod
  from pathlib import Path
  from typing import Generic, List, Optional, Union
  import torch.utils.data.dataloader
  from deprecated.sphinx import deprecated
  from flair.data import DT, FlairDataset, Sentence, Tokenizer
  from flair.tokenization import SegtokTokenizer, SpaceTokenizer
label_desc: Use image node_id 1 to create a new MongoDataset object from inherited base classes: FlairDataset with example: obj = MongoDataset(query, host, port, database, collection, text_field, categories_field, max_tokens_per_doc, max_chars_per_doc, tokenizer, in_memory, tag_type)