Skip to content

Commit

Permalink
validate: Implemented text validation (annotation/stam-rust#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
proycon committed May 25, 2024
1 parent 5296c89 commit e4bbc8a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
31 changes: 26 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ fn tsv_arguments_out<'a>() -> Vec<clap::Arg<'a>> {
Arg::with_name("columns")
.long("columns")
.short('C')
.help("Column Format, comma separated list of column names to output")
.help("Column Format, comma separated list of column names to output (or input depending on context)")
.long_help(
"In most cases, you do not need to explicitly specify this as it will be automatically guessed based on the --type or --query parameter.
However, if you want full control, you can choose from the following known columns names (case insensitive, comma seperated list):
Expand Down Expand Up @@ -380,7 +380,7 @@ In addition of the above columns, you may also parse a *custom* column by specif
Arg::with_name("validate")
.long("validate")
.help(
"Do text validation, values: strict, loose (case insensitive testing, this is the default), no"
"Do text validation on the TSV, values: strict, loose (case insensitive testing, this is the default), no"
)
.default_value("loose")
.takes_value(true),
Expand Down Expand Up @@ -655,6 +655,21 @@ fn xml_arguments<'a>() -> Vec<clap::Arg<'a>> {
args
}

fn validation_arguments<'a>() -> Vec<clap::Arg<'a>> {
let mut args: Vec<Arg> = Vec::new();
args.push(
Arg::with_name("make")
.long("make")
.help("Compute text validation information, allowing the model to be validated later.")
);
args.push(
Arg::with_name("allow-incomplete")
.long("allow-incomplete")
.help("Allow validation to pass even if validation information is missing for certain annotations (or for all)")
);
args
}

fn store_exists(args: &ArgMatches) -> bool {
if args.is_present("annotationstore") {
for filename in args
Expand Down Expand Up @@ -718,10 +733,11 @@ fn app<'a>(batchmode: bool) -> App<'a> {
)
.subcommand(
SubCommand::with_name("validate")
.about("Validate a STAM model. Set --verbose to have it output the STAM JSON or STAM CSV to standard output.")
.about("Validate a STAM model. Checks if the integrity of the annotations is still valid by checking if the text they point at remains unchanged.")
.args(&common_arguments())
.args(&store_arguments(true, false, batchmode))
.args(&config_arguments()),
.args(&config_arguments())
.args(&validation_arguments()),
)
.subcommand(
SubCommand::with_name("export")
Expand Down Expand Up @@ -1366,7 +1382,12 @@ fn run<W: Write>(store: &mut AnnotationStore, writer: &mut W, rootargs: &ArgMat
None => unreachable!(),
}
} else if rootargs.subcommand_matches("validate").is_some() {
validate(&store, args.is_present("verbose"))?;
if args.is_present("make") {
store.protect_text(stam::TextValidationMode::Auto).map_err(|e| format!("Failed to generate validation information: {}", e))?;
changed = true;
} else {
validate(&store, args.is_present("verbose"), args.is_present("allow-incomplete"))?;
}
} else if rootargs.subcommand_matches("init").is_some()
|| rootargs.subcommand_matches("annotate").is_some()
{
Expand Down
39 changes: 27 additions & 12 deletions src/validate.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
use stam::{AnnotationStore, Configurable, ToJson};
use stam::AnnotationStore;

pub fn validate(store: &AnnotationStore, verbose: bool) -> Result<(), String> {
let result = store.to_json_string(&store.config().clone().with_use_include(false));
match result {
Ok(result) => {
if verbose {
println!("{}", result)
}
}
Err(err) => {
return Err(format!("Error during serialization: {}", err));
pub fn validate(
store: &AnnotationStore,
verbose: bool,
allow_incomplete: bool,
) -> Result<(), String> {
let result = store.validate_text(!verbose);
let valid = if allow_incomplete {
result.is_ok_maybe_incomplete()
} else {
result.is_ok()
};
if valid {
if verbose {
eprintln!("Succesfully validated {} annotations", result.valid());
}
Ok(())
} else {
Err(format!(
"Failed to validate {} annotations, {} missing. {}",
result.invalid(),
result.missing(),
if result.missing() > 0 {
"Did you generate validation information? (run: stam validate --make)"
} else {
""
}
))
}
Ok(())
}

0 comments on commit e4bbc8a

Please sign in to comment.