-
Notifications
You must be signed in to change notification settings - Fork 2
/
sentences_to_paragraphs.py
125 lines (110 loc) · 3.15 KB
/
sentences_to_paragraphs.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
import pandas as pd
import csv
import torch
from tqdm import tqdm
df_dgt = pd.read_csv(
"parallel-sentences/DGT-TM-en-sv.tsv",
sep="\t",
header=None,
names=["en", "sv"],
quoting=csv.QUOTE_NONE,
engine="python",
)
df_parl = pd.read_csv(
"parallel-sentences/Europarl-en-sv.tsv.gz",
sep="\t",
header=None,
compression="gzip",
names=["en", "sv"],
quoting=csv.QUOTE_NONE,
engine="python",
)
df_emea = pd.read_csv(
"parallel-sentences/EMEA-en-sv.tsv.gz",
sep="\t",
header=None,
compression="gzip",
names=["en", "sv"],
quoting=csv.QUOTE_NONE,
engine="python",
)
df_elitr = pd.read_csv(
"parallel-sentences/ELITR-ECA-en-sv.tsv.gz",
sep="\t",
header=None,
compression="gzip",
names=["en", "sv"],
quoting=csv.QUOTE_NONE,
engine="python",
)
df_jw = pd.read_csv(
"parallel-sentences/JW300-en-sv.tsv.gz",
sep="\t",
header=None,
compression="gzip",
names=["en", "sv"],
quoting=csv.QUOTE_NONE,
engine="python",
on_bad_lines="skip",
)
df_ted = pd.read_csv(
"parallel-sentences/TED2020-en-sv-train.tsv.gz",
sep="\t",
header=None,
compression="gzip",
names=["en", "sv"],
quoting=csv.QUOTE_NONE,
engine="python",
on_bad_lines="skip",
)
df = pd.concat(
[df_dgt, df_parl, df_emea, df_elitr, df_jw, df_ted],
keys=["dgt", "parl", "emea", "elitr", "jw", "ted"],
).reset_index(level=0)
df = df.rename(columns={"level_0": "dataset"})
df[["en", "sv"]] = df[["en", "sv"]].astype(str)
df["nr_words"] = df["en"].str.split().str.len()
sv_paragraphs = []
en_paragraphs = []
dataset_paragraphs = []
for group, df_group in df.groupby("dataset"):
cum_words = 0
max_words = torch.nn.init.trunc_normal_(torch.Tensor([0]), mean=270, std=110, a=60, b=330)
max_words = int(max_words.round())
sv_paragraph = ""
en_paragraph = ""
for i, row in tqdm(df_group.iterrows(), total=len(df_group)):
cum_words += row["nr_words"]
if row["nr_words"] > max_words:
sv_paragraphs.append(row["sv"])
en_paragraphs.append(row["en"])
dataset_paragraphs.append(group)
sv_paragraph = ""
en_paragraph = ""
cum_words = 0
elif cum_words > max_words:
sv_paragraphs.append(sv_paragraph)
en_paragraphs.append(en_paragraph)
dataset_paragraphs.append(group)
sv_paragraph = ""
en_paragraph = ""
cum_words = 0
else:
sv_paragraph += " " + row["sv"]
en_paragraph += " " + row["en"]
if i % 50000 == 0:
print(
f"Nr sv paragraphs: {len(sv_paragraphs)}, nr en paragraphs: {len(en_paragraphs)}"
)
df_long = pd.DataFrame({"en": en_paragraphs, "sv": sv_paragraphs, "dataset": dataset_paragraphs})
# NKFD normalization
df_long["en"] = df_long["en"].str.normalize("NFKD")
df_long["sv"] = df_long["sv"].str.normalize("NFKD")
# df_long.to_parquet("parallel-sentences/paragraphs-en-sv.parquet")
df_long[["en", "sv"]].to_csv(
"parallel-sentences/paragraphs-en-sv.tsv.gz",
sep="\t",
header=None,
compression="gzip",
index=False,
)