3 unstable releases
0.4.2 | Apr 28, 2024 |
---|---|
0.4.1 |
|
0.4.0 | Mar 5, 2024 |
0.3.2 | Oct 24, 2023 |
#1098 in Text processing
Used in 3 crates
(via afrim-preprocessor)
26KB
247 lines
Afrim Memory
Make the handling of sequential codes easier.
lib.rs
:
Data structure to make handling of sequential code more convenient.
It takes sequential codes and generates a text buffer that will be used to easily get a corresponding character through an input.
Notes
- sequence: A sequential code corresponding to a character. Eg. af1 = "ɑ̀"
- input: The user input (or a set of sequences). Eg. ngaf7 nkwe2e2 ka7meru7n
- text buffer: The memory where our text data will be stored.
- node: A node in the text buffer.
Example
use afrim_memory::{Node, utils};
// Builds a TextBuffer.
let text_buffer = Node::default();
text_buffer.insert(vec!['a', 'f'], "ɑ".to_owned());
text_buffer.insert(vec!['a', 'f', '1'], "ɑ̀".to_owned());
// Bulk insertion of data in the TextBuffer.
let data = vec![vec!["af11", "ɑ̀ɑ̀"], vec!["?.", "ʔ"]];
let text_buffer = utils::build_map(data);
// Traverses the tree.
let node = text_buffer.goto('a').and_then(|node| node.goto('f')).and_then(|node| node.goto('1')).and_then(|node| node.goto('1'));
assert_eq!(node.unwrap().take(), Some("ɑ̀ɑ̀".to_owned()));
Example: in reading data through a file
use afrim_memory::utils;
// Import data from a string.
let data = "a1 à\ne2 é";
let data = utils::load_data(data);
let text_buffer = utils::build_map(data);
Example: with the usage of a cursor
use afrim_memory::{Cursor, Node};
use std::rc::Rc;
// Build a TextBuffer.
let text_buffer = Node::default();
text_buffer.insert(vec!['i', '-'], "ɨ".to_owned());
text_buffer.insert(vec!['i', '-', '3'], "ɨ̄".to_owned());
// Builds the cursor.
let memory = Rc::new(text_buffer);
let mut cursor = Cursor::new(memory, 16);
// Moves the cursor through the input.
let input = "i-3";
input.chars().for_each(|c| { cursor.hit(c); });
// Verify the current state.
assert_eq!(cursor.state(), (Some("ɨ̄".to_owned()), 3, '3'));
// Undo the last insertion.
assert_eq!(cursor.undo(), Some("ɨ̄".to_owned()));
// Verify the current state.
assert_eq!(cursor.state(), (Some("ɨ".to_owned()), 2, '-'));