-
Notifications
You must be signed in to change notification settings - Fork 914
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate NVText Normalizing APIs to Pylibcudf (#17072)
Apart of #15162. Authors: - Matthew Murray (https://github.com/Matt711) Approvers: - Bradley Dice (https://github.com/bdice) URL: #17072
- Loading branch information
Showing
10 changed files
with
155 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -9,3 +9,4 @@ nvtext | |
jaccard | ||
minhash | ||
ngrams_tokenize | ||
normalize |
6 changes: 6 additions & 0 deletions
6
docs/cudf/source/user_guide/api_docs/pylibcudf/nvtext/normalize.rst
This file contains 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
========= | ||
normalize | ||
========= | ||
|
||
.. automodule:: pylibcudf.nvtext.normalize | ||
:members: |
This file contains 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 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 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 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 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,11 +1,19 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from . import edit_distance, generate_ngrams, jaccard, minhash, ngrams_tokenize | ||
from . import ( | ||
edit_distance, | ||
generate_ngrams, | ||
jaccard, | ||
minhash, | ||
ngrams_tokenize, | ||
normalize, | ||
) | ||
|
||
__all__ = [ | ||
"edit_distance", | ||
"generate_ngrams", | ||
"jaccard", | ||
"minhash", | ||
"ngrams_tokenize", | ||
"normalize", | ||
] |
This file contains 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from libcpp cimport bool | ||
from pylibcudf.column cimport Column | ||
|
||
|
||
cpdef Column normalize_spaces(Column input) | ||
|
||
cpdef Column normalize_characters(Column input, bool do_lower_case) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from libcpp cimport bool | ||
from libcpp.memory cimport unique_ptr | ||
from libcpp.utility cimport move | ||
from pylibcudf.column cimport Column | ||
from pylibcudf.libcudf.column.column cimport column | ||
from pylibcudf.libcudf.nvtext.normalize cimport ( | ||
normalize_characters as cpp_normalize_characters, | ||
normalize_spaces as cpp_normalize_spaces, | ||
) | ||
|
||
|
||
cpdef Column normalize_spaces(Column input): | ||
""" | ||
Returns a new strings column by normalizing the whitespace in | ||
each string in the input column. | ||
For details, see :cpp:func:`normalize_spaces` | ||
Parameters | ||
---------- | ||
input : Column | ||
Input strings | ||
Returns | ||
------- | ||
Column | ||
New strings columns of normalized strings. | ||
""" | ||
cdef unique_ptr[column] c_result | ||
|
||
with nogil: | ||
c_result = cpp_normalize_spaces(input.view()) | ||
|
||
return Column.from_libcudf(move(c_result)) | ||
|
||
|
||
cpdef Column normalize_characters(Column input, bool do_lower_case): | ||
""" | ||
Normalizes strings characters for tokenizing. | ||
For details, see :cpp:func:`normalize_characters` | ||
Parameters | ||
---------- | ||
input : Column | ||
Input strings | ||
do_lower_case : bool | ||
If true, upper-case characters are converted to lower-case | ||
and accents are stripped from those characters. If false, | ||
accented and upper-case characters are not transformed. | ||
Returns | ||
------- | ||
Column | ||
Normalized strings column | ||
""" | ||
cdef unique_ptr[column] c_result | ||
|
||
with nogil: | ||
c_result = cpp_normalize_characters(input.view(), do_lower_case) | ||
|
||
return Column.from_libcudf(move(c_result)) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
import pyarrow as pa | ||
import pylibcudf as plc | ||
import pytest | ||
from utils import assert_column_eq | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def norm_spaces_input_data(): | ||
arr = ["a b", " c d\n", "e \t f "] | ||
return pa.array(arr) | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def norm_chars_input_data(): | ||
arr = ["éâîô\teaio", "ĂĆĖÑÜ", "ACENU", "$24.08", "[a,bb]"] | ||
return pa.array(arr) | ||
|
||
|
||
def test_normalize_spaces(norm_spaces_input_data): | ||
result = plc.nvtext.normalize.normalize_spaces( | ||
plc.interop.from_arrow(norm_spaces_input_data) | ||
) | ||
expected = pa.array(["a b", "c d", "e f"]) | ||
assert_column_eq(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize("do_lower", [True, False]) | ||
def test_normalize_characters(norm_chars_input_data, do_lower): | ||
result = plc.nvtext.normalize.normalize_characters( | ||
plc.interop.from_arrow(norm_chars_input_data), | ||
do_lower, | ||
) | ||
expected = pa.array( | ||
["eaio eaio", "acenu", "acenu", " $ 24 . 08", " [ a , bb ] "] | ||
) | ||
if not do_lower: | ||
expected = pa.array( | ||
["éâîô eaio", "ĂĆĖÑÜ", "ACENU", " $ 24 . 08", " [ a , bb ] "] | ||
) | ||
assert_column_eq(result, expected) |