Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract regex and add fancy_regex backend #908

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

.vim
.env
.venv
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. That's a user thing it does not belong here.

Please use a global .gitignore for you (then you also only define it once on your machine).

target
.idea
Cargo.lock
Expand Down
10 changes: 8 additions & 2 deletions tokenizers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ harness = false
[dependencies]
lazy_static = "1.4"
rand = "0.7"
onig = { version = "6.0", default-features = false }
regex = "1.3"
regex-syntax = "0.6"
rayon = "1.3"
Expand All @@ -59,11 +58,18 @@ cached-path = { version = "0.5", optional = true }
aho-corasick = "0.7"
paste = "1.0.6"
proc_macros = { path = "./src/utils/proc_macros" }
once_cell = "1.8"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need that either.

Even keeping the idea of fancy-regex, a simple enum is OK.
And even then, we don't want an enum. We want something that uses the correct regex engine ( onig or onig-rs) directly at compilation time.

cfg-if = "1"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's needed actually.

You can probably write this as pure rust compilation #cfg.

onig = { version = "6.0", default-features = false, optional = true }
fancy-regex = { version = "0.7", optional = true }

[features]
default = ["progressbar", "http"]
default = ["progressbar", "http", "regex-onig"]
progressbar = ["indicatif"]
http = ["reqwest", "cached-path"]
regex-fancy = ["fancy-regex"]
regex-onig = ["onig"]
regex-all-test = ["regex-onig", "regex-fancy"]

[dev-dependencies]
criterion = "0.3"
Expand Down
6 changes: 3 additions & 3 deletions tokenizers/src/normalizers/replace.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::tokenizer::{NormalizedString, Normalizer, Result};
use onig::Regex;
use crate::utils::regex::Regex;
use serde::{Deserialize, Serialize};

/// Represents the different patterns that `Replace` can use
Expand Down Expand Up @@ -65,8 +65,8 @@ impl Replace {
pub fn new<I: Into<ReplacePattern>, C: Into<String>>(pattern: I, content: C) -> Result<Self> {
let pattern: ReplacePattern = pattern.into();
let regex = match &pattern {
ReplacePattern::String(s) => Regex::new(&regex::escape(s))?,
ReplacePattern::Regex(r) => Regex::new(r)?,
ReplacePattern::String(s) => Regex::new(regex::escape(s)),
ReplacePattern::Regex(r) => Regex::new(r.to_owned()),
};

Ok(Self {
Expand Down
5 changes: 2 additions & 3 deletions tokenizers/src/pre_tokenizers/byte_level.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::{HashMap, HashSet};

use onig::Regex;
use crate::utils::regex::Regex;
use serde::{Deserialize, Serialize};

use crate::tokenizer::{
Expand Down Expand Up @@ -34,8 +34,7 @@ fn bytes_char() -> HashMap<u8, char> {

lazy_static! {
static ref RE: Regex =
Regex::new(r"'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+")
.unwrap();
Regex::new(r"'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+".to_string());
static ref BYTES_CHAR: HashMap<u8, char> = bytes_char();
static ref CHAR_BYTES: HashMap<char, u8> =
bytes_char().into_iter().map(|(c, b)| (b, c)).collect();
Expand Down
6 changes: 3 additions & 3 deletions tokenizers/src/pre_tokenizers/split.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use onig::Regex;
use crate::utils::regex::Regex;
use serde::{Deserialize, Deserializer, Serialize};

use crate::tokenizer::{
Expand Down Expand Up @@ -80,8 +80,8 @@ impl Split {
) -> Result<Self> {
let pattern: SplitPattern = pattern.into();
let regex = match &pattern {
SplitPattern::String(s) => Regex::new(&regex::escape(s))?,
SplitPattern::Regex(r) => Regex::new(r)?,
SplitPattern::String(s) => Regex::new(regex::escape(s)),
SplitPattern::Regex(r) => Regex::new(r.to_owned()),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to refrain making everything owned when the compiler asks you too.

It's the easy way out, but the incorrect way out most likely.
Enjoy the ride of understanding lifetimes :) . In my experience, it should be roughly trivial to add lifetimes (when you know how that works), and when it's not, usually the design has some flaw actually (which the compiler warns me about actually)

};

Ok(Self {
Expand Down
16 changes: 8 additions & 8 deletions tokenizers/src/tokenizer/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,20 @@ impl Pattern for &Regex {
}
}

impl Pattern for &onig::Regex {
impl Pattern for &crate::utils::regex::Regex {
fn find_matches(&self, inside: &str) -> Result<Vec<(Offsets, bool)>> {
if inside.is_empty() {
return Ok(vec![((0, 0), false)]);
}

let mut prev = 0;
let mut splits = Vec::with_capacity(inside.len());
for (start, end) in self.find_iter(inside) {
if prev != start {
splits.push(((prev, start), false));
for m in self.find_iter(inside) {
if prev != m.start() {
splits.push(((prev, m.start()), false));
}
splits.push(((start, end), true));
prev = end;
splits.push(((m.start(), m.end()), true));
prev = m.end();
}
if prev != inside.len() {
splits.push(((prev, inside.len()), false))
Expand Down Expand Up @@ -205,8 +205,8 @@ mod tests {
}

#[test]
fn onig_regex() {
let is_whitespace = onig::Regex::new(r"\s+").unwrap();
fn abstract_regex() {
let is_whitespace = crate::utils::regex::Regex::new(r"\s+".to_string());
do_test!("a b", &is_whitespace => vec![((0, 1), false), ((1, 4), true), ((4, 5), false)]);
do_test!(" a b ", &is_whitespace =>
vec![((0, 3), true), ((3, 4), false), ((4, 7), true), ((7, 8), false), ((8, 11), true)]
Expand Down
1 change: 1 addition & 0 deletions tokenizers/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod padding;
pub mod parallelism;
pub(crate) mod progress;
pub mod truncation;
pub mod regex;

use serde::{Serialize, Serializer};
use std::collections::{BTreeMap, HashMap};
Expand Down
Loading