-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: table filtering based on multiple keywords UDF (#907)
π Thanks for submitting a Pull Request to EvaDB! π We want to make contributing to EvaDB as easy and transparent as possible. Here are a few tips to get you started: - π Search existing EvaDB [PRs](https://github.com/georgia-tech-db/eva/pulls) to see if a similar PR already exists. - π Link this PR to a EvaDB [issue](https://github.com/georgia-tech-db/eva/issues) to help us understand what bug fix or feature is being implemented. - π Provide before and after profiling results to help us quantify the improvement your PR provides (if applicable). π Please see our β [Contributing Guide](https://evadb.readthedocs.io/en/stable/source/contribute/index.html) for more details. --------- Co-authored-by: jarulraj <[email protected]>
- Loading branch information
1 parent
84aef2d
commit 0da039c
Showing
4 changed files
with
125 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# coding=utf-8 | ||
# Copyright 2018-2023 EvaDB | ||
# | ||
# 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. | ||
import numpy as np | ||
import pandas as pd | ||
|
||
from evadb.catalog.catalog_type import NdArrayType | ||
from evadb.udfs.abstract.abstract_udf import AbstractUDF | ||
from evadb.udfs.decorators.decorators import forward, setup | ||
from evadb.udfs.decorators.io_descriptors.data_types import PandasDataframe | ||
|
||
|
||
class TextFilterKeyword(AbstractUDF): | ||
@setup(cacheable=False, udf_type="TextProcessing", batchable=False) | ||
def setup(self): | ||
pass | ||
|
||
@property | ||
def name(self) -> str: | ||
return "TextFilterKeyword" | ||
|
||
@forward( | ||
input_signatures=[ | ||
PandasDataframe( | ||
columns=["data", "keyword"], | ||
column_types=[NdArrayType.STR, NdArrayType.STR], | ||
column_shapes=[(1), (1)], | ||
) | ||
], | ||
output_signatures=[ | ||
PandasDataframe( | ||
columns=["filtered"], | ||
column_types=[NdArrayType.STR], | ||
column_shapes=[(1)], | ||
) | ||
], | ||
) | ||
def forward(self, df: pd.DataFrame) -> pd.DataFrame: | ||
def _forward(row: pd.Series) -> np.ndarray: | ||
import re | ||
|
||
data = row.iloc[0] | ||
keywords = row.iloc[1] | ||
flag = False | ||
for i in keywords: | ||
pattern = rf"^(.*?({i})[^$]*)$" | ||
match_check = re.search(pattern, data, re.IGNORECASE) | ||
if match_check: | ||
flag = True | ||
if flag is False: | ||
return data | ||
flag = False | ||
|
||
ret = pd.DataFrame() | ||
ret["filtered"] = df.apply(_forward, axis=1) | ||
return ret |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# coding=utf-8 | ||
# Copyright 2018-2023 EvaDB | ||
# | ||
# 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. | ||
import unittest | ||
from test.util import suffix_pytest_xdist_worker_id_to_dir | ||
|
||
import pytest | ||
|
||
from evadb.configuration.constants import EvaDB_DATABASE_DIR, EvaDB_ROOT_DIR | ||
from evadb.interfaces.relational.db import connect | ||
from evadb.server.command_handler import execute_query_fetch_all | ||
|
||
|
||
@pytest.mark.notparallel | ||
class TextFilteringTests(unittest.TestCase): | ||
def setUp(self): | ||
self.db_dir = suffix_pytest_xdist_worker_id_to_dir(EvaDB_DATABASE_DIR) | ||
self.conn = connect(self.db_dir) | ||
self.evadb = self.conn._evadb | ||
self.evadb.catalog().reset() | ||
|
||
def tearDown(self): | ||
execute_query_fetch_all(self.evadb, "DROP TABLE IF EXISTS MyPDFs;") | ||
|
||
def test_text_filter(self): | ||
pdf_path = f"{EvaDB_ROOT_DIR}/data/documents/layout-parser-paper.pdf" | ||
cursor = self.conn.cursor() | ||
cursor.load(pdf_path, "MyPDFs", "pdf").df() | ||
load_pdf_data = cursor.table("MyPDFs").df() | ||
cursor.create_udf( | ||
"TextFilterKeyword", | ||
True, | ||
f"{EvaDB_ROOT_DIR}/evadb/udfs/text_filter_keyword.py", | ||
).df() | ||
filtered_data = ( | ||
cursor.table("MyPDFs") | ||
.cross_apply("TextFilterKeyword(data, ['References'])", "objs(filtered)") | ||
.df() | ||
) | ||
filtered_data.dropna(inplace=True) | ||
import pandas as pd | ||
|
||
pd.set_option("display.max_colwidth", None) | ||
print(filtered_data) | ||
self.assertNotEqual(len(filtered_data), len(load_pdf_data)) |