2 releases

0.2.2 Jun 22, 2020
0.2.1 Jun 22, 2020

#987 in Text processing

Apache-2.0

59KB
1K SLoC

parattice: Recursive paraphrase lattice generator 🔄

This library takes a sentence and a paraphrase corpus, recursively finds paraphrases based on the corpus, expands the given sentence, and generates a paraphrase lattice.

This library also provides a method to search a phrase in the paraphrase lattice.

Example

This example generates a paraphrase lattice and searches a phrase in the generated lattice.

use parattice::Lattice;
use parattice::LatticeKMP;
use parattice::PaRattice;

// initialization
let paradict = vec![
    vec![
        vec!["blood", "stem", "cell"],
        vec!["造血", "", "細胞"],
        vec!["hematopoietic", "stem", "cell"],
    ],
    vec![
        vec!["造血", "", "細胞", "移植"],
        vec!["hematopoietic", "stem", "cell", "transplantation"],
    ],
    vec![vec!["stem", "cell"], vec!["", "細胞"]],
    vec![
        vec!["", "細胞", "移植"],
        vec!["rescue", "transplant"],
        vec!["stem", "cell", "rescue"],
    ],
    vec![vec!["rescue"], vec!["救命"]],
    vec![vec!["blood"], vec!["血液"]],
];
let parattice = PaRattice::new(paradict);

// lattice generation
let words = vec!["造血", "", "細胞", "移植"];
let lattice = parattice.get_lattice(&words, true, 2);

// dump a generated lattice
println!(lattice.dump_dot(true));

// serialization & deserialization
let bytes = lattice.to_bytes();
let new_lattice = Lattice::new_from_bytes(&bytes);

// search
let kmp = LatticeKMP::new(vec!["", "細胞"]);
let results = kmp.search(&new_lattice);
for result in &results {
    for edge in result {
        print!("({}, {}) ", edge.0, edge.1);
    }
    println!();
    let s = lattice.get_trunk_span(result.clone());
    for edge in &s {
        print!("({}, {}) ", edge.0, edge.1);
    }
    println!("\n===========");
}

Patents

License

Copyright 2020 Xcoo, Inc.

Licensed under the Apache License, Version 2.0.

Dependencies