All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.metarank.ltrlib.output.LibSVMOutputFormat.scala Maven / Gradle / Ivy

There is a newer version: 0.2.6
Show newest version
package io.github.metarank.ltrlib.output

import io.github.metarank.ltrlib.model.Dataset

import java.io.OutputStream

object LibSVMOutputFormat extends OutputFormat {
  def write(data: OutputStream, ds: Dataset) = {
    for {
      query <- ds.groups
      rowid <- 0 until query.rows
    } {
      val row  = query.getRow(rowid).zipWithIndex.filter(_._1 != 0).map(x => s"${x._2}:${x._1}")
      val line = s"${query.labels(rowid)} qid:${query.group} ${row.mkString(" ")}\n"
      data.write(line.getBytes())
    }
  }

  def write(data: OutputStream, groups: OutputStream, ds: Dataset) = {
    for {
      query <- ds.groups
    } {
      groups.write(s"${query.rows}\n".getBytes())
      for {
        rowid <- 0 until query.rows
      } {
        val row  = query.getRow(rowid).zipWithIndex.filter(_._1 != 0).map(x => s"${x._2}:${x._1}")
        val line = s"${query.labels(rowid)} ${row.mkString(" ")}\n"
        data.write(line.getBytes())
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy