Repository navigation
Expand file tree
/
Copy pathprocessor.py
More file actions
56 lines (44 loc) · 1.8 KB
/
Copy pathprocessor.py
File metadata and controls
56 lines (44 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import nltk
import csv
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import PorterStemmer
import pandas as pd
# Download necessary data
nltk.download('punkt')
nltk.download('punkt_tab')
nltk.download('stopwords')
def remove_stopwords(text):
stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(text)
filtered_text = [word for word in word_tokens if word.lower() not in stop_words]
return " ".join(filtered_text)
def apply_stemming(text):
ps = PorterStemmer()
word_tokens = word_tokenize(text)
stemmed_text = [ps.stem(word) for word in word_tokens]
return " ".join(stemmed_text)
def preprocess_text(text):
# 1. Lowercase
text = text.lower()
# 2. Tokenize
word_tokens = word_tokenize(text)
# 3. Remove Stop Words
stop_words = set(stopwords.words('english'))
filtered_text = [word for word in word_tokens if word not in stop_words]
# 4. Stemming
ps = PorterStemmer()
stemmed_text = [ps.stem(word) for word in filtered_text]
return " ".join(stemmed_text)
input_path = "C:\\Users\\elias\\Desktop\\CS 5124 Data Visualization\\Project 3 - TV Time\\The-Office-Lines-V4.csv"
output_path = "C:\\Users\\elias\\Desktop\\CS 5124 Data Visualization\\Project 3 - TV Time\\The-Office-Lines-V4-Processed.csv"
with open(input_path, newline="", encoding="utf-8") as infile, \
open(output_path, "w", newline="", encoding="utf-8") as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile)
for row in reader:
# ensure the row has at least 7 columns
row += [""] * (7 - len(row))
col_f = row[5] # column F is index 5
row[6] = preprocess_text(col_f) # column G is index 6
writer.writerow(row)