Statistics
| Revision:

root / tmp / org.txm.core / res / org / txm / xml / xsl / concordance_mot-pivot.xsl @ 187

History | View | Annotate | Download (8.4 kB)

1
<?xml version="1.0" encoding="UTF-8"?>
2
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
3
				xmlns:exsl="http://exslt.org/common">
4

    
5
	<xsl:output method="text" encoding="UTF-8" indent="no" />
6

    
7
	<xsl:strip-space elements="*" />
8

    
9
	<xsl:param name="cx" select="30"/>
10
	<!-- vary this value to change the size of the context -->
11
	
12
	<xsl:param name="ntTypes">cat</xsl:param>
13
	<!-- non-terminal pivot features to show -->
14
	
15
	<xsl:param name="tTypes" >pos</xsl:param>
16
	<!-- terminal pivot features to show -->
17
	
18
	<xsl:variable name="valueshownt" ><xsl:value-of select="$ntTypes" /></xsl:variable>
19
	
20
	<xsl:variable name="valueshowt" ><xsl:value-of select="$tTypes" /></xsl:variable>
21

    
22
	<xsl:key name="idkey" match="t|nt" use="@id"/>
23
	<!-- Lezius' lookup key -->
24

    
25
	<xsl:key name="idkey-reverse" match="edge" use="@idref"/>
26
	<!-- reverse lookup key -->
27

    
28
	<xsl:template match="/">
29
		
30
		<!--Write a header-->
31
		
32
		<xsl:text>ID&#x09;LeftCxOutsideSnt&#x09;LeftCxInsideSnt&#x09;Pivot&#x09;</xsl:text>
33
		
34
		<xsl:for-each select="tokenize($valueshowt/node(), '\s+')">
35
			
36
			<xsl:value-of select="." /><xsl:text>&#x09;</xsl:text>
37
			
38
		</xsl:for-each>
39
		
40
		<xsl:text>PivotStr&#x09;</xsl:text>
41
		
42
		<xsl:for-each select="tokenize($valueshownt/node(), '\s+')">
43
			
44
			<xsl:value-of select="." /><xsl:text>&#x09;</xsl:text>
45
			
46
		</xsl:for-each>
47
	
48
		<xsl:text>RightCxInsideSnt&#x09;RightCxOutsideSnt&#x0D;&#x0A;</xsl:text>
49

    
50
		<xsl:for-each select=".//match">
51

    
52
			<!-- only treat match nodes -->
53

    
54
			<!-- first column: print sentence ID -->
55

    
56
			<xsl:value-of select="ancestor::s/@id" />
57

    
58
			<!-- next, verify that pivot is a terminal node -->
59

    
60
			<xsl:variable name="pivot-var" select="./variable[@name='#pivot']" />
61

    
62
			<xsl:variable name="pivot-node" select="key('idkey', exsl:node-set($pivot-var)/@idref)" />
63

    
64
			<xsl:choose>
65

    
66
				<xsl:when test="exsl:node-set($pivot-node)[self::t]">
67

    
68
					<xsl:apply-templates select="exsl:node-set($pivot-node)" mode="write-table" />
69

    
70
				</xsl:when>
71

    
72
				<xsl:otherwise><xsl:text>&#x09;Query error: #pivot is not a terminal node</xsl:text></xsl:otherwise>
73

    
74
			</xsl:choose>
75

    
76
			<xsl:text>&#x0d;&#x0a;</xsl:text>
77

    
78
		</xsl:for-each>
79

    
80
	</xsl:template>
81

    
82
	<xsl:template match="t" mode="write-table">
83
		
84
		<!-- Context node is a t-node pivot -->
85

    
86
		<!-- First stage: produce left context -->
87

    
88
		<xsl:text>&#x09;</xsl:text><xsl:apply-templates select="." mode="left-cx" />
89

    
90
		<!-- Next stage, write the word in the pivot -->
91

    
92
		<xsl:text>&#x09;</xsl:text><xsl:apply-templates select="." mode="output" />
93

    
94
		<!-- Next stage, write other required terminal attributes -->
95
		
96
		<xsl:variable name="thispivot" select="." />
97
		
98
		<xsl:text>&#x09;</xsl:text>
99
		<xsl:for-each select="tokenize($valueshowt/node(), '\s+')">
100
			
101
			<xsl:variable name="attr" select="."/>
102
			
103
			<xsl:value-of select="$thispivot/@*[name()=$attr]"/><xsl:text>&#x09;</xsl:text>
104
			
105
		</xsl:for-each>
106
		
107
		<!-- Next stage, write the lexical string contained in the parent NT node -->
108

    
109
		<xsl:apply-templates select="key('idkey-reverse', @id)/ancestor::nt" mode="output" />
110

    
111
		<!-- Next stage, write the required <nt /> properties -->
112
		
113
		<xsl:variable name="pivotnt" select="key('idkey-reverse', @id)/.." />
114
		
115
		<xsl:text>&#x09;</xsl:text>
116
		<xsl:for-each select="tokenize($valueshownt/node(), '\s+')">
117
			
118
			<xsl:variable name="attr" select="."/>
119
			
120
			<xsl:value-of select="$pivotnt/@*[name()=$attr]"/><xsl:text>&#x09;</xsl:text>
121
			
122
		</xsl:for-each>
123

    
124
		<!-- Next stage, write the right-cx -->
125

    
126
		<xsl:apply-templates select="." mode="right-cx"/>
127

    
128
	</xsl:template>
129

    
130
	<xsl:template match="t" mode="left-cx">
131

    
132
		<xsl:variable name="pivot-node" select="." />
133
		<xsl:variable name="this-sid"><xsl:value-of select="ancestor::s/@id" /></xsl:variable>
134

    
135
		<xsl:for-each select="preceding::t[position() &lt; number($cx)]" >
136
			<!-- xsl:sort select="@id" / -->
137
			<xsl:choose>
138
				<!-- switch from out-of-sentence to in-sentence context -->
139
				<xsl:when test="ancestor::s[@id = $this-sid] and not(preceding-sibling::t)">
140
					<xsl:text>&#x09;</xsl:text>
141
				</xsl:when>
142

    
143
				<!-- first word of the context and in the same sentence -->
144

    
145
				<xsl:when test="position() = 1 and ancestor::s[@id=$this-sid]"><xsl:text>&#x09;</xsl:text></xsl:when>
146

    
147
				<!-- first word of the context -->
148

    
149
				<xsl:when test="position() = 1" />
150

    
151
				<!-- first word in a sentence -->
152
				<xsl:when test="not(preceding-sibling::t)"><xsl:text> / </xsl:text></xsl:when>
153

    
154
				<xsl:otherwise><xsl:text> </xsl:text></xsl:otherwise>
155

    
156
			</xsl:choose>
157

    
158
			<xsl:apply-templates select="." mode="output" />
159
	
160
			<!-- add blank column if there's no in-sentence context -->
161
			<xsl:if test="position() = last() and ancestor::s[@id != $this-sid]"><xsl:text>&#x09;</xsl:text></xsl:if>
162

    
163
		</xsl:for-each>
164

    
165
	</xsl:template>
166

    
167
	<xsl:template match="t" mode="right-cx">
168

    
169
		<xsl:variable name="pivot-node" select="." />
170
		<xsl:variable name="this-sid"><xsl:value-of select="ancestor::s/@id" /></xsl:variable>
171

    
172
		<xsl:for-each select="following::t[position() &lt; number($cx)]" >
173
			<!-- xsl:sort select="@id" / -->
174
			<xsl:choose>
175
				<!-- switch from in-sentence to out-of-sentence context -->
176
				<xsl:when test="ancestor::s[@id != $this-sid] and preceding::t[position()=1]/ancestor::s[@id = $this-sid]">
177
					<xsl:text>&#x09;</xsl:text>
178
				</xsl:when>
179

    
180
				<!-- first word of the context -->
181

    
182
				<xsl:when test="position() = 1" />
183

    
184
				<!-- first word in a sentence -->
185
				<xsl:when test="not(preceding-sibling::t)"><xsl:text> / </xsl:text></xsl:when>
186

    
187
				<xsl:otherwise><xsl:text> </xsl:text></xsl:otherwise>
188

    
189
			</xsl:choose>
190
			<xsl:apply-templates select="." mode="output" />
191
			<!-- add blank column if there's no out-of-sentence context -->
