/
LyricsForm.py
679 lines (448 loc) · 15.5 KB
/
LyricsForm.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
# -*- coding: utf-8 -*-
import copy
import time
import math
import numpy
import networkx as nx
from Visualization import Visualization
from SSMOperation import SSMOperation
from ChildrenFinder import ChildrenFinder
from FormFinder import FormFinder
class LyricsForm:
def __init__(self):
self.__viz = Visualization()
self.__ssm = SSMOperation()
self.__LENRATE = 6 / 7.0
self.__TOPK = 5
def formAnalysis(self, SSM, isViz = False):
"""
視覺化Matrix
"""
if isViz:
self.__SSMViz(SSM)
"""
NMF test
from nmf import nmf
n = SSM.shape[0]
r = 5
w = numpy.random.random([n, r])
h = numpy.random.random([r, n])
(wo,ho) = nmf(SSM, w, h, 0.001, 1000, 100)
if isViz:
self.__SSMViz(numpy.dot(wo, ho))
for k in range(r):
print k
wmask = numpy.zeros((n, r))
wmask[:, k] = numpy.ones(n)
hmask = numpy.zeros((r, n))
hmask[k, :] = numpy.ones(n)
self.__SSMViz(numpy.dot(wo * wmask, hmask * ho))
"""
"""
計算 SSM 中所有的 Block Family
"""
startTime = time.time()
self.__allFamilyM = self.__allBlockFamily(SSM)
endTime = time.time()
print "LyricsForm: Family Matrix Construction Time = %.2fsec" % (endTime - startTime)
self.__allFamilyM = numpy.insert(self.__allFamilyM, 0, 0, axis = 0)
"""
Block Family Combination
"""
startTime = time.time()
ff = FormFinder(self.__allFamilyM, self.__TOPK)
combineList = ff.computing()
endTime = time.time()
print "LyricsForm: Block Combination Time = %.2fsec" % (endTime - startTime)
print
"""
combineList 轉換成詞式格式
"""
lineNum = SSM.shape[0]
formList = self.__resultForm(combineList, lineNum)
return formList
def __resultForm(self, combineList, lineNum):
resultList = []
for combine in combineList:
lyricsLine = set(range(1, lineNum + 1))
form = []
familyList = []
cohesionList = []
for coor in combine["coors"]:
cohesion = self.__allFamilyM[coor[0]][coor[1]]["cohesion"]
family = self.__allFamilyM[coor[0]][coor[1]]["family"]
lineNumFamily = map(lambda block: [block[0] + 1, block[1] + 1], family)
familyList.append(lineNumFamily)
cohesionList.append(cohesion)
for block in lineNumFamily:
start = block[0]
end = block[1]
lyricsLine -= set(range(start, end + 1))
"""
判斷副歌
"""
maxCohesion = max(cohesionList)
tempList = []
for i in range(len(cohesionList)):
if cohesionList[i] == maxCohesion:
tempList.append((i, len(familyList[i])))
idx = numpy.argmax(map(lambda pair: pair[1], tempList))
chorusIdx = tempList[idx][0]
chorus = {"label": "chorus", "group": familyList[chorusIdx]}
form.append(chorus)
familyList.pop(chorusIdx)
"""
判斷主歌
"""
for i in range(len(familyList)):
verse = {"label": "verse" + str(i + 1), "group": familyList[i]}
form.append(verse)
"""
判斷前段、橋段與尾聲
"""
if len(lyricsLine) > 0:
lyricsLine = list(lyricsLine)
prevLineNum = lyricsLine[0] - 1
block = []
remainBlocks = []
for i in range(0, len(lyricsLine)):
if prevLineNum + 1 != lyricsLine[i]:
"""
一個 block 形成
"""
remainBlocks.append([ block[0], block[-1] ])
block = [lyricsLine[i]]
else:
block.append(lyricsLine[i])
prevLineNum = lyricsLine[i]
remainBlocks.append([ block[0], block[-1] ])
"""
加入前段
"""
if remainBlocks[0][0] == 1:
form.append({"label": "intro", "group": [remainBlocks[0]]})
"""
加入橋段
"""
if len(remainBlocks[1:-2]) > 0:
form.append({"label": "bridge", "group": remainBlocks[1:-2]})
"""
加入尾聲
"""
if remainBlocks[-1][1] == lineNum:
form.append({"label": "outro", "group": [remainBlocks[-1]]})
resultList.append({"score": combine["score"], "form": form})
return resultList
def __allBlockFamily(self, M):
M = copy.deepcopy(M)
"""
記錄所有 family 的資料結構 table
"""
familyM = []
for i in range(M.shape[0] / 2):
familyM.append([None] * M.shape[0])
"""
將 SSM 中相似度為 1.0 的值去除,記錄到 exOneArray 中
"""
exOneArray = numpy.extract(M != 1.0, M)
"""
計算 Children 與 Parent 之間的相似度(Similarity)門檻值
"""
simT = exOneArray.mean() + exOneArray.std()
#simT = exOneArray.mean()
"""
產生 ChildrenFinder 物件 cf,並且傳入這首歌詞的總行數
"""
cf = ChildrenFinder()
"""
計算所有的 Parent Block (start line & size) 的 Children
"""
for size in range(4, len(M) / 2 + 1):
for start in range(0, M.shape[0] - size):
"""
建立 SSM 的 Corridor(廊道) Matrix
"""
corridorMask = numpy.zeros(M.shape)
corridorMask[start: start + size] = 1
corridorM = M * corridorMask
"""
找出 start 到 start + size parent block 所框出的 children matrix 範圍
"""
childrenMatrix = M[start: start + size, start + size: M.shape[1] ]
"""
計算 Children 與 Parent 之間的長度(Length)門檻值
"""
lenT = math.ceil(float(size) * self.__LENRATE)
#lenT = float(size)
#if lenT > 7:
# lenT -= 1
"""
利用 Children Finding Algorithm 計算出此 Parent 最佳的 Repeating Pattern 所形成的 Children
"""
blockFamily = cf.children(childrenMatrix, lenT = lenT, simT = simT)
"""
有找到 children 才需要進一步考慮
"""
if blockFamily != []:
"""
Family Block Range 移動到絕對位置的 Range,Family 的 Block 都是從 第1行 開始算起
並且加入 Parent Block 本身到 Block Family 中
"""
for i in range(len(blockFamily)):
blockFamily[i] = [lineNum + (start + size) for lineNum in blockFamily[i]]
blockFamily.insert(0, [start, start + size - 1])
"""
計算此 family 所形成的 complete graph G(V, E) ,E 上的 weight 表示兩個 block 之間的相似度
以及 family 的 cohesion
"""
familyGraph, familyCohesion = self.__familyGraphBuild(blockFamily, M)
"""
family 的覆蓋長度總合
"""
familyCoverage = sum(map(lambda block: block[1] - block[0] + 1, blockFamily))
"""
將 familyBlock 記錄到 Block Matrix
"""
familyM[size - 1][start] = {"graph": familyGraph, "family": blockFamily,
"cohesion": familyCohesion, "coverage": familyCoverage}
"""
視覺化檢查工具
"""
#self.__viz.grayMatrix(corridorM, "Row Mask SSM: start= " + str(start) + " size= " + str(size))
#pathMask = cf.getPathMask()
#familyMask = cf.getFamilyMask()
#corridorM[start: start + size, start + size: M.shape[1] ] = pathMask
#self.__viz.grayMatrix(corridorM, "Path Mask")
#corridorM[start: start + size, start + size: M.shape[1] ] = familyMask
#self.__viz.grayMatrix(corridorM, "Family Mask")
return familyM
def __familyGraphBuild(self, family, M):
"""
建立 family 的 complete graph
"""
familyGraph = nx.complete_graph(len(family))
cohesion = 0.0
for i in range(len(family) - 1):
for j in range(i + 1, len(family)):
"""
選擇長度較短的 block length 為 i 軸
"""
shortIdx = i
longIdx = j
shortLen = family[i][1] - family[i][0] + 1
longLen = family[j][1] - family[j][0] + 1
if shortLen > longLen:
"""
交換
"""
shortIdx, longIdx = j, i
shortLen, longLen = longLen, shortLen
sim = 0.0
iSlice = slice( family[shortIdx][0], family[shortIdx][1] + 1 )
windowSize = shortLen
"""
計算兩個 block 的相似度所需跑的迴圈數
"""
loopNum = longLen - shortLen + 1
for offset in range(loopNum):
jSlice = slice( family[longIdx][0] + offset, family[longIdx][0] + windowSize + 1)
tempSim = M[iSlice, jSlice].trace() / windowSize
if tempSim > sim:
sim = tempSim
"""
將計算好的相似度(sim)放入 family graph 的 edge 上
"""
familyGraph[i][j]["sim"] = sim
cohesion += sim
cohesion = cohesion / (len(family) * (len(family) - 1) / 2.0)
#print "block family", family
#print "family graph", familyGraph.edge
#print "cohesion", cohesion
#raw_input()
return familyGraph, cohesion
def SSMGen(self, lines, simObject, matrixType='sim'):
"""
產生自比較矩陣 Self Matrix
"""
startTime = time.time()
"""
宣告句子相似度矩陣 type: numpy.array
"""
SSM = numpy.zeros([len(lines), len(lines)])
"""
Self Matrix 建立
"""
for i in range(len(lines)):
for j in range(i, len(lines)):
lineSim = simObject.similarity(lines[i][:], lines[j][:])
"""
對稱的矩陣
"""
SSM[i][j] = SSM[j][i] = lineSim
"""
計算 Matrix 中最大的數值是多少
"""
maxValue = SSM.max()
print "LyricsForm: Matrix Max Value : %f" % maxValue
"""
因為 DTW 演算法計算兩序列的距離,如果超過無限大,則會為回傳 -1
所以,如果 Matrix 中存在負的值,便將此值設為 Matrix 中的最大數值的
"""
for rowIdx in range(SSM.shape[0]):
SSM[rowIdx] = map(lambda ele: ele < 0.0 and maxValue or ele, SSM[rowIdx])
"""
如果 Similarity Object 是計算距離的話,就將 Matrix 中的數值從距離轉換成相似度,值越大越像
"""
#print simObject.__class__.__name__
#if 'Dist' in simObject.__class__.__name__:
if 'dist' in matrixType:
#由 Matrix 中最大的距離來當作最低的相似度
"""
minDist = M.min()
maxDist = M.max()
M = 1 - ((M - minDist) / (maxDist - minDist))
"""
maxM = numpy.ones(SSM.shape) * maxValue
SSM = maxM - SSM
SSM = self.__ssm.localNormalize(SSM)
endTime = time.time()
print "LyricsForm: SSM Construction Time = %.2fsec" % (endTime - startTime)
print "LyricsForm: Matrix Shape = %s" % str(SSM.shape)
#print "LyricsForm: SSM Visualization..."
#self.__SSMViz(SSM)
return SSM
def __SSMViz(self, SSM):
"""
建立好的 Self Matrix 裡頭的每個 Element
有可能是 Distance 也有可能是 Similarity
目前是將 Distance 都轉換成 Similarity
最後會得到一個 SSM
"""
"""
將 SSM 做 Local Normalize,也就是除以 SSM 中的最大值,讓 SSM 中的值介在 [0, 1]
此步驟只對 Distance Matrix 以及 沒有 Normalize 的 Similarity Matrix 有效果
"""
#self.__viz.grayMatrix(SSM, "Local Normalized SSM: " + self.__simObjClassName)
self.__viz.grayMatrix(SSM, "Local Normalized SSM: ")
#SSM = self.__ssm.secondOrder(SSM)
#self.__viz.grayMatrix(SSM, "Second Order SSM: " + self.__simObjClassName)
"""
擷取 Exact Path 出現的位置
"""
#exactMask = self.__ssm.masker(SSM, 1.0)
#self.__viz.grayMatrix(exactMask, "Exact Mask: " + self.__simObjClassName)
"""
SSM Enhancement
enhance 函數的最後一個值是設定 L, L = 4
"""
#enM = self.__ssm.enhance(SSM, 2)
#self.__viz.grayMatrix(enM, "Enhanced SSM: " + self.__simObjClassName)
"""
Higher Order Matrix
"""
#SSM = self.__ssmSecondOrder(SSM)
#SSM = self.__ssmNormalization(SSM)
#self.__viz.grayMatrix(SSM, "Second Order SSM: " + self.__simObjClassName)
"""
擷取 Enhanced Matrix 中的 Approximate Path 出現的位置
門檻值 = 平均值 + 一個標準差
"""
#threshold = enM.mean() + enM.std()
#approxMask = self.__ssm.masker(enM, threshold)
#self.__viz.grayMatrix(approxMask, "Approximate Mask: " + self.__simObjClassName)
"""
將 exact mask 與 approximate mask 做聯集
"""
#mask = map(numpy.bitwise_or, approxMask, exactMask)
#self.__viz.grayMatrix(mask, "Total Mask: " + self.__simObjClassName)
#self.__viz.grayMatrix(SSM * mask, "Original SSM Mask: " + self.__simObjClassName)
#self.__viz.grayMatrix(enM * mask, "Enhanced SSM Mask: " + self.__simObjClassName)
def __matrix2ssm(self, M):
M = copy.deepcopy(M)
"""
計算 Matrix 中最大的數值是多少
"""
maxValue = M.max()
print "LyricsForm: Matrix Max Value : %f" % maxValue
"""
因為 DTW 演算法計算兩序列的距離,如果超過無限大,則會為回傳 -1
所以,如果 Matrix 中存在負的值,便將此值設為 Matrix 中的最大數值的
"""
for rowIdx in range(M.shape[0]):
M[rowIdx] = map(lambda ele: ele < 0.0 and maxValue or ele, M[rowIdx])
"""
如果 Similarity Object 是計算距離的話,就將 Matrix 中的數值從距離轉換成相似度,值越大越像
"""
if 'Dist' in self.__simObjClassName:
#由 Matrix 中最大的距離來當作最低的相似度
"""
minDist = M.min()
maxDist = M.max()
M = 1 - ((M - minDist) / (maxDist - minDist))
"""
mm = numpy.ones(M.shape) * maxValue
M = mm - M
return M
if __name__ == '__main__':
import sys
import codecs
import os
from LyricsInput import FromFile
reload(sys)
sys.setdefaultencoding('utf-8')
def fileSelect(dirPath, fileProcess):
#lines = FromFile(fileProcess).process(dirPath + '新不了情.txt')
#lines = FromFile(fileProcess).process(dirPath + '朋友.txt')
#lines = FromFile(fileProcess).process(dirPath + '擁抱.txt')
#lines = FromFile(fileProcess).process(dirPath + '朋友未分段.txt')
#lines = FromFile(fileProcess).process(dirPath + '擁抱未分段.txt')
#lines = FromFile(fileProcess).process(dirPath + '新不了情_未分段.txt')
lines = FromFile(fileProcess).process(dirPath + 'hand.txt')
return lines
def pinyinFormAnalysis():
"""
利用注音的方法
"""
print
print "Main: Pinyin Form Analysis"
from AlgoStructAlign import AlgoStructAlign
from SimPinyin import SimPinyin
from InputProcess import Pinyin2Tuple
dirPath = 'LyricsFormTest/pinyin/old_famous/'
fileNameList = os.listdir(dirPath)
fileProcess = Pinyin2Tuple()
for fileName in fileNameList:
print "Main: %s" % fileName
lines = FromFile(fileProcess).process(dirPath + (fileName.decode('big5')))
#lines = fileSelect(dirPath, fileProcess)
simObject = SimPinyin()
simObject = AlgoStructAlign(simObject)
LyricsForm(simObject).formAnalysis(lines)
def toneFormAnalysis():
"""
利用聲調的方法
"""
print
print "Main: Pitch Tone Form Analysis"
from AlgoClassicalDTW import AlgoClassicalDTW
from AlgoDDTW import AlgoDDTW
from DistNote import DistNote
from InputProcess import Str2Int
from InputProcess import Tone2Pitch
from LocalConstraint import StepType2
from LocalConstraint import StepType3
from LocalConstraint import StepType1
dirPath = 'LyricsFormTest/tone/old_famous/'
fileNameList = os.listdir(dirPath)
fileProcess = Tone2Pitch()
fileProcess = Str2Int(fileProcess)
for fileName in fileNameList:
print "Main: %s" % fileName
#lines = fileSelect(dirPath, fileProcess)
lines = FromFile(fileProcess).process(dirPath + (fileName.decode('big5')))
simObject = DistNote()
simObject = AlgoClassicalDTW(simObject, stepType = StepType2())
simObject = AlgoDDTW(simObject)
LyricsForm(simObject).formAnalysis(lines)
#pinyinFormAnalysis()
toneFormAnalysis()