← All ivy-nodes
IVYXSTUDIO · IVY NODE
Q

Question

ivy.node.question · v0.1.0

ivyx✓

Turns a question into a vector with a text vectorizer and returns the vector with the question.

#Question

Inputs

FieldTypeDescription
text_vectorizerrequiredNot declaredA vectorizer with vectorizer_text(text) -> list of floats, from text-vectorizer.
questionrequiredstringThe question to vectorize.

Outputs

FieldTypeDescription
vectorizerequiredarrayNot declared
textrequiredstringNot declared

Source

python

# Input preparation
inp = __ivy_ctx__["nodes"][__ivy_node_id__]["input"]
text_vectorizer = inp["text_vectorizer"]
question = inp["question"]

# Compute
from typing import List

class Question:
    """
    A class for converting a textual question into a numerical vector representation 
    using a specified text vectorizer.

    Attributes:
        text_vectorizer: An instance of a text vectorization class that provides a `vectorizer_text` method.

    Methods:
        vectorize(question: str) -> List[float]:
            Converts the given question into a numerical vector representation.
    """

    def __init__(self, text_vectorizer):
        """
        Initializes the Question class with a text vectorizer.

        Args:
            text_vectorizer (TextVectorizer): An instance of a text vectorization class that 
                                              must have a callable `vectorizer_text` method.

        Raises:
            TypeError: If `text_vectorizer` does not have a callable `vectorizer_text` method.
        """
        if not hasattr(text_vectorizer, "vectorizer_text") or not callable(text_vectorizer.vectorizer_text):
            raise TypeError("text_vectorizer must be an instance of a class with a callable 'vectorizer_text' method.")
        
        self.text_vectorizer = text_vectorizer

    def vectorize(self, question: str) -> List[float]:
        """
        Converts the given question into a vector representation.

        Args:
            question (str): The input question to be vectorized.

        Returns:
            List[float]: A numerical vector representing the given question.

        Raises:
            ValueError: If `question` is not a valid non-empty string.
            RuntimeError: If vectorization fails due to an internal error.
        """
        if not isinstance(question, str):
            raise ValueError("The question must be a string.")
        if not question.strip():
            raise ValueError("The question cannot be empty.")

        try:
            return self.text_vectorizer.vectorizer_text(question)
        except Exception as e:
            raise RuntimeError(f"Vectorization failed: {str(e)}")
        
question_obj = Question(text_vectorizer=text_vectorizer)

vectorize = question_obj.vectorize(question=question)

text = question

# Output collection (runner reads __ivy_ctx__)
out = __ivy_ctx__["nodes"][__ivy_node_id__]["output"]
out["vectorize"] = vectorize
out["text"] = text

Tests

Requires: python:3.9

  • vectorizes

    The vectorizer's numbers come back with the question.

  • blank

    A blank question is refused before the vectorizer is asked.