From 4aae15d769e90daf6ce6296a1af29048a1309de4 Mon Sep 17 00:00:00 2001 From: Bently Date: Tue, 26 Nov 2024 21:38:43 +0000 Subject: [PATCH] feat(blocks): Add Word Character Count Block (#8781) * Adds Word Character Count Block Co-Authored-By: SerchioSD <69461657+serchiosd@users.noreply.github.com> * update test_output --------- Co-authored-by: SerchioSD <69461657+serchiosd@users.noreply.github.com> --- .../blocks/count_words_and_char_block.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 autogpt_platform/backend/backend/blocks/count_words_and_char_block.py diff --git a/autogpt_platform/backend/backend/blocks/count_words_and_char_block.py b/autogpt_platform/backend/backend/blocks/count_words_and_char_block.py new file mode 100644 index 000000000000..13f9e3977941 --- /dev/null +++ b/autogpt_platform/backend/backend/blocks/count_words_and_char_block.py @@ -0,0 +1,43 @@ +from backend.data.block import Block, BlockCategory, BlockOutput, BlockSchema +from backend.data.model import SchemaField + + +class WordCharacterCountBlock(Block): + class Input(BlockSchema): + text: str = SchemaField( + description="Input text to count words and characters", + placeholder="Enter your text here", + advanced=False, + ) + + class Output(BlockSchema): + word_count: int = SchemaField(description="Number of words in the input text") + character_count: int = SchemaField( + description="Number of characters in the input text" + ) + error: str = SchemaField( + description="Error message if the counting operation failed" + ) + + def __init__(self): + super().__init__( + id="ab2a782d-22cf-4587-8a70-55b59b3f9f90", + description="Counts the number of words and characters in a given text.", + categories={BlockCategory.TEXT}, + input_schema=WordCharacterCountBlock.Input, + output_schema=WordCharacterCountBlock.Output, + test_input={"text": "Hello, how are you?"}, + test_output=[("word_count", 4), ("character_count", 19)], + ) + + def run(self, input_data: Input, **kwargs) -> BlockOutput: + try: + text = input_data.text + word_count = len(text.split()) + character_count = len(text) + + yield "word_count", word_count + yield "character_count", character_count + + except Exception as e: + yield "error", str(e)