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_stsb.gen.rs"]
mod generated;
pub(crate) use generated::GLUE_STSB_PACK;
pub use generated::{GlueStsbConfig, GlueStsbSplit};
const SPEC: GlueSpec = GlueSpec {
header: true,
text_cols: &[7, 8],
label_col: 9,
label_names: &[],
classes: &[],
};
fn member(split: GlueStsbSplit) -> &'static str {
match split {
GlueStsbSplit::Train => "train.tsv",
GlueStsbSplit::Validation => "dev.tsv",
}
}
pub struct GlueStsb {
data: GlueData,
}
impl GlueStsb {
pub fn decode(config: &GlueStsbConfig) -> Result<Decoded> {
Self::decode_with_progress(config, &mut |_| true)
}
pub fn decode_with_progress(
config: &GlueStsbConfig,
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_STSB_PACK, &root, progress)?;
let (decoded, _) = gluetsv::decode_member(
"glue_stsb",
&dir.join("STS-B.zip"),
member(config.split),
&SPEC,
)?;
Ok(decoded)
}
pub fn load_with_progress_on(
engine: &Engine,
config: &GlueStsbConfig,
progress: &mut impl FnMut(FetchProgress) -> bool,
) -> Result<Self> {
let data = GlueData::load_on(
engine,
&GLUE_STSB_PACK,
std::path::Path::new("STS-B.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
}
}
impl Dataset for GlueStsb {
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)
}
}