-
Notifications
You must be signed in to change notification settings - Fork 932
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate remaining nvtext NGrams APIs to pylibcudf (#17070)
Apart of #15162 Authors: - Matthew Murray (https://github.com/Matt711) Approvers: - https://github.com/brandon-b-miller URL: #17070
- Loading branch information
Showing
9 changed files
with
135 additions
and
40 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 |
---|---|---|
|
@@ -8,3 +8,4 @@ nvtext | |
generate_ngrams | ||
jaccard | ||
minhash | ||
ngrams_tokenize |
6 changes: 6 additions & 0 deletions
6
docs/cudf/source/user_guide/api_docs/pylibcudf/nvtext/ngrams_tokenize.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 @@ | ||
=============== | ||
ngrams_tokenize | ||
=============== | ||
|
||
.. automodule:: pylibcudf.nvtext.ngrams_tokenize | ||
: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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from . cimport edit_distance, generate_ngrams, jaccard, minhash | ||
from . cimport ( | ||
edit_distance, | ||
generate_ngrams, | ||
jaccard, | ||
minhash, | ||
ngrams_tokenize, | ||
) | ||
|
||
__all__ = [ | ||
"edit_distance", | ||
"generate_ngrams", | ||
"jaccard", | ||
"minhash" | ||
"minhash", | ||
"ngrams_tokenize" | ||
] |
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,10 +1,11 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from . import edit_distance, generate_ngrams, jaccard, minhash | ||
from . import edit_distance, generate_ngrams, jaccard, minhash, ngrams_tokenize | ||
|
||
__all__ = [ | ||
"edit_distance", | ||
"generate_ngrams", | ||
"jaccard", | ||
"minhash", | ||
"ngrams_tokenize", | ||
] |
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,13 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from pylibcudf.column cimport Column | ||
from pylibcudf.libcudf.types cimport size_type | ||
from pylibcudf.scalar cimport Scalar | ||
|
||
|
||
cpdef Column ngrams_tokenize( | ||
Column input, | ||
size_type ngrams, | ||
Scalar delimiter, | ||
Scalar separator | ||
) |
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,54 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from cython.operator cimport dereference | ||
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.ngrams_tokenize cimport ( | ||
ngrams_tokenize as cpp_ngrams_tokenize, | ||
) | ||
from pylibcudf.libcudf.scalar.scalar cimport string_scalar | ||
from pylibcudf.libcudf.types cimport size_type | ||
from pylibcudf.scalar cimport Scalar | ||
|
||
|
||
cpdef Column ngrams_tokenize( | ||
Column input, | ||
size_type ngrams, | ||
Scalar delimiter, | ||
Scalar separator | ||
): | ||
""" | ||
Returns a single column of strings by tokenizing the input strings column | ||
and then producing ngrams of each string. | ||
For details, see :cpp:func:`ngrams_tokenize` | ||
Parameters | ||
---------- | ||
input : Column | ||
Input strings | ||
ngrams : size_type | ||
The ngram number to generate | ||
delimiter : Scalar | ||
UTF-8 characters used to separate each string into tokens. | ||
An empty string will separate tokens using whitespace. | ||
separator : Scalar | ||
The string to use for separating ngram tokens | ||
Returns | ||
------- | ||
Column | ||
New strings columns of tokens | ||
""" | ||
cdef unique_ptr[column] c_result | ||
|
||
with nogil: | ||
c_result = cpp_ngrams_tokenize( | ||
input.view(), | ||
ngrams, | ||
dereference(<const string_scalar*>delimiter.get()), | ||
dereference(<const string_scalar*>separator.get()), | ||
) | ||
return Column.from_libcudf(move(c_result)) |
37 changes: 37 additions & 0 deletions
37
python/pylibcudf/pylibcudf/tests/test_nvtext_ngrams_tokenize.py
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,37 @@ | ||
# 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 input_col(): | ||
arr = ["a*b*c*d", "a b c d", "a-b-c-d", "a*b c-d"] | ||
return pa.array(arr) | ||
|
||
|
||
@pytest.mark.parametrize("ngrams", [2, 3]) | ||
@pytest.mark.parametrize("delim", ["*", " ", "-"]) | ||
@pytest.mark.parametrize("sep", ["_", "&", ","]) | ||
def test_ngrams_tokenize(input_col, ngrams, delim, sep): | ||
def ngrams_tokenize(strings, ngrams, delim, sep): | ||
tokens = [] | ||
for s in strings: | ||
ss = s.split(delim) | ||
for i in range(len(ss) - ngrams + 1): | ||
token = sep.join(ss[i : i + ngrams]) | ||
tokens.append(token) | ||
return tokens | ||
|
||
result = plc.nvtext.ngrams_tokenize.ngrams_tokenize( | ||
plc.interop.from_arrow(input_col), | ||
ngrams, | ||
plc.interop.from_arrow(pa.scalar(delim)), | ||
plc.interop.from_arrow(pa.scalar(sep)), | ||
) | ||
expected = pa.array( | ||
ngrams_tokenize(input_col.to_pylist(), ngrams, delim, sep) | ||
) | ||
assert_column_eq(result, expected) |