generated from irgolic/AutoPR-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_filter.py
60 lines (47 loc) · 1.62 KB
/
response_filter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import random
import re
from typing import List
def remove_repetitive_phrases(text: str) -> str:
"""
Remove repetitive phrases from the input text.
Args:
text (str): Input text with potential repetitive phrases.
Returns:
str: Text with repetitive phrases removed.
"""
phrases = re.split(r'\s+', text)
unique_phrases = []
for phrase in phrases:
if phrase not in unique_phrases:
unique_phrases.append(phrase)
return ' '.join(unique_phrases)
def improve_creativity(text: str, synonyms: List[str]) -> str:
"""
Replace words in the input text with synonyms to improve creativity.
Args:
text (str): Input text to improve creativity.
synonyms (List[str]): List of synonyms.
Returns:
str: Text with improved creativity.
"""
words = text.split()
creative_text = []
for word in words:
if random.random() < 0.5:
synonym = random.choice(synonyms)
creative_text.append(synonym)
else:
creative_text.append(word)
return ' '.join(creative_text)
def filter_response(response: str, synonyms: List[str]) -> str:
"""
Apply both removing repetitive phrases and improving creativity methods to the given response.
Args:
response (str): The initial response generated by the bot.
synonyms (List[str]): List of synonyms.
Returns:
str: Improved response after applying both methods.
"""
filtered_response = remove_repetitive_phrases(response)
creative_response = improve_creativity(filtered_response, synonyms)
return creative_response