Loading repository data…
Loading repository data…
vyasdeepti / repository
Welcome to the NLP Basics repository! This repository serves as a comprehensive guide for beginners and enthusiasts to explore the foundational concepts and techniques in Natural Language Processing (NLP). It contains interactive Jupyter Notebooks and Python scripts that demonstrate various NLP workflows, algorithms, and applications.
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
Welcome to the NLP Basics repository! This repository serves as a comprehensive guide for beginners and enthusiasts to explore the foundational concepts and techniques in Natural Language Processing (NLP). It contains interactive Jupyter Notebooks and Python scripts that demonstrate various NLP workflows, algorithms, and applications.
Natural Language Processing (NLP) is a subfield of Artificial Intelligence (AI) and linguistics that focuses on the interaction between computers and human language. NLP enables machines to understand, interpret, and generate human language in a way that is both meaningful and useful.
Key Goals of NLP:
Language Understanding: The ability to comprehend the meaning behind words, sentences, and context. Examples: Sentiment analysis, text classification, entity recognition.
Language Generation: The ability to produce coherent and contextually relevant human-like text. Examples: Text summarization, chatbots, machine translation.
Why is NLP Important?
NLP enables machines to bridge the gap between human communication and computers, making technology more accessible and human-friendly. It powers many modern applications that improve efficiency, automate tasks, and enhance user experiences in various domains like healthcare, finance, education, and entertainment.
Languages Used:
Intended Audience:
NLP_Preprocessing_1.ipynb, NLP_Preprocessing_2.ipynbNamedEntityRecognition.ipynbspaCy library.displacy.Input Sentence: "Barack Obama was born on August 4, 1961, in Honolulu, Hawaii, and served as the 44th President of the United States."
NER Output: The sentence with recognized entities is as follows:
Barack Obama → Person August 4, 1961 → Date Honolulu → Location Hawaii → Location 44th → Ordinal United States → Location
WordFrequency_.ipynbspaCy.Example of Word Frequency Analysis Input Text: "I love programming in Python. Python is an amazing programming language that I love."
Steps for Word Frequency Analysis:
Text Cleaning: Convert text to lowercase, remove punctuation, and split into words (tokens).
Example:
["i", "love", "programming", "in", "python", "python", "is", "an", "amazing", "programming", "language", "that", "i", "love"]
Count Word Frequencies: Count the occurrences of each word.
Example:
{ "i": 2, "love": 2, "programming": 2, "in": 1, "python": 2, "is": 1, "an": 1, "amazing": 1, "language": 1, "that": 1 }
Sort by Frequency : Sort the words by their frequency in descending order.
Example:
[ ("i", 2), ("love", 2), ("programming", 2), ("python", 2), ("in", 1), ("is", 1), ("an", 1), ("amazing", 1), ("language", 1), ("that", 1) ]
convert_in_Biagram.ipynbInput Text:
"I love programming in Python"
Tokenization:
Generate Bigrams:
from nltk import bigrams
# Input text
text = "I love programming in Python"
# Tokenization
tokens = text.split()
# Generate bigrams
bigram_list = list(bigrams(tokens))
print(bigram_list)
Output:
[('I', 'love'), ('love', 'programming'), ('programming', 'in'), ('in', 'Python')]
You can visualize bigrams as a graph or a frequency distribution:
from collections import Counter
import matplotlib.pyplot as plt
# Count bigram frequencies
bigram_counts = Counter(bigram_list)
# Plot the bigram frequency distribution
bigram_labels, frequencies = zip(*bigram_counts.items())
plt.bar([" ".join(bigram) for bigram in bigram_labels], frequencies)
plt.xticks(rotation=45)
plt.xlabel("Bigrams")
plt.ylabel("Frequency")
plt.title("Bigram Frequency Distribution")
plt.show()
CYK.ipynbThe CYK algorithm is used to determine if a given string can be generated by a given context-free grammar in Chomsky Normal Form (CNF). It builds a triangular table to parse the string.
We need the grammar in Chomsky Normal Form (CNF):
S → AB | BC
A → BA | a
B → CC | b
C → AB | a
"baaba"
Prepare the Table:
Create a triangular table where rows represent substrings of increasing lengths, starting from 1.
Initialize the First Row:
The first row corresponds to substrings of length 1. Check which grammar rules produce each character in the string.
Fill the Table for Substrings of Length 2 to n:
For each cell, combine results from shorter substrings (based on grammar rules) to determine possible non-terminals.
Check for S in the Top Cell:
If the start symbol S is in the top-right cell of the table, the string can be generated by the grammar.
"baaba"Step 1: Initialize Table with Length 1 Substrings
| b | a | a | b | a |
| {B} | {A,C} | {A,C} | {B} | {A,C} |
Step 2: Fill Length 2 Substrings
| {B} | {A,C} | {A,C} | {B} | {A,C} |
| {S} | {S,A} | {S,A} | {S} | |
Step 3: Fill Length 3 Substrings
| {B} | {A,C} | {A,C} | {B} | {A,C} |
| {S} | {S,A} | {S,A} | {S} | |
| {S} | {S,A} | {S} | | |
Step 4: Fill Length 4 and 5 Substrings
| {B} | {A,C} | {A,C} | {B} | {A,C} |
| {S} | {S,A} | {S,A} | {S} | |
| {S} | {S,A} | {S} | | |
| {S} | {S} | | | |
Final Check:
The top-right cell contains {S}, meaning the string "baaba" can be generated by the grammar.
from collections import defaultdict
# Function to implement CYK Algorithm
def cyk_algorithm(grammar, string):
# Convert grammar rules to a dictionary
rules = defaultdict(list)
for head, body in grammar:
rules[body].append(head)
# Length of the input string
n = len(string)
# Initialize the CYK table
table = [[set() for _ in range(n)] for _ in range(n)]
# Fill the first row of the table
for i, char in enumerate(string):
for lhs in rules[char]:
table[i][i].add(lhs)
# Fill the rest of the table
for length in range(2, n + 1): # Length of the span
for i in range(n - length + 1): # Start of the span
j = i + length - 1 # End of the span
for k in range(i, j): # Split point
for B in table[i][k]:
for C in table[k + 1][j]:
if B + C in rules:
for lhs in rules[B + C]:
table[i][j].add(lhs)
# Check if the start symbol is in the top-right cell
return 'S' in table[0][n - 1]
# Grammar in CNF: (Head, Body)
grammar = [
("S", "AB"), ("S", "BC"),
("A", "BA"), ("A", "a"),
("B", "CC"), ("B", "b"),
("C", "AB"), ("C", "a")
]
# Input string
string = "baaba"
# Run CYK Algorithm
result = cyk_algorithm(grammar, string)
print("String can be generated by the grammar:" if result else "String cannot be generated by the grammar.")
Output:
String can be generated by the grammar.
+--------------------+
| Raw Text Input |
+--------------------+
|
v
+--------------------+
| Text Preprocessing |
+--------------------+
|
v
+--------------------+
| Feature Extraction |
| (e.g., word vectors|
| or embeddings) |
+--------------------+
|
v
+------------------------------+
| NLP Tasks |
| - Text Classification |
| - Sentiment Analysis |
| - Named Entity Recognition |
| - Machine Translation |
| - Question Answering |
| - Text Summarization |
+------------------------------+
|
v
+--------------------+
| Output Results |
| (Predictions, etc.)|
+--------------------+
This pipeline demonstrates how raw text transitions through preprocessing, feature extraction, and task-specific models