-
Notifications
You must be signed in to change notification settings - Fork 0
/
texts2columns.php
95 lines (74 loc) · 2.8 KB
/
texts2columns.php
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
<?php
//ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
$combined_arr = [];
$native_file = !empty($_FILES['native_file']) ? $_FILES['native_file'] : false;
$learning_file = !empty($_FILES['learning_file']) ? $_FILES['learning_file'] : false;
if (!$native_file OR !$learning_file) die("Waiting for files");
$native_file_tmp = $_FILES['native_file']['tmp_name'];
$learning_file_tmp = $_FILES['learning_file']['tmp_name'];
if (file_exists($native_file_tmp) && file_exists($learning_file_tmp)) {
$native_file_txt = file_get_contents($native_file_tmp);
$learning_file_txt = file_get_contents($learning_file_tmp);
$learning_file_txt = str_replace(array("\n", "\r"), ' ', $learning_file_txt);
$learning_file_txt = preg_replace('!\s+!', ' ', $learning_file_txt); // replace multiple spaces with single space
$native_file_txt = str_replace(array("\n", "\r"), ' ', $native_file_txt);
$native_file_txt = str_replace(array(".", ",", ";", "(", ")", '"'), '', $native_file_txt);
$native_file_txt = preg_replace('!\s+!', ' ', $native_file_txt);
$native_file_exp = explode(' ', $native_file_txt);
$learning_file_exp = explode(' ', $learning_file_txt);
$length_of_native_file_exp = count($native_file_exp);
$length_of_learning_file_exp = count($learning_file_exp);
if ($length_of_native_file_exp > $length_of_learning_file_exp) {
$learning_file_padded = array_pad($learning_file_exp, $length_of_native_file_exp, "");
for ($i=0; $i < $length_of_native_file_exp; $i++) {
$combined_arr[] = [$learning_file_padded[$i], $native_file_exp[$i]];
}
}
elseif ($length_of_learning_file_exp > $length_of_native_file_exp) {
$native_file_padded = array_pad($native_file_exp, $length_of_learning_file_exp, "");
for ($i=0; $i < $length_of_learning_file_exp; $i++) {
$combined_arr[] = [$learning_file_exp[$i], $native_file_padded[$i]];
}
}
else { // Equal length
for ($i=0; $i < $length_of_learning_file_exp; $i++) {
$combined_arr[] = [$learning_file_exp[$i], $native_file_exp[$i]];
}
}
//var_dump($combined_arr);
$result = '';
foreach($combined_arr as $word_arr) {
foreach ($word_arr as $word_key => $word) {
if ($word_key == 0) {
$result .= $word."\t";
//fwrite($fp, $word."\t");
continue;
}
$result .= $word.PHP_EOL;
//fwrite($fp, $word.PHP_EOL);
}
}
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=merged-translations.txt");
echo $result;
/*
$fp = fopen(__DIR__.'/result.txt',"w");
if ($fp) {
foreach($combined_arr as $word_arr) {
foreach ($word_arr as $word_key => $word) {
if ($word_key == 0) {
fwrite($fp, $word."\t");
continue;
}
fwrite($fp, $word.PHP_EOL);
}
}
fclose($fp);
echo "<p>Saved as result.txt</p>";
}
*/
}
else {
die('Something went wrong');
}
?>