use oa::data::{Dataset, Sample};
use oa::matrix::Matrix;
use oa::{Engine, Result};
use crate::core::decode::Decoded;
use crate::core::jsonl;
use crate::core::pack::FetchProgress;
use crate::text::recordl::{self, RecordData, RecordSpec};
#[path = "superglue_rte.gen.rs"]
mod generated;
pub(crate) use generated::SUPERGLUE_RTE_PACK;
pub use generated::{SuperglueRteConfig, SuperglueRteSplit};
fn map(pack: &str, value: &serde_json::Value, row: usize) -> Result<Vec<(Vec<String>, String)>> {
Ok(vec![(
vec![
jsonl::scalar_at(pack, value, "premise", row)?,
jsonl::scalar_at(pack, value, "hypothesis", row)?,
],
jsonl::scalar_at_opt(value, "label"),
)])
}
const SPEC: RecordSpec = RecordSpec {
fields: &["premise", "hypothesis"],
label_names: &["entailment", "not_entailment"],
classes: &["entailment", "not_entailment"],
map,
unlabeled: &["test.jsonl"],
};
fn member(split: SuperglueRteSplit) -> &'static str {
match split {
SuperglueRteSplit::Train => "train.jsonl",
SuperglueRteSplit::Validation => "val.jsonl",
SuperglueRteSplit::Test => "test.jsonl",
}
}
pub struct SuperglueRte {
data: RecordData,
}
impl SuperglueRte {
pub fn decode(config: &SuperglueRteConfig) -> Result<Decoded> {
Self::decode_with_progress(config, &mut |_| true)
}
pub fn decode_with_progress(
config: &SuperglueRteConfig,
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(&SUPERGLUE_RTE_PACK, &root, progress)?;
let (decoded, _) = recordl::decode_member(
"superglue_rte",
&dir.join("RTE.zip"),
member(config.split),
&SPEC,
)?;
Ok(decoded)
}
pub fn load_with_progress_on(
engine: &Engine,
config: &SuperglueRteConfig,
progress: &mut impl FnMut(FetchProgress) -> bool,
) -> Result<Self> {
let data = RecordData::load_on(
engine,
&SUPERGLUE_RTE_PACK,
std::path::Path::new("RTE.zip"),
member(config.split),
&SPEC,
config.cache_dir.clone(),
progress,
)?;
Ok(Self { data })
}
pub fn field(&self, field: usize) -> Result<&Matrix> {
self
.data
.fields
.get(field)
.ok_or_else(|| oa::Error::invalid_argument("superglue_rte field index out of range"))
}
#[must_use]
pub const fn labels(&self) -> Option<&Matrix> {
self.data.labels.as_ref()
}
#[must_use]
pub fn classes(&self) -> &[String] {
&self.data.classes
}
}
impl Dataset for SuperglueRte {
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)
}
}