adds new error classes to keywordextration error and defines new error

This commit is contained in:
hajdul88 2025-08-13 12:14:42 +02:00
parent 6870bba5a9
commit ebd4403c2f
3 changed files with 33 additions and 2 deletions

View file

@ -0,0 +1,9 @@
"""
Custom exceptions for the Cognee API.
This module defines a set of exceptions for handling various data errors
"""
from .exceptions import (
KeywordExtractionError
)

View file

@ -0,0 +1,22 @@
from cognee.exceptions import (
CogneeValidationError,
)
from fastapi import status
class KeywordExtractionError(CogneeValidationError):
"""
Raised when a provided value is syntactically valid but semantically unacceptable
for the given operation.
Example:
- Passing an empty string to a keyword extraction function.
"""
def __init__(
self,
message: str = "Extract_keywords cannot extract keywords from empty text.",
name: str = "KeywordExtractionError",
status_code: int = status.HTTP_400_BAD_REQUEST,
):
super().__init__(message, name, status_code)

View file

@ -1,6 +1,6 @@
from sklearn.feature_extraction.text import TfidfVectorizer
from cognee.exceptions import InvalidValueError
from cognee.infrastructure.data.exceptions.exceptions import KeywordExtractionError
from cognee.shared.utils import extract_pos_tags
@ -25,7 +25,7 @@ def extract_keywords(text: str) -> list[str]:
with more than 3 characters.
"""
if len(text) == 0:
raise InvalidValueError(message="extract_keywords cannot extract keywords from empty text.")
raise KeywordExtractionError()
tags = extract_pos_tags(text)
nouns = [word for (word, tag) in tags if tag == "NN"]