Loading…
DS · Text mining · Program 31 · Bonus
Text preprocessing: tokenization, stop-word removal, stemming.
1import nltk2from nltk.tokenize import word_tokenize3from nltk.corpus import stopwords4from nltk.stem import PorterStemmer5 6nltk.download('punkt_tab', quiet=True)7nltk.download('stopwords', quiet=True)8 9text = """Data Science is an interdisciplinary field that uses scientific methods,10processes, algorithms and systems to extract knowledge from data. Data scientists11are analyzing large datasets, building predictive models, and communicating their12findings clearly. The studies of machine learning are running everywhere today!"""13 14tokens = [w for w in word_tokenize(text.lower()) if w.isalnum()]15filtered = [w for w in tokens if w not in stopwords.words('english')]16stemmed = [PorterStemmer().stem(w) for w in filtered]17 18print("Tokens:", tokens)19print("\nAfter Stop-word Removal:", filtered)20print("\nAfter Stemming:", stemmed)Tokens: ['data', 'science', 'is', 'an', 'interdisciplinary', 'field', 'that', 'uses', 'scientific', 'methods', 'processes', 'algorithms', 'and', 'systems', 'to', 'extract', 'knowledge', 'from', 'data', 'data', 'scientists', 'are', 'analyzing', 'large', 'datasets', 'building', 'predictive', 'models', 'and', 'communicating', 'their', 'findings', 'clearly', 'the', 'studies', 'of', 'machine', 'learning', 'are', 'running', 'everywhere', 'today'] After Stop-word Removal: ['data', 'science', 'interdisciplinary', 'field', 'uses', 'scientific', 'methods', 'processes', 'algorithms', 'systems', 'extract', 'knowledge', 'data', 'data', 'scientists', 'analyzing', 'large', 'datasets', 'building', 'predictive', 'models', 'communicating', 'findings', 'clearly', 'studies', 'machine', 'learning', 'running', 'everywhere', 'today'] After Stemming: ['data', 'scienc', 'interdisciplinari', 'field', 'use', 'scientif', 'method', 'process', 'algorithm', 'system', 'extract', 'knowledg', 'data', 'data', 'scientist', 'analyz', 'larg', 'dataset', 'build', 'predict', 'model', 'commun', 'find', 'clearli', 'studi', 'machin', 'learn', 'run', 'everywher', 'today']
Run program 31 in your browser
Python 3.14 · Pyodide. Change the code or the input, run it, break it, and see what happens.