-
Notifications
You must be signed in to change notification settings - Fork 1
/
getData.py
753 lines (701 loc) · 22.5 KB
/
getData.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
# 数据综合分析
import os
import codecs
from aip import AipNlp
import random
root = "Data"
# # 单元函数
# 读取文件列表
def readFileList():
for dirpath, dirnames, fileList in os.walk(root):
pass
return fileList
# 读取文件列表中的弹幕数
def numSta():
fileList = readFileList()
num = 0
for name in fileList:
path = root + '\\' + name
f = codecs.open(path, 'r', 'utf-8')
num += int(f.readlines()[3].split(':')[1])
return num
# 随机数生成器
def randomList(total, rate):
need = int(total*rate)
randomNum = []
for n in range(need):
randomNum.append(random.randint(0, total-1))
randomNum.sort()
return randomNum
# 按排名分四档
def classByNum():
fl = readFileList()
totalNum = numSta()
averNum = totalNum // len(fl)
quarter = averNum // 2
c1 = []
c2 = []
c3 = []
c4 = []
for name in fl:
path = root + '\\' + name
f = codecs.open(path, 'r', 'utf-8')
num = int(f.readlines()[3].split(':')[1])
if num >= averNum + quarter:
c1.append(name)
elif averNum <= num < (averNum + quarter):
c2.append(name)
elif (averNum - quarter) <= num < averNum:
c3.append(name)
else:
c4.append(name)
f.close()
return c1, c2, c3, c4
# # 弹幕时间分布
# 总体分布
def totalRead(fileList, file = 'timeCount.txt'):
time = []
for name in fileList:
#print(name)
path = root + '\\' + name
f = codecs.open(path, 'r', 'utf-8')
timeList = []
for line in f.readlines()[5:]:
timeList.append(float(line.split()[0]))
totalTime = timeList[-1]
for n in range(len(timeList)):
timeList[n] = timeList[n] / totalTime
time.extend(timeList)
f.close()
time.sort()
size = 0.001
plus = size/2
f = open(file, 'w')
num = 0
for n in time:
if n >= 0 and n < plus:
num += 1
ws = str(num) + '\n'
f.write(ws)
front = size - plus
later = size + plus
while later <= 1 - plus:
num = 0
for n in time:
if n >= front and n < later:
num += 1
ws = str(num) + '\n'
f.write(ws)
front += size
later += size
num = 0
for n in time:
if n >= 1 - plus and n <= 1:
num += 1
ws = str(num) + '\n'
f.write(ws)
f.close()
# 分类分布
def classDistri():
c1, c2, c3, c4 = classByNum()
totalRead(c1, 'timeCount1.txt')
totalRead(c2, 'timeCount2.txt')
totalRead(c3, 'timeCount3.txt')
totalRead(c4, 'timeCount4.txt')
# # 视频开头部分
# 实际占比
def front():
c1, c2, c3, c4 = classByNum()
def ana(c, rate):
total = 0
num = 0
for name in c:
path = root + '\\' + name
f = codecs.open(path, 'r', 'utf-8')
total += int(f.readlines()[3].split(':')[1])
f.seek(0)
timeList = []
for line in f.readlines()[5:]:
timeList.append(float(line.split()[0]))
f.close()
finalTime = float(timeList[-1])
divTime = finalTime*rate
for time in timeList:
num += 1
if float(time)>divTime:
break
percent = num/total
print(percent)
return percent
f = open('front.txt', 'w')
for rate in 0.025, 0.05, 0.1:
for c in c1, c2, c3, c4:
percent = ana(c, rate)
ws = str(percent)+' '
f.write(ws)
ws = '\n'
f.write(ws)
f.close()
# 整体弹幕情绪占比
def baiduAPI(fileList, filename):
# 210条/分钟
""" 你的 APPID AK SK """
APP_ID = '11524387'
API_KEY = 'Zt01mRgDfR1fNLMy2l1ej9W2'
SECRET_KEY = 'tIdOYOUFIsBj65VFtnz5K9x2crv2AFbc'
client = AipNlp(APP_ID, API_KEY, SECRET_KEY) # 生成端口
expectTime = 60 # 期望运行时间(分钟)
commentNum = numSta()
rate = (expectTime*210)/commentNum # 调节速率
negative = 0
neutral = 0
positive = 0
fileNum = 1
fileTot = len(fileList)
for name in fileList:
try:
print("{}/{}".format(fileNum, fileTot))
fileNum += 1
path = root + '\\' + name
f = codecs.open(path, 'r', 'utf-8')
comSum = int(f.readlines()[3].split(':')[1][:-2])
f.seek(0)
time = 1
err = 0
randomNum = randomList(comSum, rate)
comSum = int(comSum*rate)
commentList = []
except:
continue
# 读取弹幕目录
for line in f.readlines()[5:]:
try:
comment = line.split()[2]
commentList.append(comment)
except:
continue
# 随机判断弹幕情绪
for n in randomNum:
comment = commentList[n]
try:
print("\t{}/{}".format(time, comSum))
result = client.sentimentClassify(comment)
sentiment = result['items'][0]['sentiment']
if sentiment == 0:
negative += 1
elif sentiment == 1:
neutral += 1
else:
positive += 1
time += 1
except:
err += 1
comSum -= 1
continue # 特殊字符直接跳过
f.close()
total = negative + neutral + positive
# 输出三种情绪所占百分比
f = open(filename, 'w')
ws = "negative {}\nneutral {}\npositive {}\nerror {}".format(negative/total, neutral/total, positive/total, err)
f.write(ws)
f.close()
# 开头和后部部分情绪占比
def fbPer():
APP_ID = '11524387'
API_KEY = 'Zt01mRgDfR1fNLMy2l1ej9W2'
SECRET_KEY = 'tIdOYOUFIsBj65VFtnz5K9x2crv2AFbc'
client = AipNlp(APP_ID, API_KEY, SECRET_KEY) # 生成端口
expectTime = 60 # 期望运行时间(分钟)
negF = 0
negB = 0
neuF = 0
neuB = 0
posF = 0
posB = 0
frontRate = 0.1
fileList = readFileList()
commentNum = numSta()
rate = (expectTime * 220) / commentNum # 调节速率
fileNum = 1
fileTot = len(fileList)
fro = 0
b = 0
for name in fileList:
print("{}/{}".format(fileNum, fileTot))
fileNum += 1
path = root + '\\' + name
f = codecs.open(path, 'r', 'utf-8')
timeList = []
try:
for line in f.readlines()[5:]:
timeList.append(float(line.split()[0]))
except:
continue
f.seek(0)
finalTime = float(timeList[-1])
divTime = finalTime * frontRate # 前后部分割时间点
frontNum = 0 # 分割点的弹幕数
for time in timeList:
frontNum += 1
if float(time) > divTime:
break
comSum = int(f.readlines()[3].split(':')[1])
f.seek(0)
time = 1
randomNum = randomList(comSum, rate)
comSum = int(comSum * rate)
commentList = []
# 读取弹幕目录
try:
for line in f.readlines()[5:]:
comment = line.split()[2]
commentList.append(comment)
except:
continue
# 随机判断弹幕情绪
flag = False
negative = 0
neutral = 0
positive = 0
for n in randomNum:
comment = commentList[n]
if n > frontNum and flag is False:
flag = True
negF += negative
neuF += neutral
posF += positive
negative = 0
neutral = 0
positive = 0
fro += 1
try:
print("\t{}/{}".format(time, comSum))
result = client.sentimentClassify(comment)
sentiment = result['items'][0]['sentiment']
if sentiment == 0:
negative += 1
elif sentiment == 1:
neutral += 1
else:
positive += 1
time += 1
except:
comSum -= 1
continue # 特殊字符直接跳过
f.close()
if flag is False:
negF = 0
neuF = 0
posF = 0
negB += negative
neuB += neutral
posB += positive
f = open('fbper.txt', 'w')
fTotal = negF + neuF + posF
bTotal = negB + neuB + posB
if fTotal == 0:
fTotal = 1
if bTotal == 0:
bTotal = 1
ws = "{} {} {}\n".format(negF/fTotal, neuF/fTotal, posF/fTotal)
f.write(ws)
ws = "{} {} {}\n".format(negB / bTotal, neuB / bTotal, posB / bTotal)
f.write(ws)
f.close()
# 方差分析
def frontBackAI():
APP_ID = '11524387'
API_KEY = 'Zt01mRgDfR1fNLMy2l1ej9W2'
SECRET_KEY = 'tIdOYOUFIsBj65VFtnz5K9x2crv2AFbc'
client = AipNlp(APP_ID, API_KEY, SECRET_KEY) # 生成端口
expectTime = 120 # 期望运行时间(分钟)
negF = []
negB = []
neuF = []
neuB = []
posF = []
posB = []
frontRate = 0.1
fileList = readFileList()
commentNum = numSta()
rate = (expectTime * 220) / commentNum # 调节速率
fileNum = 1
fileTot = len(fileList)
fro = 0
b = 0
for name in fileList:
print("{}/{}".format(fileNum, fileTot))
fileNum += 1
path = root + '\\' + name
f = codecs.open(path, 'r', 'utf-8')
timeList = []
try:
for line in f.readlines()[5:]:
timeList.append(float(line.split()[0]))
except:
continue
f.seek(0)
finalTime = float(timeList[-1])
divTime = finalTime * frontRate # 前后部分割时间点
frontNum = 0 # 分割点的弹幕数
for time in timeList:
frontNum += 1
if float(time) > divTime:
break
comSum = int(f.readlines()[3].split(':')[1])
f.seek(0)
time = 1
randomNum = randomList(comSum, rate)
comSum = int(comSum * rate)
commentList = []
# 读取弹幕目录
try:
for line in f.readlines()[5:]:
comment = line.split()[2]
commentList.append(comment)
except:
continue
# 随机判断弹幕情绪
flag = False
negative = 0
neutral = 0
positive = 0
for n in randomNum:
comment = commentList[n]
if n > frontNum and flag is False:
flag = True
total = negative + neutral + positive
if total == 0:
total = 1
negF.append(negative / total)
neuF.append(neutral / total)
posF.append(positive / total)
negative = 0
neutral = 0
positive = 0
fro += 1
try:
print("\t{}/{}".format(time, comSum))
result = client.sentimentClassify(comment)
sentiment = result['items'][0]['sentiment']
if sentiment == 0:
negative += 1
elif sentiment == 1:
neutral += 1
else:
positive += 1
time += 1
except:
comSum -= 1
continue # 特殊字符直接跳过
f.close()
if flag is False:
negF.append(0.0)
neuF.append(0.0)
posF.append(0.0)
fro += 1
total = negative + neutral + positive
if total == 0:
total = 1
negB.append(negative / total)
neuB.append(neutral / total)
posB.append(positive / total)
b += 1
def putOut(file, f, b):
for n in f:
ws = '{} '.format(n)
file.write(ws)
file.write('\n\n')
for n in b:
ws = '{} '.format(n)
file.write(ws)
negativeF = open('negative.txt', 'w')
neutralF = open('neutral.txt', 'w')
positiveF = open('positive.txt', 'w')
putOut(negativeF, negF, negB)
putOut(neutralF, neuF, neuB)
putOut(positiveF, posF, posB)
positiveF.close()
neutralF.close()
negativeF.close()
print(fro, b)
"""
MATLAB代码
a = [...]; % 开头数据,列向量
b = [...]; % 后部数据,列向量
x = [a b];
p = anova1(x)
"""
# # 视频内容类别
# 一档和其他档情绪占比
def firstToThreeOther():
c1, c2, c3, c4 = classByNum()
c2 += c3 + c4
baiduAPI(c1, "first.txt")
baiduAPI(c2, "other.txt")
# 关键词搜索法
def classify():
# 分类标准
classDic = {
"高兴": 0,
"内容与操作": 0,
"画面感": 0,
"关联性": 0,
"出乎意料": 0,
"感谢": 0,
"其他": 0,
"主动性": 0,
"负面情绪": 0,
"B站特色": 0,
}
# 关键词
# 高兴
humourList = ['233', '哈哈', 'h', '笑', '噗', '真香']
# 内容与操作
act6List = ['66', '不错', '赞', '投币', '硬币', '收藏', '!', '!', 'yeah', '强', '帅', '人才', '牛', '厉害', '稳']
# 画面感
text6List = ['福利', '良心', '打call', '喜欢', '可爱', '萌', '好看', '棒', 'dalao', '大佬', '燃', '美', '火', '哇',
'漂亮', '啊啊啊', 'cool', '酷']
# 关联性
seriesList = ['终于', 'leile', '来了', 'lei了', '期待', '失踪人口', '好久不见', '系列', '更了', '高产', '失踪人口回归',
'来晚了', '生日快乐']
# 出乎意料
surpriseList = ['意外', '惊', '没想到', '卧槽', '妈耶', 'woc', '巧了', '心脏', '吓', '高能', '社会', '妙']
# 感谢
thanksList = ['感谢', '辛苦', '心疼', '泪', '谢', '爱', '支持', '哭']
# 主动性
positiveList = ['干', '肝', '?', '?', '了解一下', 'emm', 'BGM', 'bgm', '额', '想', '感觉', '嘤']
# 负面情绪
negativeList = ['夭寿', '难受', '骂', '完了', '…', '别', '麻烦', '。。', 'gg', 'GG', '恶', '活该', '死']
# B站特色
biliList = ['♂', 'up', 'Up', 'UP', '基佬', '空降', '字幕', '计数', '1.25', '1.5', '0.5', '开头', '火钳刘明', '每日', '哲学',
'Van', '自由', '兄贵', '那个男人', '欢迎回来', 'gay', '弹幕', '鬼畜']
fileList = readFileList()
fileSum = len(fileList)
fileCount = 0
for name in fileList:
fileCount += 1
path = root + '\\' + name
f = codecs.open(path, 'r', 'utf-8')
comSum = int(f.readlines()[3].split(':')[1][:-2])
comCount = 0
f.seek(0) # 文件读取后要复位
for line in f.readlines()[5:]:
try:
comment = line.split()[2]
except:
continue
hasSel = False
# 判断
for n in humourList:
if n in comment:
classDic["高兴"] += 1
comCount += 1
hasSel = True
if not hasSel:
for n in act6List:
if n in comment:
classDic["内容与操作"] += 1
comCount += 1
hasSel = True
if not hasSel:
for n in text6List:
if n in comment:
classDic["画面感"] += 1
comCount += 1
hasSel = True
if not hasSel:
for n in seriesList:
if n in comment:
classDic["关联性"] += 1
comCount += 1
hasSel = True
if not hasSel:
for n in surpriseList:
if n in comment:
classDic["出乎意料"] += 1
comCount += 1
hasSel = True
if not hasSel:
for n in thanksList:
if n in comment:
classDic["感谢"] += 1
comCount += 1
hasSel = True
if not hasSel:
for n in positiveList:
if n in comment:
classDic["主动性"] += 1
comCount += 1
hasSel = True
if not hasSel:
for n in negativeList:
if n in comment:
classDic["负面情绪"] += 1
comCount += 1
hasSel = True
if not hasSel:
for n in biliList:
if n in comment:
classDic["B站特色"] += 1
comCount += 1
classDic["其他"] += comSum-comCount
f.close()
# 计算每部分的比例
sum = 0
for key in classDic:
sum += classDic[key]
for key in classDic:
classDic[key] /= sum
f = open('classify.txt', 'w')
for key in classDic:
ws = '{} {}\n'.format(key, classDic[key])
f.write(ws)
f.close()
# # “其他”类研究
# “其他”占各视频比例的分布
def other(fileList, fout='other.txt'):
# 关键词
# 幽默
humourList = ['233', '哈哈', 'h', '笑', '噗', '真香']
# 操作赞叹
act6List = ['66', '不错', '赞', '投币', '硬币', '收藏', '!', '!', 'yeah', '强', '帅', '人才', '牛', '厉害', '稳']
# 内容赞叹
text6List = ['福利', '良心', '打call', '喜欢', '可爱', '萌', '好看', '棒', 'dalao', '大佬', '燃', '美', '火', '哇',
'漂亮', '啊啊啊', 'cool', '酷']
# 关联性
seriesList = ['终于', 'leile', '来了', 'lei了', '期待', '失踪人口', '好久不见', '系列', '更了', '高产', '失踪人口回归',
'来晚了', '生日快乐']
# 出乎意料
surpriseList = ['意外', '惊', '没想到', '卧槽', '妈耶', 'woc', '巧了', '心脏', '吓', '高能', '社会', '妙']
# 感谢
thanksList = ['感谢', '辛苦', '心疼', '泪', '谢', '爱', '支持']
# 主动性
positiveList = ['干', '肝', '?', '?', '了解一下', 'emm', 'BGM', 'bgm', '额', '想', '感觉', '嘤']
# 负面情绪
negativeList = ['夭寿', '难受', '骂', '完了', '…', '别', '麻烦', '。。', 'gg', 'GG', '恶', '活该', '死']
# B站特色
biliList = ['♂', 'up', 'Up', 'UP', '基佬', '空降', '字幕', '计数', '1.25', '1.5', '0.5', '开头', '火钳刘明', '每日', '哲学',
'Van', '自由', '兄贵', '那个男人', '欢迎回来', 'gay', '弹幕', '鬼畜']
fileSum = len(fileList)
fileCount = 0
otherList = []
for name in fileList:
# 分类标准
classDic = {
"高兴": 0,
"操作赞叹": 0,
"内容赞叹": 0,
"关联性": 0,
"出乎意料": 0,
"感谢": 0,
"其他": 0,
"主动性": 0,
"负面情绪": 0,
"B站特色": 0,
}
fileCount += 1
path = root + '\\' + name
f = codecs.open(path, 'r', 'utf-8')
comSum = int(f.readlines()[3].split(':')[1][:-2])
comCount = 0
f.seek(0) # 文件读取后要复位
for line in f.readlines()[5:]:
try:
comment = line.split()[2]
except:
continue
hasSel = False
# 判断
for n in humourList:
if n in comment:
classDic["高兴"] += 1
comCount += 1
hasSel = True
if not hasSel:
for n in act6List:
if n in comment:
classDic["操作赞叹"] += 1
comCount += 1
hasSel = True
if not hasSel:
for n in text6List:
if n in comment:
classDic["内容赞叹"] += 1
comCount += 1
hasSel = True
if not hasSel:
for n in seriesList:
if n in comment:
classDic["关联性"] += 1
comCount += 1
hasSel = True
if not hasSel:
for n in surpriseList:
if n in comment:
classDic["出乎意料"] += 1
comCount += 1
hasSel = True
if not hasSel:
for n in thanksList:
if n in comment:
classDic["感谢"] += 1
comCount += 1
hasSel = True
if not hasSel:
for n in positiveList:
if n in comment:
classDic["主动性"] += 1
comCount += 1
hasSel = True
if not hasSel:
for n in negativeList:
if n in comment:
classDic["负面情绪"] += 1
comCount += 1
hasSel = True
if not hasSel:
for n in biliList:
if n in comment:
classDic["B站特色"] += 1
comCount += 1
classDic["其他"] += comSum-comCount
f.close()
classDic["其他"] /= comSum
otherList.append(classDic["其他"])
num = otherList
num.sort()
size = 0.01
plus = size / 2
f = open(fout, 'w')
num1 = 0
for n in num:
if n >= 0 and n < plus:
num1 += 1
ws = str(num1) + '\n'
f.write(ws)
front = size - plus
later = size + plus
while later <= 1 - plus:
num1 = 0
for n in num:
if n >= front and n < later:
num1 += 1
ws = str(num1) + '\n'
f.write(ws)
front += size
later += size
num1 = 0
for n in num:
if n >= 1 - plus and n <= 1:
num1 += 1
ws = str(num1) + '\n'
f.write(ws)
f.close()
# “其他”占各档视频比例的分布
def classOther():
c1, c2, c3, c4 = classByNum()
other(c1, 'other1.txt')
other(c2, 'other2.txt')
other(c3, 'other3.txt')
other(c4, 'other4.txt')