forked from Azure-Samples/contoso-creative-writer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyour_script.py
More file actions
244 lines (209 loc) · 8.95 KB
/
Copy pathyour_script.py
File metadata and controls
244 lines (209 loc) · 8.95 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import re
import json
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.backends import default_backend
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
class Element:
def __init__(self, name, symbol, representation, properties, interactions, defense_ability):
self.name = name
self.symbol = symbol
self.representation = representation
self.properties = properties
self.interactions = interactions
self.defense_ability = defense_ability
def display_properties(self):
print(f"Properties of {self.name} ({self.symbol}):")
for prop in self.properties:
print(f" - {prop}")
def display_interactions(self):
print(f"Interactions of {self.name} ({self.symbol}):")
for interaction in self.interactions:
print(f" - {interaction}")
def display_defense_ability(self):
print(f"Defense Ability of {self.name} ({self.symbol}): {self.defense_ability}")
def execute_defense_function(self):
defense_functions = {
"evasion": self.evasion,
"adaptability": self.adaptability,
"fortification": self.fortification,
"barrier": self.barrier,
"regeneration": self.regeneration,
"resilience": self.resilience,
"illumination": self.illumination,
"shield": self.shield,
"reflection": self.reflection,
"protection": self.protection
}
defense_function = defense_functions.get(self.defense_ability.lower(), self.no_defense)
defense_function()
def evasion(self):
print(f"{self.name} uses Evasion to avoid threats and remain undetected.")
def adaptability(self):
print(f"{self.name} adapts to changing environments and evolves to overcome challenges.")
def fortification(self):
print(f"{self.name} strengthens defenses and fortifies positions to withstand attacks.")
def barrier(self):
print(f"{self.name} creates barriers to protect against external threats.")
def regeneration(self):
print(f"{self.name} regenerates lost or damaged parts to maintain functionality.")
def resilience(self):
print(f"{self.name} exhibits resilience to recover quickly from setbacks.")
def illumination(self):
print(f"{self.name} uses illumination to reveal hidden threats and illuminate dark areas.")
def shield(self):
print(f"{self.name} uses a shield to block incoming attacks and protect allies.")
def reflection(self):
print(f"{self.name} reflects attacks back to the source, turning the enemy's power against them.")
def protection(self):
print(f"{self.name} offers protection to prevent harm and ensure safety.")
def no_defense(self):
print("No defense function available.")
class CustomRecognizer:
class RecognizerResult:
def __init__(self, text):
self.text = text
self.intents = []
class Intent:
def __init__(self, name, score):
self.name = name
self.score = score
def recognize(self, text):
recognizer_result = self.RecognizerResult(text)
regex_element = re.compile(r"^(Hydrogen|Carbon|Iron|Silicon|Oxygen|Nitrogen|Phosphorus|Gold|Silver|Lead|Diamond)$", re.IGNORECASE)
is_element = regex_element.match(text)
if is_element:
recognizer_result.intents.append(self.Intent("ElementDefense", 100))
return recognizer_result
def get_top_intent(self, recognizer_result):
recognizer_result.intents.sort(key=lambda x: x.score, reverse=True)
return recognizer_result.intents[0].name if recognizer_result.intents else None
class DataProtector:
sensitive_keywords = {"AI", "sensitive", "confidential", "data"}
@staticmethod
def contains_sensitive_info(text):
return any(keyword.lower() in text.lower() for keyword in DataProtector.sensitive_keywords)
@staticmethod
def mask_sensitive_info(text):
for keyword in DataProtector.sensitive_keywords:
text = re.sub(keyword, '*' * len(keyword), text, flags=re.IGNORECASE)
return text
@staticmethod
def encrypt_string(plain_text, key):
backend = default_backend()
key_bytes = key.encode('utf-8')
iv = key_bytes[:16]
cipher = Cipher(algorithms.AES(key_bytes), modes.CBC(iv), backend=backend)
encryptor = cipher.encryptor()
padder = padding.PKCS7(algorithms.AES.block_size).padder()
padded_data = padder.update(plain_text.encode('utf-8')) + padder.finalize()
encrypted_data = encryptor.update(padded_data) + encryptor.finalize()
return encrypted_data.hex()
@staticmethod
def decrypt_string(cipher_text, key):
backend = default_backend()
key_bytes = key.encode('utf-8')
iv = key_bytes[:16]
cipher = Cipher(algorithms.AES(key_bytes), modes.CBC(iv), backend=backend)
decryptor = cipher.decryptor()
encrypted_data = bytes.fromhex(cipher_text)
decrypted_padded_data = decryptor.update(encrypted_data) + decryptor.finalize()
unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()
decrypted_data = unpadder.update(decrypted_padded_data) + unpadder.finalize()
return decrypted_data.decode('utf-8')
def analyze_sentiment(text):
analyzer = SentimentIntensityAnalyzer()
sentiment = analyzer.polarity_scores(text)
return sentiment
def initialize_elements():
elements = [
Element(
name="Hydrogen",
symbol="H",
representation="Lua",
properties=["Simple", "Lightweight", "Versatile"],
interactions=["Easily integrates with other languages and systems"],
defense_ability="Evasion"
),
Element(
name="Carbon",
symbol="C",
representation="Python",
properties=["Flexible", "Widely used", "Powerful"],
interactions=["Can be used for a variety of tasks, from web development to data analysis"],
defense_ability="Adaptability"
),
Element(
name="Iron",
symbol="Fe",
representation="C++",
properties=["Strong", "Durable", "Efficient"],
interactions=["Used in system programming and game development"],
defense_ability="Fortification"
),
Element(
name="Silicon",
symbol="Si",
representation="Java",
properties=["Robust", "Platform-independent", "Secure"],
interactions=["Widely used in enterprise applications"],
defense_ability="Barrier"
),
Element(
name="Oxygen",
symbol="O",
representation="JavaScript",
properties=["Dynamic", "Versatile", "Ubiquitous"],
interactions=["Essential for web development"],
defense_ability="Regeneration"
),
Element(
name="Nitrogen",
symbol="N",
representation="Ruby",
properties=["Elegant", "Productive", "Flexible"],
interactions=["Popular in web development with Rails"],
defense_ability="Resilience"
),
Element(
name="Phosphorus",
symbol="P",
representation="PHP",
properties=["Server-side", "Web-focused", "Embedded"],
interactions=["Commonly used in web development"],
defense_ability="Illumination"
),
Element(
name="Gold",
symbol="Au",
representation="Swift",
properties=["Modern", "Safe", "Fast"],
interactions=["Used for iOS and macOS development"],
defense_ability="Shield"
),
Element(
name="Silver",
symbol="Ag",
representation="Go",
properties=["Concurrent", "Efficient", "Scalable"],
interactions=["Ideal for cloud services and backend systems"],
defense_ability="Reflection"
),
Element(
name="Lead",
symbol="Pb",
representation="Rust",
properties=["Safe", "Concurrent", "Fast"],
interactions=["Used for system-level programming"],
defense_ability="Protection"
),
Element(
name="Diamond",
symbol="D",
representation="Kotlin",
properties=["Modern", "Concise", "Safe"],
interactions=["Used for Android development"],
defense_ability="Adaptability"
)
]
return elements