The Power of NLP

March 31, 2024 - 255 views

4 min read

Nour Rajeh

In this blog post, Mr. Nour Rajeh, a Computer Engineering student at the Lebanese American University, explores Natural Language Processing (NLP), an AI field enabling machines to understand human language. Through a sentiment analysis example, he dissects how NLP processes emotional expressions, simplifying NLP's complexities and spotlighting its transformative impact on technology and interaction with machines. 

Picture this: you ask Alexa to play some music. She detects your mood and chooses a song that fits it perfectly. Natural Language Processing (NLP), an artificial intelligence (AI) domain bridges programming and linguistics, making responses by tools such as Alexa and ChatGPT possible. NLP isn't just about smart devices; it is integral to everything from filtering emails to advancing legal research! In this post, we demystify how machines interpret and respond to human language by delving into the journey of two simple sentences through the complex world of machine learning, highlighting NLP's important role in our interaction with technology.

 

The Building Blocks of NLP

Language conveys complex human emotions, as seen in contrasting sentences like "I forgot my keys at home this morning…" signaling distress, and "Don’t forget to always smile!" exuding encouragement. From a machine's perspective, however, these sentences are merely sequences of characters. Through tokenization, NLP breaks down text into tokens transforming "I forgot my keys..." into individual elements like {"I", "forgot", "my", "keys", ...}, and similarly for the second sentence. Such segmentation is crucial for understanding context and meaning. 

The next step is normalization. A machine won't be able to tell that both strings “forgot” and “forget” are the same verb. This is where lemmatization comes into play as a technique used to bring verbs back to their original form. In this case, “forgot” is lemmatized to "forget.” This can help the machine understand the usage of the same verbs in different contexts to convey different meanings. Other normalization techniques include removing prefixes and suffixes (aka stemming) or extracting useful information having fixed formats through regular expressions, like extracting the name from an email or the year from a date formatted as MM-DD-YYYY.

The data then enters the last phase of preparation, where it is converted into numerical data, preparing it for algorithmic analysis. This transformation is essential for machines to interpret and engage with human language effectively.

 

Data Feeding & Training

Now, imagine feeding the machine numerical data derived from our earlier examples: "I forgot my keys…" labeled as "sad" and "Don’t forget to always smile!" marked as "happy." This step involves breaking down the vast text dataset into smaller, more manageable batches for the model to process. The main objective is to construct a sophisticated computational tool that leverages machine learning to interpret human language, enabling it to perform tasks like sentiment analysis. In the following discussion, this tool will be referred to as “the model”.

Initially, the model has a basic grasp of these emotions, requiring repeated exposure to improve the accuracy of its sentiment analysis capability. In supervised learning, the model uses labels to learn how to associate specific linguistic patterns with corresponding emotions. Through repeated training cycles, it compares its predictions against the provided labels, refining its ability to discern between "sad" and "happy" contexts based on text input.

On the other hand, in unsupervised learning, the model would analyze these sentences without predefined labels, seeking out natural patterns and clustering similar expressions together. This method helps identify underlying structures or themes in text data, enhancing the model's capacity for nuanced understanding without direct instruction.

 

Accuracy Estimation & Amelioration

As the model progresses through numerous epochs (i.e., complete cycles over the dataset), it refines its ability to assign emotions to sentences like "I forgot my keys at home this morning…" (sad) and "Don’t forget to always smile!" (happy). Initially, its accuracy might be low, reflecting its reliance on learning through trial and error. Over time, as it processes more data, its predictions become more precise.

The model's accuracy is evaluated at each epoch, assessing its performance through the ratio of correct to incorrect assignments. This internal review mechanism enables the model to learn from its errors and optimize its algorithms, similar to how the human brain adapts and learns from new experiences.

While perfect accuracy is unattainable due to the complexities and nuances of languages, the model can achieve commendable accuracy, particularly in tasks like sentiment analysis. This improvement depends on the learning approach, be it supervised, where it learns from labeled examples, or unsupervised, where it discerns patterns without explicit guidance. In this way, the model continually enhances its understanding and interpretation of language, evolving to offer more nuanced and accurate analyses over time.

 

In summary, Natural Language Processing (NLP) enables machines to interpret human language with increasing sophistication. Our exploration revealed how a model processes text -  from the initial breakdown via tokenization and lemmatization to nuanced understanding through iterative training. By analyzing and adjusting its performance based on accuracy metrics, the model demonstrates its capability to discern and adapt to the nuances of human emotion in language, as illustrated with sentences conveying sadness and happiness. This journey through NLP demonstrates its potential to enhance machine-human interactions. As NLP advances, it promises more intuitive and intelligent applications, drastically transforming our interaction with machines.