Nlp

Basic Sentiment Analysis

Learn how to perform basic sentiment analysis using Python.

  1. Create sentiment dictionary:
sentiment = {
    'good': 1,
    'bad': -1,
    'great': 2,
    'terrible': -2
}
  1. Analyze text:
def analyze(text):
    words = text.lower().split()
    return sum(sentiment.get(word, 0) for word in words)
  1. Test analysis:
print(analyze("This is a great day!"))

Read more: Sentiment Analysis

Basic Text Processing for NLP

Learn basic text processing techniques for Natural Language Processing with practical examples.

  1. Tokenize text:
import re
text = "This is a sample text. It contains multiple sentences!"
tokens = re.findall(r'\w+', text.lower())
  1. Remove stopwords:
stopwords = set(['a', 'the', 'is', 'and', 'of'])
filtered_tokens = [word for word in tokens if word not in stopwords]
  1. Stem words:
from nltk.stem import PorterStemmer
stemmer = PorterStemmer()
stems = [stemmer.stem(word) for word in filtered_tokens]
  1. Create n-grams:
def create_ngrams(tokens, n=2):
    return [' '.join(tokens[i:i+n]) for i in range(len(tokens)-n+1)]
  1. Build vocabulary:
vocab = {word: idx for idx, word in enumerate(set(stems))}

Advanced techniques: