注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
麻烦请管理员忽略上一个帖子,不好意思不好意思不好意思,我重新编辑了一下再请求发布。
是UM的Python Functions, Files, and Dictionaries这门课的结课作业,最后一步实在搞不定了才来地里求大神指出错误。如果不合适我删帖...
绞尽脑汁想了哪不对,也是改了N遍才得到现在的代码,但还是有问题😢
谢谢大家🙏
题目如下:
We have provided some synthetic (fake, semi-randomly generated) twitter data in a csv file named project_twitter_data.csv which has the text of a tweet, the number of retweets of that tweet, and the number of replies to that tweet. We have also words that express positive sentiment and negative sentiment, in the files positive_words.txt and negative_words.txt.
Your task is to build a sentiment classifier, which will detect how positive or negative each tweet is. You will create a csv file, which contains columns for the Number of Retweets, Number of Replies, Positive Score (which is how many happy words are in the tweet), Negative Score (which is how many angry words are in the tweet), and the Net Score for each tweet. At the end, you upload the csv file to Excel or Google Sheets, and produce a graph of the Net Score vs Number of Retweets.
To start, define a function called strip_punctuation which takes one parameter, a string which represents a word, and removes characters considered punctuation from everywhere in the word. (Hint: remember the .replace() method for strings.)
Next, copy in your strip_punctuation function and define a function called get_pos which takes one parameter, a string which represents one or more sentences, and calculates how many words in the string are considered positive words. Use the list, positive_words to determine what words will count as positive. The function should return a positive integer - how many occurrences there are of positive words in the text. Note that all of the words in positive_words are lower cased, so you’ll need to convert all the words in the input string to lower case as well.
Next, copy in your strip_punctuation function and define a function called get_neg which takes one parameter, a string which represents one or more sentences, and calculates how many words in the string are considered negative words. Use the list, negative_words to determine what words will count as negative. The function should return a positive integer - how many occurrences there are of negative words in the text. Note that all of the words in negative_words are lower cased, so you’ll need to convert all the words in the input string to lower case as well.
卡在最后一步:
Finally, copy in your previous functions and write code that opens the file project_twitter_data.csv which has the fake generated twitter data (the text of a tweet, the number of retweets of that tweet, and the number of replies to that tweet). Your task is to build a sentiment classifier, which will detect how positive or negative each tweet is. Copy the code from the code windows above, and put that in the top of this code window. Now, you will write code to create a csv file called resulting_data.csv, which contains the Number of Retweets, Number of Replies, Positive Score (which is how many happy words are in the tweet), Negative Score (which is how many angry words are in the tweet), and the Net Score (how positive or negative the text is overall) for each tweet. The file should have those headers in that order. Remember that there is another component to this project. You will upload the csv file to Excel or Google Sheets and produce a graph of the Net Score vs Number of Retweets. Check Coursera for that portion of the assignment, if you’re accessing this textbook from Coursera.
下一段是原题给出的代码:
punctuation_chars = ["'", '"', ",", ".", "!", ":", ";", '#', '@']
# lists of words to use
positive_words = []
with open("positive_words.txt") as pos_f:
for lin in pos_f:
if lin[0] != ';' and lin[0] != '\n':
positive_words.append(lin.strip())
negative_words = []
with open("negative_words.txt") as pos_f:
for lin in pos_f:
if lin[0] != ';' and lin[0] != '\n':
negative_words.append(lin.strip())
我写的代码如下:
def strip_punctuation(s):
for i in s:
if i in punctuation_chars:
s = s.replace(i, " ")
return s
def get_pos(s):
s_lower = s.lower()
s_lower_new = strip_punctuation(s_lower)
s_list = s_lower_new.split(" ")
num = 0
for i in s_list:
if i in positive_words:
num = num + 1
else:
num = num
return num
def get_neg(s):
s_lower = s.lower()
s_lower_new = strip_punctuation(s_lower)
s_list = s_lower_new.split(" ")
num = 0
for i in s_list:
if i in negative_words:
num = num + 1
else:
num = num
return num
------------------------以上都是对的,下面不知道哪里错了----------------------------------------
resultfile = open("resulting_data.csv", "w")
resultfile.write('Number of Retweets, Number of Replies, Positive Score, Negative Score, Net Score')
resultfile.write('\n')
csv_file = open('project_twitter_data.csv', "r")
lines = csv_file.readlines()
for line in lines:
line_list = line.strip().split(",")
resultfile.write('{},{},{},{},{}'.format(line_list[1], line_list[2], get_pos(line_list[0]), get_neg(line_list[0]), get_pos(line_list[0]) - get_neg(line_list[0])))
resultfile.write("\n")
csv_file.close()
resultfile.close()
得到的结果下:
result
Number of Retweets, Number of Replies, Positive Score, Negative Score, Net Score
retweet_count,reply_count,0,0,0 (不明白这是哪里来的)
3,0,0,0,0
1,0,2,2,0
1,2,1,0,1
3,1,1,0,1
6,0,2,0,2
9,5,2,0,2
19,0,2,0,2
0,0,0,3,-3
0,0,0,2,-2
82,2,4,0,4
0,0,0,1,-1
0,0,1,0,1
47,0,2,0,2
2,1,1,0,1
0,2,1,0,1
0,0,2,1,1
4,6,3,0,3
19,0,3,1,2
0,0,1,1,0
Result Actual Value Expected Value Notes
Pass 'Numbe...core\n' 'Numbe...core\n' checking that the headers are set correctly.
Fail '9' '19' checking that the value for a particular cell matches.
Fail '2' '-3' checking that the value of the net score is correct for a particular cell.
Fail 21 20 checking that the file has the correct number of rows.
Pass 5 5 checking that the file has the correct number of columns.
|