-
Notifications
You must be signed in to change notification settings - Fork 912
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR creates the `pylibcudf.strings.capitalize` namespace and migrates the cuDF cython to use it. Depends on #15489 Part of #15162 Authors: - https://github.com/brandon-b-miller Approvers: - Kyle Edwards (https://github.com/KyleFromNVIDIA) - Vyas Ramasubramani (https://github.com/vyasr) URL: #15503
- Loading branch information
1 parent
27220d6
commit bdafa73
Showing
17 changed files
with
217 additions
and
49 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
10 changes: 10 additions & 0 deletions
10
python/cudf/cudf/_lib/pylibcudf/libcudf/scalar/scalar_factories.pxd
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,10 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from libcpp.memory cimport unique_ptr | ||
from libcpp.string cimport string | ||
|
||
from cudf._lib.pylibcudf.libcudf.scalar.scalar cimport scalar | ||
|
||
|
||
cdef extern from "cudf/scalar/scalar_factories.hpp" namespace "cudf" nogil: | ||
cdef unique_ptr[scalar] make_string_scalar(const string & _string) except + |
23 changes: 23 additions & 0 deletions
23
python/cudf/cudf/_lib/pylibcudf/libcudf/strings/CMakeLists.txt
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,23 @@ | ||
# ============================================================================= | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
# in compliance with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software distributed under the License | ||
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
# or implied. See the License for the specific language governing permissions and limitations under | ||
# the License. | ||
# ============================================================================= | ||
|
||
set(cython_sources char_types.pyx) | ||
|
||
set(linked_libraries cudf::cudf) | ||
|
||
rapids_cython_create_modules( | ||
CXX | ||
SOURCE_FILES "${cython_sources}" | ||
LINKED_LIBRARIES "${linked_libraries}" ASSOCIATED_TARGETS cudf MODULE_PREFIX cpp_strings | ||
) |
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
Empty file.
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,3 +1,3 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from . cimport case, find | ||
from . cimport capitalize, case, char_types, find |
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,3 +1,3 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from . import case, find | ||
from . import capitalize, case, char_types, find |
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 cudf._lib.pylibcudf.column cimport Column | ||
from cudf._lib.pylibcudf.scalar cimport Scalar | ||
|
||
|
||
cpdef Column capitalize(Column input, Scalar delimiters=*) | ||
cpdef Column title(Column input) | ||
cpdef Column is_title(Column input) |
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,62 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from libcpp.memory cimport unique_ptr | ||
from libcpp.utility cimport move | ||
|
||
from cudf._lib.pylibcudf.column cimport Column | ||
from cudf._lib.pylibcudf.libcudf.column.column cimport column | ||
from cudf._lib.pylibcudf.libcudf.scalar.scalar cimport string_scalar | ||
from cudf._lib.pylibcudf.libcudf.scalar.scalar_factories cimport ( | ||
make_string_scalar as cpp_make_string_scalar, | ||
) | ||
from cudf._lib.pylibcudf.libcudf.strings cimport capitalize as cpp_capitalize | ||
from cudf._lib.pylibcudf.scalar cimport Scalar | ||
from cudf._lib.pylibcudf.strings.char_types cimport string_character_types | ||
|
||
from cython.operator import dereference | ||
|
||
|
||
cpdef Column capitalize( | ||
Column input, | ||
Scalar delimiters=None | ||
# TODO: default scalar values | ||
# https://github.com/rapidsai/cudf/issues/15505 | ||
): | ||
|
||
cdef unique_ptr[column] c_result | ||
|
||
if delimiters is None: | ||
delimiters = Scalar.from_libcudf( | ||
cpp_make_string_scalar("".encode()) | ||
) | ||
|
||
cdef const string_scalar* cpp_delimiters = <const string_scalar*>( | ||
delimiters.c_obj.get() | ||
) | ||
|
||
with nogil: | ||
c_result = cpp_capitalize.capitalize( | ||
input.view(), | ||
dereference(cpp_delimiters) | ||
) | ||
|
||
return Column.from_libcudf(move(c_result)) | ||
|
||
|
||
cpdef Column title( | ||
Column input, | ||
string_character_types sequence_type=string_character_types.ALPHA | ||
): | ||
cdef unique_ptr[column] c_result | ||
with nogil: | ||
c_result = cpp_capitalize.title(input.view(), sequence_type) | ||
|
||
return Column.from_libcudf(move(c_result)) | ||
|
||
|
||
cpdef Column is_title(Column input): | ||
cdef unique_ptr[column] c_result | ||
with nogil: | ||
c_result = cpp_capitalize.is_title(input.view()) | ||
|
||
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,5 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from cudf._lib.pylibcudf.libcudf.strings.char_types cimport ( | ||
string_character_types, | ||
) |
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,4 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from cudf._lib.pylibcudf.libcudf.strings.char_types import \ | ||
string_character_types as StringCharacterTypes # no-cython-lint |
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
54 changes: 54 additions & 0 deletions
54
python/cudf/cudf/pylibcudf_tests/test_string_capitalize.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,54 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
import pyarrow as pa | ||
import pytest | ||
from utils import assert_column_eq | ||
|
||
import cudf._lib.pylibcudf as plc | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def pa_data(): | ||
data = [ | ||
"leopard", | ||
"Golden Eagle", | ||
"SNAKE", | ||
"", | ||
"!A", | ||
"hello World", | ||
"A B C", | ||
"#", | ||
"AƻB", | ||
"Ⓑⓖ", | ||
"Art of War", | ||
"The quick bRoWn fox juMps over the laze DOG", | ||
'123nr98nv9rev!$#INF4390v03n1243<>?}{:-"', | ||
"accénted", | ||
None, | ||
] | ||
return pa.array(data) | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def plc_data(pa_data): | ||
return plc.interop.from_arrow(pa_data) | ||
|
||
|
||
def test_capitalize(plc_data, pa_data): | ||
got = plc.strings.capitalize.capitalize(plc_data) | ||
expected = pa.compute.utf8_capitalize(pa_data) | ||
assert_column_eq(got, expected) | ||
|
||
|
||
def test_title(plc_data, pa_data): | ||
got = plc.strings.capitalize.title( | ||
plc_data, plc.strings.char_types.StringCharacterTypes.CASE_TYPES | ||
) | ||
expected = pa.compute.utf8_title(pa_data) | ||
assert_column_eq(got, expected) | ||
|
||
|
||
def test_is_title(plc_data, pa_data): | ||
got = plc.strings.capitalize.is_title(plc_data) | ||
expected = pa.compute.utf8_is_title(pa_data) | ||
assert_column_eq(got, expected) |