gliner/model/input/
text.rs1use std::path::Path;
2use crate::util::result::Result;
3
4#[derive(Debug, Clone)]
6pub struct TextInput {
7 pub texts: Vec<String>,
8 pub entities: Vec<String>,
9}
10
11
12impl TextInput {
13
14 pub fn new(texts: Vec<String>, entities: Vec<String>) -> Result<Self> {
17 if texts.is_empty() || entities.is_empty() {
18 Err("invalid input: empty texts and/or entities".into())
19 }
20 else {
21 Ok(Self { texts, entities })
22 }
23 }
24
25 pub fn from_str(texts: &[&str], entities: &[&str]) -> Result<Self> {
27 Self::new(
28 texts.iter().map(|s| s.to_string()).collect(),
29 entities.iter().map(|s| s.to_string()).collect(),
30 )
31 }
32
33 pub fn new_from_csv<P: AsRef<Path>>(path: P, column: usize, limit: usize, entities: Vec<String>) -> Result<Self> {
36 let mut csv = csv::Reader::from_path(path)?;
37 let texts: Vec<String> = csv.records()
38 .take(limit)
39 .map(|r| r.unwrap().get(column).unwrap().to_string())
40 .collect();
41 Self::new(texts, entities)
42 }
43
44}