Pseudo Code Generator | JavaScript
MindMapExtractor Class for Text Structure Extraction
The MindMapExtractor class is designed for extracting structured mind maps from text sections using machine learning, featuring error handling, threading for efficiency, and functions for data merging and transformation.
Prompt
Answer
Pseudo Code for MindMapExtractor Class
Overview
The MindMapExtractor
class is designed to extract a mind map structure from a list of text sections using a machine learning model. It includes error handling, threading for concurrent processes, and functions for merging and transforming the extracted data.
Class Definition
Class MindMapResult:
Attributes:
output: Dictionary
Class MindMapExtractor:
Attributes:
_llm: CompletionLLM
_input_text_key: String
_mind_map_prompt: String
_on_error: ErrorHandlerFn
Method __init__(llm_invoker, prompt=None, input_text_key=None, on_error=None):
Initialize attributes with provided parameters
If input_text_key is None, set default to "input_text"
If prompt is None, set default to MIND_MAP_EXTRACTION_PROMPT
If on_error is None, set default to a lambda that does nothing
Method _key(k):
Remove asterisks from the key and return it
Method _be_children(obj, keyset):
If obj is a string:
Convert obj to a list with a single element
If obj is a list:
For each item in obj:
Add item to keyset
Return list of dictionaries with 'id' and empty 'children'
Initialize an empty array arr
For each key-value pair (k, v) in obj:
k = _key(k)
If k is empty or k is in keyset:
Continue
Add k to keyset
Append dictionary with 'id' as k and 'children' from recursive call to _be_children(v, keyset)
Return arr
Method __call__(sections, prompt_variables=None):
If prompt_variables is None:
Set prompt_variables to an empty dictionary
Initialize a ThreadPoolExecutor with 12 workers
Initialize threads, token_count, texts, res, and cnt
For each section in sections:
Calculate token count of section
If accumulated token count exceeds token_count and texts is not empty:
Submit a task to process_document with concatenated texts and prompt_variables
Reset texts and cnt
Append section to texts
Increment cnt by section token count
If texts is not empty:
Submit a final task for remaining texts
For each thread in threads:
Append result to res
If res is empty:
Return MindMapResult with empty root
Merge results from res into merge_json
Perform checks to structure merge_json based on keys
Handle merging and creating structured output
Method _merge(d1, d2):
For each key k in d1:
If k exists in d2:
If both values are dictionaries:
Call _merge recursively
If both values are lists:
Extend list in d2 with values from d1
Else set d2[k] to d1[k]
Else:
Set d2[k] to d1[k]
Return d2
Method _list_to_kv(data):
For each key-value pair in data:
If value is a dictionary:
Call _list_to_kv recursively on value
If value is a list:
Initialize new_value as an empty dictionary
For index in range of value length:
If value[index] is a list:
Assign value at index - 1 to new_value as key
Assign first element of value[index] as the value
Set data[key] to new_value
Return data
Method _todict(layer):
Convert layer to a standard dictionary if it's an OrderedDict
Attempt to iterate over items in the flattened dictionary:
Recursively call _todict on each item's value
Handle AttributeError exceptions
Return transformed layer
Method _process_document(text, prompt_variables):
Prepare variables for the prompt
Replace variables in _mind_map_prompt with the prepared variables
Send request to the language model with the prompt text
Clean up response to remove code block markers
Convert response to a dictionary and transform it
Return transformed output
Key Processing Steps
- Initialization: Set up the LLM, fetching the required prompt and error handling variables.
- Input Processing: Iterate through the sections, managing token limits using threading.
- Data Transformation: Merge and structure the extracted data into a mind map format.
- Error Handling: Log errors and execute the designated error handler.
- Final Output: Return a structured output encapsulated in the
MindMapResult
.
Notes
- This pseudo code captures the essence of the original Python code while ensuring clarity and understanding.
- Specific methods within the class manage tasks such as merging, structuring, and processing the documents, providing a clean separation of functionalities.
Description
The MindMapExtractor class is designed for extracting structured mind maps from text sections using machine learning, featuring error handling, threading for efficiency, and functions for data merging and transformation.