forked from donnekgit/autoglosser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff_my_files.php
140 lines (115 loc) · 5.49 KB
/
diff_my_files.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/*
*********************************************************************
Copyright Kevin Donnelly 2010, 2011.
kevindonnelly.org.uk
This file is part of the Bangor Autoglosser.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License and the GNU
Affero General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
and the GNU Affero General Public License along with this program.
If not, see <http://www.gnu.org/licenses/>.
*********************************************************************
*/
// This script diffs two files, then selects the changed lines and presents them in the terminal and in a LaTeX document, with the changes highlighted in colour.
if (empty($filename))
{
include("includes/fns.php");
include("/opt/autoglosser/config.php");
list($chafile, $filename, $utterances, $words, $cgfinished)=get_filename();
}
$capfile=ucfirst($filename);
include("includes/finediff.php");
// We need some mechanism (in apply_traced_cg.php?) to save the current autoglossed.txt as autoglossed_old.txt.
//$old_file="outputs/$filename/".$filename."_autoglossed.old";
$old_file="inputs/patagonia/autoglossed/".$filename.".cha";
$new_file="outputs/$filename/".$filename."_autoglossed.txt";
$fp=fopen("outputs/$filename/".$filename."_diff.tex", "w") or die("Can't create the file");
//$header=file_get_contents('cognates/tex_header.tex');
//fwrite($fp, "$header");
$lines=file("cognates/tex_header.tex"); // Open header file containing LaTeX markup to set up the document.
foreach ($lines as $line)
{
if (preg_match("/(filename|infotext)/", $line)) // replace the holder in the TeX file with the name of the conversation
{
$line=preg_replace("/filename/", "$capfile diffs from previous version", $line);
$line=preg_replace("/infotext/", "Removed text is in RED, added text is in GREEN\n", $line);
}
else
{
$line=$line;
}
//echo $line."\n";
fwrite($fp, $line);
}
exec("diff $old_file $new_file", $diff_output); // Diff the two documents.
foreach ($diff_output as $diff_line) // Store all diff info in the $thisline array.
{
if (preg_match("/^[0-9]+/", $diff_line, $lines)) // Collect the line number.
{
$lineno=$lines[0];
$thisline[$lineno][]=$lines[0];
unset($lines);
}
if (preg_match("/^</", $diff_line)) // Collect the old text where a change was made.
{
//$old=$diff_line;
$thisline[$lineno][]=$diff_line;
unset($diff_line);
}
if (preg_match("/^>/", $diff_line)) // Collect the new text reflecting the change.
{
//$new=$diff_line;
$thisline[$lineno][]=$diff_line;
unset($diff_line);
}
}
foreach ($thisline as $thisdiff) // Loop through the $thisline array, picking out the lines highlighted by diff and feeding them to FineDiff.
{
echo "\033[1m".$thisdiff[0]."\033[0m\n"; // Write the line number in the old text.
fwrite($fp, $thisdiff[0].": ");
$prevline=$thisdiff[0]-1;
$surface=exec("sed -n ".$prevline."p $old_file"); // Get the utterance line before the autogloss line.
list($surface, $timing)=explode('', $surface); // Remove the timing info.
$surface=substr($surface, 0, -1);
echo $surface."\n"; // Write the utterance line.
fwrite($fp, "\\textbf{".tex_surface($surface)."} \\\\ \n");
$old_text=substr($thisdiff[1], 3); // Remove the "< " from the old text.
$new_text=substr($thisdiff[2], 3); // Remove the "> " from the new text.
$diff = new FineDiff($old_text, $new_text, FineDiff::$wordGranularity);
// "word" can be replaced by character, sentence and paragraph to give different levels of granularity.
// Render to HTML:
$rendered_diff = $diff->renderDiffToHTML();
//echo $rendered_diff."\n";
// Edit the HTML to view in a Bash shell:
$coloured_diff=$rendered_diff;
$coloured_diff=preg_replace("/<del>/", "\033[1;37;41m", $coloured_diff); // Set white text, red background.
$coloured_diff=preg_replace("/<\\/del>/", "\033[0m", $coloured_diff); // Reset to default.
$coloured_diff=preg_replace("/<ins>/", "\033[30;42m", $coloured_diff); // Set black text, green background.
$coloured_diff=preg_replace("/<\\/ins>/", "\033[0m", $coloured_diff); // Reset to default.
$coloured_diff=preg_replace("/ /", " / ", $coloured_diff); // Insert a slash between lexeme+POS groups.
//echo $coloured_diff."\n\n";
// Edit the HTML to view in LaTeX:
$tex_diff=$rendered_diff;
$tex_diff=preg_replace("/<del>/", "\\colorbox{redbg}{\color{Red}", $tex_diff); // Set red text, red background.
$tex_diff=preg_replace("/<\\/del>/", "}", $tex_diff); // Reset to default.
$tex_diff=preg_replace("/<ins>/", "\\colorbox{greenbg}{\color{Green}", $tex_diff); // Set green text, green background.
$tex_diff=preg_replace("/<\\/ins>/", "}", $tex_diff); // Reset to default.
//$tex_diff=preg_replace("/ /", " / ", $tex_diff); // Insert a slash between lexeme+POS groups.
$tex_diff=preg_replace("/ /", " ", $tex_diff);
$tex_diff=tex_surface($tex_diff);
fwrite($fp, $tex_diff." \\bigskip \\\\ \n\n");
}
$footer=file_get_contents('cognates/tex_footer.tex');
fwrite($fp, "$footer");
fclose($fp);
// Use pdflatex to generate a pdf file.
exec("pdflatex -interaction=nonstopmode -output-directory=outputs/$filename outputs/$filename/".$filename."_diff.tex 2>&1");
?>