from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize
import stringFunction to clean the text by removing special characters, converting to lowercase and tokenizing
def clean_text(text):
text = re.sub('[^a-zA-Z0-9\s]', '', text) # Remove special characters
text = text.lower() # Convert to lowercase
tokens = word_tokenize(text) # Tokenize the text
return tokensFunction to remove stopwords from the text
def remove_stopwords(tokens):
stop_words = stopwords.words('english') + ['n', 't'] # Add 'n' and 't' as they are often added due to regex removal of newlines and tabs
tokens = [token for token in tokens if token not in stop_words] # Remove stop words from the list
return tokensFunction to stem the text using Porter Stemmer
def stem_text(tokens):
stemmer = PorterStemmer() # Initialize Porter Stemmer
stems = [stemmer.stem(token) for token in tokens] # Apply Porter Stemming to each word in the list
return stemsFunction to remove duplicates from a list
def remove_duplicates(tokens):
return list(set(tokens)) # Remove duplicates using set() function which automatically removes duplicates
Function to generate keywords from the cleaned text
def generate_keywords(text):
tokens = clean_text(text) # Clean the text by removing special characters and tokenizing
tokens = remove_stopwords(tokens) # Remove stop words from the list
tokens = stem_text(tokens) # Apply Porter Stemming to each word in the list
keywords = remove_duplicates(tokens) # Remove duplicates using set() function which automatically removes duplicates
return keywordsInput text
input_text = "We're CFO Plans, a outsourced accounting, tax, and CFO advisory services business in Los Angeles, CA. CFO Plans is an external finance partner for growing businesses. They offer four core service lines: accounting (bookkeeping, AP/AR, payroll), tax filing coordination, fractional/outsourced CFO services (financial reporting, forecasting, strategic planning), and operational services (vendor management, back-office support). They serve industries including real estate, tech startups, hospitality, professional services, healthcare & wellness, and e-commerce/DTC brands — positioning themselves as a scalable back-office finance team so founders can focus on building their companies rather than managing spreadsheets."
Generate keywords from the input text
keywords = generate_keywords(input_text)
Print the generated keywords
print("Generated Keywords:")
print(keywords)

