use oa::data::{Dataset, Sample};
use oa::matrix::Matrix;
use oa::{Engine, Result};
use crate::core::decode::Decoded;
use crate::core::pack::FetchProgress;
use crate::text::gluetsv::{self, GlueData, GlueSpec};
#[path = "glue_qqp.gen.rs"]
mod generated;
pub(crate) use generated::GLUE_QQP_PACK;
pub use generated::{GlueQqpConfig, GlueQqpSplit};
const SPEC: GlueSpec = GlueSpec {
header: true,
text_cols: &[3, 4],
label_col: 5,
label_names: &[],
classes: &["not_duplicate", "duplicate"],
};
fn member(split: GlueQqpSplit) -> &'static str {
match split {
GlueQqpSplit::Train => "train.tsv",
GlueQqpSplit::Validation => "dev.tsv",
}
}
pub struct GlueQqp {
data: GlueData,
}
impl GlueQqp {
pub fn decode(config: &GlueQqpConfig) -> Result<Decoded> {
Self::decode_with_progress(config, &mut |_| true)
}
pub fn decode_with_progress(
config: &GlueQqpConfig,
progress: &mut impl FnMut(FetchProgress) -> bool,
) -> Result<Decoded> {
let root = match &config.cache_dir {
Some(dir) => dir.clone(),
None => crate::core::pack::data_root()?,
};
let dir = crate::core::pack::ensure_pack_with_progress(&GLUE_QQP_PACK, &root, progress)?;
let (decoded, _) = gluetsv::decode_member(
"glue_qqp",
&dir.join("QQP.zip"),
member(config.split),
&SPEC,
)?;
Ok(decoded)
}
pub fn load_with_progress_on(
engine: &Engine,
config: &GlueQqpConfig,
progress: &mut impl FnMut(FetchProgress) -> bool,
) -> Result<Self> {
let data = GlueData::load_on(
engine,
&GLUE_QQP_PACK,
std::path::Path::new("QQP.zip"),
member(config.split),
&SPEC,
config.cache_dir.clone(),
progress,
)?;
Ok(Self { data })
}
#[must_use]
pub const fn text1(&self) -> &Matrix {
&self.data.text1
}
#[must_use]
pub const fn text2(&self) -> Option<&Matrix> {
self.data.text2.as_ref()
}
#[must_use]
pub const fn labels(&self) -> &Matrix {
&self.data.labels
}
#[must_use]
pub fn classes(&self) -> &[String] {
&self.data.classes
}
}
impl Dataset for GlueQqp {
fn len(&self) -> i64 {
i64::try_from(self.data.rows()).unwrap_or(i64::MAX)
}
fn get_item(&self, index: i64) -> Result<Matrix> {
self.data.item(index)
}
fn get_sample(&self, index: i64) -> Result<Sample> {
self.data.sample(index)
}
}