Rename emojify_text.py to ner_tool.py
Browse files- emojify_text.py +0 -33
- ner_tool.py +23 -0
emojify_text.py
DELETED
|
@@ -1,33 +0,0 @@
|
|
| 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 = ["text"] # Explicitly specify the output component
|
| 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": emojified_text} # Return a dictionary with the specified output component
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ner_tool.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
from transformers import Tool
|
| 3 |
+
|
| 4 |
+
class NamedEntityRecognitionTool(Tool):
|
| 5 |
+
name = "ner_tool"
|
| 6 |
+
description = "Identifies and labels entities such as persons, organizations, and locations in a given text."
|
| 7 |
+
inputs = ["text"]
|
| 8 |
+
outputs = ["entities"]
|
| 9 |
+
|
| 10 |
+
def __call__(self, text: str):
|
| 11 |
+
# Initialize the named entity recognition pipeline
|
| 12 |
+
ner_analyzer = pipeline("ner")
|
| 13 |
+
|
| 14 |
+
# Perform named entity recognition on the input text
|
| 15 |
+
entities = ner_analyzer(text)
|
| 16 |
+
|
| 17 |
+
# Print the identified entities
|
| 18 |
+
print(f"Identified Entities: {entities}")
|
| 19 |
+
|
| 20 |
+
# Extract entity labels and return as a list
|
| 21 |
+
entity_labels = [entity["label"] for entity in entities]
|
| 22 |
+
|
| 23 |
+
return entity_labels
|