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

Polymorphic span replacement #200

Merged
merged 7 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ setup:
$(shell "${PROTOBUF_SETUP}")
$(shell "${OPENSSL_SETUP}")
which cargo || curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
which maturin || pip install maturin[patchelf]
undfined marked this conversation as resolved.
Show resolved Hide resolved
which maturin || pip install maturin

publish:
maturin publish
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "dolma"
version = "1.0.12"
version = "1.0.13"
description = "Data filters"
license = { text = "Apache-2.0" }
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion python/dolma/cli/mixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SpanReplacementConfig:
default=None,
help="Maximum score for the span to be replaced. Either min_score or max_score must be specified.",
)
replacement: str = field(default="", help="Replacement for the span")
undfined marked this conversation as resolved.
Show resolved Hide resolved
replacement: str = field(default="", help="Replacement config for the span(s).")
syntax: str = field(
default="jsonpath",
help="Syntax to use for filter expressions. Currently only JSONPath is supported. Defaults to JSONPath.",
Expand Down
38 changes: 33 additions & 5 deletions src/shard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,10 @@ impl Shard {
);
new_text.push_str(&replacement_text);
}

data["text"] = Value::String(new_text);
}
// }

for f in self.discard_fields.iter().flatten() {
data.as_object_mut().unwrap().remove(f);
}
Expand Down Expand Up @@ -467,7 +468,7 @@ impl Shard {
}

pub mod shard_config {
use crate::filters::Selector;
use crate::filters::{JqSelector, Selector};
use jsonpath_rust::JsonPathFinder;
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand Down Expand Up @@ -543,7 +544,34 @@ pub mod shard_config {
selector: Selector,
min_score: f64,
max_score: f64,
replacement: String,
replacement: Replacement,
}

pub enum Replacement {
Selectors(JqSelector),
String(String),
}

impl Replacement {
pub fn new(string: &str) -> Result<Replacement, IoError> {
// Note: Users should escape leading $ in replacement strings
if string.starts_with("$") {
// Strip leading $ and create a selector
let selector = JqSelector::new(&string[1..])?;
Ok(Replacement::Selectors(selector))
} else {
Ok(Replacement::String(string.to_string()))
}
}

pub fn get(&self, json: &Value) -> Result<String, IoError> {
match self {
Replacement::Selectors(selector) => {
Ok(serde_json::from_value(selector.select(json)?.to_owned()).unwrap())
}
Replacement::String(s) => Ok(s.clone()),
}
}
}

impl SpanReplacer {
Expand All @@ -553,7 +581,7 @@ pub mod shard_config {
selector: Selector::new(&config).unwrap(),
min_score: config.min_score.unwrap_or(f64::NEG_INFINITY),
max_score: config.max_score.unwrap_or(f64::INFINITY),
replacement: config.replacement.clone(),
replacement: Replacement::new(&config.replacement).unwrap(),
}
}

Expand All @@ -575,7 +603,7 @@ pub mod shard_config {
let replacement = SpanReplacement {
start: start as usize,
end: end as usize,
replacement: self.replacement.clone(),
replacement: self.replacement.get(json).unwrap(),
};
Some(replacement)
} else {
Expand Down
Loading