我已经以.csv文件的形式获得了YouTube评论,我想做的就是在评论中搜索特定的单词。我有一个列表,我认为我正在将包含注释的行与之进行比较,但是当遇到该列表中的一个术语时,似乎并没有将其添加到slurCount中,而noSlurCount会计算所有注释。 >
class EnumUtils {
constructor() {}
// This method takes a map of elements and convert them to freeze objects (enum-like object).
createEnum(mapItems) {
// Check the existence of the mapItems parameter. If not exists, return null instance.
if (!mapItems || mapItems.length <= 0) {
return null;
}
// This new object will hold the freeze array object.
const symbolMap = {};
// Assign each object.
mapItems.forEach((value, key) => {
symbolMap[key] = value;
});
// Freeze the object and return it.
return Object.freeze(symbolMap);
}
}
const enumUtils = new EnumUtils();
module.exports = enumUtils;
任何帮助都会很棒
答案 0 :(得分:1)
您至少应该针对您的清单列表进行测试。 这是错误的:
commentText = {row[2]} if commentText in {row[2]}:
这绝不是真的,因为您进行了测试:
if {"something"} in { "something" }:
False
是因为..不在其中:o)
更好的方法是使用集合和set.intersection():
创建配置文件:
with open('target_file.csv', "w", encoding="utf8") as f:
f.write("id,no idea,comment,likes, what columns,you,have\n")
f.write("1,,bla SlurZ bla,10,,,\n")
f.write("2,,bla SlurZ bla,20,,,\n")
f.write("3,,bla SlurZ. bla,30,,,\n")
f.write("4,,bla no bla,40,,,\n")
f.write("5,,bla no bla,50,,,\n")
f.write("6,,bla no bla,60,,,\n")
f.write("7,,bla no bla,70,,,\n")
f.write("8,,bla slurX- bla,80,,,\n")
f.write("9,,bla SlurZ bla,90,,,\n")
f.write("10,,bla SlurZ bla,100,,,\n")
f.write("11,,bla SlurZ bla,110,,,\n")
程序:
import csv
slurCount = 0
noSlurCount = 0
line_count = 0
with open('target_file.csv', encoding="utf8") as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
# use a set
slurs = {"slurX", "slurY", "SlurZ", "slurETC"}
# get the header
header = ", ".join(next(csv_reader))
print(f'Column names are {header}')
for row in csv_reader:
line_count += 1
# you need to clean the comment-words from punctuation marks
# so it detects slurY. or slurY- as slur as well
if slurs.intersection( (x.strip(",.-!?: ") for x in row[2].split() ) ):
slurCount += 1
print ("\t Comment contained a slur:")
print (f"\t\t{row[2]}")
else:
noSlurCount += 1
print ("\t Comment didn't contain a slur")
print(f'\t\t The comment ID is: {row[0]}')
print(f'\t\t Their comment was: {row[2]}')
print(f'\t\t The comment received: {row[3]} likes.')
print(f'Processed {line_count} lines.')
print(f'Found {slurCount} comments with slurs.')
print(f'Found {noSlurCount} comments without slurs.')
输出:
Column names are id, no idea, comment, likes, what columns, you, have
Comment contained a slur:
bla SlurZ bla
Comment contained a slur:
bla SlurZ bla
Comment contained a slur:
bla SlurZ. bla
Comment didn't contain a slur
The comment ID is: 4
Their comment was: bla no bla
The comment received: 40 likes.
Comment didn't contain a slur
The comment ID is: 5
Their comment was: bla no bla
The comment received: 50 likes.
Comment didn't contain a slur
The comment ID is: 6
Their comment was: bla no bla
The comment received: 60 likes.
Comment didn't contain a slur
The comment ID is: 7
Their comment was: bla no bla
The comment received: 70 likes.
Comment contained a slur:
bla slurX- bla
Comment contained a slur:
bla SlurZ bla
Comment contained a slur:
bla SlurZ bla
Comment contained a slur:
bla SlurZ bla
Processed 11 lines.
Found 7 comments with slurs.
Found 4 comments without slurs.
Doku: