summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/rst/urml2latex.perl
blob: f7f3170a90d9693b328c00a07729bdfe475fab2c (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/perl

# create diagrams with the rst package in LaTeX from URML
# args: urml2latex  -i xmlfile docid analysis-id
 
 
 # change this for relname replacements
 sub replace_relname($)
 {
 	my $rel = $_[0];
 	
 	$rel =~ s/evaluation/eval/i;
 	$rel =~ s/elaboration/elab/i;
 	$rel =~ s/object/obj/i;
 	$rel =~ s/attribute/attr/i;
 	$rel =~ s/additional/addl/i;
 	return $rel;
 }
 
@args = ();
$putSegmentsInTree=0;
foreach(@ARGV)
{
	if (/\-i/i)
	{
		$putSegmentsInTree=1;
	} else
	{
		push @args, $_;
	}
}

$fn =  $args[0];

unless($fn)
{
	print "Converts a fully specified URML analysis to LaTeX rst format.\nUsage: urml2latex [-i]  xmlfile [docid [analysis-id]]\n";
	exit;
}
warn "loading $fn\n";

{
	local(*INPUT, $/);
	open (INPUT, $fn) 	|| die "can't open $fn: $!";
	$f = <INPUT>;
	close INPUT;
}  

$docid = $args[1];
$anaid = $args[2];

if ($docid eq '')
{

print "printing all document ids: \n";
 
while ($f =~ /\<document\s+id\s*=\s*(\"?)([^\">]+)(\"?)\s*>/isg)
{
	print $2."\n";
}

} else
{
	 
	unless ($f =~ /<document\s+id\s*=\s*(\"?)$docid(\"?)\s*>(.*?)<\s*\/\s*document\s*>/is)
	{
		die "Document $docid not found.";
		
	}
	
	
	$doc = $3;
	
	
	
	$docid =~ s/_//g;
	
	# more than one analysis?
	my $mdoc;
	
	%ana = ();
	while ($doc =~/<analysis\s+id\s*=\s*(\"?)(.*?)(\"?)\s*>(.*?)<\/\s*analysis\s*>/isg)
	{
		$ana{$2} = $4;
		
	}
	if (scalar keys %ana >1)
	{ 
		if ($anaid && exists($ana{$anaid}))
		{
			$mdoc = $ana{$anaid};
		} else
		{
			print "Please specify the analysis of this document as argument. Printing all available analyses:\n";
			
			foreach (keys %ana)
			{
				print $_."\n";
			}
			exit;
			
		}
	
	} else
	{
		$mdoc = $doc;
	} 
	print $mdoc;
	print "% rhetorical diagram\n";
	
	# store all relations in a hash
	
	 
	while ($mdoc =~ /<\s*(relation|hypRelation|parRelation|segment)\s*(.*?)>(.*?)<\s*\/\1\s*>/isg)
	{
		my $relclass = $1;
		my $relattr = $2;
		my $relcontent = $3;
	

		if ($relattr =~ /id\s*=\s*\"(.+?)\"/)
		{
			my $relid = $1;
			
				# check relation
			if ($relclass eq 'hypRelation' && !($relcontent =~ /<\s*nucleus/is))
			{
				warn "Node $relid is a $relclass, but has no nucleus -- ignoring that node!";
			} else
			{
			
				$relid =~ s/_//g;
				
				$relsxml{$relid} = $relcontent;
				$relclass{$relid} = $relclass;
				  
				if ($relattr =~ /type\s*\=\s*\"(.*?)\"/is)
				{
					$reltype{$relid} = &replace_relname($1);
				}
				if ($relattr =~ /group\s*=\s*\"(.*?)\"/is)
				{
					$relgroup{$relid} = $1;
				}
			
			
			}
			
			
		}	
	}
	
	# bottom-up approach, start with segment

	# assign labels
	foreach(keys %relclass)
	{
		if ($relclass{$_} eq 'segment')
		{
			if ($putSegmentsInTree)
			{
				$rellatex{$_} = '\rstsegment{'.&conv_text($relsxml{$_}).'}';
			} else
			{
				$rellatex{$_} = "\\rstsegment{\\refr{$_}}";
			}
			$complete{$_} = 1;
		} else
		{
			$rellatex{$_} = $relsxml{$_};
			$complete{$_} = 0;
		}
		
	}
	
	# there is no top node, so we need to expand nodes sucessively. 
	 
	 while(1)
	 {
	 	my $nodescompleted=0;  # success count
	 	my $uncompletenodes=0;  # number of non-complete nodes in the chart
	 
		foreach(keys %relclass)
		{
		# 	if ($relclass{$_} eq 'segment')
	# 		{
	# 			my @parents = ();
	# 			getParents($relid, \@parents);
	# 		
	# 		}
	my $id = $_;
	  $rt = $reltype{$_}; 
	  $rc = $relclass{$_};
			#only un-complete nodes
			unless ($complete{$id})
			{
				
				$parentcomplete=1;
				#print "before: $rellatex{$id} \n";
				$rellatex{$id} =~ s/(<(satellite|nucleus|element)\s*(.*?)>)/&replaceDaughter($1,$2,$3);/isge;
	#print "after: $rellatex{$id} \n";
	
	$rellatex{$id} =~ s/^\s+//sg;
	$rellatex{$id} =~ s/\s+$//sg;
	
				if ($parentcomplete)  # is complete after conv. of XML to LaTeX code
				{
					if ($rc eq 'hypRelation')
					{
						$rellatex{$id} = '\dirrel'.$rellatex{$id} ;
					} elsif ($rc eq 'parRelation')
					{
						$rellatex{$id} = '\multirel{'.$rt.'}'.$rellatex{$id} ;
					} else
					{
						warn "unspecified relation type: <relation> tag used!";
						$rellatex{$id} = '\multirel'.$rellatex{$id} ;
					}
					$complete{$id} = 1;
					$nodescompleted++;
				}
				$uncompletenodes++;
			}
		
			
		}
		print "uncomp: $uncompletenodes         compl: $nodescompleted\n";
		last if ($uncompletenodes == 0);
		if ($nodescompleted == 0)
		{
			warn "Not all nodes could be transformed to tree nodes -- circularities or undefined but referenced nodes in graph structure?\nPrinting largest existing analysis!"; 
last;
		}
		
	}
	
	 
	 #find biggest tree (with most nodes)
	 
	my $maxnodes=0;
	my $largestnode = '';
	foreach(keys %relclass)
	{
		if ($complete{$_})
		{
			#count nodes
			
			my $nodes = 0;
			while($rellatex{$_} =~ /\\(dir|multi)rel/isg)
			{
				$nodes++;
			}
			if ($nodes > $maxnodes)
			{
				$maxnodes = $nodes;
				$largestnode = $_;
			}
		}
	}
	 
	 if($largestnode)
	 {
	 	print $rellatex{$largestnode};
	 } else
	 {
	 	warn "could not find largest node!";
		foreach(keys %relclass)
		{
			if ($complete{$_})
			{
				print $rellatex{$_}."\n\n";
			}
			
		}
	 }
	 
	print "\n\n";
	
	&pr_corpustext unless ($putSegmentsInTree);
	 
	exit;
	
	

}

sub replaceDaughter($$$$$)
{
	my $tag = $1;		 
	my $role = $2;
	my $attr = $3;
#	my $rc = $4;  must be passed down as global vars
#	my $rt = $5;
	
	#print "replace daughter tag=$tag role=$role attr=$attr \n";
	 
	
	if ($attr =~ /id\s*=\s*\"(.*?)\"/is)
	{
		my $did = $1;  # daughter id
		$did =~ s/_//g;
		if ($complete{$did})
		{
			my $text = "";
			
			if ($rc eq 'hypRelation')
			{
				if ($role eq 'satellite')
				{
					$text .= '{'.$rt.'}';
				} else
				{
					$text .= '{}';
				}
				
			} else
			{
				$text .= '';
			}		
			 
			return $text.'{'.$rellatex{$did}.'}';
		}
	}
 
	$parentcomplete=0;  # parent is not complete because XML code returned
	return $tag;
}
# 
# sub getParents($$)
# {
# 	my \@parents = $_[1];
# 	my $id = $_[0];
# 	foreach(keys %relclass)
# 	{
# 		if ($relcontent =~ /id\s*=\s*\"$id\"/)
# 		{
# 			push @parents, $id;
# 		}
# 	}
# 	
# }


sub pr_corpustext 
{

print '\begin{rhetoricaltext}'."\n";
while($doc =~ /<segment\s+id\s*=\s*(\"?)([^\">]+)(\"?)[^>]*>(.*?)<\/\s*segment\s*>/isg )
{
	my $segid = $2;
	my $seg = $4;
	 
	#$segid =~ s/$docid\.?//;
	 $segid =~ s/_//g;
	 
	
	 $seg = &conv_text($seg);
	 

	if ($seg)
	{ print "\\unit[$segid]{$seg}\n";
	}
}
print '\source{'."$docid}".'\end{rhetoricaltext}'."\n";
return;

}


sub conv_text ($)
{
my $seg = $_[0];

 $seg =~ s/\<\/?sign[^>]*>//isg;
	 
	$seg =~ s/(Ä|\&Auml;)/\\\"\{A\}/sg;
	$seg =~ s/(Ö|\&Ouml;)/\\\"\{O\}/sg;
	$seg =~ s/(Ü|\&Uuml;)/\\\"\{U\}/sg;
	$seg =~ s/(ä|\&auml;)/\\\"\{a\}/sg;
	$seg =~ s/(ö|\&ouml;)/\\\"\{o\}/sg;
	$seg =~ s/(ü|\&uuml;)/\\\"\{u\}/sg;
	$seg =~ s/(ß|\&szlig;)/\\\"\{ss\}/sg;
	$seg =~ s/\$/\\\$/sg;
	return $seg;
}