-
Notifications
You must be signed in to change notification settings - Fork 915
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
Migrate string capitalize
APIs to pylibcudf
#15503
Changes from 19 commits
3d74c52
1f88eb3
d343a62
9f18dd0
f0ad27d
724c9fb
b47f4ee
080e552
226769c
c890a6c
a15dde3
98de87f
16b53f8
53c5cac
316fb5a
40d3cf9
8cbf5c1
9b66294
1081d66
4314467
6dcd38c
cc9ff42
e9e8b35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 + |
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 | ||
) |
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 |
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 |
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) |
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()) | ||
|
||
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)) |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine leaving this as-is, but if the entire module is just the enum I sort of wonder if we shouldn't just lift it up directly into the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For me the main justification for leaving it is that if I'd usually |
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, | ||
) |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# 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): | ||
# A sequence shall be all characters that are not a space | ||
# matches arrow for now | ||
str_char_type = ( | ||
plc.strings.char_types.StringCharacterTypes.ALL_TYPES | ||
& ~plc.strings.char_types.StringCharacterTypes.SPACE | ||
) | ||
brandon-b-miller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
got = plc.strings.capitalize.title(plc_data, str_char_type) | ||
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) |
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.
For consistency with other files I'd suggest putting this after the
rapids_cython_create_modules
call, but it doesn't really matter.