Update utils.py
Browse files
utils.py
CHANGED
@@ -48,6 +48,12 @@ from PIL import Image, ImageDraw, ImageOps, ImageFont
|
|
48 |
import base64
|
49 |
from tempfile import NamedTemporaryFile
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
52 |
from sklearn.metrics.pairwise import cosine_similarity
|
53 |
import numpy as np
|
@@ -126,6 +132,38 @@ def is_response_similar(response, threshold=0.7):
|
|
126 |
return False
|
127 |
return False
|
128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
##################################################
|
130 |
#RAG Hilfsfunktionen - Dokumenten bearbeiten für Vektorstore
|
131 |
##################################################
|
|
|
48 |
import base64
|
49 |
from tempfile import NamedTemporaryFile
|
50 |
|
51 |
+
import nltk
|
52 |
+
from nltk.corpus import stopwords
|
53 |
+
from nltk.tokenize import word_tokenize
|
54 |
+
from nltk.stem import WordNetLemmatizer
|
55 |
+
nltk.download('punkt')
|
56 |
+
|
57 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
58 |
from sklearn.metrics.pairwise import cosine_similarity
|
59 |
import numpy as np
|
|
|
132 |
return False
|
133 |
return False
|
134 |
|
135 |
+
|
136 |
+
##################################################
|
137 |
+
#Normalisierung eines Prompts
|
138 |
+
##################################################
|
139 |
+
def normalise_prompt (prompt):
|
140 |
+
#alles Kleinbuchstaben
|
141 |
+
prompt_klein =prompt.lower()
|
142 |
+
#Word Tokenisation
|
143 |
+
tokens = word_tokenize(prompt_klein)
|
144 |
+
#Punktuierung entfernen
|
145 |
+
tokens = [word for word in tokens if word.isalnum()]
|
146 |
+
# Stop Word Entfernung
|
147 |
+
#nltk.download('stopwords')
|
148 |
+
#stop_words = set(stopwords.words('english'))
|
149 |
+
#tokens = [word for word in tokens if not word in stop_words]
|
150 |
+
# 5. Lemmatisierung: Worte in Grundform bringen, um Text besser vergleichen zu können
|
151 |
+
#nltk.download('wordnet')
|
152 |
+
#lemmatizer = WordNetLemmatizer()
|
153 |
+
#tokens = [lemmatizer.lemmatize(word) for word in tokens]
|
154 |
+
# 6. Handling Special Characters (Remove non-alphanumeric characters)
|
155 |
+
tokens = [re.sub(r'\W+', '', word) for word in tokens]
|
156 |
+
# 7. Spell Check (optional, using a library like pyspellchecker)
|
157 |
+
# from spellchecker import SpellChecker
|
158 |
+
# spell = SpellChecker()
|
159 |
+
# tokens = [spell.correction(word) for word in tokens]
|
160 |
+
# Join tokens back to sentence
|
161 |
+
normalized_prompt = ' '.join(tokens)
|
162 |
+
print("normaiserd prompt..................................")
|
163 |
+
print(normalized_prompt)
|
164 |
+
return normalized_prompt
|
165 |
+
|
166 |
+
|
167 |
##################################################
|
168 |
#RAG Hilfsfunktionen - Dokumenten bearbeiten für Vektorstore
|
169 |
##################################################
|