From d8ca41ed3febd588e1f842b3e70af595fd260e6a Mon Sep 17 00:00:00 2001 From: Sphericalkat Date: Sat, 25 May 2024 13:28:57 +0530 Subject: [PATCH] feat: implement nlp Signed-off-by: Sphericalkat --- main.py | 29 ++++++++++++++++++++++------- nlp.py | 23 +++++++++++++++++++++++ requirements.txt | 45 +++++++++++++++++++++++++++++++++++++++++++++ test.py | 0 4 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 nlp.py create mode 100644 requirements.txt create mode 100644 test.py diff --git a/main.py b/main.py index a847ac3..b42f41d 100644 --- a/main.py +++ b/main.py @@ -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__": diff --git a/nlp.py b/nlp.py new file mode 100644 index 0000000..6e5964c --- /dev/null +++ b/nlp.py @@ -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") \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..b7cc31c --- /dev/null +++ b/requirements.txt @@ -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 diff --git a/test.py b/test.py new file mode 100644 index 0000000..e69de29