Interset/FeatureStructure.pm untested

filter: Statementstmt 0.0% 0 / 1446 Branchbran 0.0% 0 / 216 Conditioncond 0.0% 0 / 186 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 218 Podpod 0.0% 0 / 207 totaltotal 0.0% 0 / 2273 CC 108 SCAR 37.4
CC
108
Cov
0%
set
420.0
value_valid
110.0
is_other
110.0
CRAP 42.1
1 # ABSTRACT: Definition of morphosyntactic features and their values.
2 # Copyright © 2008-2016 Dan Zeman <zeman@ufal.mff.cuni.cz>
3  
4 package Lingua::Interset::FeatureStructure;
5 use strict;
6 use warnings;
7 our $VERSION = '3.014';
8  
9 use utf8;
10 use open ':utf8';
11 use namespace::autoclean;
12 use Moose;
13 use MooseX::SemiAffordanceAccessor; # attribute x is written using set_x($value) and read using x()
14 # Allow the user to import static functions into their namespace by stating
15 # use Lingua::Interset::FeatureStructure qw(feature_valid value_valid);
16 use Exporter::Easy ( OK => [ 'feature_valid', 'value_valid' ] );
17 use Carp; # confess()
18 use List::MoreUtils qw(any);
19  
20  
21  
22 # Should the features have their own classes, too?
23 # Pluses:
24 # + We could bind all properties of one feature tighter (its name, its priority, list of values and their intuitive ordering, list of default value changes).
25 # + There would be more space for additional services such as documentation and examples of the feature values.
26 # Minuses:
27 # - It is good to have them all together here. Better overall picture, mutual relations checking etc. If they are spread across 30 files, the picture will be lost.
28 # - Handling classes might turn out to be more complicated than handling simple attributes?
29 # - Perhaps efficiency issues?
30  
31 # We want to know the following about the features:
32 # 1. List of known feature names
33 # 1.1. Each feature named using the 'has' keyword of Moose
34 # 1.2. Print order of features, i.e. the order in which we want to sort a list of features when printed
35 # 1.3. Default priority order of features, to be used when restricting value combinations (can be overriden for particular tagsets)
36 # 2. For each feature, list of known values
37 # 2.1. Each value named using the 'enum' keyword of Moose to trigger Moose data validation
38 # 2.2. Print order of values, e.g. we want to print 'sing' before 'plur' although alphabetical order says otherwise
39 # 3. For each value, order in which the other values should be considered as replacements when value restrictions are in effect
40  
41 # Solution: One big matrix of features, values and their properties follows.
42 # The Moose declarations will be simply derived from the matrix below.
43 # The order of the features and values in the matrix is their print order.
44 # Default priority order of features is defined by the 'priority' values, ascending.
45  
46 # Help to the replacement arrays:
47  
48 # What follows is an attempt to describe the system of value replacements
49 # in an easily readable and maintainable way. The algorithm to process it
50 # may be complicated but the human interpretation should (hopefully) be simple.
51  
52 # Rule 1: Values of each feature are ordered. This is the order of priority
53 # when searching for replacement of an empty value.
54 # Rule 2: A non-empty value is replaced by empty value in the first place.
55 # If the empty value is not permitted, it is replaced according to
56 # Rule 1.
57 # Rule 3: Some values of some features have customized replacement sequences.
58 # They contain replacements that are used prior to the default empty value.
59 # For instance, if we have pos=det (determiner), we want to try
60 # pos=adj (adjective) first, and only if this value is not permitted,
61 # we default to the empty value.
62 # Customized replacement sequences, if present, are specified
63 # immediately next to the value being replaced (in one array).
64 # The last element is a link: if we pass this value, we proceed to
65 # its own customized replacement sequence. If the value does not
66 # have a customized replacement sequence or if the link constitutes
67 # a loop, proceed as if replacing an empty value according to Rule 1.
68  
69 # The empty value does not need to be specified in the main (top-down) list
70 # of values of a feature. However, should a customized replacement sequence
71 # (left-to-right list) contain an empty value, it must explicitely state it.
72  
73 # The algorithm:
74 # $valord{$feature}[$first][$second]
75 # 1. Find the 2D array according to feature name.
76 # 2. Find the value to be replaced in the first dimension.
77 # 3. Try to find replacement in the second dimension (respect ordering).
78 # 4. If unsuccessful and the second dimension has more than one member, try to replace the last member (go back to step 2). Check loops!
79 # 5. In case of a loop go to next step and act as if there was only one member.
80 # 6. If unsuccessful and the second dimension has only one member (the value to replace), check empty value as replacement.
81 # 7. If unsuccessful, try to find replacement in the first dimension (respect ordering).
82  
83 # Since the order of the hash keys is important (it encodes the print order of the features),
84 # also store the hash as array (we will subsequently separate the keys from the values).
85 my @_matrix;
86 my %matrix = @_matrix =
87 (
88 # Main part of speech
89 'pos' =>
90 {
91 'priority' => 10,
92 'values' => ['noun', 'adj', 'num', 'verb', 'adv', 'adp', 'conj', 'part', 'int', 'punc', 'sym', ''],
93 'replacements' =>
94 [
95 ['part' ],
96 ['noun', 'verb'],
97 ['verb' ],
98 ['sym', 'punc' ],
99 ['punc', 'sym' ],
100 ['adj', 'noun'],
101 ['num', 'adj' ],
102 ['adv' ],
103 ['adp', 'adv' ],
104 ['conj', 'adp' ],
105 ['int' ]
106 ],
107 },
108 # Special type of noun if applicable and if known.
109 'nountype' =>
110 {
111 'priority' => 80,
112 'values' => ['com', 'prop', 'class', ''],
113 'replacements' =>
114 [
115 ['com'],
116 ['prop'],
117 ['class'],
118 ],
119 'uname' => 'NounType'
120 },
121 # Named entity type. Typically used together with nountype = 'prop'.
122 'nametype' =>
123 {
124 'priority' => 85,
125 'values' => ['geo', 'prs', 'giv', 'sur', 'nat', 'com', 'pro', 'oth', 'col', 'sci', 'che', 'med', 'tec', 'cel', 'gov', 'jus', 'fin', 'env', 'cul', 'spo', 'hob', ''],
126 'replacements' =>
127 [
128 ['geo'],
129 ['prs', 'giv', 'sur'],
130 ['giv', 'prs'],
131 ['sur', 'prs'],
132 ['nat', 'prs'],
133 ['com', 'pro'],
134 ['pro', 'com'],
135 ['oth'],
136 ['col'],
137 ['sci'],
138 ['che', 'sci'],
139 ['med'],
140 ['tec'],
141 ['cel', 'tec'],
142 ['gov'],
143 ['jus'],
144 ['fin'],
145 ['env'],
146 ['cul'],
147 ['spo'],
148 ['hob']
149 ],
150 'uname' => 'NameType'
151 },
152 # Special type of adjective if applicable and if known.
153 'adjtype' =>
154 {
155 'priority' => 90,
156 'values' => ['pdt', ''],
157 'replacements' =>
158 [
159 ['pdt']
160 ],
161 'uname' => 'AdjType'
162 },
163 # Pronominality and its type for nouns (pronouns), adjectives (determiners), numerals, adverbs etc.
164 'prontype' =>
165 {
166 'priority' => 100,
167 'values' => ['prn', 'prs', 'rcp', 'art', 'int', 'rel', 'exc', 'dem', 'emp', 'neg', 'ind', 'tot', ''],
168 'replacements' =>
169 [
170 ['prn'],
171 ['ind'],
172 ['dem'],
173 ['prs'],
174 ['rcp', 'prs'],
175 ['int', 'rel', 'exc'],
176 ['rel', 'int'],
177 ['exc', 'int'],
178 ['emp', 'dem'],
179 ['neg', 'ind'],
180 ['tot', 'ind'],
181 ['art']
182 ],
183 'uname' => 'PronType'
184 },
185 # Numeral types.
186 # Note that it is not guaranteed that pos eq 'num'. Typically only cardinal numbers
187 # get 'num'. Others may have pos 'adj' (e.g. ordinal numerals) or 'adv' while their
188 # numtype value indicates that they have something to do with numbers.
189 'numtype' =>
190 {
191 'priority' => 110,
192 'values' => ['card', 'ord', 'mult', 'frac', 'sets', 'dist', 'range', ''],
193 'replacements' =>
194 [
195 ['card', '', 'ord'],
196 ['ord', '', 'card'],
197 ['mult'],
198 ['frac', 'card'],
199 ['sets', 'card'],
200 ['dist', 'card'],
201 ['range', 'card']
202 ],
203 'uname' => 'NumType'
204 },
205 # Presentation form of numerals.
206 'numform' =>
207 {
208 'priority' => 120,
209 'values' => ['word', 'digit', 'roman', 'combi', ''],
210 'replacements' =>
211 [
212 ['word'],
213 ['digit', 'roman'],
214 ['roman', 'digit'],
215 ['combi', 'digit']
216 ],
217 'uname' => 'NumForm'
218 },
219 # Numeric value (class of values) of numerals.
220 # Some low-value numerals in some languages behave differently.
221 'numvalue' =>
222 {
223 'priority' => 130,
224 'values' => ['1', '2', '3', ''],
225 'replacements' =>
226 [
227 ['1'],
228 ['2', '3'],
229 ['3', '2']
230 ],
231 'uname' => 'NumValue'
232 },
233 # Special type of verb if applicable and if known.
234 'verbtype' =>
235 {
236 'priority' => 140,
237 'values' => ['aux', 'cop', 'mod', 'light', 'verbconj', ''],
238 'replacements' =>
239 [
240 ['aux'],
241 ['cop', 'aux'],
242 ['mod', 'aux'],
243 ['light'],
244 ['verbconj'],
245 ],
246 'uname' => 'VerbType'
247 },
248 # Semantic type of adverb.
249 'advtype' =>
250 {
251 'priority' => 150,
252 'values' => ['man', 'loc', 'tim', 'sta', 'deg', 'cau', 'mod', 'adadj', 'ex', ''],
253 'replacements' =>
254 [
255 ['man'],
256 ['loc'],
257 ['tim'],
258 ['sta'],
259 ['deg'],
260 ['cau'],
261 ['mod'],
262 ['adadj'],
263 ['ex']
264 ],
265 'uname' => 'AdvType'
266 },
267 # Special type of adposition if applicable and if known.
268 'adpostype' =>
269 {
270 'priority' => 155,
271 'values' => ['prep', 'post', 'circ', 'voc', 'preppron', 'comprep', ''],
272 'replacements' =>
273 [
274 ['prep'],
275 ['post'],
276 ['circ'],
277 ['voc', 'prep'],
278 ['preppron'],
279 ['comprep']
280 ],
281 'uname' => 'AdpType'
282 },
283 # Conjunction type.
284 'conjtype' =>
285 {
286 'priority' => 160,
287 'values' => ['coor', 'sub', 'comp', 'oper', ''],
288 'replacements' =>
289 [
290 ['coor'],
291 ['sub'],
292 ['comp'],
293 ['oper']
294 ],
295 'uname' => 'ConjType'
296 },
297 # Particle type.
298 'parttype' =>
299 {
300 'priority' => 165,
301 'values' => ['mod', 'emp', 'res', 'inf', 'vbp', ''],
302 'replacements' =>
303 [
304 ['mod'],
305 ['emp'],
306 ['res'],
307 ['inf'],
308 ['vbp']
309 ],
310 'uname' => 'PartType'
311 },
312 # Punctuation type.
313 'punctype' =>
314 {
315 'priority' => 170,
316 'values' => ['peri', 'qest', 'excl', 'quot', 'brck', 'comm', 'colo', 'semi', 'dash', 'root', ''],
317 'replacements' =>
318 [
319 ['colo'],
320 ['comm', 'colo'],
321 ['peri', 'colo'],
322 ['qest', 'peri'],
323 ['excl', 'peri'],
324 ['quot', 'brck'],
325 ['brck', 'quot'],
326 ['semi', 'comm'],
327 ['dash', 'colo'],
328 ['root']
329 ],
330 'uname' => 'PunctType'
331 },
332 # Distinction between opening and closing brackets and other paired punctuation.
333 'puncside' =>
334 {
335 'priority' => 180,
336 'values' => ['ini', 'fin', ''],
337 'replacements' =>
338 [
339 ['ini'],
340 ['fin']
341 ],
342 'uname' => 'PunctSide'
343 },
344 # Syntactic part of speech.
345 ###!!! DO WE STILL NEED THIS?
346 # It was originally used with pronouns and numerals that behaved syntactically as nouns, adjectives or even adverbs.
347 # The problem of pronouns has been solved by making pronominality a separate feature.
348 # If we do something similar with numerals (but what about cardinals?), the synpos feature will probably become superfluous.
349 # Before removing the feature we should analyze all existing tagsets to see which tagsets set synpos and where.
350 'synpos' =>
351 {
352 'priority' => 200,
353 'values' => ['subst', 'attr', 'adv', 'pred', ''],
354 'replacements' =>
355 [
356 ['subst'],
357 ['attr'],
358 ['adv'],
359 ['pred']
360 ],
361 },
362 # Morphological part of speech.
363 # A word's morphological paradigm may behave like a different part of speech than the word is assigned to.
364 # For example, Slovak noun vstupné “admission (fee)” behaves syntactically as noun, is tagged as noun,
365 # but it originates from an adjective and retains adjectival paradigm. The paradigm feature of the sk::snk
366 # tagset maps to this Interset feature.
367 'morphpos' =>
368 {
369 'priority' => 205,
370 'values' => ['noun', 'adj', 'pron', 'num', 'adv', 'mix', 'def', ''],
371 'replacements' =>
372 [
373 ['mix'],
374 ['noun'],
375 ['adj'],
376 ['pron'],
377 ['num'],
378 ['adv'],
379 ['def']
380 ],
381 'uname' => 'MorphPos'
382 },
383 #--------------------------------------------------------------------------
384 # For the following group of almost-boolean attributes I am not sure what would be the best internal representation.
385 # Many of them constitute a distinct part of speech in some tagsets but they are in principle orthogonal to the part-of-speach feature.
386 # However, regardless the representation, I would like the setter (writer) method to accept boolean values (zero/nonzero), too.
387 # Possessivity.
388 'poss' =>
389 {
390 'priority' => 210,
391 'values' => ['yes', ''], ###!!! OR yes-no-empty? But I do not think it would be practical.
392 'replacements' =>
393 [
394 ['yes']
395 ],
396 'uname' => 'Poss'
397 },
398 # Reflexivity.
399 'reflex' =>
400 {
401 'priority' => 220,
402 'values' => ['yes', ''], ###!!! OR yes-no-empty? But I do not think it would be practical.
403 'replacements' =>
404 [
405 ['yes']
406 ],
407 'uname' => 'Reflex'
408 },
409 # Foreign word? (Not a loanword but a quotation from a foreign language.)
410 'foreign' =>
411 {
412 'priority' => 400,
413 'values' => ['yes', ''],
414 'replacements' =>
415 [
416 ['yes']
417 ],
418 'uname' => 'Foreign'
419 },
420 # Abbreviation?
421 'abbr' =>
422 {
423 'priority' => 20,
424 'values' => ['yes', ''], ###!!! OR yes-no-empty? But I do not think it would be practical.
425 'replacements' =>
426 [
427 ['yes']
428 ],
429 'uname' => 'Abbr'
430 },
431 # Is this a part of a hyphenated compound?
432 # Typically one part gets a normal part of speech and the other gets this flag.
433 # Whether this is the first or the second part depends on the original tagset and language.
434 'hyph' =>
435 {
436 'priority' => 30,
437 'values' => ['yes', ''], ###!!! OR yes-no-empty? But I do not think it would be practical.
438 'replacements' =>
439 [
440 ['yes']
441 ],
442 'uname' => 'Hyph'
443 },
444 # Is this / does this word contain a typo?
445 'typo' =>
446 {
447 'priority' => 430,
448 'values' => ['yes', ''], ###!!! OR yes-no-empty? But I do not think it would be practical.
449 'replacements' =>
450 [
451 ['yes']
452 ],
453 'uname' => 'Typo'
454 },
455 # Is this a reduplicated or echo word?
456 'echo' =>
457 {
458 'priority' => 40,
459 'values' => ['rdp', 'ech', ''],
460 'replacements' =>
461 [
462 ['rdp', 'ech'],
463 ['ech', 'rdp']
464 ],
465 'uname' => 'Echo'
466 },
467 # Polarity (do not confuse with negative pro-forms).
468 # Note: this feature was called 'negativeness' until Interset 2.052.
469 'polarity' =>
470 {
471 'priority' => 240,
472 'values' => ['pos', 'neg', ''],
473 'replacements' =>
474 [
475 ['pos'],
476 ['neg']
477 ],
478 'uname' => 'Polarity'
479 },
480 # Definiteness (or state in Arabic).
481 'definite' =>
482 {
483 'priority' => 250,
484 'values' => ['ind', 'spec', 'def', 'cons', 'com', ''],
485 'replacements' =>
486 [
487 ['ind'],
488 ['spec', 'ind'],
489 ['def'],
490 ['cons', 'def'],
491 ['com', 'cons', 'def']
492 ],
493 'uname' => 'Definite'
494 },
495 # Gender.
496 'gender' =>
497 {
498 'priority' => 300,
499 'values' => ['masc', 'fem', 'com', 'neut', ''],
500 'replacements' =>
501 [
502 ['neut'],
503 ['com', 'masc', 'fem'],
504 ['masc', 'com'],
505 ['fem', 'com']
506 ],
507 'uname' => 'Gender'
508 },
509 # Animacy (considered part of gender in some tagsets, but still orthogonal).
510 'animacy' =>
511 {
512 'priority' => 310,
513 'values' => ['anim', 'hum', 'nhum', 'inan', ''],
514 'replacements' =>
515 [
516 ['anim'],
517 ['hum', 'anim'],
518 ['nhum', 'anim', 'inan'],
519 ['inan']
520 ],
521 'uname' => 'Animacy'
522 },
523 # Grammatical number.
524 'number' =>
525 {
526 'priority' => 320,
527 'values' => ['sing', 'dual', 'tri', 'pauc', 'grpa', 'plur', 'grpl', 'inv', 'ptan', 'coll', 'count', ''],
528 'replacements' =>
529 [
530 ['sing'],
531 ['dual', 'plur'],
532 ['tri', 'plur'],
533 ['pauc', 'plur'],
534 ['grpa', 'plur'],
535 ['plur'],
536 ['grpl', 'plur'],
537 ['inv'],
538 ['ptan', 'plur'],
539 ['coll', 'sing'],
540 ['count', 'plur']
541 ],
542 'uname' => 'Number'
543 },
544 # Grammatical case.
545 'case' =>
546 {
547 'priority' => 330,
548 'values' => ['nom', 'gen', 'dat', 'acc', 'voc', 'loc', 'ins',
549 'abl', 'del', 'par', 'dis', 'ess', 'tra', 'com', 'abe', 'ine', 'ela', 'ill', 'ade', 'all', 'sub', 'sup', 'lat', 'per',
550 'add', 'tem', 'ter', 'abs', 'erg', 'cau', 'ben', 'cns', 'equ', 'cmp', ''],
551 'replacements' =>
552 [
553 ['nom'],
554 ['acc'],
555 ['dat', 'ben'],
556 ['gen'],
557 ['loc', 'ine', 'ade', 'sup', 'tem'],
558 ['ins'],
559 ['voc'],
560 ['abl', 'del', 'lat', 'loc'],
561 ['del', 'abl', 'lat', 'loc'],
562 ['par', 'gen'],
563 ['dis'],
564 ['ess'],
565 ['tra'],
566 ['com', 'ins'],
567 ['abe'],
568 ['ine', 'loc'],
569 ['ela', 'loc'],
570 ['ill', 'lat', 'loc'],
571 ['add', 'ill'],
572 ['ade', 'sup', 'loc'],
573 ['sup', 'ade', 'loc'],
574 ['all', 'sub', 'lat', 'loc'],
575 ['sub', 'all', 'lat', 'loc'],
576 ['lat', 'all', 'sub', 'loc'],
577 ['per', 'lat'],
578 ['tem', 'loc'],
579 ['ter', 'ill'],
580 ['abs', 'nom', 'acc'],
581 ['erg', 'nom'],
582 ['cau'],
583 ['ben', 'dat'],
584 ['cns', 'ben'],
585 ['equ', 'cmp'],
586 ['cmp', 'equ']
587 ],
588 'uname' => 'Case'
589 },
590 # Is this a special form (case) after a preposition?
591 # Typically applies to personal pronouns, e.g. in Czech and Portuguese.
592 'prepcase' =>
593 {
594 'priority' => 340,
595 'values' => ['npr', 'pre', ''],
596 'replacements' =>
597 [
598 ['npr'],
599 ['pre']
600 ],
601 'uname' => 'PrepCase'
602 },
603 # Degree of comparison.
604 'degree' =>
605 {
606 'priority' => 230,
607 'values' => ['pos', 'cmp', 'sup', 'abs', 'equ', 'dim', 'aug', ''],
608 'replacements' =>
609 [
610 ['pos'],
611 ['cmp'],
612 ['sup', 'cmp'],
613 ['abs', 'sup'],
614 ['equ', 'cmp', 'pos'],
615 ['dim'],
616 ['aug']
617 ],
618 'uname' => 'Degree'
619 },
620 # Person.
621 'person' =>
622 {
623 'priority' => 260,
624 'values' => ['0', '1', '2', '3', '4', ''],
625 'replacements' =>
626 [
627 ['3'],
628 ['1'],
629 ['2'],
630 ['0'],
631 ['4', '3']
632 ],
633 'uname' => 'Person'
634 },
635 # Clusivity distinguishes inclusive and exclusive first person plural pronouns.
636 'clusivity' =>
637 {
638 'priority' => 265,
639 'values' => ['in', 'ex', ''],
640 'replacements' =>
641 [
642 ['in'], # I + you (+ optionally they)
643 ['ex'] # I + they (excluding you)
644 ],
645 'uname' => 'Clusivity'
646 },
647 # Politeness, formal vs. informal word forms.
648 'polite' =>
649 {
650 'priority' => 350,
651 'values' => ['infm', 'form', 'elev', 'humb', ''],
652 'replacements' =>
653 [
654 ['infm'],
655 ['form', 'elev', 'humb'],
656 ['elev', 'form', 'humb'],
657 ['humb', 'form', 'elev']
658 ],
659 'uname' => 'Polite'
660 },
661 # Possessor's gender. (The gender feature typically holds the possession's gender in this case.)
662 'possgender' =>
663 {
664 'priority' => 360,
665 'values' => ['masc', 'fem', 'com', 'neut', ''],
666 'replacements' =>
667 [
668 ['neut'],
669 ['com', 'masc', 'fem'],
670 ['masc', 'com'],
671 ['fem', 'com']
672 ],
673 'uname' => 'Gender[psor]'
674 },
675 # Possessor's person.
676 # Used e.g. in Hungarian where possessive morphemes can be attached to possessed nouns ("apple-mine").
677 'possperson' =>
678 {
679 'priority' => 370,
680 'values' => ['1', '2', '3', ''],
681 'replacements' =>
682 [
683 ['3'],
684 ['1'],
685 ['2']
686 ],
687 'uname' => 'Person[psor]'
688 },
689 # Possessor's number.
690 'possnumber' =>
691 {
692 'priority' => 380,
693 'values' => ['sing', 'dual', 'plur', ''],
694 'replacements' =>
695 [
696 ['sing'],
697 ['dual', 'plur'],
698 ['plur']
699 ],
700 'uname' => 'Number[psor]'
701 },
702 # Possession's number.
703 # In Hungarian, it is possible (though rare) that a noun has three numbers:
704 # 1. its own grammatical number; 2. number of its possessor; 3. number of its possession.
705 'possednumber' =>
706 {
707 'priority' => 390,
708 'values' => ['sing', 'dual', 'plur', ''],
709 'replacements' =>
710 [
711 ['sing'],
712 ['dual', 'plur'],
713 ['plur']
714 ],
715 'uname' => 'Number[psee]'
716 },
717 # Person of the absolutive argument of the verb.
718 # Used with synthetic verbs in Basque. They agree with multiple arguments in person and number.
719 'absperson' =>
720 {
721 'priority' => 400,
722 'values' => ['1', '2', '3', ''],
723 'replacements' =>
724 [
725 ['3'],
726 ['1'],
727 ['2']
728 ],
729 'uname' => 'Person[abs]'
730 },
731 # Person of the ergative argument of the verb.
732 # Used with synthetic verbs in Basque. They agree with multiple arguments in person and number.
733 'ergperson' =>
734 {
735 'priority' => 401,
736 'values' => ['1', '2', '3', ''],
737 'replacements' =>
738 [
739 ['3'],
740 ['1'],
741 ['2']
742 ],
743 'uname' => 'Person[erg]'
744 },
745 # Person of the dative argument of the verb.
746 # Used with synthetic verbs in Basque. They agree with multiple arguments in person and number.
747 'datperson' =>
748 {
749 'priority' => 402,
750 'values' => ['1', '2', '3', ''],
751 'replacements' =>
752 [
753 ['3'],
754 ['1'],
755 ['2']
756 ],
757 'uname' => 'Person[dat]'
758 },
759 # Number of the absolutive argument of the verb.
760 # Used with synthetic verbs in Basque. They agree with multiple arguments in person and number.
761 'absnumber' =>
762 {
763 'priority' => 403,
764 'values' => ['sing', 'dual', 'plur', ''],
765 'replacements' =>
766 [
767 ['sing'],
768 ['dual', 'plur'],
769 ['plur']
770 ],
771 'uname' => 'Number[abs]'
772 },
773 # Number of the ergative argument of the verb.
774 # Used with synthetic verbs in Basque. They agree with multiple arguments in person and number.
775 'ergnumber' =>
776 {
777 'priority' => 404,
778 'values' => ['sing', 'dual', 'plur', ''],
779 'replacements' =>
780 [
781 ['sing'],
782 ['dual', 'plur'],
783 ['plur']
784 ],
785 'uname' => 'Number[erg]'
786 },
787 # Number of the dative argument of the verb.
788 # Used with synthetic verbs in Basque. They agree with multiple arguments in person and number.
789 'datnumber' =>
790 {
791 'priority' => 405,
792 'values' => ['sing', 'dual', 'plur', ''],
793 'replacements' =>
794 [
795 ['sing'],
796 ['dual', 'plur'],
797 ['plur']
798 ],
799 'uname' => 'Number[dat]'
800 },
801 # Politeness of the absolutive argument of the verb.
802 # Used with synthetic verbs in Basque. They agree with multiple arguments in person and number.
803 'abspolite' =>
804 {
805 'priority' => 406,
806 'values' => ['infm', 'form', 'elev', 'humb', ''],
807 'replacements' =>
808 [
809 ['infm'],
810 ['form', 'elev', 'humb'],
811 ['elev', 'form', 'humb'],
812 ['humb', 'form', 'elev']
813 ],
814 'uname' => 'Polite[abs]'
815 },
816 # Politeness of the ergative argument of the verb.
817 # Used with synthetic verbs in Basque. They agree with multiple arguments in person and number.
818 'ergpolite' =>
819 {
820 'priority' => 407,
821 'values' => ['infm', 'form', 'elev', 'humb', ''],
822 'replacements' =>
823 [
824 ['infm'],
825 ['form', 'elev', 'humb'],
826 ['elev', 'form', 'humb'],
827 ['humb', 'form', 'elev']
828 ],
829 'uname' => 'Polite[erg]'
830 },
831 # Politeness of the dative argument of the verb.
832 # Used with synthetic verbs in Basque. They agree with multiple arguments in person and number.
833 'datpolite' =>
834 {
835 'priority' => 408,
836 'values' => ['infm', 'form', 'elev', 'humb', ''],
837 'replacements' =>
838 [
839 ['infm'],
840 ['form', 'elev', 'humb'],
841 ['elev', 'form', 'humb'],
842 ['humb', 'form', 'elev']
843 ],
844 'uname' => 'Polite[dat]'
845 },
846 # Gender of the ergative argument of the verb.
847 # Used with synthetic verbs in Basque. They agree with multiple arguments in person and number.
848 'erggender' =>
849 {
850 'priority' => 409,
851 'values' => ['masc', 'fem', 'com', 'neut', ''],
852 'replacements' =>
853 [
854 ['neut'],
855 ['com', 'masc', 'fem'],
856 ['masc', 'com'],
857 ['fem', 'com']
858 ],
859 'uname' => 'Gender[erg]'
860 },
861 # Gender of the dative argument of the verb.
862 # Used with synthetic verbs in Basque. They agree with multiple arguments in person and number.
863 'datgender' =>
864 {
865 'priority' => 410,
866 'values' => ['masc', 'fem', 'com', 'neut', ''],
867 'replacements' =>
868 [
869 ['neut'],
870 ['com', 'masc', 'fem'],
871 ['masc', 'com'],
872 ['fem', 'com']
873 ],
874 'uname' => 'Gender[dat]'
875 },
876 # Position / usage of adjectives, determiners, participles etc.
877 'position' =>
878 {
879 'priority' => 411,
880 'values' => ['prenom', 'postnom', 'nom', 'free', ''],
881 'replacements' =>
882 [
883 ['prenom'],
884 ['postnom'],
885 ['nom'],
886 ['free']
887 ],
888 'uname' => 'Position'
889 },
890 # Subcategorization.
891 # So far this feature only keeps the transitive-intransitive distinction encoded in some tagsets.
892 # However, real verb subcategorization is in fact much more complex.
893 'subcat' =>
894 {
895 'priority' => 50,
896 'values' => ['intr', 'tran', ''],
897 'replacements' =>
898 [
899 ['intr'],
900 ['tran']
901 ],
902 'uname' => 'Subcat'
903 },
904 # Verb form. May apply to non-verbs as well:
905 # part (participle, properties of both verbs and adjectives)
906 # vnoun (verbal noun)
907 # ger (gerund, properties of both verbs and nouns, deprecated)
908 # gdv (gerundive, properties of both verbs and adjectives)
909 # conv (converb, properties of both verbs and adverbs)
910 'verbform' =>
911 {
912 'priority' => 60,
913 'values' => ['fin', 'inf', 'sup', 'part', 'conv', 'vnoun', 'ger', 'gdv', ''],
914 'replacements' =>
915 [
916 ['inf'],
917 ['fin'],
918 ['part'],
919 ['vnoun', 'ger'],
920 ['ger', 'vnoun'],
921 ['gdv', 'part'],
922 ['sup'],
923 ['conv']
924 ],
925 'uname' => 'VerbForm'
926 },
927 # Mood.
928 'mood' =>
929 {
930 'priority' => 70,
931 'values' => ['ind', 'imp', 'cnd', 'pot', 'sub', 'jus', 'prp', 'opt', 'des', 'nec', 'qot', 'adm', ''],
932 'replacements' =>
933 [
934 ['ind'],
935 ['imp', 'nec'],
936 ['cnd', 'sub'],
937 ['pot', 'cnd'],
938 ['sub', 'cnd', 'jus', 'opt'],
939 ['jus', 'sub', 'opt'],
940 ['prp', 'jus'],
941 ['opt', 'jus'],
942 ['des', 'jus'],
943 ['nec', 'imp'],
944 ['qot', 'ind'],
945 ['adm']
946 ],
947 'uname' => 'Mood'
948 },
949 # Tense.
950 'tense' =>
951 {
952 'priority' => 270,
953 'values' => ['pres', 'fut', 'past', 'aor', 'imp', 'pqp', ''],
954 'replacements' =>
955 [
956 ['pres'],
957 ['fut'],
958 ['past', 'aor', 'imp'],
959 ['aor', 'past'],
960 ['imp', 'past'],
961 ['pqp', 'past']
962 ],
963 'uname' => 'Tense'
964 },
965 # Voice.
966 'voice' =>
967 {
968 'priority' => 280,
969 'values' => ['act', 'mid', 'pass', 'rcp', 'cau', 'int', 'antip', 'dir', 'inv', ''],
970 'replacements' =>
971 [
972 ['act'],
973 ['mid'],
974 ['pass'],
975 ['rcp'],
976 ['cau'],
977 ['int'],
978 ['antip'],
979 ['dir'],
980 ['inv']
981 ],
982 'uname' => 'Voice'
983 },
984 # Evidentiality.
985 'evident' =>
986 {
987 'priority' => 285,
988 'values' => ['fh', 'nfh', ''],
989 'replacements' =>
990 [
991 ['fh'],
992 ['nfh']
993 ],
994 'uname' => 'Evident'
995 },
996 # Aspect (lexical or grammatical; but see also the 'imp' tense).
997 'aspect' =>
998 {
999 'priority' => 290,
1000 'values' => ['imp', 'perf', 'prosp', 'prog', 'hab', 'iter', ''],
1001 'replacements' =>
1002 [
1003 ['imp'],
1004 ['perf'],
1005 ['prosp'],
1006 ['prog'],
1007 ['hab'],
1008 ['iter']
1009 ],
1010 'uname' => 'Aspect'
1011 },
1012 ###!!! Experimental for ro::multext and its conversion to UD!
1013 ###!!! Not yet fully accepted and documented!
1014 'strength' =>
1015 {
1016 'priority' => 437,
1017 'values' => ['weak', 'strong'],
1018 'replacements' =>
1019 [
1020 ['weak'],
1021 ['strong']
1022 ],
1023 'uname' => 'Strength'
1024 },
1025 # Variant. Used in some tagsets to distinguish between forms of the same lemma that would otherwise get the same tag.
1026 # The meaning of the values is not and cannot be universal, not even within the scope of one tagset.
1027 'variant' =>
1028 {
1029 'priority' => 440,
1030 'values' => ['short', 'long', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ''],
1031 'replacements' =>
1032 [
1033 ['0'],
1034 ['1'],
1035 ['2'],
1036 ['3'],
1037 ['4'],
1038 ['5'],
1039 ['6'],
1040 ['7'],
1041 ['8'],
1042 ['9'],
1043 ['short'],
1044 ['long']
1045 ],
1046 'uname' => 'Variant'
1047 },
1048 # Style.
1049 # Either lexical category of the lemma, or grammatical
1050 # (e.g. standard and colloquial suffix of the same lemma, cf. Czech "zelený" vs. "zelenej").
1051 'style' =>
1052 {
1053 'priority' => 420,
1054 'values' => ['arch', 'rare', 'form', 'poet', 'norm', 'coll', 'vrnc', 'slng', 'expr', 'derg', 'vulg', ''],
1055 'replacements' =>
1056 [
1057 ['norm'],
1058 ['form'],
1059 ['rare', 'arch', 'form'],
1060 ['arch', 'rare', 'form'],
1061 ['coll'],
1062 ['poet'],
1063 ['vrnc'],
1064 ['slng'],
1065 ['derg', 'coll'],
1066 ['expr', 'coll'],
1067 ['vulg', 'derg']
1068 ],
1069 'uname' => 'Style'
1070 },
1071 # Tagset identifier. Where does this feature structure come from? Used to interpret the 'other' feature.
1072 # The expected values can be checked by a regular expression but they cannot be enumerated.
1073 # isa => subtype as 'Str', where { m/^([a-z]+::[a-z]+)?$/ }, message { "'$_' does not look like a tagset identifier ('lang::corpus')." } );
1074 'tagset' =>
1075 {
1076 'priority' => 9998,
1077 },
1078 # Tagset-specific information that does not fit elsewhere.
1079 # Any value is permitted, even a hash reference.
1080 'other' =>
1081 {
1082 'priority' => 9999,
1083 }
1084 );
1085 my @features_in_print_order = grep {ref($_) eq ''} @_matrix;
1086 # The list of replacements (defaults) will be derived from the above matrix.
1087 # Same as the matrix, it will be a class-static variable that will not depend on any particular object.
1088 # It will be created lazily on the first demand, using the function _preprocess_list_of_replacements();
1089 # to ensure the lazy creation, it should be accessed using get_replacements().
1090 my $replacements = undef;
1091  
1092  
1093  
1094 # Define the features as Moose attributes.
1095 my $meta = __PACKAGE__->meta();
1096 foreach my $feature (keys(%matrix))
1097 {
1098 unless($feature eq 'other')
1099 {
1100 # Multivalues are internally implemented as arrays and a feature value is either a scalar or an array reference.
1101 # This is unhandy for the users. For them it would be better if they knew they would always get a scalar (or always an array).
1102 # Therefore we want to define attribute setters and getters that will do simple conversion in addition to just setting/getting the hybrid value.
1103 # We do this using meta() from Class::MOP::Class. The $feature attribute is defined as 'bare' because we will create the accessor methods ourselves.
1104 has $feature => (is => 'bare', default => '');
1105 # The setter will accept scalars, scalars with multivalues ('fem|masc'), array references and lists.
1106 $meta->add_method(qq/set_$feature/, sub
1107 {
1108 my $self = shift;
1109 my @values = @_;
1110 return $self->set($feature, @values);
1111 });
1112 # A specialized setter that sets the empty value: the clearer.
1113 $meta->add_method(qq/clear_$feature/, sub
1114 {
1115 my $self = shift;
1116 return $self->clear($feature);
1117 });
1118 # The getter will always return a scalar and multivalues will be serialized ('fem|masc').
1119 # The user can call get_list() if they want a list.
1120 $meta->add_method(qq/$feature/, sub
1121 {
1122 my $self = shift;
1123 return $self->get_joined($feature);
1124 });
1125 }
1126 }
1127 # The 'other' feature will have a standard setter/getter that will just take/return anything. Usually a hash reference.
1128 ###!!! In future we may want to limit the values of 'other' to be references to hashes of scalar-valued subfeatures.
1129 ###!!! Then we will probably define new methods for subfeatures and a serialization method. And the standard getter will be redefined to return strings only.
1130 has 'other' => (is => 'rw', default => '');
1131  
1132  
1133  
1134 #------------------------------------------------------------------------------
1135 # Static function. Returns the list of known features (in print order).
1136 #------------------------------------------------------------------------------
1137 sub known_features
1138 {
1139 return @features_in_print_order;
1140 }
1141  
1142  
1143  
1144 #------------------------------------------------------------------------------
1145 # Static function. Returns the list of features according to their priority
1146 # (used when enforcing permitted feature-value combinations during strict
1147 # encoding).
1148 #------------------------------------------------------------------------------
1149 sub priority_features
1150 {
1151 my @features = keys(%matrix);
1152 @features = sort {$matrix{$a}{priority} <=> $matrix{$b}{priority}} (@features);
1153 return @features;
1154 }
1155  
1156  
1157  
1158 #------------------------------------------------------------------------------
1159 # Static function. Returns the list of known values (in print order) of
1160 # a feature. Dies if asked about an unknown feature.
1161 #------------------------------------------------------------------------------
1162 sub known_values
1163 {
1164 my $feature = shift;
1165 if(exists($matrix{$feature}))
1166 {
1167 if(defined($matrix{$feature}{values}))
1168 {
1169 return @{$matrix{$feature}{values}};
1170 }
1171 else # features tagset and other do not have lists of values
1172 {
1173 return ();
1174 }
1175 }
1176 else
1177 {
1178 confess("Unknown feature $feature");
1179 }
1180 }
1181  
1182  
1183  
1184 #------------------------------------------------------------------------------
1185 # Static function. Tells whether a string is the name of a known feature.
1186 #------------------------------------------------------------------------------
1187 sub feature_valid
1188 {
1189 my $feature = shift;
1190 return exists($matrix{$feature}) ? 1 : 0;
1191 }
1192  
1193  
1194  
1195 #------------------------------------------------------------------------------
1196 # Static function. Tells for a given feature-value pair whether both the
1197 # feature and the value are known and valid. References to lists of valid
1198 # values are also valid. Does not die when the feature is not valid.
1199 #------------------------------------------------------------------------------
1200 sub value_valid
1201 {
1202 my $feature = shift;
1203 my $value = shift;
1204 # Avoid warnings if feature is not defined.
1205 if(!defined($feature))
1206 {
1207 return 0;
1208 }
1209 # Value of the 'other' feature can be anything.
1210 elsif($feature eq 'other')
1211 {
1212 return 1;
1213 }
1214 # For the 'tagset' feature, a regular expression is used instead of a list of values.
1215 elsif($feature eq 'tagset')
1216 {
1217 return $value =~ m/^([a-z]+::[a-z0-9]+)?$/;
1218 }
1219 # Other known features all have their lists of values (including the empty string).
1220 else
1221 {
1222 return 0 unless(feature_valid($feature));
1223 my @known_values = known_values($feature);
1224 # If the value is a list of values, each of them must be valid.
1225 # This method recognizes array references and serialized arrays using '|' as the separator.
1226 my $ref = ref($value);
1227 my @myarray;
1228 if($ref eq '' && $value =~ m/\|/)
1229 {
1230 @myarray = split(/\|/, $value);
1231 $value = \@myarray;
1232 $ref = ref($value);
1233 }
1234 if($ref eq 'ARRAY')
1235 {
1236 foreach my $svalue (@{$value})
1237 {
1238 # No nested arrays are expected.
1239 if(ref($svalue) ne '')
1240 {
1241 return 0;
1242 }
1243 else
1244 {
1245 return 0 unless(grep {$_ eq $svalue} (@known_values));
1246 }
1247 }
1248 # All values tested successfully.
1249 return 1;
1250 }
1251 # Single value.
1252 elsif($ref eq '')
1253 {
1254 return scalar(grep {$_ eq $value} (@known_values));
1255 }
1256 # No other references expected.
1257 else
1258 {
1259 return 0;
1260 }
1261 }
1262 }
1263  
1264  
1265  
1266 #------------------------------------------------------------------------------
1267 # This method ensures backwards compatibility of features and values that have
1268 # been renamed. If the feature-value pair is valid, the value is returned. If
1269 # the feature/value is not valid in the current version of Interset but it is
1270 # known to have existed in the past, the closest match is returned. If it is
1271 # unknown and no mapping is available, we return undef. This method does not
1272 # throw exceptions.
1273 #------------------------------------------------------------------------------
1274 my %fvbct =
1275 (
1276 'number' => { 'plu' => 'plur' }, # renamed in fall 2014
1277 'degree' => { 'comp' => 'cmp' }, # renamed in Interset 2.049, 2015-09-29
1278 'aspect' => { 'pro' => 'prosp' }, # renamed in UD v2, 2016-12-01
1279 'verbform' => { 'trans' => 'conv' }, # renamed in UD v2, 2016-12-01
1280 'polite' => { 'inf' => 'infm', # renamed in UD v2, 2016-12-01
1281 'pol' => 'form' }, # renamed in UD v2, 2016-12-01
1282 'abspolite' => { 'inf' => 'infm', # renamed in UD v2, 2016-12-01
1283 'pol' => 'form' }, # renamed in UD v2, 2016-12-01
1284 'datpolite' => { 'inf' => 'infm', # renamed in UD v2, 2016-12-01
1285 'pol' => 'form' }, # renamed in UD v2, 2016-12-01
1286 'ergpolite' => { 'inf' => 'infm', # renamed in UD v2, 2016-12-01
1287 'pol' => 'form' }, # renamed in UD v2, 2016-12-01
1288 'numtype' => { 'gen' => 'mult' }, # value removed in UD v2, 2016-12-01
1289 'foreign' => { 'foreign' => 'yes', # value renamed in Interset 3.005, 2017-07-08
1290 'fscript' => 'yes', # value removed in UD v2, 2016-12-01
1291 'tscript' => 'yes' }, # value removed in UD v2, 2016-12-01
1292 'poss' => { 'poss' => 'yes' }, # value renamed in Interset 3.005, 2017-07-08
1293 'reflex' => { 'reflex' => 'yes' }, # value renamed in Interset 3.005, 2017-07-08
1294 'abbr' => { 'abbr' => 'yes' }, # value renamed in Interset 3.005, 2017-07-08
1295 'hyph' => { 'hyph' => 'yes' }, # value renamed in Interset 3.005, 2017-07-08
1296 'typo' => { 'typo' => 'yes' }, # value renamed in Interset 3.005, 2017-07-08
1297 'tense' => { 'nar' => 'past' }, # value removed in UD v2, should use evident=nfh + past, 2017-07-11
1298 );
1299 sub _get_compatible_value
1300 {
1301 my $self = shift;
1302 my $feature = shift;
1303 my $value = shift;
1304 if(value_valid($feature, $value))
1305 {
1306 return $value;
1307 }
1308 else
1309 {
1310 if(exists($fvbct{$feature}{$value}))
1311 {
1312 return $fvbct{$feature}{$value};
1313 }
1314 # If we are here, the feature-value pair is unknown and we do not know where to map it to.
1315 return;
1316 }
1317 }
1318  
1319  
1320  
1321 #------------------------------------------------------------------------------
1322 # This method is called from several places in the set() method where it is
1323 # necessary to deal with unknown values of features. It may throw exceptions!
1324 # The method takes two arguments besides $self, $feature and $value. Only
1325 # single values are expected, no arrayrefs and no serialized lists (the set()
1326 # method takes care of lists before calling this). The method calls the
1327 # _get_compatible_value() method. It differs in that it throws an exception if
1328 # the value is invalid and cannot be replaced.
1329 #------------------------------------------------------------------------------
1330 sub _validate_value
1331 {
1332 my $self = shift;
1333 my $feature = shift;
1334 my $value = shift;
1335 my $value2 = $self->_get_compatible_value($feature, $value);
1336 if(defined($value2))
1337 {
1338 return $value2;
1339 }
1340 else
1341 {
1342 confess("Unknown value '$value' of feature '$feature'");
1343 }
1344 }
1345  
1346  
1347  
1348 #------------------------------------------------------------------------------
1349 # Named setters for each feature are nice but we also need a generic setter
1350 # that takes both the feature name and value.
1351 #------------------------------------------------------------------------------
1352 sub set
1353 {
1354 my $self = shift;
1355 my $feature = shift;
1356 if(!feature_valid($feature))
1357 {
1358 ###!!! Since there are old files with Interset-based data, we need backward compatibility.
1359 ###!!! In the future we may need a separate method.
1360 ###!!! Features that had existed and were removed.
1361 if($feature eq 'subpos')
1362 {
1363 print STDERR ("Ignoring deprecated Interset feature 'subpos'.\n");
1364 return;
1365 }
1366 ###!!! Features that were renamed.
1367 elsif($feature eq 'abspoliteness')
1368 {
1369 $feature = 'abspolite';
1370 }
1371 elsif($feature eq 'animateness')
1372 {
1373 $feature = 'animacy';
1374 }
1375 elsif($feature eq 'datpoliteness')
1376 {
1377 $feature = 'datpolite';
1378 }
1379 elsif($feature eq 'definiteness')
1380 {
1381 $feature = 'definite';
1382 }
1383 elsif($feature eq 'ergpoliteness')
1384 {
1385 $feature = 'ergpolite';
1386 }
1387 elsif($feature eq 'negativeness')
1388 {
1389 $feature = 'polarity';
1390 }
1391 elsif($feature eq 'politeness')
1392 {
1393 $feature = 'polite';
1394 }
1395 else
1396 {
1397 confess("Unknown feature '$feature'");
1398 }
1399 }
1400 my @values = grep {defined($_) && $_ ne ''} @_;
1401 # Undefined value means that we want to clear the feature.
1402 if(scalar(@values)==0)
1403 {
1404 return $self->clear($feature);
1405 }
1406 # The interpretation of the @values if different if the feature is 'other'.
1407 if($feature eq 'other')
1408 {
1409 $self->{$feature} = _duplicate_recursive($values[0]);
1410 return 1;
1411 }
1412 my @values1;
1413 my %values; # map values and do not add the same value twice
1414 foreach my $value (@values)
1415 {
1416 if(ref($value) eq 'ARRAY')
1417 {
1418 foreach my $subvalue (@{$value})
1419 {
1420 # No unlimited recursion. Referenced arrays are not supposed to contain subarrays.
1421 confess('Plain scalar expected') unless(ref($subvalue) eq '');
1422 $subvalue = $self->_validate_value($feature, $subvalue);
1423 push(@values1, $subvalue) unless($values{$subvalue});
1424 $values{$subvalue}++;
1425 }
1426 }
1427 elsif($value =~ m/\|/)
1428 {
1429 my @subvalues = split(/\|/, $value);
1430 foreach my $subvalue (@subvalues)
1431 {
1432 $subvalue = $self->_validate_value($feature, $subvalue);
1433 push(@values1, $subvalue) unless($values{$subvalue});
1434 $values{$subvalue}++;
1435 }
1436 }
1437 else
1438 {
1439 $value = $self->_validate_value($feature, $value);
1440 push(@values1, $value) unless($values{$value});
1441 $values{$value}++;
1442 }
1443 }
1444 # Current Interset convention is that multi-values are stored as array references.
1445 # The above copying solves three problems:
1446 # 1. multiple ways for the user to provide the values
1447 # 2. if the user provides array reference, the array will not be shared but copied
1448 # 3. repeated occurrence of one value will not be allowed
1449 my $value;
1450 if(scalar(@values1)>1)
1451 {
1452 $value = \@values1;
1453 }
1454 elsif(scalar(@values1)==1)
1455 {
1456 $value = $values1[0];
1457 }
1458 else
1459 {
1460 confess('Missing value');
1461 }
1462 $self->{$feature} = $value;
1463 }
1464  
1465  
1466  
1467 #------------------------------------------------------------------------------
1468 # Sets several features at once. Takes list of value assignments, i.e. an array
1469 # of an even number of elements (feature1, value1, feature2, value2...)
1470 # This is useful when defining decoders from physical tagsets. Typically, one
1471 # wants to define a table of assignments for each part of speech or input
1472 # feature:
1473 # 'CC' => ['pos' => 'conj', 'conjtype' => 'coor']
1474 #------------------------------------------------------------------------------
1475 sub add
1476 {
1477 my $self = shift;
1478 my @assignments = @_;
1479 for(my $i = 0; $i<=$#assignments; $i += 2)
1480 {
1481 my $feature = $assignments[$i];
1482 my $value = $assignments[$i+1];
1483 # Support for hashes of subfeatures in 'other'.
1484 # We want to merge the two hashes, not to replace the current hash by the new one.
1485 if($feature eq 'other' && ref($value) eq 'HASH')
1486 {
1487 my $current = $self->other();
1488 if(ref($current) eq 'HASH')
1489 {
1490 foreach my $subfeature (keys(%{$value}))
1491 {
1492 # Do not concern about deep copying now. It will be taken care of in set().
1493 $current->{$subfeature} = $value->{$subfeature};
1494 }
1495 $value = $current;
1496 }
1497 }
1498 $self->set($feature, $value);
1499 }
1500 }
1501  
1502  
1503  
1504 #------------------------------------------------------------------------------
1505 # Takes a reference to a hash of features and their values. Sets the values of
1506 # the features in $self. Unknown features and values are ignored. Known
1507 # features that are not set in the hash will be (re-)set to empty values in
1508 # $self.
1509 #------------------------------------------------------------------------------
1510 sub set_hash
1511 {
1512 my $self = shift;
1513 my $fs = shift;
1514 foreach my $feature ($self->known_features())
1515 {
1516 my $value = defined($fs->{$feature}) ? $fs->{$feature} : '';
1517 $self->set($feature, $value);
1518 }
1519 }
1520  
1521  
1522  
1523 #------------------------------------------------------------------------------
1524 # Takes a reference to a hash of features and their values. Sets the values of
1525 # the features in $self; does not touch values of features not mentioned in the
1526 # hash.
1527 #------------------------------------------------------------------------------
1528 sub merge_hash_hard
1529 {
1530 my $self = shift;
1531 my $fs = shift;
1532 foreach my $feature ($self->known_features())
1533 {
1534 if(exists($fs->{$feature}) && defined($fs->{$feature}) && $fs->{$feature} ne '')
1535 {
1536 $self->set($feature, $fs->{$feature});
1537 }
1538 }
1539 }
1540  
1541  
1542  
1543 #------------------------------------------------------------------------------
1544 # Takes a reference to a hash of features and their values. Sets the values of
1545 # the features in $self; does not touch values of features not mentioned in the
1546 # hash.
1547 #------------------------------------------------------------------------------
1548 sub merge_hash_soft
1549 {
1550 my $self = shift;
1551 my $fs = shift;
1552 foreach my $feature ($self->known_features())
1553 {
1554 if(exists($fs->{$feature}) && defined($fs->{$feature}) && $fs->{$feature} ne '')
1555 {
1556 my @current_values = $self->get_list($feature);
1557 if(scalar(@current_values)>1 || scalar(@current_values)==1 && $current_values[0] ne '')
1558 {
1559 # The new value(s) may be given in multiple ways and the set() method has mechanisms to distinguish them.
1560 # Let's normalize the input values by running them through set() first.
1561 $self->set($feature, $fs->{$feature});
1562 my @new_values = $self->get_list($feature);
1563 # Merge the two lists of values.
1564 my %map;
1565 my @values = ();
1566 foreach my $value (@current_values, @new_values)
1567 {
1568 push(@values, $value) unless($map{$value});
1569 $map{$value}++;
1570 }
1571 $self->set($feature, \@values);
1572 }
1573 else # no previous value found
1574 {
1575 $self->set($feature, $fs->{$feature});
1576 }
1577 }
1578 }
1579 }
1580  
1581  
1582  
1583 #------------------------------------------------------------------------------
1584 # Generic setter that clears the value of a feature, i.e. removes the feature.
1585 #------------------------------------------------------------------------------
1586 sub clear
1587 {
1588 my $self = shift;
1589 my @features = grep {defined($_)} @_;
1590 confess() if(!@features);
1591 foreach my $feature (@features)
1592 {
1593 delete($self->{$feature});
1594 }
1595 }
1596  
1597  
1598  
1599 #------------------------------------------------------------------------------
1600 # Returns a list of names of (known) features whose values are not empty.
1601 #------------------------------------------------------------------------------
1602 sub get_nonempty_features
1603 {
1604 my $self = shift;
1605 return grep {defined($self->{$_}) && $self->{$_} ne ''} known_features();
1606 }
1607  
1608  
1609  
1610 #------------------------------------------------------------------------------
1611 # get() is a generic feature value getter.
1612 #------------------------------------------------------------------------------
1613 sub get
1614 {
1615 my $self = shift;
1616 my $feature = shift;
1617 confess() if(!defined($feature));
1618 return defined($self->{$feature}) ? _duplicate_recursive($self->{$feature}) : '';
1619 }
1620  
1621  
1622  
1623 #------------------------------------------------------------------------------
1624 # Similar to get but always returns scalar. If there is an array of disjoint
1625 # values, it does not pick just one. Instead, it sorts all values and joins
1626 # them using the vertical bar. Example: 'fem|masc'.
1627 #------------------------------------------------------------------------------
1628 sub get_joined
1629 {
1630 my $self = shift;
1631 my $feature = shift;
1632 return array_to_scalar_value($self->get($feature));
1633 }
1634  
1635  
1636  
1637 #------------------------------------------------------------------------------
1638 # Similar to get but always returns list of values. If there is an array of
1639 # disjoint values, this is the list. If there is a single value (empty or not),
1640 # this value is the only member of the list.
1641 #------------------------------------------------------------------------------
1642 sub get_list
1643 {
1644 my $self = shift;
1645 my $feature = shift;
1646 my $value = $self->get($feature);
1647 my @list;
1648 if(ref($value) eq 'ARRAY')
1649 {
1650 @list = @{$value};
1651 }
1652 else
1653 {
1654 @list = ($value);
1655 }
1656 return @list;
1657 }
1658  
1659  
1660  
1661 #------------------------------------------------------------------------------
1662 # Creates a hash of all features and their values. Returns a reference to the
1663 # hash.
1664 #------------------------------------------------------------------------------
1665 sub get_hash
1666 {
1667 my $self = shift;
1668 my %fs;
1669 foreach my $feature ($self->known_features())
1670 {
1671 my $value = $self->get($feature);
1672 if(defined($value) && $value ne '')
1673 {
1674 $fs{$feature} = $value;
1675 }
1676 }
1677 return \%fs;
1678 }
1679  
1680  
1681  
1682 #------------------------------------------------------------------------------
1683 # Tests tagset + other features.
1684 #------------------------------------------------------------------------------
1685 sub get_other_for_tagset
1686 {
1687 my $self = shift;
1688 my $tagset = shift;
1689 # We normally set the tagset feature even if there is no other feature.
1690 # However, it is not required that it is defined. (And sometimes we clear the tagset, e.g. during certain tests.)
1691 # Calling this method without defining $tagset makes much less sense but it is not fatal either.
1692 if(defined($self->tagset()) && defined($tagset) && $self->tagset() eq $tagset && defined($self->other()))
1693 {
1694 return _duplicate_recursive($self->other());
1695 }
1696 else
1697 {
1698 return '';
1699 }
1700 }
1701  
1702  
1703  
1704 #------------------------------------------------------------------------------
1705 # If other is a reference to a hash of subfeatures, this method adds (replaces)
1706 # a value of one subfeature. If other is undefined, empty or non-hash, this
1707 # method replaces the current value of other by a reference to a new hash, then
1708 # adds the subfeature.
1709 #------------------------------------------------------------------------------
1710 sub set_other_subfeature
1711 {
1712 my $self = shift;
1713 my $subfeature = shift;
1714 my $value = shift;
1715 return unless(defined($subfeature) && defined($value) && $subfeature ne '' && $value ne '');
1716 # This will just return the hash reference (provided it is a hashref), no deep copying takes place.
1717 my $other = $self->other();
1718 if(!defined($other) || ref($other) ne 'HASH')
1719 {
1720 my %other;
1721 $other = \%other;
1722 # This will just set the hash reference as the value, no deep copying takes place.
1723 # (Unlike $self->set('other', $other), which would deep-copy the hash.)
1724 $self->set_other($other);
1725 }
1726 $other->{$subfeature} = $value;
1727 }
1728  
1729  
1730  
1731 #------------------------------------------------------------------------------
1732 # If tagset matches and if other is a hash, this method returns the value of
1733 # a subfeature, i.e. a value stored in the hash under a particular key.
1734 #------------------------------------------------------------------------------
1735 sub get_other_subfeature
1736 {
1737 my $self = shift;
1738 my $tagset = shift;
1739 my $subfeature = shift;
1740 my $other = $self->other();
1741 if(defined($self->tagset()) && $tagset eq $self->tagset() && defined($other) && ref($other) eq 'HASH' && exists($other->{$subfeature}))
1742 {
1743 # This method was created because of simple hashes whose values are strings.
1744 # But there is no guarantee that the value is not a reference and thus we must return a deep copy.
1745 return _duplicate_recursive($other->{$subfeature});
1746 }
1747 return '';
1748 }
1749  
1750  
1751  
1752 #------------------------------------------------------------------------------
1753 # Tests tagset + other features. Instead of returning a deep copy of the
1754 # possibly structured value (see get_other_for_tagset()), this method only
1755 # queries whether the other feature has/contains one particular scalar value.
1756 #------------------------------------------------------------------------------
1757 sub is_other
1758 {
1759 my $self = shift;
1760 my $tagset = shift;
1761 my $arg1 = shift;
1762 my $arg2 = shift;
1763 $arg1 = '' if(!defined($arg1));
1764 $arg2 = '' if(!defined($arg2));
1765 if($self->tagset() eq $tagset)
1766 {
1767 my $other = $self->other();
1768 $other = '' if(!defined($other));
1769 my $ref = ref($other);
1770 if($ref eq '' && $other eq $arg1)
1771 {
1772 return 1;
1773 }
1774 elsif($ref eq 'ARRAY' && any { $_ eq $arg1 } (@{$other}))
1775 {
1776 return 1;
1777 }
1778 elsif($ref eq 'HASH' && exists($other->{$arg1}))
1779 {
1780 my $value = $other->{$arg1};
1781 $value = '' if(!defined($value));
1782 if($value eq $arg2)
1783 {
1784 return 1;
1785 }
1786 else
1787 {
1788 return 0;
1789 }
1790 }
1791 else
1792 {
1793 return 0;
1794 }
1795 }
1796 else
1797 {
1798 return 0;
1799 }
1800 }
1801  
1802  
1803  
1804 #------------------------------------------------------------------------------
1805 # Tests whether values of a feature contain one particular value. Useful if we
1806 # know that a feature may have array of values.
1807 #------------------------------------------------------------------------------
1808 sub contains
1809 {
1810 my $self = shift;
1811 my $feature = shift;
1812 my $value = shift;
1813 return any { $_ eq $value } ($self->get_list($feature));
1814 }
1815  
1816  
1817  
1818 #==============================================================================
1819 # Methods for the universal part-of-speech tags and the universal features as
1820 # defined in October 2014 for the Universal Dependencies
1821 # (http://universaldependencies.github.io/docs/).
1822 #==============================================================================
1823 use Lingua::Interset::Tagset::MUL::Upos;
1824 has '_upos_driver' => ( isa => 'Lingua::Interset::Tagset::MUL::Upos', is => 'ro', builder => '_create_upos_driver', lazy => 1 );
1825 sub _create_upos_driver { return new Lingua::Interset::Tagset::MUL::Upos; }
1826 has '_map_from_uf' => ( isa => 'HashRef', is => 'ro', builder => '_create_map_from_uf', lazy => 1 );
1827 sub _create_map_from_uf
1828 {
1829 # A reverse hash will help translate Universal Features to Interset features.
1830 my %map_from_uf;
1831 foreach my $feature (keys(%matrix))
1832 {
1833 if(defined($matrix{$feature}{uname}) && !defined($map_from_uf{$matrix{$feature}{uname}}))
1834 {
1835 $map_from_uf{$matrix{$feature}{uname}} = $feature;
1836 }
1837 }
1838 # Backward compatibility with UD v1:
1839 $map_from_uf{Negative} = 'polarity';
1840 return \%map_from_uf;
1841 }
1842  
1843  
1844  
1845 #------------------------------------------------------------------------------
1846 # Sets feature values according to a universal part-of-speech tag as defined in
1847 # 2014 for the Universal Dependencies
1848 # (http://universaldependencies.github.io/docs/).
1849 #------------------------------------------------------------------------------
1850 sub set_upos
1851 {
1852 my $self = shift;
1853 my $upos = shift;
1854 # We will use the mul::upos driver to decode the tag. However, the driver
1855 # may destroy the current value of prontype: it may replace it by a generic
1856 # value 'prn'. Save the current prontype if the new one is 'prn'.
1857 my $old_prontype = $self->prontype();
1858 my $driver = $self->_upos_driver();
1859 $driver->decode_and_merge_hard($upos, $self);
1860 my $new_prontype = $self->prontype();
1861 if($new_prontype eq 'prn' && $old_prontype ne '' && $old_prontype ne 'prn')
1862 {
1863 $self->set('prontype', $old_prontype);
1864 }
1865 }
1866  
1867  
1868  
1869 #------------------------------------------------------------------------------
1870 # Returns the universal part-of-speech tag as defined in 2014 for the Universal
1871 # Dependencies (http://universaldependencies.github.io/docs/).
1872 #------------------------------------------------------------------------------
1873 sub get_upos
1874 {
1875 my $self = shift;
1876 my $driver = $self->_upos_driver();
1877 return $driver->encode($self);
1878 }
1879 sub upos { return get_upos(@_); }
1880  
1881  
1882  
1883 #------------------------------------------------------------------------------
1884 # Takes a list of feature=value pairs in the format prescribed by the
1885 # Universal Dependencies (http://universaldependencies.github.io/docs/), i.e.
1886 # identifiers are capitalized, some features are renamed and all pairs are
1887 # ordered alphabetically. Sets our feature values accordingly.
1888 #------------------------------------------------------------------------------
1889 sub add_ufeatures
1890 {
1891 my $self = shift;
1892 my @pairs = @_;
1893 my $map = $self->_map_from_uf();
1894 foreach my $pair (@pairs)
1895 {
1896 if($pair =~ m/^(.+)=(.+)$/)
1897 {
1898 my $ufeature = $1;
1899 my $uvalue = $2;
1900 my $feature = $map->{$ufeature};
1901 if(defined($feature))
1902 {
1903 my $value = lc($uvalue);
1904 # Universal Features use comma to join multi-values but we use the vertical bar.
1905 $value =~ s/,/\|/g;
1906 # Backward compatibility: if this is an old value that has been renamed, get the new value.
1907 # If the value has never been known, we will get back an undefined value.
1908 $value = $self->_get_compatible_value($feature, $value);
1909 if(defined($feature) && defined($value))
1910 {
1911 $self->set($feature, $value);
1912 }
1913 else # other
1914 {
1915 $self->set_tagset('mul::uposf');
1916 $self->set_other_subfeature($ufeature, $uvalue);
1917 }
1918 }
1919 else # other
1920 {
1921 $self->set_tagset('mul::uposf');
1922 $self->set_other_subfeature($ufeature, $uvalue);
1923 }
1924 }
1925 }
1926 }
1927  
1928  
1929  
1930 #------------------------------------------------------------------------------
1931 # Returns the list of feature=value pairs in the format prescribed by the
1932 # Universal Dependencies (http://universaldependencies.github.io/docs/), i.e.
1933 # identifiers are capitalized, some features are renamed and all pairs are
1934 # ordered alphabetically.
1935 #------------------------------------------------------------------------------
1936 sub get_ufeatures
1937 {
1938 my $self = shift;
1939 my $fh = $self->get_hash();
1940 # The Universal Features must be sorted alphabetically, that is, lowercase letters equal to uppercase
1941 # and 'Number' < 'NumType'.
1942 my @features = sort {lc($matrix{$a}{uname}) cmp lc($matrix{$b}{uname})} (grep {defined($matrix{$_}{uname})} (keys(%{$fh})));
1943 my @pairs;
1944 foreach my $feature (@features)
1945 {
1946 my $uname = $matrix{$feature}{uname};
1947 my @values = grep {defined($_) && $_ ne ''} $self->get_list($feature);
1948 next unless(@values);
1949 # Sort multivalues alphabetically and capitalize them.
1950 @values = sort {lc($a) cmp lc($b)} (map {my $x = $_; $x =~ s/^(.)/\u$1/; $x} (@values));
1951 # Join values using comma (unlike in get_joined(), with Universal Features we cannot use the vertical bar).
1952 my $value = join(',', @values);
1953 my $pair = "$uname=$value";
1954 # Some values of some features became obsolete because the distinction was moved to the POS tag level.
1955 next if($pair =~ m/^(PronType=Prn|ConjType=(Coor|Sub)|NounType=(Com|Prop)|VerbType=Aux|Variant=[0-9])$/);
1956 push(@pairs, $pair);
1957 }
1958 # Check whether there are language-specific features or values that Interset
1959 # does not know and that thus had to be stored in the 'other' feature.
1960 my $other = $self->get_other_for_tagset ('mul::uposf');
1961 if(defined($other) && ref($other) eq 'HASH')
1962 {
1963 my @ufeatures = keys(%{$other});
1964 my @upairs = map {"$_=$other->{$_}"} (@ufeatures);
1965 push(@pairs, @upairs);
1966 @pairs = sort {lc($a) cmp lc($b)} (@pairs);
1967 }
1968 return @pairs;
1969 }
1970  
1971  
1972  
1973 #------------------------------------------------------------------------------
1974 # Tests multiple Interset features simultaneously. Input is a list of feature-
1975 # value pairs, return value is 1 if the node matches all these values. This
1976 # function is an abbreviation for a series of get_iset() calls in an if
1977 # statement:
1978 #
1979 # if($node->match_iset('pos' => 'noun', 'gender' => 'masc')) { ... }
1980 #------------------------------------------------------------------------------
1981 sub matches
1982 {
1983 my $self = shift;
1984 my @req = @_;
1985 for ( my $i = 0; $i <= $#req; $i += 2 )
1986 {
1987 my $feature = $req[$i];
1988 my $expected = $req[$i+1];
1989 confess("Undefined feature") unless ($feature);
1990 my $value = $self->get_joined($feature);
1991 my $comp =
1992 $expected =~ s/^\!\~// ? 'nr' :
1993 $expected =~ s/^\!// ? 'ne' :
1994 $expected =~ s/^\~// ? 're' : 'eq';
1995 if (
1996 $comp eq 'eq' && $value ne $expected ||
1997 $comp eq 'ne' && $value eq $expected ||
1998 $comp eq 're' && $value !~ m/$expected/ ||
1999 $comp eq 'nr' && $value =~ m/$expected/
2000 )
2001 {
2002 return 0;
2003 }
2004 }
2005 return 1;
2006 }
2007  
2008  
2009  
2010 #------------------------------------------------------------------------------
2011 # Generates text from contents of feature structure so it can be printed.
2012 #------------------------------------------------------------------------------
2013 sub as_string
2014 {
2015 my $self = shift;
2016 my @features = $self->get_nonempty_features();
2017 my @assignments = map
2018 {
2019 my $f = $_;
2020 my $v;
2021 if($f eq 'other')
2022 {
2023 $v = structure_to_string($self->{$f});
2024 }
2025 else
2026 {
2027 $v = '"'.$self->get_joined($f).'"';
2028 }
2029 "$f=$v";
2030 }
2031 (@features);
2032 return "[".join(', ', @assignments)."]";
2033 }
2034  
2035  
2036  
2037 #------------------------------------------------------------------------------
2038 # Generates text from contents of feature structure in the form used in the
2039 # FEATS column of the CoNLL-X file format. The tagset and other features will
2040 # be omitted. Multivalues will be separated by a comma because the vertical bar
2041 # separates features.
2042 #------------------------------------------------------------------------------
2043 sub as_string_conllx
2044 {
2045 my $self = shift;
2046 my @features = grep {$_ ne 'tagset' && $_ ne 'other' && defined($self->{$_}) && $self->{$_} ne ''} known_features();
2047 my @assignments = map
2048 {
2049 my $f = $_;
2050 my $v = $self->get_joined($f);
2051 $v =~ s/\|/,/g;
2052 "$f=$v";
2053 }
2054 (@features);
2055 return (scalar(@assignments) > 0) ? join('|', @assignments) : '_';
2056 }
2057  
2058  
2059  
2060 #------------------------------------------------------------------------------
2061 # Recursively converts a structure to string describing a Perl constant.
2062 # Useful for using eval.
2063 #------------------------------------------------------------------------------
2064 sub structure_to_string
2065 {
2066 my $source = shift;
2067 my $string;
2068 my $ref = ref($source);
2069 if($ref eq 'ARRAY')
2070 {
2071 $string = "[".join(", ", map{structure_to_string($_)}(@{$source}))."]";
2072 }
2073 elsif($ref eq 'HASH')
2074 {
2075 $string = "{".join(", ", map{structure_to_string($_)." => ".structure_to_string($source->{$_})}(keys(%{$source})))."}";
2076 }
2077 else
2078 {
2079 $string = $source;
2080 $string = '' if(!defined($string));
2081 $string =~ s/([\\"\$\@])/\\$1/g; # "
2082 $string = "\"$string\"";
2083 }
2084 return $string;
2085 }
2086  
2087  
2088  
2089 ###############################################################################
2090 # Shortcuts for some frequent tests people want to do against Interset.
2091 ###############################################################################
2092  
2093 #------------------------------------------------------------------------------
2094 sub is_noun {my $self = shift; return $self->contains('pos', 'noun');}
2095  
2096 #------------------------------------------------------------------------------
2097 sub is_abbreviation {my $self = shift; return $self->abbr() eq 'yes';}
2098  
2099 #------------------------------------------------------------------------------
2100 sub is_abessive {my $self = shift; return $self->contains('case', 'abe');}
2101  
2102 #------------------------------------------------------------------------------
2103 sub is_ablative {my $self = shift; return $self->contains('case', 'abl');}
2104  
2105 #------------------------------------------------------------------------------
2106 sub is_absolute_superlative {my $self = shift; return $self->contains('degree', 'abs');}
2107  
2108 #------------------------------------------------------------------------------
2109 sub is_absolutive {my $self = shift; return $self->contains('case', 'abs');}
2110  
2111 #------------------------------------------------------------------------------
2112 sub is_accusative {my $self = shift; return $self->contains('case', 'acc');}
2113  
2114 #------------------------------------------------------------------------------
2115 sub is_active {my $self = shift; return $self->contains('voice', 'act');}
2116  
2117 #------------------------------------------------------------------------------
2118 sub is_additive {my $self = shift; return $self->contains('case', 'add');}
2119  
2120 #------------------------------------------------------------------------------
2121 sub is_adessive {my $self = shift; return $self->contains('case', 'ade');}
2122  
2123 #------------------------------------------------------------------------------
2124 sub is_adjective {my $self = shift; return $self->contains('pos', 'adj');}
2125  
2126 #------------------------------------------------------------------------------
2127 sub is_admirative {my $self = shift; return $self->contains('mood', 'adm');}
2128  
2129 #------------------------------------------------------------------------------
2130 sub is_adposition {my $self = shift; return $self->contains('pos', 'adp');}
2131  
2132 #------------------------------------------------------------------------------
2133 sub is_adverb {my $self = shift; return $self->contains('pos', 'adv');}
2134  
2135 #------------------------------------------------------------------------------
2136 sub is_affirmative {my $self = shift; return $self->contains('negativeness', 'pos');}
2137  
2138 #------------------------------------------------------------------------------
2139 sub is_allative {my $self = shift; return $self->contains('case', 'all');}
2140  
2141 #------------------------------------------------------------------------------
2142 sub is_animate {my $self = shift; return $self->contains('animacy', 'anim');}
2143  
2144 #------------------------------------------------------------------------------
2145 sub is_antipassive {my $self = shift; return $self->contains('voice', 'antip');}
2146  
2147 #------------------------------------------------------------------------------
2148 sub is_aorist {my $self = shift; return $self->contains('tense', 'aor');}
2149  
2150 #------------------------------------------------------------------------------
2151 sub is_archaic {my $self = shift; return $self->contains('style', 'arch');}
2152  
2153 #------------------------------------------------------------------------------
2154 sub is_article {my $self = shift; return $self->contains('prontype', 'art');}
2155  
2156 #------------------------------------------------------------------------------
2157 sub is_associative {my $self = shift; return $self->contains('case', 'com');}
2158  
2159 #------------------------------------------------------------------------------
2160 sub is_augmentative {my $self = shift; return $self->contains('degree', 'aug');}
2161  
2162 #------------------------------------------------------------------------------
2163 sub is_auxiliary {my $self = shift; return $self->contains('verbtype', 'aux');}
2164  
2165 #------------------------------------------------------------------------------
2166 sub is_benefactive {my $self = shift; return $self->contains('case', 'ben');}
2167  
2168 #------------------------------------------------------------------------------
2169 sub is_cardinal {my $self = shift; return $self->contains('numtype', 'card');}
2170  
2171 #------------------------------------------------------------------------------
2172 sub is_colloquial {my $self = shift; return $self->contains('style', 'coll');}
2173  
2174 #------------------------------------------------------------------------------
2175 sub is_comitative {my $self = shift; return $self->contains('case', 'com');}
2176  
2177 #------------------------------------------------------------------------------
2178 sub is_common_gender {my $self = shift; return $self->contains('gender', 'com');}
2179  
2180 #------------------------------------------------------------------------------
2181 sub is_comparative {my $self = shift; return $self->contains('degree', 'cmp') || $self->contains('case', 'cmp');}
2182  
2183 #------------------------------------------------------------------------------
2184 sub is_conditional {my $self = shift; return $self->contains('mood', 'cnd');}
2185  
2186 #------------------------------------------------------------------------------
2187 sub is_conjunction {my $self = shift; return $self->contains('pos', 'conj');}
2188  
2189 #------------------------------------------------------------------------------
2190 sub is_conjunctive {my $self = shift; return $self->contains('mood', 'sub');}
2191  
2192 #------------------------------------------------------------------------------
2193 sub is_construct {my $self = shift; return $self->contains('definite', 'cons');}
2194  
2195 #------------------------------------------------------------------------------
2196 sub is_converb {my $self = shift; return $self->contains('verbform', 'conv');}
2197  
2198 #------------------------------------------------------------------------------
2199 sub is_coordinator {my $self = shift; return $self->is_conjunction() && $self->conjtype() eq 'coor';}
2200  
2201 #------------------------------------------------------------------------------
2202 sub is_count_plural {my $self = shift; return $self->contains('number', 'count');}
2203  
2204 #------------------------------------------------------------------------------
2205 sub is_dative {my $self = shift; return $self->contains('case', 'dat');}
2206  
2207 #------------------------------------------------------------------------------
2208 sub is_definite {my $self = shift; return $self->contains('definite', 'def');}
2209  
2210 #------------------------------------------------------------------------------
2211 sub is_delative {my $self = shift; return $self->contains('case', 'del');}
2212  
2213 #------------------------------------------------------------------------------
2214 sub is_demonstrative {my $self = shift; return $self->contains('prontype', 'dem');}
2215  
2216 #------------------------------------------------------------------------------
2217 sub is_desiderative {my $self = shift; return $self->contains('mood', 'des');}
2218  
2219 #------------------------------------------------------------------------------
2220 sub is_destinative {my $self = shift; return $self->contains('case', 'ben');}
2221  
2222 #------------------------------------------------------------------------------
2223 sub is_determiner {my $self = shift; return $self->is_pronominal() && $self->is_adjective();}
2224  
2225 #------------------------------------------------------------------------------
2226 sub is_diminutive {my $self = shift; return $self->contains('degree', 'dim');}
2227  
2228 #------------------------------------------------------------------------------
2229 sub is_direct_voice {my $self = shift; return $self->contains('voice', 'dir');}
2230  
2231 #------------------------------------------------------------------------------
2232 sub is_distributive {my $self = shift; return $self->contains('case', 'dis');}
2233  
2234 #------------------------------------------------------------------------------
2235 sub is_dual {my $self = shift; return $self->contains('number', 'dual');}
2236  
2237 #------------------------------------------------------------------------------
2238 sub is_elative {my $self = shift; return $self->contains('case', 'ela');}
2239  
2240 #------------------------------------------------------------------------------
2241 sub is_elevating {my $self = shift; return $self->contains('polite', 'elev');}
2242  
2243 #------------------------------------------------------------------------------
2244 sub is_equative {my $self = shift; return $self->contains('degree', 'equ') || $self->contains('case', 'equ');}
2245  
2246 #------------------------------------------------------------------------------
2247 sub is_ergative {my $self = shift; return $self->contains('case', 'erg');}
2248  
2249 #------------------------------------------------------------------------------
2250 sub is_essive {my $self = shift; return $self->contains('case', 'ess');}
2251  
2252 #------------------------------------------------------------------------------
2253 sub is_exclamative {my $self = shift; return $self->contains('prontype', 'exc');}
2254  
2255 #------------------------------------------------------------------------------
2256 sub is_exclusive {my $self = shift; return $self->contains('clusivity', 'ex');}
2257  
2258 #------------------------------------------------------------------------------
2259 sub is_factive {my $self = shift; return $self->contains('case', 'tra');}
2260  
2261 #------------------------------------------------------------------------------
2262 sub is_feminine {my $self = shift; return $self->contains('gender', 'fem');}
2263  
2264 #------------------------------------------------------------------------------
2265 sub is_finite_verb {my $self = shift; return $self->contains('verbform', 'fin');}
2266  
2267 #------------------------------------------------------------------------------
2268 sub is_first_hand {my $self = shift; return $self->contains('evident', 'fh');}
2269  
2270 #------------------------------------------------------------------------------
2271 sub is_first_person {my $self = shift; return $self->contains('person', '1');}
2272  
2273 #------------------------------------------------------------------------------
2274 sub is_foreign {my $self = shift; return $self->foreign() eq 'yes';}
2275  
2276 #------------------------------------------------------------------------------
2277 sub is_formal {my $self = shift; return $self->contains('polite', 'form');}
2278  
2279 #------------------------------------------------------------------------------
2280 sub is_fourth_person {my $self = shift; return $self->contains('person', '4');}
2281  
2282 #------------------------------------------------------------------------------
2283 sub is_future {my $self = shift; return $self->contains('tense', 'fut');}
2284  
2285 #------------------------------------------------------------------------------
2286 sub is_genitive {my $self = shift; return $self->contains('case', 'gen');}
2287  
2288 #------------------------------------------------------------------------------
2289 sub is_gerund {my $self = shift; return $self->contains('verbform', 'ger');}
2290  
2291 #------------------------------------------------------------------------------
2292 sub is_gerundive {my $self = shift; return $self->contains('verbform', 'gdv');}
2293  
2294 #------------------------------------------------------------------------------
2295 sub is_greater_paucal {my $self = shift; return $self->contains('number', 'grpa');}
2296  
2297 #------------------------------------------------------------------------------
2298 sub is_greater_plural {my $self = shift; return $self->contains('number', 'grpl');}
2299  
2300 #------------------------------------------------------------------------------
2301 sub is_habitual {my $self = shift; return $self->contains('aspect', 'hab');}
2302  
2303 #------------------------------------------------------------------------------
2304 sub is_human {my $self = shift; return $self->contains('animacy', 'hum');}
2305  
2306 #------------------------------------------------------------------------------
2307 sub is_humbling {my $self = shift; return $self->contains('polite', 'humb');}
2308  
2309 #------------------------------------------------------------------------------
2310 sub is_hyph {my $self = shift; return $self->hyph() eq 'yes';}
2311  
2312 #------------------------------------------------------------------------------
2313 sub is_illative {my $self = shift; return $self->contains('case', 'ill');}
2314  
2315 #------------------------------------------------------------------------------
2316 sub is_imperative {my $self = shift; return $self->contains('mood', 'imp');}
2317  
2318 #------------------------------------------------------------------------------
2319 sub is_imperfect {my $self = shift; return $self->contains('tense', 'imp') || $self->contains('aspect', 'imp');}
2320  
2321 #------------------------------------------------------------------------------
2322 sub is_impersonal {my $self = shift; return $self->contains('person', '0');}
2323  
2324 #------------------------------------------------------------------------------
2325 sub is_inanimate {my $self = shift; return $self->contains('animacy', 'inan');}
2326  
2327 #------------------------------------------------------------------------------
2328 sub is_inclusive {my $self = shift; return $self->contains('clusivity', 'in');}
2329  
2330 #------------------------------------------------------------------------------
2331 sub is_indefinite {my $self = shift; return $self->contains('prontype', 'ind') || $self->contains('definite', 'ind');}
2332  
2333 #------------------------------------------------------------------------------
2334 sub is_indicative {my $self = shift; return $self->contains('mood', 'ind');}
2335  
2336 #------------------------------------------------------------------------------
2337 sub is_inessive {my $self = shift; return $self->contains('case', 'ine');}
2338  
2339 #------------------------------------------------------------------------------
2340 sub is_infinitive {my $self = shift; return $self->contains('verbform', 'inf');}
2341  
2342 #------------------------------------------------------------------------------
2343 sub is_informal {my $self = shift; return $self->contains('polite', 'infm');}
2344  
2345 #------------------------------------------------------------------------------
2346 sub is_instructive {my $self = shift; return $self->contains('case', 'ins');}
2347  
2348 #------------------------------------------------------------------------------
2349 sub is_instrumental {my $self = shift; return $self->contains('case', 'ins');}
2350  
2351 #------------------------------------------------------------------------------
2352 sub is_interjection {my $self = shift; $self->contains('pos', 'int');}
2353  
2354 #------------------------------------------------------------------------------
2355 sub is_interrogative {my $self = shift; $self->contains('prontype', 'int');}
2356  
2357 #------------------------------------------------------------------------------
2358 sub is_intransitive {my $self = shift; return $self->contains('subcat', 'intr');}
2359  
2360 #------------------------------------------------------------------------------
2361 sub is_inverse_number {my $self = shift; return $self->contains('number', 'inv');}
2362  
2363 #------------------------------------------------------------------------------
2364 sub is_inverse_voice {my $self = shift; return $self->contains('voice', 'inv');}
2365  
2366 #------------------------------------------------------------------------------
2367 sub is_iterative {my $self = shift; return $self->contains('aspect', 'iter');}
2368  
2369 #------------------------------------------------------------------------------
2370 sub is_jussive {my $self = shift; return $self->contains('mood', 'jus');}
2371  
2372 #------------------------------------------------------------------------------
2373 sub is_lative {my $self = shift; return $self->contains('case', 'lat');}
2374  
2375 #------------------------------------------------------------------------------
2376 sub is_locative {my $self = shift; return $self->contains('case', 'loc');}
2377  
2378 #------------------------------------------------------------------------------
2379 sub is_masculine {my $self = shift; return $self->contains('gender', 'masc');}
2380  
2381 #------------------------------------------------------------------------------
2382 sub is_mediopassive {my $self = shift; return $self->is_middle_voice() && $self->is_passive();}
2383  
2384 #------------------------------------------------------------------------------
2385 sub is_middle_voice {my $self = shift; return $self->contains('voice', 'mid');}
2386  
2387 #------------------------------------------------------------------------------
2388 sub is_modal {my $self = shift; return $self->contains('verbtype', 'mod');}
2389  
2390 #------------------------------------------------------------------------------
2391 sub is_motivative {my $self = shift; return $self->contains('case', 'cau');}
2392  
2393 #------------------------------------------------------------------------------
2394 sub is_multiplicative {my $self = shift; return $self->contains('numtype', 'mult');}
2395  
2396 #------------------------------------------------------------------------------
2397 sub is_narrative {my $self = shift; return $self->contains('tense', 'past') && $self->contains('evident', 'nfh');}
2398  
2399 #------------------------------------------------------------------------------
2400 sub is_necessitative {my $self = shift; return $self->contains('mood', 'nec');}
2401  
2402 #------------------------------------------------------------------------------
2403 sub is_negative {my $self = shift; return $self->contains('negativeness', 'neg') || $self->prontype() eq 'neg';} # prontype: don't use contains() here, we don't want true on 'ind|tot|neg'
2404  
2405 #------------------------------------------------------------------------------
2406 sub is_nominative {my $self = shift; return $self->contains('case', 'nom');}
2407  
2408 #------------------------------------------------------------------------------
2409 sub is_non_first_hand {my $self = shift; return $self->contains('evident', 'nfh');}
2410  
2411 #------------------------------------------------------------------------------
2412 sub is_nonhuman {my $self = shift; return $self->contains('animacy', 'nhum');}
2413  
2414 #------------------------------------------------------------------------------
2415 sub is_neuter {my $self = shift; return $self->contains('gender', 'neut');}
2416  
2417 #------------------------------------------------------------------------------
2418 sub is_numeral {my $self = shift; return $self->contains('pos', 'num') || $self->numtype() ne '';}
2419  
2420 #------------------------------------------------------------------------------
2421 sub is_optative {my $self = shift; return $self->contains('mood', 'opt');}
2422  
2423 #------------------------------------------------------------------------------
2424 sub is_ordinal {my $self = shift; return $self->contains('numtype', 'ord');}
2425  
2426 #------------------------------------------------------------------------------
2427 sub is_participle {my $self = shift; return $self->contains('verbform', 'part');}
2428  
2429 #------------------------------------------------------------------------------
2430 sub is_particle {my $self = shift; return $self->contains('pos', 'part');}
2431  
2432 #------------------------------------------------------------------------------
2433 sub is_partitive {my $self = shift; return $self->contains('case', 'par');}
2434  
2435 #------------------------------------------------------------------------------
2436 sub is_passive {my $self = shift; return $self->contains('voice', 'pass');}
2437  
2438 #------------------------------------------------------------------------------
2439 sub is_past {my $self = shift; return $self->contains('tense', 'past');}
2440  
2441 #------------------------------------------------------------------------------
2442 sub is_paucal {my $self = shift; return $self->contains('number', 'pauc');}
2443  
2444 #------------------------------------------------------------------------------
2445 sub is_perfect {my $self = shift; return $self->contains('aspect', 'perf');}
2446  
2447 #------------------------------------------------------------------------------
2448 sub is_personal {my $self = shift; return $self->contains('prontype', 'prs');}
2449  
2450 #------------------------------------------------------------------------------
2451 sub is_personal_pronoun {my $self = shift; return $self->contains('prontype', 'prs');}
2452  
2453 #------------------------------------------------------------------------------
2454 sub is_pluperfect {my $self = shift; return $self->contains('tense', 'pqp');}
2455  
2456 #------------------------------------------------------------------------------
2457 sub is_plural {my $self = shift; return $self->contains('number', 'plur');}
2458  
2459 #------------------------------------------------------------------------------
2460 sub is_polite {my $self = shift; return $self->contains('polite', 'form');}
2461  
2462 #------------------------------------------------------------------------------
2463 sub is_positive {my $self = shift; return $self->contains('degree', 'pos');}
2464  
2465 #------------------------------------------------------------------------------
2466 sub is_possessive {my $self = shift; return $self->poss() eq 'yes';}
2467  
2468 #------------------------------------------------------------------------------
2469 sub is_potential {my $self = shift; return $self->contains('mood', 'pot');}
2470  
2471 #------------------------------------------------------------------------------
2472 sub is_present {my $self = shift; return $self->contains('tense', 'pres');}
2473  
2474 #------------------------------------------------------------------------------
2475 sub is_prolative {my $self = shift; return $self->contains('case', 'ess');}
2476  
2477 #------------------------------------------------------------------------------
2478 sub is_pronominal {my $self = shift; return $self->prontype() ne '';}
2479  
2480 #------------------------------------------------------------------------------
2481 sub is_pronoun {my $self = shift; return $self->is_pronominal() && $self->is_noun();}
2482  
2483 #------------------------------------------------------------------------------
2484 sub is_proper_noun {my $self = shift; return $self->contains('nountype', 'prop');}
2485  
2486 #------------------------------------------------------------------------------
2487 sub is_progressive {my $self = shift; return $self->contains('aspect', 'prog');}
2488  
2489 #------------------------------------------------------------------------------
2490 sub is_prospective {my $self = shift; return $self->contains('aspect', 'prosp');}
2491  
2492 #------------------------------------------------------------------------------
2493 sub is_punctuation {my $self = shift; return $self->contains('pos', 'punc');}
2494  
2495 #------------------------------------------------------------------------------
2496 sub is_purposive {my $self = shift; return $self->contains('mood', 'prp');}
2497  
2498 #------------------------------------------------------------------------------
2499 sub is_quotative {my $self = shift; return $self->contains('mood', 'qot');}
2500  
2501 #------------------------------------------------------------------------------
2502 sub is_rare {my $self = shift; return $self->contains('style', 'rare');}
2503  
2504 #------------------------------------------------------------------------------
2505 sub is_reciprocal {my $self = shift; return $self->contains('prontype', 'rcp') || $self->contains('voice', 'rcp');}
2506  
2507 #------------------------------------------------------------------------------
2508 sub is_reflexive {my $self = shift; return $self->reflex() eq 'yes';}
2509  
2510 #------------------------------------------------------------------------------
2511 sub is_relative {my $self = shift; $self->contains('prontype', 'rel');}
2512  
2513 #------------------------------------------------------------------------------
2514 sub is_second_person {my $self = shift; return $self->contains('person', '2');}
2515  
2516 #------------------------------------------------------------------------------
2517 sub is_singular {my $self = shift; return $self->contains('number', 'sing');}
2518  
2519 #------------------------------------------------------------------------------
2520 sub is_specific {my $self = shift; return $self->contains('definite', 'spec');}
2521  
2522 #------------------------------------------------------------------------------
2523 sub is_subjunctive {my $self = shift; return $self->contains('mood', 'sub');}
2524  
2525 #------------------------------------------------------------------------------
2526 sub is_sublative {my $self = shift; return $self->contains('case', 'sub');}
2527  
2528 #------------------------------------------------------------------------------
2529 sub is_subordinator {my $self = shift; return $self->is_conjunction() && $self->conjtype() eq 'sub';}
2530  
2531 #------------------------------------------------------------------------------
2532 sub is_superessive {my $self = shift; return $self->contains('case', 'sup');}
2533  
2534 #------------------------------------------------------------------------------
2535 sub is_superlative {my $self = shift; return $self->contains('degree', 'sup');}
2536  
2537 #------------------------------------------------------------------------------
2538 sub is_supine {my $self = shift; return $self->contains('verbform', 'sup');}
2539  
2540 #------------------------------------------------------------------------------
2541 sub is_symbol {my $self = shift; return $self->contains('pos', 'sym');}
2542  
2543 #------------------------------------------------------------------------------
2544 sub is_temporal {my $self = shift; return $self->contains('case', 'tem');}
2545  
2546 #------------------------------------------------------------------------------
2547 sub is_terminative {my $self = shift; return $self->contains('case', 'ter');}
2548  
2549 #------------------------------------------------------------------------------
2550 sub is_third_person {my $self = shift; return $self->contains('person', '3');}
2551  
2552 #------------------------------------------------------------------------------
2553 sub is_total {my $self = shift; return $self->contains('prontype', 'tot');}
2554  
2555 #------------------------------------------------------------------------------
2556 sub is_transgressive {my $self = shift; return $self->contains('verbform', 'conv');}
2557  
2558 #------------------------------------------------------------------------------
2559 sub is_transitive {my $self = shift; return $self->contains('subcat', 'tran');}
2560  
2561 #------------------------------------------------------------------------------
2562 sub is_translative {my $self = shift; return $self->contains('case', 'tra');}
2563  
2564 #------------------------------------------------------------------------------
2565 sub is_trial {my $self = shift; return $self->contains('number', 'tri');}
2566  
2567 #------------------------------------------------------------------------------
2568 sub is_typo {my $self = shift; return $self->typo() eq 'yes';}
2569  
2570 #------------------------------------------------------------------------------
2571 sub is_verb {my $self = shift; return $self->contains('pos', 'verb');}
2572  
2573 #------------------------------------------------------------------------------
2574 sub is_verbal_noun {my $self = shift; return $self->contains('verbform', 'vnoun');}
2575  
2576 #------------------------------------------------------------------------------
2577 sub is_vocative {my $self = shift; return $self->contains('case', 'voc');}
2578  
2579 #------------------------------------------------------------------------------
2580 sub is_wh {my $self = shift; return any {m/^(int|rel)$/} ($self->get_list('prontype'));}
2581  
2582 #------------------------------------------------------------------------------
2583 sub is_zero_person {my $self = shift; return $self->contains('person', '0');}
2584  
2585  
2586  
2587 ###############################################################################
2588 # ENFORCING PERMITTED (EXPECTED) VALUES IN FEATURE STRUCTURES
2589 ###############################################################################
2590  
2591  
2592  
2593 #------------------------------------------------------------------------------
2594 # Returns the set of replacement values for the case a feature value is not
2595 # permitted in a given context. The set is derived from the feature matrix
2596 # above. It is a hash{feature}{value0}, leading to a list of values that can be
2597 # used to replace the value0, ordered by priority.
2598 #------------------------------------------------------------------------------
2599 sub get_replacements
2600 {
2601 if(!defined($replacements))
2602 {
2603 $replacements = _preprocess_list_of_replacements();
2604 }
2605 return $replacements;
2606 }
2607  
2608  
2609  
2610 #------------------------------------------------------------------------------
2611 # Preprocesses the lists of replacement values defined above in the %matrix.
2612 # In the original tagset::common module, this code was in the BEGIN block and
2613 # it created the global hash %defaults1 from %defaults.
2614 #------------------------------------------------------------------------------
2615 sub _preprocess_list_of_replacements
2616 {
2617 # This is a lazy attribute and the builder can be called anytime, even
2618 # from map() or grep(). Avoid damaging the caller's $_!
2619 local $_;
2620 my %defaults1;
2621 # Loop over features.
2622 my @keys = keys(%matrix);
2623 foreach my $feature (@keys)
2624 {
2625 # For each feature, there is an array of arrays.
2626 # The first member of each second-order array is the value to replace.
2627 # The rest (if any) are the preferred replacements for this particular value.
2628 # First of all, collect preferred replacements for all values of this feature.
2629 my %map;
2630 foreach my $valarray (@{$matrix{$feature}{replacements}})
2631 {
2632 my $value = $valarray->[0];
2633 $map{$value}{$value}++;
2634 my @backoff;
2635 # Add all preferred replacements (if any) to the list.
2636 for(my $i = 1; $i<=$#{$valarray}; $i++)
2637 {
2638 push(@backoff, $valarray->[$i]);
2639 # Remember all values that have been added as replacements of $value.
2640 $map{$value}{$valarray->[$i]}++;
2641 }
2642 $defaults1{$feature}{$value} = \@backoff;
2643 }
2644 # The primary list of values constitutes the sequence of replacements for the empty value.
2645 foreach my $valarray (@{$matrix{$feature}{replacements}})
2646 {
2647 my $replacement = $valarray->[0];
2648 unless($map{''}{$replacement} || $replacement eq '')
2649 {
2650 push(@{$defaults1{$feature}{''}}, $replacement);
2651 $map{''}{$replacement}++;
2652 }
2653 }
2654 # If a value had preferred replacements, add replacements of the last preferred replacement. Check loops!
2655 # Loop over values again.
2656 foreach my $value (keys(%{$defaults1{$feature}}))
2657 {
2658 # Remember all visited values to prevent loops!
2659 my %visited;
2660 $visited{$value}++;
2661 # Find the last preferred replacement, if any.
2662 my $last;
2663 for(;;)
2664 {
2665 my $new_last;
2666 if(scalar(@{$defaults1{$feature}{$value}}))
2667 {
2668 $last = $defaults1{$feature}{$value}[$#{$defaults1{$feature}{$value}}];
2669 }
2670 # Unless the last preferred replacement has been visited, try to find its replacements.
2671 if($last)
2672 {
2673 unless($visited{$last})
2674 {
2675 $visited{$last}++;
2676 if(ref($defaults1{$feature}{$last}) ne 'ARRAY')
2677 {
2678 confess("Something went wrong when preparing replacement values for feature '$feature' and value '$last'");
2679 }
2680 my @replacements_of_last = @{$defaults1{$feature}{$last}};
2681 # If $last has replacements that $value does not have, add them to $value.
2682 foreach my $replacement (@replacements_of_last)
2683 {
2684 unless($map{$value}{$replacement} || $replacement eq $value)
2685 {
2686 push(@{$defaults1{$feature}{$value}}, $replacement);
2687 $map{$value}{$replacement}++;
2688 $new_last++;
2689 }
2690 }
2691 }
2692 }
2693 # If no $last has been found or if it has been visited, break the loop.
2694 last unless($new_last);
2695 }
2696 # The empty value and all other unvisited values are the next replacements to consider.
2697 foreach my $valarray ([''], @{$matrix{$feature}{replacements}})
2698 {
2699 my $replacement = $valarray->[0];
2700 unless($map{$value}{$replacement} || $replacement eq $value)
2701 {
2702 push(@{$defaults1{$feature}{$value}}, $replacement);
2703 $map{$value}{$replacement}++;
2704 }
2705 }
2706 # Debugging: print the complete list of replacements.
2707 # print STDERR ("$feature: $value:\t", join(', ', @{$defaults1{$feature}{$value}}), "\n");
2708 }
2709 }
2710 return \%defaults1;
2711 }
2712  
2713  
2714  
2715 #------------------------------------------------------------------------------
2716 # Compares two arrays of values. Prefers precision over recall. Accepts that
2717 # value X can serve as replacement of value Y, and counts it as 1/N occurrences
2718 # of Y. Replacements are retrieved from the global %matrix.
2719 #------------------------------------------------------------------------------
2720 sub get_similarity_of_arrays
2721 {
2722 my $feature = shift; # feature name needed to find default values
2723 my $srch = shift; # array reference
2724 my $eval = shift; # array reference
2725 my $defaults = get_replacements();
2726 # For each scalar searched, get replacement array (beginning with the scalar itself).
2727 my @menu; # 2-dimensional matrix
2728 for(my $i = 0; $i<=$#{$srch}; $i++)
2729 {
2730 push(@{$menu[$i]}, $srch->[$i]);
2731 push(@{$menu[$i]}, @{$defaults->{$feature}{$srch->[$i]}});
2732 }
2733 # Look for menu values in array being evaluated. If not found, look for replacements.
2734 my @found; # srch values matched to something in eval
2735 my @used; # eval values identified as something searched for
2736 my $n_found = 0; # how many srch values have been found
2737 my $n_used = 0; # how many eval values have been used
2738 my $n_srch = scalar(@{$srch});
2739 my $n_eval = scalar(@{$eval});
2740 my $score = 0; # number of hits, weighed (replacement is not a full hit, original value is)
2741 if(@menu)
2742 {
2743 # Loop over levels of replacement.
2744 for(my $i = 0; $i<=$#{$menu[0]} && $n_found<$n_srch && $n_used<$n_eval; $i++)
2745 {
2746 # Loop over searched values.
2747 for(my $j = 0; $j<=$#menu && $n_found<$n_srch && $n_used<$n_eval; $j++)
2748 {
2749 next if($found[$j]);
2750 # Look for i-th replacement of j-th value in the evaluated array.
2751 for(my $k = 0; $k<=$#{$eval}; $k++)
2752 {
2753 if(!$used[$k] && $eval->[$k] eq $menu[$j][$i])
2754 {
2755 $found[$j]++;
2756 $used[$k]++;
2757 $n_found++;
2758 $n_used++;
2759 # Add reward for this level of replacement.
2760 # (What fraction of an occurrence are we going to count for this?)
2761 $score += 1/($i+1);
2762 last;
2763 }
2764 }
2765 }
2766 }
2767 }
2768 # Use the score to compute precision and recall.
2769 my ($p, $r);
2770 $p = $score/$n_srch if($n_srch);
2771 $r = $score/$n_eval if($n_eval);
2772 # Prefer precision over recall.
2773 my $result = (2*$p+$r)/3;
2774 return $result;
2775 }
2776  
2777  
2778  
2779 #------------------------------------------------------------------------------
2780 # Selects the most suitable replacement. Can deal with arrays of values.
2781 #------------------------------------------------------------------------------
2782 sub select_replacement
2783 {
2784 my $feature = shift; # feature name needed to get default replacements of a value
2785 my $value = shift; # scalar or array reference
2786 my $permitted = shift; # hash reference; keys are permitted values; array values joint
2787 # The "tagset" and "other" features are special. All values are permitted.
2788 if($feature =~ m/^(tagset|other)$/)
2789 {
2790 return $value;
2791 }
2792 # If value is not an array, make it an array.
2793 my @values = ref($value) eq 'ARRAY' ? @{$value} : ($value);
2794 # Convert every permitted value to an array as well.
2795 my @permitted = keys(%{$permitted});
2796 if(!scalar(@permitted))
2797 {
2798 print STDERR ("Feature = $feature\n");
2799 print STDERR ("Value to replace = ", array_to_scalar_value($value), "\n");
2800 confess("Cannot select a replacement if no values are permitted.\n");
2801 }
2802 my %suitability;
2803 foreach my $p (@permitted)
2804 {
2805 # Warning: split converts empty values to empty array but we want array with one empty element.
2806 my @pvalues = split(/\|/, $p);
2807 $pvalues[0] = '' unless(@pvalues);
2808 # Get suitability evaluation for $p.
2809 $suitability{$p} = get_similarity_of_arrays($feature, \@values, \@pvalues);
2810 }
2811 # Return the most suitable permitted value.
2812 @permitted = sort {$suitability{$b} <=> $suitability{$a}} (@permitted);
2813 # If the replacement is an array, return a reference to it.
2814 my @repl = split(/\|/, $permitted[0]);
2815 if(scalar(@repl)==0)
2816 {
2817 return '';
2818 }
2819 elsif(scalar(@repl)==1)
2820 {
2821 return $repl[0];
2822 }
2823 else
2824 {
2825 return \@repl;
2826 }
2827 }
2828  
2829  
2830  
2831 #------------------------------------------------------------------------------
2832 # Makes sure that a feature structure complies with the permitted combinations
2833 # recorded in a trie. Replaces feature values if needed.
2834 #------------------------------------------------------------------------------
2835 sub enforce_permitted_values
2836 {
2837 my $self = shift;
2838 my $trie = shift; # Lingua::Interset::Trie
2839 my $pointer = shift; # reference to a hash inside the trie, not necessarily the root hash
2840 if(!defined($pointer))
2841 {
2842 $pointer = $trie->root_hash();
2843 }
2844 my $debug = 0;
2845 my $features = $trie->features();
2846 my @features = @{$features};
2847 foreach my $feature (@features)
2848 {
2849 my $value = $self->get($feature);
2850 print("$feature: ", join(', ', map {"'$_' => '$pointer->{$_}'"} sort keys(%{$pointer})), "\n") if($debug);
2851 unless(exists($pointer->{$value}))
2852 {
2853 my $replacement = select_replacement($feature, $value, $pointer);
2854 print("$feature: '$value' => '$replacement'\n") if($debug);
2855 $self->set($feature, $replacement);
2856 $value = $replacement;
2857 }
2858 elsif($debug)
2859 {
2860 print("$feature: '$value' is ok\n");
2861 }
2862 $pointer = $trie->advance_pointer($pointer, $feature, $value);
2863 }
2864 }
2865  
2866  
2867  
2868 ###############################################################################
2869 # GENERIC FEATURE STRUCTURE MANIPULATION
2870 # The following section contains feature-structure-related static functions,
2871 # not just methods (no $self parameter is expected).
2872 ###############################################################################
2873  
2874  
2875  
2876 #------------------------------------------------------------------------------
2877 # Compares two values, scalars or arrays, whether they are equal or not.
2878 #------------------------------------------------------------------------------
2879 sub iseq
2880 {
2881 my $a = shift;
2882 my $b = shift;
2883 if(ref($a) ne ref($b))
2884 {
2885 return 0;
2886 }
2887 elsif(ref($a) eq 'ARRAY')
2888 {
2889 return array_to_scalar_value($a) eq array_to_scalar_value($b);
2890 }
2891 else
2892 {
2893 return $a eq $b;
2894 }
2895 }
2896  
2897  
2898  
2899 #------------------------------------------------------------------------------
2900 # Converts array values to scalars. Sorts the array and combines all elements
2901 # in one string, using the vertical bar as delimiter. Does not care about
2902 # occurrences of vertical bars inside the elements (there should be none
2903 # anyway).
2904 #------------------------------------------------------------------------------
2905 sub array_to_scalar_value
2906 {
2907 my $value = shift;
2908 if(ref($value) eq 'ARRAY')
2909 {
2910 # The sorting helps to ensure that values from two arrays with the same
2911 # elements will be stringwise identical.
2912 $value = join('|', sort(@{$value}));
2913 }
2914 return $value;
2915 }
2916  
2917  
2918  
2919 #------------------------------------------------------------------------------
2920 # Creates a deep copy of a feature structure. If there is a reference to an
2921 # array of values, a copy of the array is created and the copy is referenced
2922 # from the new structure, rather than just copying the reference to the old
2923 # array. The same holds for the "other" feature, which can contain references
2924 # to arrays and / or hashes nested in unlimited number of levels. In fact, this
2925 # function could be used for any nested structures, not just feature
2926 # structures.
2927 #------------------------------------------------------------------------------
2928 sub duplicate
2929 {
2930 my $self = shift;
2931 my $srchash = $self->get_hash();
2932 my $tgthash = _duplicate_recursive($srchash);
2933 my $duplicate = new Lingua::Interset::FeatureStructure();
2934 $duplicate->set_hash($tgthash);
2935 return $duplicate;
2936 }
2937 sub _duplicate_recursive
2938 {
2939 my $source = shift;
2940 my $duplicate;
2941 my $ref = ref($source);
2942 if($ref eq 'ARRAY')
2943 {
2944 my @new_array;
2945 foreach my $element (@{$source})
2946 {
2947 push(@new_array, _duplicate_recursive($element));
2948 }
2949 $duplicate = \@new_array;
2950 }
2951 elsif($ref eq 'HASH')
2952 {
2953 my %new_hash;
2954 foreach my $key (keys(%{$source}))
2955 {
2956 $new_hash{$key} = _duplicate_recursive($source->{$key});
2957 }
2958 $duplicate = \%new_hash;
2959 }
2960 else
2961 {
2962 $duplicate = $source;
2963 }
2964 return $duplicate;
2965 }
2966  
2967  
2968  
2969 1;
2970  
2971 __END__
2972  
2973 =pod
2974  
2975 =encoding UTF-8
2976  
2977 =head1 NAME
2978  
2979 Lingua::Interset::FeatureStructure - Definition of morphosyntactic features and their values.
2980  
2981 =head1 VERSION
2982  
2983 version 3.014
2984  
2985 =head1 SYNOPSIS
2986  
2987 use Lingua::Interset::FeatureStructure;
2988  
2989 print(Lingua::Interset::FeatureStructure->known_features(), "\n");
2990  
2991 =head1 DESCRIPTION
2992  
2993 DZ Interset is a universal framework for reading, writing, converting and
2994 interpreting part-of-speech and morphosyntactic tags from multiple tagsets
2995 of many different natural languages.
2996  
2997 The C<FeatureStructure> class defines all morphosyntactic features and their values used
2998 in DZ Interset. An object of this class represents a morphosyntactic tag
2999 for a natural language word.
3000  
3001 More information is given at the DZ Interset project page,
3002 L<https://wiki.ufal.ms.mff.cuni.cz/user:zeman:interset:features>.
3003  
3004 =head1 METHODS
3005  
3006 =head2 set()
3007  
3008 A generic setter for any feature. These two statements do the same thing:
3009  
3010 $fs->set ('pos', 'noun');
3011 $fs->set_pos ('noun');
3012  
3013 If you want to set multiple values of a feature, there are several ways to do it:
3014  
3015 $fs->set ('tense', ['pres', 'fut']);
3016 $fs->set ('tense', 'pres', 'fut');
3017 $fs->set ('tense', 'pres|fut');
3018  
3019 All of the above mean that the word is either in present or in future tense.
3020  
3021 Note that the 'other' feature behaves differently.
3022 Its value can be structured, C<set()> will keep the structure and will not try to interpret it.
3023  
3024 Using generic C<set()> is more flexible than using specialized setters such as C<set_pos()>.
3025 Even if flexibility is not needed it is recommended to avoid the specialized setters and
3026 use the generic C<set()> method. If multiple values are set using an array reference,
3027 the specialized setters will not create a deep copy of the array, they will only copy
3028 the reference. Generic C<set()> will create a deep copy. The array will thus not be shared
3029 among several feature structures. If someone later retrieves the array reference
3030 via C<get()>, and decides to modify the array, they will probably expect to
3031 change only that particular feature structure and not others that happen to
3032 use the same array.
3033  
3034 =head2 add()
3035  
3036 $fs->add ('pos' => 'conj', 'conjtype' => 'coor');
3037  
3038 Sets several features at once.
3039 Takes a list of value assignments, i.e. an array of an even number of elements
3040 (feature1, value1, feature2, value2, ...)
3041 This is useful when defining decoders from physical tagsets.
3042 Typically, one wants to define a table of assignments for each part of speech or input feature:
3043  
3044 'CC' => ['pos' => 'conj', 'conjtype' => 'coor']
3045  
3046 =head2 set_hash()
3047  
3048 my %hash = ('pos' => 'noun', 'number' => 'plur');
3049 $fs->set_hash (\%hash);
3050  
3051 Takes a reference to a hash of features and their values.
3052 Sets the values of the features in this C<FeatureStructure>.
3053 Unknown features are ignored.
3054 Known features that are not set in the hash will be (re-)set to empty values.
3055  
3056 =head2 merge_hash_hard()
3057  
3058 my %hash = ('pos' => 'noun', 'number' => 'plur');
3059 $fs->merge_hash_hard (\%hash);
3060  
3061 Takes a reference to a hash of features and their values.
3062 Sets the values of the features in this C<FeatureStructure>.
3063 Unknown features are ignored.
3064 Known features that are not set in the hash will be left untouched;
3065 this is the difference from C<set_hash()>.
3066 However, if the current value of a feature is non-empty and the hash contains
3067 a different non-empty value, the current value will be replaced by the one from
3068 the hash.
3069  
3070 =head2 merge_hash_soft()
3071  
3072 my %hash = ('pos' => 'noun', 'number' => 'plur');
3073 $fs->merge_hash_soft (\%hash);
3074  
3075 Takes a reference to a hash of features and their values.
3076 Sets the values of the features in this C<FeatureStructure>.
3077 Unknown features are ignored.
3078 Known features that are not set in the hash will be left untouched;
3079 this is the difference from C<set_hash()>.
3080 Known features that are set both in the hash and in this feature structure
3081 will be merged into a list of values (any single value will occur at most once).
3082 This is the difference from C<merge_hash_hard()>.
3083  
3084 =head2 clear()
3085  
3086 A generic setter that clears the value of a feature, i.e. removes the feature
3087 from the feature structure. All the following statements do the same thing:
3088  
3089 $fs->clear ('pos');
3090 $fs->clear_pos();
3091 $fs->set ('pos', '');
3092 $fs->set ('pos', undef);
3093 $fs->set_pos ('');
3094 $fs->set_pos (undef);
3095  
3096 We can also clear several features at once:
3097  
3098 $fs->clear ('pos', 'prontype', 'gender');
3099  
3100 =head2 get_nonempty_features()
3101  
3102 Returns a list of names of features whose values are not empty.
3103  
3104 my @features = $fs->get_nonempty_features();
3105 my @values = map { $fs->get_joined ($_) } @features;
3106  
3107 The features are returned in a pre-defined (but not alphabetical) order.
3108  
3109 =head2 get()
3110  
3111 A generic getter for any feature. These two statements do the same thing:
3112  
3113 $pos = $fs->get ('pos');
3114 $pos = $fs->pos();
3115  
3116 Be warned that B<you can get an array reference> if the feature has multiple
3117 values. It is probably better to use one of the alternative C<get...()> functions
3118 where it is better defined what you can get.
3119  
3120 =head2 get_joined()
3121  
3122 Similar to C<get()> but always returns scalar.
3123 If there is an array of disjoint values, it sorts them alphabetically and
3124 joins them using the vertical bar. Example: C<'fem|masc'>.
3125 The sorting makes comparisons easier;
3126 it is assumed that the actual ordering is not significant
3127 and that C<'fem|masc'> is identical to C<'masc|fem'>.
3128  
3129 =head2 get_list()
3130  
3131 Similar to get but always returns list of values.
3132 If there is an array of disjoint values, this is the list.
3133 If there is a single value (empty or not), this value will be the only member of the list.
3134  
3135 Unlike in C<get_joined()>, this method does I<not sort> the list before returning it.
3136  
3137 =head2 get_hash()
3138  
3139 my $hashref = $fs->get_hash();
3140  
3141 Creates a hash of all non-empty features and their values.
3142 The values are identical to what the C<get($feature)> method would return;
3143 in particular, the value may be a reference to an array.
3144  
3145 Returns a reference to the hash.
3146  
3147 =head2 get_other_for_tagset()
3148  
3149 my $other = $fs->get_other_for_tagset ('cs::pdt');
3150  
3151 Takes a tagset id.
3152 If it matches the value of the C<tagset> feature, returns the value of the
3153 C<other> feature (it returns a deep copy, which the caller may freely modify).
3154 If the tagset id does not match, the method returns an empty string.
3155  
3156 =head2 set_other_subfeature()
3157  
3158 $fs->set_other_subfeature ('my_weird_feature', 'my_weird_value');
3159  
3160 Takes a non-Interset feature and its value and stores it as a subfeature of
3161 the feature C<other>. If C<other> is currently undefined, empty or anything
3162 else than a hash reference, the method will first create a new hash and store
3163 its reference in C<other>, overwriting its previous value (if any).
3164  
3165 If C<other> is a reference to a hash of subfeatures, the method will add the
3166 new subfeature and its value to the hash. If there has been a subfeature of the
3167 same name, its value will be overwritten.
3168  
3169 Only simple scalar values of subfeatures are assumed. It is not verified but
3170 no deep copy will be made if the value is a reference. Both the feature name
3171 and the value must be defined and non-empty, otherwise the method will do
3172 nothing.
3173  
3174 Note that the function does not check the current value of the C<tagset>
3175 feature. It is silently assumed that if you put anything in C<other>, you know
3176 that this is “your” feature structure.
3177  
3178 =head2 get_other_subfeature()
3179  
3180 my $value = $fs->get_other_subfeature ('cs::pdt', 'my_weird_feature');
3181  
3182 Takes a tagset id and name of a non-Interset feature, stored as a subfeature of
3183 C<other>. The C<other> feature may have arbitrary values ranging from plain
3184 scalars to references to multi-level nested structures of hashes and arrays.
3185 This method focuses on the case that C<other> contains a single-level hash.
3186 The hash keys can be seen as names of additional features that are otherwise
3187 not available in Interset. These additional features are subfeatures of
3188 C<other> and their values are strings. This is one of the most useful ways of
3189 deploying the C<other> feature.
3190  
3191 If the given tagset id matches the value of the C<tagset> feature,
3192 and the value of C<other> is a hash reference,
3193 the method uses the subfeature name as a key to the hash.
3194 If there is a value stored under the hash, it returns the value.
3195 (In case the value is not a string but a reference, a deep copy is created.)
3196 Otherwise it returns the empty string.
3197  
3198 =head2 is_other()
3199  
3200 if ($fs->is_other ('cs::pdt', 'my_weird_tag') ||
3201 $fs->is_other ('cs::pdt', 'my_weird_feature', 'much_weirder_value'))
3202 {
3203 ...
3204 }
3205  
3206 Takes a tagset id.
3207 If it does not match the value of the C<tagset> feature, returns an empty string.
3208 If the tagset ids do match, the method queries the value of the C<other>
3209 feature. Unlike C<get_other_for_tagset()>, it does not create a deep copy of the
3210 possibly structured value. Instead, it only checks whether the feature has or
3211 contains one particular scalar value.
3212  
3213 There are no a priori restrictions on the values of the C<other> feature.
3214 The value can be a multi-level nested structure of hashes and arrays if necessary.
3215 However, most of the time it will be either a scalar value, or a flat (one-level)
3216 hash of feature-value pairs that cannot be stored using standard Interset features.
3217  
3218 Besides the tagset id, this method takes one or two additional arguments.
3219 If the current value of C<other> is scalar, the method checks whether it equals
3220 to Argument 1. If the current value is an array reference, the method checks
3221 whether the array contains Argument 1. If the current value is a hash reference,
3222 the method interprets Argument 1 as hash key and checks whether the value
3223 stored under that key equals to Argument 2.
3224  
3225 It returns 1 when a match is found and 0 otherwise.
3226  
3227 =head2 contains()
3228  
3229 $fs->set ('prontype', 'int|rel');
3230 if($fs->contains ('prontype', 'int'))
3231 {
3232 print("One of the possible pronominal classes for this word is 'interrogative'.\n");
3233 }
3234  
3235 Takes a feature and a value.
3236 Tests whether the given value is one of the current values of the feature.
3237 This function can be used instead of simple C<< if($fs->prontype() eq 'int') >>
3238 whenever we believe that arrays of values could occur.
3239  
3240 =head2 set_upos()
3241  
3242 Sets feature values according to a universal part-of-speech tag as defined in 2014
3243 for the Universal Dependencies
3244 (L<http://universaldependencies.github.io/docs/>).
3245  
3246 =head2 get_upos(), upos()
3247  
3248 Returns the universal part-of-speech tag as defined in 2014
3249 for the Universal Dependencies
3250 (L<http://universaldependencies.github.io/docs/>).
3251  
3252 =head2 add_ufeatures()
3253  
3254 $fs->add_ufeatures ('Case=Nom', 'Gender=Masc,Neut');
3255  
3256 Takes a list of feature-value pairs in the format prescribed by the
3257 Universal Dependencies (L<http://universaldependencies.org/>), i.e.
3258 all features and values are capitalized, some features are renamed and all
3259 feature-value pairs are ordered alphabetically.
3260 Sets our feature values accordingly.
3261 Values of our features that are not mentioned in the input list will be left untouched.
3262  
3263 This method does not complain about unknown features or values.
3264 They will be stored as subfeatures of the C<other> feature.
3265 Hence it is possible to read the input even if it contains language-specific
3266 extensions that are not yet known to Interset.
3267  
3268 =head2 get_ufeatures()
3269  
3270 my @ufpairs = $fs->get_ufeatures();
3271 print (join ('|', @ufpairs));
3272  
3273 Returns the list of feature-value pairs in the format prescribed by the
3274 Universal Dependencies (L<http://universaldependencies.github.io/docs/>), i.e.
3275 all features and values are capitalized, some features are renamed and all
3276 feature-value pairs are ordered alphabetically.
3277  
3278 =head2 matches()
3279  
3280 if ($fs->matches ('pos' => 'noun', 'gender' => '!masc', 'number' => '~(dual|plur)'))
3281 {
3282 ...
3283 }
3284  
3285 Tests multiple features simultaneously.
3286 Input is a list of feature-value pairs, return value is 1 if the structure matches all these values.
3287 This function is an abbreviation for a series of C<get_joined()> calls in an if statement.
3288  
3289 If the expected value is preceded by "!", the actual value must not be equal to the expected value.
3290 If the expected value is preceded by "~", then it is a regular expression which the actual value must match.
3291 If the expected value is preceded by "!~", then it is a regular expression which the actual value must not match.
3292  
3293 =head2 as_string()
3294  
3295 Generates a textual representation of the feature structure so it can be printed.
3296 Features are in a predefined (but not alphabetical) order. Complex values of
3297 the C<other> feature are serialized in depth. If a feature has multiple values,
3298 they are sorted alphabetically and delimited by the vertical bar character.
3299 What follows is a sample output for the C<cs::pdt> tags C<NNMS1-----A---->,
3300 C<Ck-P1----------> and C<VpQW---XR-AA--->:
3301  
3302 [pos="noun", polarity="pos", gender="masc", animacy="anim", number="sing", case="nom", tagset="cs::pdt"]
3303 [pos="adj", numtype="ord", number="plur", case="nom", tagset="cs::pdt", other={"numtype" => "suffix"}]
3304 [pos="verb", polarity="pos", gender="fem|neut", number="plur|sing", verbform="part", tense="past", voice="act", tagset="cs::pdt"]
3305  
3306 =head2 as_string_conllx()
3307  
3308 Generates a textual representation of the feature structure in the form used in
3309 the FEATS column of the CoNLL-X file format. The C<tagset> and C<other>
3310 features are omitted. Features are in predefined (but not alphabetical) order.
3311 If a feature has multiple values, they are sorted alphabetically and delimited
3312 by comma (because the vertical bar is used to separate features).
3313 What follows is a sample output for the C<cs::pdt> tags C<NNMS1-----A---->,
3314 C<Ck-P1----------> and C<VpQW---XR-AA--->:
3315  
3316 pos=noun|polarity=pos|gender=masc|animacy=anim|number=sing|case=nom
3317 pos=adj|numtype=ord|number=plur|case=nom
3318 pos=verb|polarity=pos|gender=fem,neut|number=plur,sing|verbform=part|tense=past|voice=act
3319  
3320 If the values of all features (including C<pos>) are empty, the method
3321 returns the underscore character. Thus the result is never undefined or empty.
3322  
3323 =head2 is_noun()
3324 Also returns 1 if the C<pos> feature has multiple values and one of them is C<noun>, e.g.
3325 if C<get_joined('pos') eq 'noun|adj'>. Note that pronouns also have C<pos=noun>.
3326 If you want to exclude pronouns, test C<is_noun() && !is_pronominal()>.
3327  
3328 =head2 is_abbreviation()
3329  
3330 =head2 is_abessive()
3331  
3332 =head2 is_ablative()
3333  
3334 =head2 is_absolute_superlative()
3335  
3336 =head2 is_absolutive()
3337  
3338 =head2 is_accusative()
3339  
3340 =head2 is_active()
3341  
3342 =head2 is_additive()
3343  
3344 =head2 is_adessive()
3345  
3346 =head2 is_adjective()
3347  
3348 =head2 is_admirative()
3349  
3350 =head2 is_adposition()
3351  
3352 =head2 is_adverb()
3353  
3354 =head2 is_affirmative()
3355  
3356 =head2 is_allative()
3357  
3358 =head2 is_animate()
3359  
3360 =head2 is_antipassive()
3361  
3362 =head2 is_aorist()
3363  
3364 =head2 is_archaic()
3365  
3366 =head2 is_article()
3367  
3368 =head2 is_associative()
3369  
3370 =head2 is_augmentative()
3371  
3372 =head2 is_auxiliary()
3373  
3374 =head2 is_benefactive()
3375  
3376 =head2 is_cardinal()
3377  
3378 =head2 is_colloquial()
3379  
3380 =head2 is_comitative()
3381  
3382 =head2 is_common_gender()
3383  
3384 =head2 is_comparative()
3385  
3386 =head2 is_conditional()
3387  
3388 =head2 is_conjunction()
3389  
3390 =head2 is_conjunctive()
3391  
3392 =head2 is_construct()
3393  
3394 =head2 is_converb()
3395  
3396 =head2 is_coordinator()
3397  
3398 =head2 is_count_plural()
3399  
3400 =head2 is_dative()
3401  
3402 =head2 is_definite()
3403  
3404 =head2 is_delative()
3405  
3406 =head2 is_demonstrative()
3407  
3408 =head2 is_desiderative()
3409  
3410 =head2 is_destinative()
3411  
3412 =head2 is_determiner()
3413  
3414 =head2 is_diminutive()
3415  
3416 =head2 is_direct_voice()
3417  
3418 =head2 is_distributive()
3419  
3420 =head2 is_dual()
3421  
3422 =head2 is_elative()
3423  
3424 =head2 is_elevating()
3425  
3426 =head2 is_equative()
3427  
3428 =head2 is_ergative()
3429  
3430 =head2 is_essive()
3431  
3432 =head2 is_exclamative()
3433  
3434 =head2 is_exclusive()
3435  
3436 =head2 is_factive()
3437  
3438 =head2 is_feminine()
3439  
3440 =head2 is_finite_verb()
3441  
3442 =head2 is_first_hand()
3443  
3444 =head2 is_first_person()
3445  
3446 =head2 is_foreign()
3447  
3448 =head2 is_formal()
3449  
3450 =head2 is_fourth_person()
3451  
3452 =head2 is_future()
3453  
3454 =head2 is_genitive()
3455  
3456 =head2 is_gerund()
3457  
3458 =head2 is_gerundive()
3459  
3460 =head2 is_greater_paucal()
3461  
3462 =head2 is_greater_plural()
3463  
3464 =head2 is_habitual()
3465  
3466 =head2 is_human()
3467  
3468 =head2 is_humbling()
3469  
3470 =head2 is_hyph()
3471  
3472 =head2 is_illative()
3473  
3474 =head2 is_imperative()
3475  
3476 =head2 is_imperfect()
3477  
3478 =head2 is_impersonal()
3479  
3480 =head2 is_inanimate()
3481  
3482 =head2 is_inclusive()
3483  
3484 =head2 is_indefinite()
3485  
3486 =head2 is_indicative()
3487  
3488 =head2 is_inessive()
3489  
3490 =head2 is_infinitive()
3491  
3492 =head2 is_informal()
3493  
3494 =head2 is_instructive()
3495  
3496 =head2 is_instrumental()
3497  
3498 =head2 is_interjection()
3499  
3500 =head2 is_interrogative()
3501  
3502 =head2 is_intransitive()
3503  
3504 =head2 is_inverse_number()
3505  
3506 =head2 is_inverse_voice()
3507  
3508 =head2 is_iterative()
3509  
3510 =head2 is_jussive()
3511  
3512 =head2 is_lative()
3513  
3514 =head2 is_locative()
3515  
3516 =head2 is_masculine()
3517  
3518 =head2 is_mediopassive()
3519  
3520 =head2 is_middle_voice()
3521  
3522 =head2 is_modal()
3523  
3524 =head2 is_motivative()
3525  
3526 =head2 is_multiplicative()
3527  
3528 =head2 is_narrative()
3529  
3530 =head2 is_necessitative()
3531  
3532 =head2 is_negative()
3533  
3534 =head2 is_nominative()
3535  
3536 =head2 is_non_first_hand()
3537  
3538 =head2 is_nonhuman()
3539  
3540 =head2 is_neuter()
3541  
3542 =head2 is_numeral()
3543  
3544 =head2 is_optative()
3545  
3546 =head2 is_ordinal()
3547  
3548 =head2 is_participle()
3549  
3550 =head2 is_particle()
3551  
3552 =head2 is_partitive()
3553  
3554 =head2 is_passive()
3555  
3556 =head2 is_past()
3557  
3558 =head2 is_paucal()
3559  
3560 =head2 is_perfect()
3561  
3562 =head2 is_personal()
3563  
3564 =head2 is_personal_pronoun()
3565  
3566 =head2 is_pluperfect()
3567  
3568 =head2 is_plural()
3569  
3570 =head2 is_polite()
3571  
3572 =head2 is_positive()
3573  
3574 =head2 is_possessive()
3575  
3576 =head2 is_potential()
3577  
3578 =head2 is_present()
3579  
3580 =head2 is_prolative()
3581  
3582 =head2 is_pronominal()
3583  
3584 =head2 is_pronoun()
3585  
3586 =head2 is_proper_noun()
3587  
3588 =head2 is_progressive()
3589  
3590 =head2 is_prospective()
3591  
3592 =head2 is_punctuation()
3593  
3594 =head2 is_purposive()
3595  
3596 =head2 is_quotative()
3597  
3598 =head2 is_rare()
3599  
3600 =head2 is_reciprocal()
3601  
3602 =head2 is_reflexive()
3603  
3604 =head2 is_relative()
3605  
3606 =head2 is_second_person()
3607  
3608 =head2 is_singular()
3609  
3610 =head2 is_specific()
3611  
3612 =head2 is_subjunctive()
3613  
3614 =head2 is_sublative()
3615  
3616 =head2 is_subordinator()
3617  
3618 =head2 is_superessive()
3619  
3620 =head2 is_superlative()
3621  
3622 =head2 is_supine()
3623  
3624 =head2 is_symbol()
3625  
3626 =head2 is_temporal()
3627  
3628 =head2 is_terminative()
3629  
3630 =head2 is_third_person()
3631  
3632 =head2 is_total()
3633  
3634 =head2 is_transgressive()
3635  
3636 =head2 is_transitive()
3637  
3638 =head2 is_translative()
3639  
3640 =head2 is_trial()
3641  
3642 =head2 is_typo()
3643  
3644 =head2 is_verb()
3645  
3646 =head2 is_verbal_noun()
3647  
3648 =head2 is_vocative()
3649  
3650 =head2 is_wh()
3651  
3652 =head2 is_zero_person()
3653  
3654 =head2 enforce_permitted_values()
3655  
3656 $fs->enforce_permitted_values ($permitted_trie);
3657  
3658 Makes sure that a feature structure complies with the permitted combinations
3659 recorded in a trie.
3660 Takes a L<Lingua::Interset::Trie> object as a parameter.
3661 Replaces feature values if needed.
3662 (Note that even the empty value may or may not be permitted.)
3663  
3664 =head2 duplicate()
3665  
3666 Returns a new C<Lingua::Interset::FeatureStructure> object that is
3667 a duplicate of the current structure.
3668 Makes sure that a deep copy is constructed if there are any complex feature values.
3669  
3670 =head1 FUNCTIONS
3671  
3672 =head2 known_features()
3673  
3674 Returns the list of known feature names in print order.
3675  
3676 =head2 priority_features()
3677  
3678 Returns the list of known features ordered according to their default priority.
3679 The priority is used in L<Lingua::Interset::Trie> when one looks for the closest
3680 matching permitted structure.
3681  
3682 =head2 known_values()
3683  
3684 Returns the list of known values of a feature, in print order.
3685 Dies if asked about an unknown feature.
3686  
3687 =head2 feature_valid()
3688  
3689 Takes a string and returns a nonzero value if the string is a name of a known
3690 feature.
3691  
3692 =head2 value_valid()
3693  
3694 Takes two scalars, C<$feature> and C<$value>. Tells whether they are a valid
3695 (known) pair of feature name and value. A reference to a list of valid values
3696 is also a valid value. This function does not die when the feature is not valid.
3697  
3698 =head2 structure_to_string()
3699  
3700 Recursively converts a structure to a string.
3701 The string uses Perl syntax for constant structures, so it can be used in eval.
3702  
3703 =head2 get_replacements()
3704  
3705 my $replacements = Lingua::Interset::FeatureStructure->get_replacements();
3706 my $rep_adverb = $replacements->{pos}{adv};
3707 foreach my $r (@{$rep_adverb})
3708 {
3709 if(...)
3710 {
3711 # This replacement matches our constraints, let's use it.
3712 return $r;
3713 }
3714 }
3715  
3716 Returns the set of replacement values for the case a feature value is not
3717 permitted in a given context.
3718 It is a hash{feature}{value0}, leading to a list of values that can be
3719 used to replace the value0, ordered by priority.
3720  
3721 =head2 iseq()
3722  
3723 if (Lingua::Interset::FeatureStructure->iseq ($a, $b)) { ... }
3724  
3725 Compares two values, scalars or arrays, whether they are equal or not.
3726 Takes two parameters.
3727 Each of them can be a scalar or an array reference.
3728  
3729 =head2 array_to_scalar_value()
3730  
3731 Converts array values to scalars. Sorts the array and combines all elements
3732 in one string, using the vertical bar as delimiter. Does not care about
3733 occurrences of vertical bars inside the elements (there should be none
3734 anyway).
3735  
3736 Takes an array reference as parameter.
3737 If the parameter turns out to be a plain scalar, the function just returns it.
3738  
3739 =head1 SEE ALSO
3740  
3741 L<Lingua::Interset>,
3742 L<Lingua::Interset::Tagset>
3743  
3744 =head1 AUTHOR
3745  
3746 Dan Zeman <zeman@ufal.mff.cuni.cz>
3747  
3748 =head1 COPYRIGHT AND LICENSE
3749  
3750 This software is copyright (c) 2019 by Univerzita Karlova (Charles University).
3751  
3752 This is free software; you can redistribute it and/or modify it under
3753 the same terms as the Perl 5 programming language system itself.
3754  
3755 =cut