Create emojify_text.py
Browse files- emojify_text.py +33 -0
emojify_text.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import emoji
|
| 2 |
+
from transformers import Tool
|
| 3 |
+
|
| 4 |
+
class EmojifyTextTool(Tool):
|
| 5 |
+
name = "emojify_text"
|
| 6 |
+
description = "Emojifies text by adding relevant emojis to enhance expressiveness."
|
| 7 |
+
inputs = ["text"]
|
| 8 |
+
outputs = ["emojified_text"]
|
| 9 |
+
|
| 10 |
+
def __call__(self, text: str):
|
| 11 |
+
# Define a dictionary mapping keywords to emojis
|
| 12 |
+
keyword_to_emoji = {
|
| 13 |
+
"happy": "😄",
|
| 14 |
+
"sad": "😢",
|
| 15 |
+
"love": "❤️",
|
| 16 |
+
"confused": "😕",
|
| 17 |
+
"excited": "🎉",
|
| 18 |
+
# Add more keywords and corresponding emojis as needed
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
# Emojify the input text based on keywords
|
| 22 |
+
emojified_text = self._emojify_keywords(text, keyword_to_emoji)
|
| 23 |
+
|
| 24 |
+
# Print the emojified text
|
| 25 |
+
print(f"Emojified Text: {emojified_text}")
|
| 26 |
+
|
| 27 |
+
return emojified_text
|
| 28 |
+
|
| 29 |
+
def _emojify_keywords(self, text, keyword_to_emoji):
|
| 30 |
+
# Replace keywords in the text with corresponding emojis
|
| 31 |
+
for keyword, emoji_char in keyword_to_emoji.items():
|
| 32 |
+
text = text.replace(keyword, emoji_char)
|
| 33 |
+
return text
|