192
			<xsl:if test="position() = last() and ancestor::s[@id = $this-sid]"><xsl:text>&#x09;</xsl:text></xsl:if>
193

    
194
		</xsl:for-each>
195

    
196
	</xsl:template>
197

    
198
	<xsl:template match="nt|t|s" mode="id">
199
		
200
		<xsl:value-of select="@id" />
201

    
202
	</xsl:template>
203

    
204
	<xsl:template match="variable" mode="name">
205
		
206
		<xsl:value-of select="@name" />
207

    
208
	</xsl:template>
209

    
210
	<xsl:template match="t" mode="output">
211
		
212
		<xsl:value-of select="@word" />
213

    
214
	</xsl:template>
215

    
216

    
217

    
218
	<xsl:template match="nt" mode="output">
219

    
220
		<!-- prints lexical content of nt node in correct order with constituents not in the pivot bracketed -->
221

    
222
		<!-- stuff a copy of all the tnodes in the pivot in $in-pivot -->
223
		<xsl:variable name='in-pivot'>
224
			<xsl:apply-templates select="." mode="get-terminals" />
225
		</xsl:variable>
226

    
227
		<!-- create a copy of terminals, with an attribute "in-pivot='True'" -->
228

    
229
		<xsl:variable name="new-terminals">
230

    
231
			<xsl:for-each select="ancestor::s//t">
232

    
233
				<xsl:variable name="current-id"><xsl:value-of select="@id" /></xsl:variable>
234

    
235
				<!-- copy element into variable -->
236

    
237
				<xsl:copy>
238

    
239
					<xsl:if test="exsl:node-set($in-pivot)//t[@id = $current-id]">
240
						<xsl:attribute name="in-pivot">True</xsl:attribute>
241
					</xsl:if>
242
					<xsl:apply-templates select="@*" mode="copy"/>
243

    
244
				</xsl:copy>
245

    
246
			</xsl:for-each>
247

    
248
		</xsl:variable>
249

    
250
		<!-- $new-terminals is in the correct order, so we can calculate the limits of the segment -->
251

    
252
		<xsl:variable name="first-pivot-id"><xsl:value-of select="exsl:node-set($new-terminals)//t[@in-pivot][position()=1]/@id" /></xsl:variable>
253

    
254
		<xsl:variable name="last-pivot-id"><xsl:value-of select="exsl:node-set($new-terminals)//t[@in-pivot][position()=last()]/@id" /></xsl:variable>
255

    
256
		<!-- At last! At last! We can output the MF! -->
257

    
258
		<xsl:for-each select="exsl:node-set($new-terminals)//t">
259

    
260
			<xsl:if test="@id = $first-pivot-id or @id = $last-pivot-id or (preceding-sibling::t[@id = $first-pivot-id] and following-sibling::t[@id = $last-pivot-id])">
261

    
262
				<!-- print the word -->
263
				
264
				<xsl:if test="not(@id = $first-pivot-id)"><xsl:text> </xsl:text></xsl:if>
265

    
266
				<xsl:choose>
267

    
268
					<xsl:when test="@in-pivot"><xsl:apply-templates select="." mode="output" /></xsl:when>
269

    
270
					<xsl:otherwise><xsl:text>[</xsl:text><xsl:apply-templates select="." mode="output" /><xsl:text>]</xsl:text></xsl:otherwise>
271

    
272
				</xsl:choose>
273

    
274
			</xsl:if>
275

    
276
		</xsl:for-each>
277

    
278
	</xsl:template>
279

    
280
	<xsl:template match="@*" mode="copy">
281
		<!-- returns a copy of all attribute nodes -->
282
		<xsl:copy />
283
	</xsl:template>	
284

    
285
	<xsl:template match="t" mode="get-terminals">
286

    
287
		<xsl:copy-of select="." />
288

    
289
	</xsl:template>
290

    
291
	<xsl:template match="nt" mode="get-terminals">
292

    
293
		<xsl:for-each select="edge">
294

    
295
			<xsl:apply-templates select="key('idkey', @idref)" mode="get-terminals" />
296
	
297
		</xsl:for-each>
298

    
299
	</xsl:template>
300

    
301
</xsl:stylesheet>