class_docstr: A parser for LibSVM-formatted ranking files.
  
  This parser can parse an iterable of lines into an iterable of
  `RankingExampleTuple`s that contain the per-query labels and features.
  
  Example usage:
  >>> lines = ["1 qid:1 1:5.0 2:3.0", "2 qid:1 1:2.0 2:1.0"]
  >>> feature_names = {1: "bm25", 2: "tfidf"}
  >>> parser = LibSVMRankingParser(lines, feature_names)
  >>> for example in parser:
  ...   print(example[0])
  {'bm25': array([5., 2.]), 'tfidf': array([3., 1.]), 'label': array([1, 2])}
  
  The parser will raise a ParserError with an informative error message if the
  content being parsed does not match LibSVM ranking format.
class_docstr_tok: ["A","parser","for","LibSVM-formatted","ranking","files",".","This","parser","can","parse","an","iterable","of","lines","into","an","iterable","of","`","RankingExampleTuple","`","s","that","contain","the","per-query","labels","and","features",".","Example","usage",":",">",">",">","lines","=","[","``","1","qid:1","1:5.0","2:3.0","''",",","``","2","qid:1","1:2.0","2:1.0","''","]",">",">",">","feature_names","=","{","1",":","``","bm25","''",",","2",":","``","tfidf","''","}",">",">",">","parser","=","LibSVMRankingParser","(","lines",",","feature_names",")",">",">",">","for","example","in","parser",":","...","print","(","example","[","0","]",")","{","'bm25","'",":","array","(","[","5.",",","2",".","]",")",",","'tfidf","'",":","array","(","[","3.",",","1",".","]",")",",","'label","'",":","array","(","[","1",",","2","]",")","}","The","parser","will","raise","a","ParserError","with","an","informative","error","message","if","the","content","being","parsed","does","not","match","LibSVM","ranking","format","."]
docstr: Parses a single line of input from a LibSVM ranking file.
  
  This method will update the internal state of the parser and may produce a
  new item to be made available in `self._available_examples`.
  
  Args:
    line_number: The current line number. This may be used to generate more
      informative ParserError messages if such an error occurs.
    line: The line to parse.
  
  Raises:
    ParserError: If the parsing of given line failed.
docstr_tok: ["Parses","a","single","line","of","input","from","a","LibSVM","ranking","file",".","This","method","will","update","the","internal","state","of","the","parser","and","may","produce","a","new","item","to","be","made","available","in","`","self._available_examples","`",".","Args",":","line_number",":","The","current","line","number",".","This","may","be","used","to","generate","more","informative","ParserError","messages","if","such","an","error","occurs",".","line",":","The","line","to","parse",".","Raises",":","ParserError",":","If","the","parsing","of","given","line","failed","."]
code: def _parse_line(self, line_number: int, line: str):
      """Parses a single line of input from a LibSVM ranking file.
  
      This method will update the internal state of the parser and may produce a
      new item to be made available in `self._available_examples`.
  
      Args:
        line_number: The current line number. This may be used to generate more
          informative ParserError messages if such an error occurs.
        line: The line to parse.
  
      Raises:
        ParserError: If the parsing of given line failed.
      """
      # Remove comments and leading/trailing whitespace from line.
      line_clean, *_ = line.split("#", maxsplit=2)
      line_clean = line_clean.strip()
  
      # An empty line is allowed but should be skipped during parsing.
      if not line_clean:
          return
  
      # Split by space separators.
      try:
          label, qid, *features = re.split(r"\s+", line_clean)
      except ValueError as value_error:
          raise ParserError(
              line_number,
              line,
              "could not extract label, qid and features",
          ) from value_error
  
      # Convert relevance label to float.
      try:
          label = float(label)
      except ValueError as value_error:
          raise ParserError(
              line_number,
              line,
              f"label '{label}' could not be converted to a float",
          ) from value_error
  
      # Extract qid.
      if qid[:4] != "qid:":
          raise ParserError(
              line_number,
              line,
              "line must contain a qid after the relevance label",
          )
      qid = qid[4:]
      if not qid:
          raise ParserError(line_number, line, "qid can not be empty")
  
      # Construct a feature dict containing default values.
      feature_dict = {
          feature_name: self._default_feature_value
          for feature_name in self._feature_names.values()
      }
  
      # Parse all features and add them to the feature dict.
      for feature in features:
          try:
              index, value = feature.split(":", maxsplit=2)
              index = int(index)
              value = float(value)
              # Only add features if they map to a feature name in the
              # `_feature_names` dict. All other features are ignored.
              if index in self._feature_names:
                  feature_dict[self._feature_names[index]] = value
          except ValueError as value_error:
              raise ParserError(
                  line_number,
                  line,
                  f"failed to extract feature index and value from '{feature}'",
              ) from value_error
  
      # Add label to feature dict.
      feature_dict[self._label_feature_name] = label
  
      # Add document identifier. We use the line number as a unique document
      # identifier since LibSVMRanking files do not have any other way to identify
      # documents.
      feature_dict[self._doc_id_feature_name] = line_number
  
      # Add the parsed qid and feature dictionary to the current example.
      self._add_to_current_example(qid, feature_dict)
  
file_path: datasets/tensorflow_datasets/ranking/libsvm_ranking_parser.py
imports: import collections
  import dataclasses
  import re
  from typing import Iterable, List, Mapping, Tuple
  import numpy
label_desc: Use image node_id 2 for calling the LibSVMRankingParser obj's underlying member method code with example usage: obj._parse_line(line_number, line) without return types