-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyword_sentence.py
103 lines (87 loc) · 4.92 KB
/
keyword_sentence.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# import libraries
import pandas as pd
import os
from transformers import pipeline
import re
# # combine txt files that start with "01" in the folder "Year_2021_corpus" into one txt file called "Year_2021_corpus_01.txt" and so on.
# for i in range(7):
# with open("Year_2021_corpus_0" + str(i+1) + ".txt", "w") as f:
# for file_name in os.listdir("Year_2021_corpus"):
# if file_name.startswith("0"+str(i+1)):
# with open("Year_2021_corpus/" + file_name, "r") as f1:
# f.write(f1.read())
# read a file from local folder and save it as a data frame called "dictionary"
dictionary = pd.read_excel("dictionary 2.0.xlsx")
# extract keyword list from dictionary
keyword_list = dictionary["traget n-gram"].tolist()
# print(keyword_list)
def keyword_sentence(file_name, number):
# read a txt file, extract sentences which are separated by period, exclamation point or question mark while keeping the punctuations in the sentence, remove any new line, and save sentences in a list called "sentences"
# with open(file_name, "r") as f:
# text = f.read()
# # split the text into sentences using regex, which handles edge cases like abbreviations and multiple spaces
# sentences = re.findall(r'(\b\S.+?[.!?]+["’”]?)(?=\s+|$)', text, re.DOTALL)
# # remove any leading/trailing whitespace and newlines from sentences
# sentences = [s.strip() for s in sentences]
with open(file_name, "r") as f:
text = f.read().replace("\n", " ")
sentences = re.findall(r'\b.+?[.!?]+(?:\s|\n\n|$)', text)
sentence_df = pd.DataFrame({"sentence": sentences})
sentence_df.to_csv("Year_2022_Sentiment_Analysis/frank_2022_sentence_0" + str(number+1) + ".csv", index=False)
# find out sentences in sentences list which contain any keyword in keyword list and save those sentences in a list called "keyword_sentences"
keyword_sentences = []
for sentence in sentences:
for keyword in keyword_list:
if keyword in sentence:
keyword_sentences.append(sentence)
break
return keyword_sentences
def keyword_sentence_double(file_name, number):
# read a txt file, extract sentences which are separated by period, exclamation point or question mark while keeping the punctuations in the sentence, remove any new line, and save sentences in a list called "sentences"
# with open(file_name, "r") as f:
# text = f.read()
# # split the text into sentences using regex, which handles edge cases like abbreviations and multiple spaces
# sentences = re.findall(r'(\b\S.+?[.!?]+["’”]?)(?=\s+|$)', text, re.DOTALL)
# # remove any leading/trailing whitespace and newlines from sentences
# sentences = [s.strip() for s in sentences]
with open(file_name, "r") as f:
text = f.read().replace("\n", " ")
sentences = re.findall(r'\b.+?[.!?]+(?:\s|\n\n|$)', text)
sentence_df = pd.DataFrame({"sentence": sentences})
sentence_df.to_csv("Year_2022_Sentiment_Analysis/frank_2022_sentence_" + str(number) + ".csv", index=False)
# find out sentences in sentences list which contain any keyword in keyword list and save those sentences in a list called "keyword_sentences"
keyword_sentences = []
for sentence in sentences:
for keyword in keyword_list:
if keyword in sentence:
keyword_sentences.append(sentence)
break
return keyword_sentences
# frank_2022_01 = keyword_sentence("Year_2022_corpus/01_frank.txt")
#
classifer = pipeline("sentiment-analysis", model="cardiffnlp/twitter-xlm-roberta-base-sentiment")
# res = classifer(frank_2022_01)
# print(res)
# print(frank_2022_01)
# write out the res and frank_2022_01 to a csv file, each represents a column
# df = pd.DataFrame({"sentence": frank_2022_01, "sentiment": res})
# df.to_csv("frank_2022_01.csv", index=False)
for i in range(9):
frank_2022 = keyword_sentence("Year_2022_corpus/0" + str(i+1) + "_frank.txt", i)
res = classifer(frank_2022)
df = pd.DataFrame({"sentence": frank_2022, "sentiment": res})
df.to_csv("Year_2022_Sentiment_Analysis/frank_2022_SA_0" + str(i+1) + ".csv", index=False)
for i in range(10, 13):
frank_2022 = keyword_sentence_double("Year_2022_corpus/" + str(i) + "_frank.txt", i)
res = classifer(frank_2022)
df = pd.DataFrame({"sentence": frank_2022, "sentiment": res})
df.to_csv("Year_2022_Sentiment_Analysis/frank_2022_SA_" + str(i) + ".csv", index=False)
# Project for Year_2022_corpus
# for i in range(12):
# Year_2021_s1 = keyword_sentence("Year_2021_corpus_01.txt")
# Year_2021_s2 = keyword_sentence("Year_2021_corpus_02.txt")
# Year_2021_s3 = keyword_sentence("Year_2021_corpus_03.txt")
# Year_2021_s4 = keyword_sentence("Year_2021_corpus_04.txt")
# Year_2021_s5 = keyword_sentence("Year_2021_corpus_05.txt")
# Year_2021_s6 = keyword_sentence("Year_2021_corpus_06.txt")
# Year_2021_s7 = keyword_sentence("Year_2021_corpus_07.txt")