docstr: Initialize the Reddit Entity Linking corpus containing gold annotations only.
  
  see https://arxiv.org/abs/2101.01228v2
  
  The first time you call this constructor it will automatically download the dataset.
  
  Args:
      base_path: Default is None, meaning that corpus gets auto-downloaded and loaded. You can override this to point to a different folder but typically this should not be necessary.
      in_memory: If True, keeps dataset in memory giving speedups in training.
      document_as_sequence: If True, all sentences of a document are read into a single Sentence object
docstr_tok: ["Initialize","the","Reddit","Entity","Linking","corpus","containing","gold","annotations","only",".","see","https",":","\/\/arxiv.org\/abs\/2101.01228v2","The","first","time","you","call","this","constructor","it","will","automatically","download","the","dataset",".","Args",":","base_path",":","Default","is","None",",","meaning","that","corpus","gets","auto-downloaded","and","loaded",".","You","can","override","this","to","point","to","a","different","folder","but","typically","this","should","not","be","necessary",".","in_memory",":","If","True",",","keeps","dataset","in","memory","giving","speedups","in","training",".","document_as_sequence",":","If","True",",","all","sentences","of","a","document","are","read","into","a","single","Sentence","object"]
code: def __init__(
      self,
      base_path: Optional[Union[str, Path]] = None,
      in_memory: bool = True,
      **corpusargs,
  ) -> None:
      """Initialize the Reddit Entity Linking corpus containing gold annotations only.
  
      see https://arxiv.org/abs/2101.01228v2
  
      The first time you call this constructor it will automatically download the dataset.
  
      Args:
          base_path: Default is None, meaning that corpus gets auto-downloaded and loaded. You can override this to point to a different folder but typically this should not be necessary.
          in_memory: If True, keeps dataset in memory giving speedups in training.
          document_as_sequence: If True, all sentences of a document are read into a single Sentence object
      """
      base_path = (
          flair.cache_root / "datasets"
          if not base_path
          else Path(base_path)
      )
  
      # this dataset name
      dataset_name = self.__class__.__name__.lower()
  
      data_folder = base_path / dataset_name
  
      # download and parse data if necessary
      reddit_el_path = (
          "https://zenodo.org/record/3970806/files/reddit_el.zip"
      )
      corpus_file_name = "reddit_el_gold.txt"
      parsed_dataset = data_folder / corpus_file_name
  
      if not parsed_dataset.exists():
          reddit_el_zip = cached_path(
              f"{reddit_el_path}", Path("datasets") / dataset_name
          )
          unpack_file(reddit_el_zip, data_folder, "zip", False)
  
          with open(
              data_folder / corpus_file_name, "w", encoding="utf-8"
          ) as txtout:
              # First parse the post titles
              with open(
                  data_folder / "posts.tsv", encoding="utf-8"
              ) as tsvin1, open(
                  data_folder / "gold_post_annotations.tsv",
                  encoding="utf-8",
              ) as tsvin2:
                  posts = csv.reader(tsvin1, delimiter="\t")
                  self.post_annotations = csv.reader(
                      tsvin2, delimiter="\t"
                  )
                  self.curr_annot = next(self.post_annotations)
  
                  for row in posts:  # Go through all the post titles
                      txtout.writelines(
                          "-DOCSTART-\n\n"
                      )  # Start each post with a -DOCSTART- token
  
                      # Keep track of how many and which entity mentions does a given post title have
                      link_annots = (
                          []
                      )  # [start pos, end pos, wiki page title] of an entity mention
  
                      # Check if the current post title has an entity link and parse accordingly
                      if row[0] == self.curr_annot[0]:
                          link_annots.append(
                              (
                                  int(self.curr_annot[4]),
                                  int(self.curr_annot[5]),
                                  self.curr_annot[3],
                              )
                          )
                          link_annots = self._fill_annot_array(
                              link_annots, row[0], post_flag=True
                          )
  
                          # Post titles with entity mentions (if any) are handled via this function
                          self._text_to_cols(
                              Sentence(row[2], use_tokenizer=True),
                              link_annots,
                              txtout,
                          )
                      else:
                          self._text_to_cols(
                              Sentence(row[2], use_tokenizer=True),
                              link_annots,
                              txtout,
                          )
  
              # Then parse the comments
              with open(
                  data_folder / "comments.tsv", encoding="utf-8"
              ) as tsvin3, open(
                  data_folder / "gold_comment_annotations.tsv",
                  encoding="utf-8",
              ) as tsvin4:
                  self.comments = csv.reader(tsvin3, delimiter="\t")
                  self.comment_annotations = csv.reader(
                      tsvin4, delimiter="\t"
                  )
                  self.curr_annot = next(self.comment_annotations)
                  self.curr_row: Optional[List[str]] = next(
                      self.comments
                  )
                  self.stop_iter = False
  
                  # Iterate over the comments.tsv file, until the end is reached
                  while not self.stop_iter:
                      txtout.writelines(
                          "-DOCSTART-\n"
                      )  # Start each comment thread with a -DOCSTART- token
  
                      # Keep track of the current comment thread and its corresponding key, on which the annotations are matched.
                      # Each comment thread is handled as one 'document'.
                      self.curr_comm: str = self.curr_row[4]
                      comm_key = self.curr_row[0]
  
                      # Python's csv package for some reason fails to correctly parse a handful of rows inside the comments.tsv file.
                      # This if-condition is needed to handle this problem.
                      if comm_key in {"en5rf4c", "es3ia8j", "es3lrmw"}:
                          if comm_key == "en5rf4c":
                              self.parsed_row = (
                                  r.split("\t")
                                  for r in self.curr_row[4].split("\n")
                              )
                              self.curr_comm = next(self.parsed_row)  # type: ignore  # noqa: PGH003
                          self._fill_curr_comment(fix_flag=True)
                      # In case we are dealing with properly parsed rows, proceed with a regular parsing procedure
                      else:
                          self._fill_curr_comment(fix_flag=False)
  
                      link_annots = (
                          []
                      )  # [start pos, end pos, wiki page title] of an entity mention
  
                      # Check if the current comment thread has an entity link and parse accordingly, same as with post titles above
                      if comm_key == self.curr_annot[0]:
                          link_annots.append(
                              (
                                  int(self.curr_annot[4]),
                                  int(self.curr_annot[5]),
                                  self.curr_annot[3],
                              )
                          )
                          link_annots = self._fill_annot_array(
                              link_annots, comm_key, post_flag=False
                          )
                          self._text_to_cols(
                              Sentence(
                                  self.curr_comm, use_tokenizer=True
                              ),
                              link_annots,
                              txtout,
                          )
                      else:
                          # In two of the comment thread a case of capital letter spacing occurs, which the SegtokTokenizer cannot properly handle.
                          # The following if-elif condition handles these two cases and as result writes full capitalized words in each corresponding row,
                          # and not just single letters into single rows.
                          if comm_key == "dv74ybb":
                              self.curr_comm = " ".join(
                                  [
                                      word.replace(" ", "")
                                      for word in self.curr_comm.split(
                                          "  "
                                      )
                                  ]
                              )
                          elif comm_key == "eci2lut":
                              self.curr_comm = (
                                  self.curr_comm[:18]
                                  + self.curr_comm[18:27].replace(
                                      " ", ""
                                  )
                                  + self.curr_comm[27:55]
                                  + self.curr_comm[55:68].replace(
                                      " ", ""
                                  )
                                  + self.curr_comm[68:85]
                                  + self.curr_comm[85:92].replace(
                                      " ", ""
                                  )
                                  + self.curr_comm[92:]
                              )
  
                          self._text_to_cols(
                              Sentence(
                                  self.curr_comm, use_tokenizer=True
                              ),
                              link_annots,
                              txtout,
                          )
  
      super().__init__(
          data_folder,
          column_format={0: "text", 1: "nel"},
          train_file=corpus_file_name,
          in_memory=in_memory,
          **corpusargs,
      )
  
imports: import csv
  import logging
  import os
  from pathlib import Path
  from typing import Dict, List, Optional, Union
  import requests
  import flair
  from flair.data import Corpus, MultiCorpus, Sentence
  from flair.datasets.sequence_labeling import ColumnCorpus, MultiFileColumnCorpus
  from flair.file_utils import cached_path, unpack_file
  from flair.splitter import SegtokSentenceSplitter, SentenceSplitter
label_desc: Use image node_id 1 to create a new NEL_ENGLISH_REDDIT object from inherited base classes: ColumnCorpus with example: obj = NEL_ENGLISH_REDDIT(base_path, in_memory)