feat: implement nlp

Signed-off-by: Sphericalkat <me@kat.bio>
This commit is contained in:
Amogh Lele 2024-05-25 13:28:57 +05:30
parent 98809e62a4
commit d8ca41ed3f
Signed by: sphericalkat
GPG Key ID: 1C022B9CED2425B4
4 changed files with 90 additions and 7 deletions

29
main.py
View File

@ -2,19 +2,21 @@ import logging
from telegram import Update
from telegram.ext import (
ContextTypes,
ApplicationBuilder,
CommandHandler,
ContextTypes,
MessageHandler,
filters,
)
from nlp import is_noun_follows_verb
from settings import Settings
# Load environment variables
settings = Settings()
# Set the log level
log_level = logging.getLevelNamesMapping().get(settings.log_level, logging.INFO)
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=log_level
)
@ -27,10 +29,23 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
async def message_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(
chat_id=update.effective_chat.id,
text="I'm sorry, I'm a bot and I can't understand that.",
)
# Get the message text content
msg_content = update.effective_message.text
# Ignore messages without text
if not msg_content:
return
# Check that the message doesn't have more than 5 words
if len(msg_content.split()) > 5:
return
# Check if a noun immediately follows a verb
is_follows_verb, verb = is_noun_follows_verb(msg_content)
if is_follows_verb:
await update.effective_message.reply_text(f"{verb} deez")
return
if __name__ == "__main__":

23
nlp.py Normal file
View File

@ -0,0 +1,23 @@
import spacy
# Load the English NLP model
nlp = spacy.load("en_core_web_sm")
# Define a function to check if a noun immediately follows a verb
def is_noun_follows_verb(text: str) -> bool:
doc = nlp(text)
for i in range(len(doc) - 1):
# Check if the current token is a verb and the next token is a noun
if doc[i].pos_ == "VERB" and doc[i + 1].pos_ in ["NOUN", "PROPN", "PRON"]:
return True, doc[i].lemma_
return False, None
if __name__ == "__main__":
text = "I was eating pizza"
is_follows_verb, verb = is_noun_follows_verb(text)
if is_follows_verb:
print(f"{verb} deez")
else:
print("No noun follows a verb in the text")

45
requirements.txt Normal file
View File

@ -0,0 +1,45 @@
annotated-types==0.7.0
anyio==4.3.0
blis==0.7.11
catalogue==2.0.10
certifi==2024.2.2
charset-normalizer==3.3.2
click==8.1.7
cloudpathlib==0.16.0
confection==0.1.4
cymem==2.0.8
# en-core-web-sm==3.7.1 (from https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.7.1/en_core_web_sm-3.7.1-py3-none-any.whl)
h11==0.14.0
httpcore==1.0.5
httpx==0.27.0
idna==3.7
jinja2==3.1.4
langcodes==3.4.0
language-data==1.2.0
marisa-trie==1.1.1
markupsafe==2.1.5
murmurhash==1.0.10
numpy==1.26.4
packaging==24.0
pip==24.0
preshed==3.0.9
pydantic==2.7.1
pydantic-core==2.18.2
python-telegram-bot==21.2
requests==2.32.2
setuptools==70.0.0
smart-open==6.4.0
sniffio==1.3.1
spacy==3.7.4
https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.7.1/en_core_web_sm-3.7.1-py3-none-any.whl
spacy-legacy==3.0.12
spacy-loggers==1.0.5
srsly==2.4.8
thinc==8.2.3
tqdm==4.66.4
typer==0.9.4
typing-extensions==4.12.0
urllib3==2.2.1
wasabi==1.1.2
weasel==0.3.4
wheel==0.43.0

0
test.py Normal file
View File