

def iter_lines(lines, has_targets=True, one_indexed=True, missing=0.0):
    "Transforms an iterator of lines to an iterator of LETOR rows.\n\n    Each row is represented by a (x, y, qid, comment) tuple.\n\n    Parameters\n    ----------\n    lines : iterable of lines\n        Lines to parse.\n    has_targets : bool, optional\n        Whether the file contains targets. If True, will expect the first token\n        of every line to be a real representing the sample's target (i.e.\n        score). If False, will use -1 as a placeholder for all targets.\n    one_indexed : bool, optional\n        Whether feature ids are one-indexed. If True, will subtract 1 from each\n        feature id.\n    missing : float, optional\n        Placeholder to use if a feature value is not provided for a sample.\n\n    Yields\n    ------\n    x : array of floats\n        Feature vector of the sample.\n    y : float\n        Target value (score) of the sample, or -1 if no target was parsed.\n    qid : object\n        Query id of the sample. This is currently guaranteed to be a string.\n    comment : str\n        Comment accompanying the sample.\n\n    "
    for line in lines:
        (data, _, comment) = line.rstrip().partition('#')
        toks = data.split()
        num_features = 0
        x = np.repeat(missing, 8)
        y = (- 1.0)
        if has_targets:
            y = float(toks[0])
            toks = toks[1:]
        qid = _parse_qid_tok(toks[0])
        for tok in toks[1:]:
            (fid, _, val) = tok.partition(':')
            fid = int(fid)
            val = float(val)
            if one_indexed:
                fid -= 1
            assert (fid >= 0)
            while (len(x) <= fid):
                orig = len(x)
                x.resize((len(x) * 2))
                x[orig:(orig * 2)] = missing
            x[fid] = val
            num_features = max((fid + 1), num_features)
        assert (num_features > 0)
        x.resize(num_features)
        (yield (x, y, qid, comment))

dataset/ETHPy150Open jma127/pyltr/pyltr/data/letor.py iter_lines/original