You are on page 1of 3

• Write a program to implement TF-IDF

from sklearn.feature_extraction.text import TfidfVectorizer


import numpy as np

# Sample documents
documents = [
"This is the first document.",
"This document is the second document.",
"And this is the third one.",
"Is this the first document?"
]

# Create the TF-IDF vectorizer


vectorizer = TfidfVectorizer()

# Fit and transform the documents


tfidf_matrix = vectorizer.fit_transform(documents)

# Get the feature names (words)


feature_names = vectorizer.get_feature_names_out()

# Convert the TF-IDF matrix to a dense array for easier manipulation


dense_tfidf_matrix = tfidf_matrix.toarray()

# Display the TF-IDF matrix


print("TF-IDF Matrix:")
print(np.round(dense_tfidf_matrix, 2))
print()

# Display the feature names


print("Feature Names:")
print(feature_names)
• Write a program to implement Rhymes for a word
import nltk
from nltk.corpus import cmudict

nltk.download('cmudict')

def get_rhymes(word):
pronouncing_dict = cmudict.dict()

# Convert the word to lowercase


word = word.lower()

# Check if the word is in the pronunciation dictionary


if word in pronouncing_dict:
# Get the pronunciation of the word
pronunciation = pronouncing_dict[word][0]

# Extract the rhyming part of the pronunciation (from the stressed vowel to the end)
rhyming_part = pronunciation[pronunciation.index('1'):]

# Find words that have the same rhyming part in the pronunciation dictionary
rhymes = [w for w, pron in pronouncing_dict.items() if pron[-len(rhyming_part):] ==
rhyming_part]

return rhymes
else:
return []

# Example usage
word_to_rhyme = "cat"
rhymes = get_rhymes(word_to_rhyme)

if rhymes:
print(f"Rhymes for '{word_to_rhyme}':")
print(rhymes)
else:
print(f"No rhymes found for '{word_to_rhyme}'.")

You might also like