NexusAI

Online
Creative
Professional
Analytical
Hello Alex! I'm NexusAI, your premium AI assistant. How can I help you today? I can assist with creative tasks, professional writing, code generation, and much more.
9:30 AM
A
Can you help me write a short Python function to extract keywords from text?
9:31 AM
Absolutely! Here's a Python function that extracts keywords from text using the NLTK library:
Python
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from collections import Counter

def extract_keywords(text, num_keywords=5):
    """
    Extract key words from a text string.
    
    Args:
        text (str): The text to extract keywords from
        num_keywords (int): Number of keywords to return
        
    Returns:
        list: A list of the most common keywords
    """
    # Download necessary NLTK data (only needed once)
    nltk.download('punkt', quiet=True)
    nltk.download('stopwords', quiet=True)
    
    # Tokenize and convert to lowercase
    words = word_tokenize(text.lower())
    
    # Remove stopwords and non-alphabetic tokens
    stop_words = set(stopwords.words('english'))
    words = [word for word in words if word.isalpha() and word not in stop_words]
    
    # Count word frequencies
    word_freq = Counter(words)
    
    # Return the most common words
    return [word for word, freq in word_freq.most_common(num_keywords)]
You can use this function by passing in any text string and it will return the most common meaningful words. The num_keywords parameter lets you control how many keywords are returned. Would you like me to explain how it works in more detail or modify the function in any way?
9:32 AM
A
That's perfect! Can you also show a simple example of how to use it?
9:34 AM
Home
Chat
Discover
Profile