|
| 1 | +""" |
| 2 | +Module for parsing the HTML node |
| 3 | +""" |
| 4 | +from langchain.text_splitter import RecursiveCharacterTextSplitter |
| 5 | +from .base_node import BaseNode |
| 6 | + |
| 7 | + |
| 8 | +class ParseTextNode(BaseNode): |
| 9 | + """ |
| 10 | + A node for extracting content from HTML documents based on provided tags. |
| 11 | +
|
| 12 | + This node leverages the BeautifulSoupTransformer to offer flexible parsing |
| 13 | + capabilities. It allows you to isolate specific elements within an HTML |
| 14 | + document, making it valuable for targeted content extraction in scraping workflows. |
| 15 | +
|
| 16 | + Attributes: |
| 17 | + node_name (str): Unique name for the node (defaults to "ParseHTMLNode"). |
| 18 | + node_type (str): Indicates a standard operational node (set to "node"). |
| 19 | +
|
| 20 | + Args: |
| 21 | + node_name (str, optional): Custom name for the node (defaults to "ParseHTMLNode"). |
| 22 | +
|
| 23 | + Methods: |
| 24 | + execute(state): |
| 25 | + * Extracts content from the 'document' field in the state based on tags (if provided in the state). |
| 26 | + * Stores the result in the 'parsed_document' field of the state. |
| 27 | + * Employs the RecursiveCharacterTextSplitter for handling larger documents. |
| 28 | + """ |
| 29 | + |
| 30 | + def __init__(self, node_name: str = "ParseHTMLNode"): |
| 31 | + """ |
| 32 | + Initializes the ParseHTMLNode. |
| 33 | +
|
| 34 | + Args: |
| 35 | + node_name (str, optional): Custom name for the node (defaults to "ParseHTMLNode"). |
| 36 | + """ |
| 37 | + super().__init__(node_name, "node") |
| 38 | + |
| 39 | + def execute(self, state): |
| 40 | + """ |
| 41 | + Parses HTML content and updates the state. |
| 42 | +
|
| 43 | + Args: |
| 44 | + state (dict): Expects the following keys: |
| 45 | + 'document': The HTML content to parse. |
| 46 | + 'tags' (optional): A list of HTML tags to target for extraction. |
| 47 | +
|
| 48 | + Returns: |
| 49 | + dict: Updated state with the following: |
| 50 | + 'parsed_document': The extracted content |
| 51 | + (or the original document if no tags were provided). |
| 52 | + 'document_chunks': The original document split into chunka |
| 53 | + (using RecursiveCharacterTextSplitter) |
| 54 | + for larger documents. |
| 55 | +
|
| 56 | + Raises: |
| 57 | + KeyError: If the required 'document' key is missing from the state. |
| 58 | + """ |
| 59 | + |
| 60 | + print("---PARSING TEXT DOCUMENT---") |
| 61 | + |
| 62 | + try: |
| 63 | + document = state["document"] |
| 64 | + except KeyError as e: |
| 65 | + print(f"Error: {e} not found in state.") |
| 66 | + raise |
| 67 | + |
| 68 | + # ... (Add logic for parsing with BeautifulSoup based on 'tags' if present) |
| 69 | + |
| 70 | + text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder( |
| 71 | + chunk_size=4000, |
| 72 | + chunk_overlap=0, |
| 73 | + ) |
| 74 | + state["document_chunks"] = text_splitter.split_text(document) |
| 75 | + |
| 76 | + return state |
0 commit comments