Keshav Memorial Engineering College: Next Word Predictor
Keshav Memorial Engineering College: Next Word Predictor
On
BACHELOR OF ENGINEERING
In
By
S. DHANUSH 245522733053
K. RAMCHARAN 245522733030
M. PRAVEEN 245522733035
Mrs.D.Mounika
Assistant Professor, Department of CSE
CERTIFICATE
This is to certify that this is a bonafide record of the project report titled “NEXT
WORD PREDICTOR” which is being presented as the Mini Project report by
1. S DHANUSH 245522733053
2. K RAMCHARAN 245522733030
3. M PRAVEEN 245522733035
To impart skills in Computer Science which will not necessarily be part of the
university prescribed curriculum. These skills may include programming languages,
frameworks, platforms and communication skills which will help in the development
of a well-rounded Computer Science professional.
To ensure that the student’s scope of knowledge is not limited to the conventional and
traditional use of Computer Science. The student will be given exposure to different
domains, different paradigms and exposed to the financial and commercial
underpinning of the modern business environment. This will be enabled through the
creation of an entrepreneur development cell on the campus.
To create a socially conscious and emotionally mature individuals who are aware of
India’s challenges and opportunities and who understand and appreciate the role and
responsibility of engineers towards achieving the goal of job and wealth creation.
PROGRAM OUTCOMES(POs)
PO2. Problem Analysis: Identify formulate, review research literature, and analyze
complex engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences
PO5. Modern Tool Usage: Create select, and, apply appropriate techniques, resources,
and modern engineering and IT tools including prediction and modeling to complex
engineering activities with an understanding of the limitations.
PO6. The Engineer and Society: Apply reasoning informed by contextual knowledge to
societal, health, safety. Legal und cultural issues and the consequent responsibilities
relevant to professional engineering practice.
PO8. Ethics: Apply ethical principles and commit professional ethics and responsibilities
and norms of the engineering practice.
PO9. Individual and Team Work: Function effectively as an individual, and as a
member or leader in diverse teams and in multidisciplinary settings.
PO12. Life-Long Learning: Recognize the need for, and have the preparation and ability
to engage in independent and life-long learning in the broadest context of technological
change.
PROGRAM SPECIFIC OUTCOMES(PSOs)
PSO1. An ability to analyze the common business functions to design and develop
appropriate Information Technology solutions for social upliftment.
PSO2. Shall have expertise on the evolving technologies like Mobile Apps, CRM, ERP,
Big Data, etc.
PEO1. Graduates will have successful careers in computer related engineering fields or
will be able to successfully pursue advanced higher education degrees.
PEO2. Graduates will try and provide solutions to challenging problems in their
profession by applying computer engineering principles.
PEO4. Graduates will communicate effectively, work collaboratively and exhibit high
levels of professionalism and ethical responsibility.
PROJECT OUTCOMES
P1: Demonstrate the ability to synthesize and apply the knowledge and skills acquired in the
academic program to the real-world problems.
P2: Evaluate different solutions based on economic and technical feasibility
P3: Effectively plan a project and confidently perform all aspects of project management.
P4: Demonstrate effective coding, written, presentation and oral communication skills.
PO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
P1 H H M M M L L
P2 H M M L H L L
P3 M M H L M M M
P4 M L M L L L L
P1 H H
P2 H M
P3 M M
P4 M H
P1 H H M M
P2 M H L M
P3 M M H M
P4 M M M H
DECLARATION
We hereby declare that the results embodied in the dissertation entitled “NEXT
WORD PREDICTOR'' has been carried out by us together during the academic year 2024-25
as a partial fulfillment of the award of the B.E degree in Computer Science & Engineering
from Osmania University. We have not submitted this report to any other university or
S. DHANUSH 245522733053
K. RAMCHARAN 245522733030
M. PRAVEEN 245522733035
ACKNOWLEDGEMENT
We take this opportunity to thank all the people who have rendered their full
support to our project work. We render our thanks to Prof. P.V.N. Prasad, Principal,
who encouraged us to do the Project.
We are grateful to Mr. Neil Gogte, Founder & Director, and Mr. S. Nitin,
Director, for facilitating all the amenities required for carrying out this project.
We express our sincere gratitude to Ms. Deepa Ganu, Academic Director, for
providing an excellent environment in the college.
We are also thankful to Dr. CH. Rathan Kumar, Dr. Bala Krishna, Heads of
the Department for providing us with time to make this project a success within the
given schedule.
We are also thankful to our Faculty Supervisor Dr. / Mr. / Mrs. / Ms. XXXX,
for her/his valuable guidance and encouragement given to us throughout the project
work.
We would like to thank the entire CSE Department faculty, who helped us
directly or indirectly in the completion of the project.
We sincerely thank our friends and family for their constant motivation during
the project work.
S. DHANUSH 245522733053
K. RAMCHARAN 245522733030
M. PRAVEEN 245522733035
ABSTRACT
i
LIST OF FIGURES
ARCHITECTURE
1. 2
DIAGRAM
USE CASE
2. 7
DIAGRAM
ACTIVITY
3. 8
DIAGRAM
SEQUENCE
4. 9
DIAGRAM
5. CLASS DIAGRAM 10
ii
CONTENTS
Abstract i
List of Figures ii
List of Tables iii
CHAPTER-1 INTRODUCTION 1
1.1 Purpose of the Project 1
1.2 Scope of the Project 1
1.3 Problem with existing systems 1
1.4 Architecture Diagram 1
2.1 Overview 3
2.2 Comparative study of existing systems 4
2.2.1 GPT-2 Text Generator CLI 4
2.2.2 HumanEval + GPT-2 Benchmark 4
3.1 Requirements 6
3.2 Functional Requirements 6
3.3 Non-Functional Requirements 6
3.4 Software Requirements 6
3.5 Hardware Requirements 7
CHAPTER-5 IMPLEMENTATION
iii
5.1 Code 11
CHAPTER-6 RESULTS 12
CHAPTER-7 CONCLUSION
7.1 Conclusion 14
REFERENCES
iv
NEXT WORD PREDICTOR
CHAPTER-I
INTRODUCTION
1.1 Background
In today’s digital landscape, the interaction between humans and machines is becoming
increasingly natural and intelligent, driven by advancements in artificial intelligence (AI).
One of the most prominent and rapidly evolving fields within AI is natural language
processing (NLP), which focuses on enabling computers to understand, interpret, and generate
human language in a meaningful way.The emergence of transformer-based models has
significantly improved the capabilities of NLP applications. Among them, the generative pre-
trained transformer 2 (GPT-2), developed by OpenAI, has set a benchmark for generating
coherent and contextually appropriate text. GPT-2 is trained on a massive amount of internet
data and is capable of performing various language tasks with remarkable fluency.One
practical application of these language models is next word prediction, which plays a crucial
role in applications such as predictive text keyboards, writing assistants, and conversational
agents. However, traditional next word prediction systems often generate outputs based solely
on statistical likelihoods, which may not always align with semantic context or user intentTo
address this, the present project proposes a system that combines the generative strength of
GPT-2 with linguistic filtering techniques. The aim is to predict words that are not only
syntactically correct but also semantically meaningful—limited to parts of speech such as
nouns, verbs, adjectives, and adverbs.
Conventional next word predictors typically focus on the probability of word sequences based
on large corpora. While they may produce grammatically correct outputs, they often suggest
words that lack contextual or semantic value, such as conjunctions, prepositions, or filler
words.This project addresses the need for a more context-aware word prediction system that
provides outputs with higher semantic importance. By incorporating part-of-speech (POS)
tagging, the system filters out low-value words and retains only those that add meaning to the
sentence, thereby improving the quality and usefulness of the predictions.
1.3 Objectives
The primary objectives of this project are as follows:
to implement a word prediction system using the pre-trained GPT-2 model
to filter the generated outputs using POS tagging, thereby allowing only meaningful word types
such as nouns, verbs, adjectives, and adverbs
to enhance the relevance of predicted words in the context of user input
to improve typing efficiency and usability in applications that require next word suggestions
ARCHITECTURE DIAGRAM
CHAPTER-2
Literature Survey
2.1 Overview
In recent years, large language models have significantly transformed the field of natural
language processing. These models, including GPT-2, BERT, and T5, have demonstrated an
exceptional ability to generate, understand, and manipulate human language. Applications like
writing assistants, chatbots, and autocomplete systems are becoming increasingly
sophisticated due to these advancements.
This literature survey aims to analyze and compare previously developed word prediction and
language generation systems to identify techniques, evaluate their effectiveness, and highlight
the improvements implemented in the proposed project.
2.2.2 GPT-2 Text Generator CLI (Author: Hugging Face Contributors, Date: 20 Nov
2020)
Features:
Command-line text generation tool
Based on Transformers library
Simple prompt input and response output
What we took:
Reference for prompt-token-output logic
Usage of top-k and top-p sampling techniques
What we did differently:
Added stop word filtering using NLTK
Filtered only meaningful POS tags for output
2.2.4 BERT-based Word Prediction (Author: Stanford NLP Group, Date: 18 Sep 2020)
Features:
2.2.5 AI21 Studio – Wordtune (Author: AI21 Labs, Date: 22 Dec 2021)
Features:
Advanced rewrite and suggestion engine
Provides alternative phrasing and word suggestions
Real-time web-based system
What we took:
Understanding of improving productivity through smart text suggestions
What we did differently:
Built lightweight CLI tool instead of browser-based assistant
Restricted output to single word suggestions only
CHAPTER 3
System Analysis
Existing next word prediction systems are commonly integrated into mobile keyboards,
writing assistants, and chatbots. Most of these tools rely on statistical methods such as n-gram
models, or basic neural networks that predict the next probable word based on previous
inputs. While they can provide grammatically correct suggestions, their predictions often lack
semantic richness or relevance.
Many existing systems do not consider the contextual importance or grammatical category of
the predicted word. As a result, they may suggest common filler words such as articles,
conjunctions, or prepositions that do not add value to the user’s sentence. Additionally, these
systems often lack flexibility and are usually embedded within proprietary applications,
making customization and extension difficult.
lack of semantic filtering: most systems do not filter predictions by parts of speech,
resulting in less meaningful suggestions
low relevance: suggestions may be grammatically correct but not contextually
appropriate
limited customization: users cannot easily modify the prediction logic or apply
linguistic constraints
internet dependence: many systems rely on server-side processing, reducing offline
usability
lack of transparency: users cannot observe or control the internal functioning of the
model
The proposed system leverages a pre-trained GPT-2 model for generating next word
predictions. Unlike conventional tools, it applies linguistic filters using natural language
toolkit (nltk) to retain only meaningful word types such as nouns, verbs, adjectives, and
adverbs. The system uses part-of-speech (POS) tagging and stop word removal to improve the
semantic value of the output.
The process begins with the user providing a partial sentence. The input is tokenized and
passed to GPT-2, which generates multiple possible continuations. These are then filtered
using nltk to remove irrelevant or unimportant words. Finally, the top three most relevant
suggestions are presented to the user.
the system supports execution on both cpu and gpu platforms and is implemented in python
using the transformers and nltk libraries.
hardware requirements:
software requirements:
libraries:
CHAPTER 4
System Design
USECASE DIAGRAM
The Use Case Diagram captures the functionalities that the system offers from a user’s
perspective. It outlines the interaction between the user and the voice assistant.
Activity Diagram:
Shows the dynamic flow of activities or actions in a system or process. It’s useful for
modeling workflows, business processes, or use case behavior.
Sequence Diagram:
Illustrates how objects interact with each other in a particular sequence over time. It focuses
on the order of messages exchanged between objects.
Class Diagram:
Describes the structure of classes in a system including their fields and functions. Also
displays relationships like inheritance, association, and dependencies.
CHAPTER 5
IMPLEMENTATION
6.1 INTRODUCTION
This chapter presents the implementation details of the proposed system — a next word
predictor based on GPT-2 and linguistic filtering using NLTK. The implementation is carried
out using Python, with the integration of the Hugging Face Transformers library for GPT-2
and the Natural Language Toolkit (NLTK) for linguistic processing. The system is structured
to be simple, modular, and efficient.
6.2 CODE
import nltk
import torch
from nltk import pos_tag, word_tokenize
from nltk.corpus import stopwords
from transformers import GPT2LMHeadModel, GPT2Tokenizer
# One-time download
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
nltk.download('stopwords')
# Load GPT-2
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
model = GPT2LMHeadModel.from_pretrained("gpt2")
model.eval()
# Device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
# Stopwords to ignore
stop_words = set(stopwords.words("english"))
def get_next_meaningful_words(prompt):
inputs = tokenizer(prompt, return_tensors="pt").to(device)
outputs = model.generate(
**inputs,
max_new_tokens=5,
num_return_sequences=20,
do_sample=True,
KESHAV MEMORIAL ENGINEERING COLLEGE 11
NEXT WORD PREDICTOR
temperature=1.0,
top_k=50,
top_p=0.95,
pad_token_id=tokenizer.eos_token_id
)
next_words = []
# Input loop
while True:
prompt = input("\n🗣 Enter a sentence (or type 'exit' to quit):\n>> ").strip()
if prompt.lower() == "exit":
print("\n🙏 Exiting... Har Har Mahadev!")
break
top3 = get_next_meaningful_words(prompt)
print("\n🔥 Top 3 Meaningful Next Words:")
print(top3 if top3 else ["No strong prediction. Try a different sentence."])
CHAPTER 6
RESULTS
6.1 SCREENSHOTS
6.1.6 Exit
CHAPTER 7
CONCLUSION
7.1 CONCLUSION
The project titled "Next Word Predictor using GPT-2" successfully demonstrates how
state-of-the-art transformer-based language models can be integrated with linguistic filters to
provide semantically meaningful word predictions. Unlike conventional predictive text
systems that primarily rely on statistical or shallow neural techniques, this system leverages
the deep contextual understanding of GPT-2 along with NLTK-based linguistic constraints
to ensure the output suggestions are not only grammatically valid but also contextually
relevant.
The architecture was designed to be flexible, lightweight, and efficient. It supports execution
on both CPU and GPU platforms, making it accessible to a wide range of users and
developers. By filtering GPT-2’s generated outputs using part-of-speech (POS) tagging and
excluding irrelevant or filler words through stop word removal, the system enhances both the
quality and usefulness of the predictions. This contributes significantly to applications such as
typing assistants, writing enhancers, and conversational AI interfaces.
The core contribution of this project lies in its hybrid approach, which combines deep
learning with rule-based linguistic filtering. This results in more human-like interactions,
improving not just prediction accuracy but also the end-user experience. The successful
integration of the Hugging Face Transformers library and NLTK demonstrates how modern
open-source tools can be effectively used to build intelligent, personalized applications.
Furthermore, the system’s modularity makes it a strong foundation for future enhancements,
such as GUI integration, speech-to-text pipelines, multi-language support, and offline
capabilities. Overall, this project stands as a practical example of applying advanced Natural
Language Processing (NLP) in real-world utility, proving that the synergy between AI and
linguistics can significantly enhance human-computer communication.
REFERENCES
[1] Radford, A., Narasimhan, K., Salimans, T., & Sutskever, I.; Improving Language
Understanding by Generative Pre-Training; OpenAI, June 2018;
https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_pape
r.pdf
[2] Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., ... &
Polosukhin, I.; Attention is All You Need; Proceedings of the 31st International Conference on
Neural Information Processing Systems (NIPS), 2017, pp. 5998–6008;
https://doi.org/10.48550/arXiv.1706.03762
[3] Wolf, T., Debut, L., Sanh, V., Chaumond, J., Delangue, C., Moi, A., ... & Rush, A. M.;
Transformers: State-of-the-Art Natural Language Processing; Proceedings of the 2020
Conference on Empirical Methods in Natural Language Processing: System Demonstrations
(EMNLP 2020), October 2020, pp. 38–45; https://doi.org/10.18653/v1/2020.emnlp-demos.6
[4] Devlin, J., Chang, M. W., Lee, K., & Toutanova, K.; BERT: Pre-training of Deep
Bidirectional Transformers for Language Understanding; Proceedings of the 2019
Conference of the North American Chapter of the Association for Computational Linguistics
(NAACL), June 2019, pp. 4171–4186; https://doi.org/10.48550/arXiv.1810.04805
[6] Loper, E., & Bird, S.; NLTK: The Natural Language Toolkit; Proceedings of the ACL-02
Workshop on Effective Tools and Methodologies for Teaching Natural Language Processing
and Computational Linguistics, July 2002, pp. 63–70;
https://doi.org/10.3115/1118108.1118117