-
Notifications
You must be signed in to change notification settings - Fork 7
Use internal result as regex return type #51
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include "pytorch/tokenizers/regex.h" | ||
#include "pytorch/tokenizers/re2_regex.h" | ||
#include "pytorch/tokenizers/pcre2_regex.h" | ||
#include "pytorch/tokenizers/re2_regex.h" | ||
#include "pytorch/tokenizers/regex.h" | ||
#include "pytorch/tokenizers/result.h" | ||
|
||
// Test basic functionality | ||
TEST(RegexTest, BasicMatching) { | ||
auto regex = createRegex("\\w+"); | ||
auto regex = TK_UNWRAP_THROW(createRegex("\\w+")); | ||
ASSERT_TRUE(regex->ok()); | ||
|
||
std::string text = "Hello world"; | ||
|
@@ -24,9 +25,9 @@ TEST(RegexTest, Pcre2Specific) { | |
const std::string pattern = "(?<=@)\\w+"; | ||
Re2Regex re2_regex(pattern); | ||
ASSERT_FALSE(re2_regex.ok()); | ||
|
||
// Now verify that the factory function fallsback on a PCRE2 regex | ||
auto regex = createRegex(pattern); | ||
auto regex = TK_UNWRAP_THROW(createRegex(pattern)); | ||
ASSERT_TRUE(regex->ok()); | ||
|
||
std::string text = "[email protected]"; | ||
|
@@ -40,20 +41,21 @@ TEST(RegexTest, Pcre2Specific) { | |
// This specific pattern is from the Qwen2.5 1.5B pretokenizer. | ||
// https://huggingface.co/Qwen/Qwen2.5-1.5B/raw/main/tokenizer.json | ||
TEST(RegexTest, ComplexPatternWithNegativeLookahead) { | ||
const std::string complex_pattern = "(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\\r\\n\\p{L}\\p{N}]?\\p{L}+|\\p{N}| ?[^\\s\\p{L}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+"; | ||
|
||
const std::string complex_pattern = | ||
"(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\\r\\n\\p{L}\\p{N}]?\\p{L}+|\\p{N}| ?[^\\s\\p{L}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+"; | ||
|
||
// First verify that RE2 cannot handle this pattern | ||
Re2Regex re2_regex(complex_pattern); | ||
ASSERT_FALSE(re2_regex.ok()); | ||
|
||
// Now verify that the factory function fallsback on a PCRE2 regex | ||
auto regex = createRegex(complex_pattern); | ||
auto regex = TK_UNWRAP_THROW(createRegex(complex_pattern)); | ||
ASSERT_TRUE(regex->ok()); | ||
|
||
// Test the pattern with some sample text | ||
std::string text = "Hello's world\n test"; | ||
auto matches = regex->findAll(text); | ||
|
||
// We expect to match: | ||
// 1. "Hello" (word) | ||
// 2. "'s" (contraction) | ||
|
@@ -62,22 +64,22 @@ TEST(RegexTest, ComplexPatternWithNegativeLookahead) { | |
// 5. " " (whitespace) | ||
// 6. " test" (word with leading space) | ||
ASSERT_EQ(matches.size(), 6); | ||
|
||
EXPECT_EQ(matches[0].text, "Hello"); | ||
EXPECT_EQ(matches[0].position, 0); | ||
|
||
EXPECT_EQ(matches[1].text, "'s"); | ||
EXPECT_EQ(matches[1].position, 5); | ||
|
||
EXPECT_EQ(matches[2].text, " world"); | ||
EXPECT_EQ(matches[2].position, 7); | ||
|
||
EXPECT_EQ(matches[3].text, "\n"); | ||
EXPECT_EQ(matches[3].position, 13); | ||
|
||
EXPECT_EQ(matches[4].text, " "); | ||
EXPECT_EQ(matches[4].position, 14); | ||
|
||
EXPECT_EQ(matches[5].text, " test"); | ||
EXPECT_EQ(matches[5].position, 15); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No ET code throws exceptions afaik, will we guard it from tokenizers now?