-
Notifications
You must be signed in to change notification settings - Fork 0
/
tachkil.py
88 lines (63 loc) · 2.43 KB
/
tachkil.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from consts.static import tachkil_vocab
from helper.helper_functions import get_sentence_tachkil, tachkil_sentence2tokens, rebuild_sentence
from models.tachkil.model import TachkilLstmModel
from helper.prosody_postprocessing import prosody_form, normalize
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
device
special = ['َ', 'ُ', 'ِ', 'ً', 'ٌ', 'ٍ', 'َّ',
'ُّ', 'ِّ', 'ًّ', 'ٌّ', 'ٍّ', 'ْ', 'ّ']
sps_dict = {}
for i, c in enumerate(special):
sps_dict[c] = i
VOCAB_SIZE = len(tachkil_vocab)+1 # 0 is for the padding
OUTPUT_SIZE = len(special)+1 # +1 for the non tachkil
model_tachkil = TachkilLstmModel(
256,
VOCAB_SIZE,
OUTPUT_SIZE).to(device)
model_tachkil.load_state_dict(torch.load(
'models/tachkil/tachkil_model.pt', map_location=device))
poem = """كَأَنَّما البَصرَةُ مُذ جُزتَها
أَشلاءُ غِمدٍ سُلَّ مِنهُ الحُسام
بُدِّلَتِ الوَحشَة مِن أُنسِها
وَأُلبِسَت بَعدَ الضِياءِ الظَلام
وَكُلُّ أَرضٍ فَقَدَت شَمسَها
غَشّى الدُجى ساحاتِها وَالخِيام
وَالآنَ إِذ عُدتَ إِلَيها زَهَت
وَاِبتَسَمَت بِالبِشرِ أَيَّ اِبتِسام
فَلا خَلَت مِنكَ الرَعايا وَلا
زِلتَ لِمُرفَضِّ المَعالي نِظام""".strip().split('\n')
def get_tachkil(line):
"""
Predict the tachkil on a given text
Input:
line (str): the verse to tachkil
Output:
predicted (str): the new verse with tachkil
aroudi (str): the aroudi form of the verse
"""
S, T, _ = get_sentence_tachkil(line)
x = tachkil_sentence2tokens(S)
x = torch.tensor(x, dtype=torch.int64)
L = len(x)
x = nn.ConstantPad1d((0, 50 - x.shape[0]), 0)(x)
x = x.unsqueeze(0).to(device)
out = model_tachkil(x)
out = torch.argmax(out, dim=-1)[0, :L].cpu()
out = list(np.array(out))
out = rebuild_sentence(S, out, T)
norm_out = normalize(out)
prosody_out = prosody_form(norm_out)
return {
'predicted': norm_out,
'aroudi' : prosody_out
}
# for line in poem:
# out = get_tachkil(line)
# print('predicted', out['predicted'])
# print('aroudi' , out['aroudi'])
# print()