-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessing.py
More file actions
87 lines (64 loc) · 2.24 KB
/
Copy pathprocessing.py
File metadata and controls
87 lines (64 loc) · 2.24 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#import numpy as np
import pandas as pd
import csv
import spacy
# specify path to material
path = "materials\chakoteya\StarTrek_NextGen_transcript_complete.txt"
# specify list of characters
list = ["PICARD", "DATA", "Q", "RIKER", "TROI"]
def process(list,path):
# process raw text for each character
for name in list:
# create file name
title = "quotes_%s.csv" % name
#print(title)
# read lines from script into csv
with open(path) as script:
with open(title, "w") as file:
for line in script:
if line.startswith(name):
#print(line)
file.write(line)
file.write("\n")
# paths to newly created .csv's (same order as characters in 'list'!)
filepaths = ["quotes_PICARD.csv", "quotes_DATA.csv", "quotes_Q.csv", "quotes_RIKER.csv", "quotes_TROI.csv"]
testlist = ["testfile.csv"]
def keywords(list):
# process each of the files from the list
for path in list:
# convert to dataframe
dataframe = pd.read_csv(path, names=["line"])
dataframe = dataframe.dropna()
print(dataframe)
all_lemmas = []
# read file
with open(path) as file:
filereader = csv.reader(file)
# per row
for row in filereader:
# concatenate row to string
string = "".join(str(x) for x in row)
# load spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp(string)
# collect lemmas
lemmas = [word.lemma_ for word in doc]
# add lemmas as new column
all_lemmas.append(lemmas)
print("done")
# clean list of all lemmas
all = [list for list in all_lemmas if list != []]
print(all)
# add as second column
dataframe.insert(1, "lemmas", all)
print(dataframe)
print("this is where the keyword function goes")
def main():
# process raw data
#process(list,path)
print("raw data has been processed")
# test keyword function
keywords(testlist)
print("keyword function has been tested")
if __name__ == "__main__":
main()