Skip to content
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

Merged
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -3,11 +3,14 @@ from libcpp.memory cimport unique_ptr

from cudf._lib.pylibcudf.libcudf.column.column cimport column
from cudf._lib.pylibcudf.libcudf.column.column_view cimport column_view
from cudf._lib.pylibcudf.libcudf.scalar.scalar cimport string_scalar


cdef extern from "cudf/strings/capitalize.hpp" namespace "cudf::strings" nogil:
cdef unique_ptr[column] capitalize(
const column_view & strings) except +
const column_view & strings,
const string_scalar & delimiters
) except +

cdef unique_ptr[column] title(
const column_view & strings) except +
Expand Down
6 changes: 6 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/libcudf/strings/case.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ from cudf._lib.pylibcudf.libcudf.column.column_view cimport column_view


cdef extern from "cudf/strings/case.hpp" namespace "cudf::strings" nogil:
cdef unique_ptr[column] capitalize(
const column_view & input) except +

cdef unique_ptr[column] is_title(
const column_view & input) except +

cdef unique_ptr[column] to_lower(
const column_view & strings) except +

Expand Down
5 changes: 4 additions & 1 deletion python/cudf/cudf/_lib/pylibcudf/strings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
# the License.
# =============================================================================

set(cython_sources case.pyx find.pyx)
set(cython_sources capitalize.pyx case.pyx find.pyx)

set(linked_libraries cudf::cudf)
rapids_cython_create_modules(
CXX
SOURCE_FILES "${cython_sources}"
LINKED_LIBRARIES "${linked_libraries}" MODULE_PREFIX pylibcudf_ ASSOCIATED_TARGETS cudf
)

link_to_pyarrow_headers(pylibcudf_capitalize)
brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion python/cudf/cudf/_lib/pylibcudf/strings/__init__.pxd
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, find
2 changes: 1 addition & 1 deletion python/cudf/cudf/_lib/pylibcudf/strings/__init__.py
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, find
9 changes: 9 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/strings/capitalize.pxd
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)
59 changes: 59 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/strings/capitalize.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# 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 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
cdef const string_scalar* cpp_delimiters
brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved

if delimiters is None:
delimiters = Scalar.from_libcudf(
cpp_make_string_scalar("".encode())
)

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):
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))
48 changes: 17 additions & 31 deletions python/cudf/cudf/_lib/strings/capitalize.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,33 @@

from cudf.core.buffer import acquire_spill_lock

from libcpp.memory cimport unique_ptr
from libcpp.utility cimport move

from cudf._lib.column cimport Column
from cudf._lib.pylibcudf.libcudf.column.column cimport column
from cudf._lib.pylibcudf.libcudf.column.column_view cimport column_view
from cudf._lib.pylibcudf.libcudf.strings.capitalize cimport (
capitalize as cpp_capitalize,
is_title as cpp_is_title,
title as cpp_title,
)

import cudf._lib.pylibcudf as plc


@acquire_spill_lock()
def capitalize(Column source_strings):
cdef unique_ptr[column] c_result
cdef column_view source_view = source_strings.view()

with nogil:
c_result = move(cpp_capitalize(source_view))

return Column.from_unique_ptr(move(c_result))
return Column.from_pylibcudf(
plc.strings.capitalize.capitalize(
source_strings.to_pylibcudf(mode="read")
)
)


@acquire_spill_lock()
def title(Column source_strings):
cdef unique_ptr[column] c_result
cdef column_view source_view = source_strings.view()

with nogil:
c_result = move(cpp_title(source_view))

return Column.from_unique_ptr(move(c_result))
return Column.from_pylibcudf(
plc.strings.capitalize.title(
source_strings.to_pylibcudf(mode="read")
)
)


@acquire_spill_lock()
def is_title(Column source_strings):
cdef unique_ptr[column] c_result
cdef column_view source_view = source_strings.view()

with nogil:
c_result = move(cpp_is_title(source_view))

return Column.from_unique_ptr(move(c_result))
return Column.from_pylibcudf(
plc.strings.capitalize.is_title(
source_strings.to_pylibcudf(mode="read")
)
)
57 changes: 57 additions & 0 deletions python/cudf/cudf/pylibcudf_tests/test_string_capitalize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 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 is_title_data():
data = [
"leopard",
"Golden Eagle",
"SNAKE",
"",
"!A",
"hello World",
"A B C",
"#",
"AƻB",
"Ⓑⓖ",
"Art of War",
]
return pa.array(data)


@pytest.fixture(scope="module")
def title_data():
data = [
None,
"The quick bRoWn fox juMps over the laze DOG",
'123nr98nv9rev!$#INF4390v03n1243<>?}{:-"',
"accénted",
]
return pa.array(data)


def test_capitalize(title_data):
plc_col = plc.interop.from_arrow(title_data)
brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved
got = plc.strings.capitalize.capitalize(plc_col)
expected = pa.compute.utf8_capitalize(title_data)
assert_column_eq(got, expected)


def test_title(title_data):
plc_col = plc.interop.from_arrow(title_data)
got = plc.strings.capitalize.title(plc_col)
expected = pa.compute.utf8_title(title_data)
assert_column_eq(got, expected)


def test_is_title(is_title_data):
plc_col = plc.interop.from_arrow(is_title_data)
brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved
got = plc.strings.capitalize.is_title(plc_col)
expected = pa.compute.utf8_is_title(is_title_data)
assert_column_eq(got, expected)
Loading