-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathadd_ph_num.py
More file actions
69 lines (57 loc) · 1.91 KB
/
Copy pathadd_ph_num.py
File metadata and controls
69 lines (57 loc) · 1.91 KB
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
import click
import csv
def find_ph_num(i, phonemes_split, dict):
ph_tmp = []
left = i
right = i
for j in range(i, len(phonemes_split)):
ph_tmp.append(phonemes_split[j])
if ph_tmp in dict.values():
right = j
return left, right
@click.command()
@click.option('--csv_path',required = True, help='Path to CSV file')
@click.option('--dictionary',required = True, help='Path to dictionary file')
def add_ph_num(csv_path,dictionary):
ph_seq_index = 1
with open(csv_path, mode='r', newline='', encoding='utf-8') as csvfile:
phonemes_tmp = []
csv_reader = csv.reader(csvfile)
for row in csv_reader:
phonemes_tmp.append(row[ph_seq_index])
dict = {}
with open(dictionary, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
key = line.split('\t')[0]
values = (line.split('\t')[1]).split(' ')
dict.update({key: values})
ph_num = []
for phonemes in phonemes_tmp:
tmp = []
phonemes_split = phonemes.split(' ')
i=0
while i < len(phonemes_split):
if phonemes_split[i] == "AP" or phonemes_split[i] == "SP":
tmp.append("1")
i+=1
else:
left,right = find_ph_num(i,phonemes_split,dict)
tmp.append(str(right-left+1))
i = right+1
ph_num.append(tmp)
ph_num_str = []
for i in ph_num:
string = ' '.join(i)
ph_num_str.append(string)
ph_num_str[0] = "ph_num"
with open(csv_path, 'r', newline='') as csvfile:
reader = csv.reader(csvfile)
rows = list(reader)
for i, value in enumerate(ph_num_str):
rows[i].append(value)
with open(csv_path, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(rows)
if __name__ == '__main__':
add_ph_num()