sleepygorgoyle
commited on
Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,141 @@
|
|
1 |
---
|
2 |
license: mit
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
license: mit
|
3 |
---
|
4 |
+
The model uses only sign `ӏ` for explosive consonants (small cyrillic palochka letter)!
|
5 |
+
|
6 |
+
```python
|
7 |
+
import torch
|
8 |
+
from transformers import BertTokenizer, AutoModel
|
9 |
+
import numpy as np
|
10 |
+
import pandas as pd
|
11 |
+
import razdel
|
12 |
+
import matplotlib.pyplot as plt
|
13 |
+
from tqdm.auto import tqdm, trange
|
14 |
+
```
|
15 |
+
Download the model from Huggingface repository:
|
16 |
+
```python
|
17 |
+
model_name = 'NM-development/labse-en-ru-ce-prototype'
|
18 |
+
tokenizer = BertTokenizer.from_pretrained(model_name)
|
19 |
+
model = AutoModel.from_pretrained(model_name)
|
20 |
+
```
|
21 |
+
Assign files with the texts you want to split into parallel sentences:
|
22 |
+
```python
|
23 |
+
file_ru = None
|
24 |
+
file_nm = None
|
25 |
+
|
26 |
+
if file_ru is None or file_nm is None:
|
27 |
+
nm_text = 'Ламро. Сахьт. Къена. Адам. Зуда. Вокха. Тӏулг.'
|
28 |
+
ru_text = 'Горец. Час. Старый. Человек. Жена. Высокий. Камень.'
|
29 |
+
else:
|
30 |
+
with open(file_nm, 'r') as f1, open(file_ru, 'r') as f2:
|
31 |
+
nm_text = f1.read()
|
32 |
+
ru_text = f2.read()
|
33 |
+
```
|
34 |
+
In the following section define auxillary functions for parallel sentence comparison:
|
35 |
+
```python
|
36 |
+
def embed(text):
|
37 |
+
encoded_input = tokenizer(text, padding=True, truncation=True, max_length=128, return_tensors='pt')
|
38 |
+
with torch.inference_mode():
|
39 |
+
model_output = model(**encoded_input.to(model.device))
|
40 |
+
embeddings = model_output.pooler_output
|
41 |
+
embeddings = torch.nn.functional.normalize(embeddings)
|
42 |
+
return embeddings[0].cpu().numpy()
|
43 |
+
|
44 |
+
def center_norm(v):
|
45 |
+
v = v - v.mean(0)
|
46 |
+
return v / (v**2).sum(1, keepdims=True) ** 0.5
|
47 |
+
|
48 |
+
|
49 |
+
def center_dot(x, y):
|
50 |
+
m = (x.sum(0) + y.sum(0)) / (x.shape[0] + y.shape[0])
|
51 |
+
x = x - m
|
52 |
+
y = y - m
|
53 |
+
x = x / (x**2).sum(1, keepdims=True) ** 0.5
|
54 |
+
y = y / (y**2).sum(1, keepdims=True) ** 0.5
|
55 |
+
return np.dot(x, y.T)
|
56 |
+
|
57 |
+
def get_top_mean_by_row(x, k=5):
|
58 |
+
m, n = x.shape
|
59 |
+
k = min(k, n)
|
60 |
+
topk_indices = np.argpartition(x, -k, axis=1)[:, -k:]
|
61 |
+
rows, _ = np.indices((m, k))
|
62 |
+
return x[rows, topk_indices].mean(1)
|
63 |
+
|
64 |
+
def align3(sims):
|
65 |
+
|
66 |
+
#sims = np.dot(center_norm(orig_vecs), center_norm(sum_vecs).T) ** 3
|
67 |
+
#sims = center_dot(orig_embeds, sum_embeds) #** 3
|
68 |
+
|
69 |
+
rewards = np.zeros_like(sims)
|
70 |
+
choices = np.zeros_like(sims).astype(int) # 1: choose this pair, 2: decrease i, 3: decrease j
|
71 |
+
|
72 |
+
# алгоритм, разрешающий пропускать сколько угодно пар, лишь бы была монотонность
|
73 |
+
for i in range(sims.shape[0]):
|
74 |
+
for j in range(0, sims.shape[1]):
|
75 |
+
# вариант первый: выровнять i-тое предложение с j-тым
|
76 |
+
score_add = sims[i, j]
|
77 |
+
if i > 0 and j > 0: # вот как тогда выровняются предыдущие
|
78 |
+
score_add += rewards[i-1, j-1]
|
79 |
+
choices[i, j] = 1
|
80 |
+
best = score_add
|
81 |
+
if i > 0 and rewards[i-1, j] > best:
|
82 |
+
best = rewards[i-1, j]
|
83 |
+
choices[i, j] = 2
|
84 |
+
if j > 0 and rewards[i, j-1] > best:
|
85 |
+
best = rewards[i, j-1]
|
86 |
+
choices[i, j] = 3
|
87 |
+
rewards[i, j] = best
|
88 |
+
alignment = []
|
89 |
+
i = sims.shape[0] - 1
|
90 |
+
j = sims.shape[1] - 1
|
91 |
+
while i > 0 and j > 0:
|
92 |
+
if choices[i, j] == 1:
|
93 |
+
alignment.append([i, j])
|
94 |
+
i -= 1
|
95 |
+
j -= 1
|
96 |
+
elif choices[i, j] == 2:
|
97 |
+
i -= 1
|
98 |
+
else:
|
99 |
+
j -= 1
|
100 |
+
return alignment[::-1]
|
101 |
+
|
102 |
+
def make_sents(text):
|
103 |
+
sents = [s.text.replace('\n', ' ').strip() for p in text.split('\n\n') for s in razdel.sentenize(p)]
|
104 |
+
sents = [s for s in sents if s]
|
105 |
+
return sents
|
106 |
+
```
|
107 |
+
Firstly split your texts into sentences:
|
108 |
+
```python
|
109 |
+
sents_nm = make_sents(nm_text)
|
110 |
+
sents_ru = make_sents(ru_text)
|
111 |
+
```
|
112 |
+
Then embed all the chunks:
|
113 |
+
```python
|
114 |
+
emb_ru = np.stack([embed(s) for s in tqdm(sents_ru)])
|
115 |
+
emb_nm = np.stack([embed(s) for s in tqdm(sents_nm)])
|
116 |
+
```
|
117 |
+
Now compare sentenses' semanics vectors and build correlation heatmap:
|
118 |
+
```python
|
119 |
+
pen = np.array([[min(len(x), len(y)) / max(len(x), len(y)) for x in sents_nm] for y in sents_ru])
|
120 |
+
sims = np.maximum(0, np.dot(emb_ru, emb_nm.T)) ** 1 * pen
|
121 |
+
|
122 |
+
alpha = 0.2
|
123 |
+
penalty = 0.2
|
124 |
+
sims_rel = (sims.T - get_top_mean_by_row(sims) * alpha).T - get_top_mean_by_row(sims.T) * alpha - penalty
|
125 |
+
|
126 |
+
alignment = align3(sims_rel)
|
127 |
+
|
128 |
+
print(sum(sims[i, j] for i, j in alignment) / min(sims.shape))
|
129 |
+
plt.figure(figsize=(12, 6))
|
130 |
+
plt.subplot(1, 2, 1)
|
131 |
+
plt.imshow(sims_rel)
|
132 |
+
plt.subplot(1, 2, 2)
|
133 |
+
plt.scatter(*list(zip(*alignment)), s=5);
|
134 |
+
```
|
135 |
+
Finally, save the parallel corpus into a json file:
|
136 |
+
```python
|
137 |
+
nm_ru_parallel_corpus = pd.DataFrame({'nm_text' : [sents_nm[x[1]] for x in alignment], 'ru_text' : [sents_ru[x[0]] for x in alignment]})
|
138 |
+
corpus_filename = 'nm_ru_corpus.json'
|
139 |
+
with open(corpus_filename, 'w') as f:
|
140 |
+
nm_ru_parallel_corpus.to_json(f, force_ascii=False, indent=4)
|
141 |
+
```
|