Add in spell check for lex file and more format for LIST panel

This commit is contained in:
2024-11-05 14:32:00 +00:00
parent a531307464
commit 031c1414ec
11 changed files with 112 additions and 66 deletions

View File

@@ -12,8 +12,7 @@ from pathlib import Path
import traceback
import os
from datetime import datetime, timedelta
from spellchecker import SpellChecker
#
# To Do
@@ -30,7 +29,41 @@ OPENAI_API_KEY = ""
def example_function(**kwargs):
print(kwargs)
def spell_check_and_correct(text):
try:
# Initialize the spell checker
spell = SpellChecker()
# Tokenize the text into words
words = text.split()
# Create a new list to store corrected words
corrected_words = []
for word in words:
# Skip words that are entirely uppercase
if word.isupper():
corrected_words.append(word)
continue
# Check if the word is correctly spelled
if word in spell:
# Word is correct, add the original word
corrected_words.append(word)
else:
# Word is misspelled, get the correction
correction = spell.correction(word)
# If a correction is found, use it; otherwise, keep the original word
corrected_words.append(correction if correction else word)
# Join the corrected words back into a single string
corrected_text = ' '.join(corrected_words)
except:
print(f"Spelling checker exception ({text})")
return text
return corrected_text
def python_to_perl_structure(data):
#print(data)
if data:
@@ -301,7 +334,7 @@ def format_text(text):
formatted_sentences.append(formatted_sentence)
# Join the formatted sentences back together
formatted_text = ". ".join(formatted_sentences).lstrip()
return formatted_text
return spell_check_and_correct(capitalize_words_if_all_caps(formatted_text))
def get_completion(prompt):
@@ -399,6 +432,14 @@ def convert_lex_to_dict(pairs_string):
formatted_dict = [{"id": key, "text": value} for key, value in data_dict.items()]
return formatted_dict
def capitalize_words_if_all_caps(s):
# Check if all characters in the string are uppercase or spaces
if s.replace(" ", "").isupper():
# Capitalize each word
return s.title()
else:
return s
def extract_input_fields(json_data, value_type):
result = {}