Interset/FeatureStructure.pm untested

filter: Statementstmt 0.0% 0 / 1449 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 / 2276 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.015';
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 # Distinguishes between strong and weak forms of adjectives or pronouns.
1013 # Used e.g. in Romanian UD. See also the 'variant' feature. Some tagsets
1014 # use variant=long instead of strength=strong, and variant=short instead
1015 # of strength=weak. However, the 'strength' feature has been tentatively
1016 # added to Interset because it is slightly more specific and also because
1017 # we want to be able to seamlessly read the features from the UD corpora
1018 # that use it.
1019 'strength' =>
1020 {
1021 'priority' => 437,
1022 'values' => ['weak', 'strong'],
1023 'replacements' =>
1024 [
1025 ['weak'],
1026 ['strong']
1027 ],
1028 'uname' => 'Strength'
1029 },
1030 # Variant. Used in some tagsets to distinguish between forms of the same lemma that would otherwise get the same tag.
1031 # The meaning of the values is not and cannot be universal, not even within the scope of one tagset.
1032 'variant' =>
1033 {
1034 'priority' => 440,
1035 'values' => ['short', 'long', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', ''],
1036 'replacements' =>
1037 [
1038 ['0'],
1039 ['1'],
1040 ['2'],
1041 ['3'],
1042 ['4'],
1043 ['5'],
1044 ['6'],
1045 ['7'],
1046 ['8'],
1047 ['9'],
1048 ['a'],
1049 ['b'],
1050 ['c'],
1051 ['short'],
1052 ['long']
1053 ],
1054 'uname' => 'Variant'
1055 },
1056 # Style.
1057 # Either lexical category of the lemma, or grammatical
1058 # (e.g. standard and colloquial suffix of the same lemma, cf. Czech "zelený" vs. "zelenej").
1059 'style' =>
1060 {
1061 'priority' => 420,
1062 'values' => ['arch', 'rare', 'form', 'poet', 'norm', 'coll', 'vrnc', 'slng', 'expr', 'derg', 'vulg', ''],
1063 'replacements' =>
1064 [
1065 ['norm'],
1066 ['form'],
1067 ['rare', 'arch', 'form'],
1068 ['arch', 'rare', 'form'],
1069 ['coll'],
1070 ['poet'],
1071 ['vrnc'],
1072 ['slng'],
1073 ['derg', 'coll'],
1074 ['expr', 'coll'],
1075 ['vulg', 'derg']
1076 ],
1077 'uname' => 'Style'
1078 },
1079 # Tagset identifier. Where does this feature structure come from? Used to interpret the 'other' feature.
1080 # The expected values can be checked by a regular expression but they cannot be enumerated.
1081 # isa => subtype as 'Str', where { m/^([a-z]+::[a-z]+)?$/ }, message { "'$_' does not look like a tagset identifier ('lang::corpus')." } );
1082 'tagset' =>
1083 {
1084 'priority' => 9998,
1085 },
1086 # Tagset-specific information that does not fit elsewhere.
1087 # Any value is permitted, even a hash reference.
1088 'other' =>
1089 {
1090 'priority' => 9999,
1091 }
1092 );
1093 my @features_in_print_order = grep {ref($_) eq ''} @_matrix;
1094 # The list of replacements (defaults) will be derived from the above matrix.
1095 # Same as the matrix, it will be a class-static variable that will not depend on any particular object.
1096 # It will be created lazily on the first demand, using the function _preprocess_list_of_replacements();
1097 # to ensure the lazy creation, it should be accessed using get_replacements().
1098 my $replacements = undef;
1099  
1100  
1101  
1102 # Define the features as Moose attributes.
1103 my $meta = __PACKAGE__->meta();
1104 foreach my $feature (keys(%matrix))
1105 {
1106 unless($feature eq 'other')
1107 {
1108 # Multivalues are internally implemented as arrays and a feature value is either a scalar or an array reference.
1109 # 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).
1110 # Therefore we want to define attribute setters and getters that will do simple conversion in addition to just setting/getting the hybrid value.
1111 # We do this using meta() from Class::MOP::Class. The $feature attribute is defined as 'bare' because we will create the accessor methods ourselves.
1112 has $feature => (is => 'bare', default => '');
1113 # The setter will accept scalars, scalars with multivalues ('fem|masc'), array references and lists.
1114 $meta->add_method(qq/set_$feature/, sub
1115 {
1116 my $self = shift;
1117 my @values = @_;
1118 return $self->set($feature, @values);
1119 });
1120 # A specialized setter that sets the empty value: the clearer.
1121 $meta->add_method(qq/clear_$feature/, sub
1122 {
1123 my $self = shift;
1124 return $self->clear($feature);
1125 });
1126 # The getter will always return a scalar and multivalues will be serialized ('fem|masc').
1127 # The user can call get_list() if they want a list.
1128 $meta->add_method(qq/$feature/, sub
1129 {
1130 my $self = shift;
1131 return $self->get_joined($feature);
1132 });
1133 }
1134 }
1135 # The 'other' feature will have a standard setter/getter that will just take/return anything. Usually a hash reference.
1136 ###!!! In future we may want to limit the values of 'other' to be references to hashes of scalar-valued subfeatures.
1137 ###!!! Then we will probably define new methods for subfeatures and a serialization method. And the standard getter will be redefined to return strings only.
1138 has 'other' => (is => 'rw', default => '');
1139  
1140  
1141  
1142 #------------------------------------------------------------------------------
1143 # Static function. Returns the list of known features (in print order).
1144 #------------------------------------------------------------------------------
1145 sub known_features
1146 {
1147 return @features_in_print_order;
1148 }
1149  
1150  
1151  
1152 #------------------------------------------------------------------------------
1153 # Static function. Returns the list of features according to their priority
1154 # (used when enforcing permitted feature-value combinations during strict
1155 # encoding).
1156 #------------------------------------------------------------------------------
1157 sub priority_features
1158 {
1159 my @features = keys(%matrix);
1160 @features = sort {$matrix{$a}{priority} <=> $matrix{$b}{priority}} (@features);
1161 return @features;
1162 }
1163  
1164  
1165  
1166 #------------------------------------------------------------------------------
1167 # Static function. Returns the list of known values (in print order) of
1168 # a feature. Dies if asked about an unknown feature.
1169 #------------------------------------------------------------------------------
1170 sub known_values
1171 {
1172 my $feature = shift;
1173 if(exists($matrix{$feature}))
1174 {
1175 if(defined($matrix{$feature}{values}))
1176 {
1177 return @{$matrix{$feature}{values}};
1178 }
1179 else # features tagset and other do not have lists of values
1180 {
1181 return ();
1182 }
1183 }
1184 else
1185 {
1186 confess("Unknown feature $feature");
1187 }
1188 }
1189  
1190  
1191  
1192 #------------------------------------------------------------------------------
1193 # Static function. Tells whether a string is the name of a known feature.
1194 #------------------------------------------------------------------------------
1195 sub feature_valid
1196 {
1197 my $feature = shift;
1198 return exists($matrix{$feature}) ? 1 : 0;
1199 }
1200  
1201  
1202  
1203 #------------------------------------------------------------------------------
1204 # Static function. Tells for a given feature-value pair whether both the
1205 # feature and the value are known and valid. References to lists of valid
1206 # values are also valid. Does not die when the feature is not valid.
1207 #------------------------------------------------------------------------------
1208 sub value_valid
1209 {
1210 my $feature = shift;
1211 my $value = shift;
1212 # Avoid warnings if feature is not defined.
1213 if(!defined($feature))
1214 {
1215 return 0;
1216 }
1217 # Value of the 'other' feature can be anything.
1218 elsif($feature eq 'other')
1219 {
1220 return 1;
1221 }
1222 # For the 'tagset' feature, a regular expression is used instead of a list of values.
1223 elsif($feature eq 'tagset')
1224 {
1225 return $value =~ m/^([a-z]+::[a-z0-9]+)?$/;
1226 }
1227 # Other known features all have their lists of values (including the empty string).
1228 else
1229 {
1230 return 0 unless(feature_valid($feature));
1231 my @known_values = known_values($feature);
1232 # If the value is a list of values, each of them must be valid.
1233 # This method recognizes array references and serialized arrays using '|' as the separator.
1234 my $ref = ref($value);
1235 my @myarray;
1236 if($ref eq '' && $value =~ m/\|/)
1237 {
1238 @myarray = split(/\|/, $value);
1239 $value = \@myarray;
1240 $ref = ref($value);
1241 }
1242 if($ref eq 'ARRAY')
1243 {
1244 foreach my $svalue (@{$value})
1245 {
1246 # No nested arrays are expected.
1247 if(ref($svalue) ne '')
1248 {
1249 return 0;
1250 }
1251 else
1252 {
1253 return 0 unless(grep {$_ eq $svalue} (@known_values));
1254 }
1255 }
1256 # All values tested successfully.
1257 return 1;
1258 }
1259 # Single value.
1260 elsif($ref eq '')
1261 {
1262 return scalar(grep {$_ eq $value} (@known_values));
1263 }
1264 # No other references expected.
1265 else
1266 {
1267 return 0;
1268 }
1269 }
1270 }
1271  
1272  
1273  
1274 #------------------------------------------------------------------------------
1275 # This method ensures backwards compatibility of features and values that have
1276 # been renamed. If the feature-value pair is valid, the value is returned. If
1277 # the feature/value is not valid in the current version of Interset but it is
1278 # known to have existed in the past, the closest match is returned. If it is
1279 # unknown and no mapping is available, we return undef. This method does not
1280 # throw exceptions.
1281 #------------------------------------------------------------------------------
1282 my %fvbct =
1283 (
1284 'number' => { 'plu' => 'plur' }, # renamed in fall 2014
1285 'degree' => { 'comp' => 'cmp' }, # renamed in Interset 2.049, 2015-09-29
1286 'aspect' => { 'pro' => 'prosp' }, # renamed in UD v2, 2016-12-01
1287 'verbform' => { 'trans' => 'conv' }, # renamed in UD v2, 2016-12-01
1288 'polite' => { 'inf' => 'infm', # renamed in UD v2, 2016-12-01
1289 'pol' => 'form' }, # renamed in UD v2, 2016-12-01
1290 'abspolite' => { 'inf' => 'infm', # renamed in UD v2, 2016-12-01
1291 'pol' => 'form' }, # renamed in UD v2, 2016-12-01
1292 'datpolite' => { 'inf' => 'infm', # renamed in UD v2, 2016-12-01
1293 'pol' => 'form' }, # renamed in UD v2, 2016-12-01
1294 'ergpolite' => { 'inf' => 'infm', # renamed in UD v2, 2016-12-01
1295 'pol' => 'form' }, # renamed in UD v2, 2016-12-01
1296 'numtype' => { 'gen' => 'mult' }, # value removed in UD v2, 2016-12-01
1297 'foreign' => { 'foreign' => 'yes', # value renamed in Interset 3.005, 2017-07-08
1298 'fscript' => 'yes', # value removed in UD v2, 2016-12-01
1299 'tscript' => 'yes' }, # value removed in UD v2, 2016-12-01
1300 'poss' => { 'poss' => 'yes' }, # value renamed in Interset 3.005, 2017-07-08
1301 'reflex' => { 'reflex' => 'yes' }, # value renamed in Interset 3.005, 2017-07-08
1302 'abbr' => { 'abbr' => 'yes' }, # value renamed in Interset 3.005, 2017-07-08
1303 'hyph' => { 'hyph' => 'yes' }, # value renamed in Interset 3.005, 2017-07-08
1304 'typo' => { 'typo' => 'yes' }, # value renamed in Interset 3.005, 2017-07-08
1305 'tense' => { 'nar' => 'past' }, # value removed in UD v2, should use evident=nfh + past, 2017-07-11
1306 );
1307 sub _get_compatible_value
1308 {
1309 my $self = shift;
1310 my $feature = shift;
1311 my $value = shift;
1312 if(value_valid($feature, $value))
1313 {
1314 return $value;
1315 }
1316 else
1317 {
1318 if(exists($fvbct{$feature}{$value}))
1319 {
1320 return $fvbct{$feature}{$value};
1321 }
1322 # If we are here, the feature-value pair is unknown and we do not know where to map it to.
1323 return;
1324 }
1325 }
1326  
1327  
1328  
1329 #------------------------------------------------------------------------------
1330 # This method is called from several places in the set() method where it is
1331 # necessary to deal with unknown values of features. It may throw exceptions!
1332 # The method takes two arguments besides $self, $feature and $value. Only
1333 # single values are expected, no arrayrefs and no serialized lists (the set()
1334 # method takes care of lists before calling this). The method calls the
1335 # _get_compatible_value() method. It differs in that it throws an exception if
1336 # the value is invalid and cannot be replaced.
1337 #------------------------------------------------------------------------------
1338 sub _validate_value
1339 {
1340 my $self = shift;
1341 my $feature = shift;
1342 my $value = shift;
1343 my $value2 = $self->_get_compatible_value($feature, $value);
1344 if(defined($value2))
1345 {
1346 return $value2;
1347 }
1348 else
1349 {
1350 confess("Unknown value '$value' of feature '$feature'");
1351 }
1352 }
1353  
1354  
1355  
1356 #------------------------------------------------------------------------------
1357 # Named setters for each feature are nice but we also need a generic setter
1358 # that takes both the feature name and value.
1359 #------------------------------------------------------------------------------
1360 sub set
1361 {
1362 my $self = shift;
1363 my $feature = shift;
1364 if(!feature_valid($feature))
1365 {
1366 ###!!! Since there are old files with Interset-based data, we need backward compatibility.
1367 ###!!! In the future we may need a separate method.
1368 ###!!! Features that had existed and were removed.
1369 if($feature eq 'subpos')
1370 {
1371 print STDERR ("Ignoring deprecated Interset feature 'subpos'.\n");
1372 return;
1373 }
1374 ###!!! Features that were renamed.
1375 elsif($feature eq 'abspoliteness')
1376 {
1377 $feature = 'abspolite';
1378 }
1379 elsif($feature eq 'animateness')
1380 {
1381 $feature = 'animacy';
1382 }
1383 elsif($feature eq 'datpoliteness')
1384 {
1385 $feature = 'datpolite';
1386 }
1387 elsif($feature eq 'definiteness')
1388 {
1389 $feature = 'definite';
1390 }
1391 elsif($feature eq 'ergpoliteness')
1392 {
1393 $feature = 'ergpolite';
1394 }
1395 elsif($feature eq 'negativeness')
1396 {
1397 $feature = 'polarity';
1398 }
1399 elsif($feature eq 'politeness')
1400 {
1401 $feature = 'polite';
1402 }
1403 else
1404 {
1405 confess("Unknown feature '$feature'");
1406 }
1407 }
1408 my @values = grep {defined($_) && $_ ne ''} @_;
1409 # Undefined value means that we want to clear the feature.
1410 if(scalar(@values)==0)
1411 {
1412 return $self->clear($feature);
1413 }
1414 # The interpretation of the @values if different if the feature is 'other'.
1415 if($feature eq 'other')
1416 {
1417 $self->{$feature} = _duplicate_recursive($values[0]);
1418 return 1;
1419 }
1420 my @values1;
1421 my %values; # map values and do not add the same value twice
1422 foreach my $value (@values)
1423 {
1424 if(ref($value) eq 'ARRAY')
1425 {
1426 foreach my $subvalue (@{$value})
1427 {
1428 # No unlimited recursion. Referenced arrays are not supposed to contain subarrays.
1429 confess('Plain scalar expected') unless(ref($subvalue) eq '');
1430 $subvalue = $self->_validate_value($feature, $subvalue);
1431 push(@values1, $subvalue) unless($values{$subvalue});
1432 $values{$subvalue}++;
1433 }
1434 }
1435 elsif($value =~ m/\|/)
1436 {
1437 my @subvalues = split(/\|/, $value);
1438 foreach my $subvalue (@subvalues)
1439 {
1440 $subvalue = $self->_validate_value($feature, $subvalue);
1441 push(@values1, $subvalue) unless($values{$subvalue});
1442 $values{$subvalue}++;
1443 }
1444 }
1445 else
1446 {
1447 $value = $self->_validate_value($feature, $value);
1448 push(@values1, $value) unless($values{$value});
1449 $values{$value}++;
1450 }
1451 }
1452 # Current Interset convention is that multi-values are stored as array references.
1453 # The above copying solves three problems:
1454 # 1. multiple ways for the user to provide the values
1455 # 2. if the user provides array reference, the array will not be shared but copied
1456 # 3. repeated occurrence of one value will not be allowed
1457 my $value;
1458 if(scalar(@values1)>1)
1459 {
1460 $value = \@values1;
1461 }
1462 elsif(scalar(@values1)==1)
1463 {
1464 $value = $values1[0];
1465 }
1466 else
1467 {
1468 confess('Missing value');
1469 }
1470 $self->{$feature} = $value;
1471 }
1472  
1473  
1474  
1475 #------------------------------------------------------------------------------
1476 # Sets several features at once. Takes list of value assignments, i.e. an array
1477 # of an even number of elements (feature1, value1, feature2, value2...)
1478 # This is useful when defining decoders from physical tagsets. Typically, one
1479 # wants to define a table of assignments for each part of speech or input
1480 # feature:
1481 # 'CC' => ['pos' => 'conj', 'conjtype' => 'coor']
1482 #------------------------------------------------------------------------------
1483 sub add
1484 {
1485 my $self = shift;
1486 my @assignments = @_;
1487 for(my $i = 0; $i<=$#assignments; $i += 2)
1488 {
1489 my $feature = $assignments[$i];
1490 my $value = $assignments[$i+1];
1491 # Support for hashes of subfeatures in 'other'.
1492 # We want to merge the two hashes, not to replace the current hash by the new one.
1493 if($feature eq 'other' && ref($value) eq 'HASH')
1494 {
1495 my $current = $self->other();
1496 if(ref($current) eq 'HASH')
1497 {
1498 foreach my $subfeature (keys(%{$value}))
1499 {
1500 # Do not concern about deep copying now. It will be taken care of in set().
1501 $current->{$subfeature} = $value->{$subfeature};
1502 }
1503 $value = $current;
1504 }
1505 }
1506 $self->set($feature, $value);
1507 }
1508 }
1509  
1510  
1511  
1512 #------------------------------------------------------------------------------
1513 # Takes a reference to a hash of features and their values. Sets the values of
1514 # the features in $self. Unknown features and values are ignored. Known
1515 # features that are not set in the hash will be (re-)set to empty values in
1516 # $self.
1517 #------------------------------------------------------------------------------
1518 sub set_hash
1519 {
1520 my $self = shift;
1521 my $fs = shift;
1522 foreach my $feature ($self->known_features())
1523 {
1524 my $value = defined($fs->{$feature}) ? $fs->{$feature} : '';
1525 $self->set($feature, $value);
1526 }
1527 }
1528  
1529  
1530  
1531 #------------------------------------------------------------------------------
1532 # Takes a reference to a hash of features and their values. Sets the values of
1533 # the features in $self; does not touch values of features not mentioned in the
1534 # hash.
1535 #------------------------------------------------------------------------------
1536 sub merge_hash_hard
1537 {
1538 my $self = shift;
1539 my $fs = shift;
1540 foreach my $feature ($self->known_features())
1541 {
1542 if(exists($fs->{$feature}) && defined($fs->{$feature}) && $fs->{$feature} ne '')
1543 {
1544 $self->set($feature, $fs->{$feature});
1545 }
1546 }
1547 }
1548  
1549  
1550  
1551 #------------------------------------------------------------------------------
1552 # Takes a reference to a hash of features and their values. Sets the values of
1553 # the features in $self; does not touch values of features not mentioned in the
1554 # hash.
1555 #------------------------------------------------------------------------------
1556 sub merge_hash_soft
1557 {
1558 my $self = shift;
1559 my $fs = shift;
1560 foreach my $feature ($self->known_features())
1561 {
1562 if(exists($fs->{$feature}) && defined($fs->{$feature}) && $fs->{$feature} ne '')
1563 {
1564 my @current_values = $self->get_list($feature);
1565 if(scalar(@current_values)>1 || scalar(@current_values)==1 && $current_values[0] ne '')
1566 {
1567 # The new value(s) may be given in multiple ways and the set() method has mechanisms to distinguish them.
1568 # Let's normalize the input values by running them through set() first.
1569 $self->set($feature, $fs->{$feature});
1570 my @new_values = $self->get_list($feature);
1571 # Merge the two lists of values.
1572 my %map;
1573 my @values = ();
1574 foreach my $value (@current_values, @new_values)
1575 {
1576 push(@values, $value) unless($map{$value});
1577 $map{$value}++;
1578 }
1579 $self->set($feature, \@values);
1580 }
1581 else # no previous value found
1582 {
1583 $self->set($feature, $fs->{$feature});
1584 }
1585 }
1586 }
1587 }
1588  
1589  
1590  
1591 #------------------------------------------------------------------------------
1592 # Generic setter that clears the value of a feature, i.e. removes the feature.
1593 #------------------------------------------------------------------------------
1594 sub clear
1595 {
1596 my $self = shift;
1597 my @features = grep {defined($_)} @_;
1598 confess() if(!@features);
1599 foreach my $feature (@features)
1600 {
1601 delete($self->{$feature});
1602 }
1603 }
1604  
1605  
1606  
1607 #------------------------------------------------------------------------------
1608 # Returns a list of names of (known) features whose values are not empty.
1609 #------------------------------------------------------------------------------
1610 sub get_nonempty_features
1611 {
1612 my $self = shift;
1613 return grep {defined($self->{$_}) && $self->{$_} ne ''} known_features();
1614 }
1615  
1616  
1617  
1618 #------------------------------------------------------------------------------
1619 # get() is a generic feature value getter.
1620 #------------------------------------------------------------------------------
1621 sub get
1622 {
1623 my $self = shift;
1624 my $feature = shift;
1625 confess() if(!defined($feature));
1626 return defined($self->{$feature}) ? _duplicate_recursive($self->{$feature}) : '';
1627 }
1628  
1629  
1630  
1631 #------------------------------------------------------------------------------
1632 # Similar to get but always returns scalar. If there is an array of disjoint
1633 # values, it does not pick just one. Instead, it sorts all values and joins
1634 # them using the vertical bar. Example: 'fem|masc'.
1635 #------------------------------------------------------------------------------
1636 sub get_joined
1637 {
1638 my $self = shift;
1639 my $feature = shift;
1640 return array_to_scalar_value($self->get($feature));
1641 }
1642  
1643  
1644  
1645 #------------------------------------------------------------------------------
1646 # Similar to get but always returns list of values. If there is an array of
1647 # disjoint values, this is the list. If there is a single value (empty or not),
1648 # this value is the only member of the list.
1649 #------------------------------------------------------------------------------
1650 sub get_list
1651 {
1652 my $self = shift;
1653 my $feature = shift;
1654 my $value = $self->get($feature);
1655 my @list;
1656 if(ref($value) eq 'ARRAY')
1657 {
1658 @list = @{$value};
1659 }
1660 else
1661 {
1662 @list = ($value);
1663 }
1664 return @list;
1665 }
1666  
1667  
1668  
1669 #------------------------------------------------------------------------------
1670 # Creates a hash of all features and their values. Returns a reference to the
1671 # hash.
1672 #------------------------------------------------------------------------------
1673 sub get_hash
1674 {
1675 my $self = shift;
1676 my %fs;
1677 foreach my $feature ($self->known_features())
1678 {
1679 my $value = $self->get($feature);
1680 if(defined($value) && $value ne '')
1681 {
1682 $fs{$feature} = $value;
1683 }
1684 }
1685 return \%fs;
1686 }
1687  
1688  
1689  
1690 #------------------------------------------------------------------------------
1691 # Tests tagset + other features.
1692 #------------------------------------------------------------------------------
1693 sub get_other_for_tagset
1694 {
1695 my $self = shift;
1696 my $tagset = shift;
1697 # We normally set the tagset feature even if there is no other feature.
1698 # However, it is not required that it is defined. (And sometimes we clear the tagset, e.g. during certain tests.)
1699 # Calling this method without defining $tagset makes much less sense but it is not fatal either.
1700 if(defined($self->tagset()) && defined($tagset) && $self->tagset() eq $tagset && defined($self->other()))
1701 {
1702 return _duplicate_recursive($self->other());
1703 }
1704 else
1705 {
1706 return '';
1707 }
1708 }
1709  
1710  
1711  
1712 #------------------------------------------------------------------------------
1713 # If other is a reference to a hash of subfeatures, this method adds (replaces)
1714 # a value of one subfeature. If other is undefined, empty or non-hash, this
1715 # method replaces the current value of other by a reference to a new hash, then
1716 # adds the subfeature.
1717 #------------------------------------------------------------------------------
1718 sub set_other_subfeature
1719 {
1720 my $self = shift;
1721 my $subfeature = shift;
1722 my $value = shift;
1723 return unless(defined($subfeature) && defined($value) && $subfeature ne '' && $value ne '');
1724 # This will just return the hash reference (provided it is a hashref), no deep copying takes place.
1725 my $other = $self->other();
1726 if(!defined($other) || ref($other) ne 'HASH')
1727 {
1728 my %other;
1729 $other = \%other;
1730 # This will just set the hash reference as the value, no deep copying takes place.
1731 # (Unlike $self->set('other', $other), which would deep-copy the hash.)
1732 $self->set_other($other);
1733 }
1734 $other->{$subfeature} = $value;
1735 }
1736  
1737  
1738  
1739 #------------------------------------------------------------------------------
1740 # If tagset matches and if other is a hash, this method returns the value of
1741 # a subfeature, i.e. a value stored in the hash under a particular key.
1742 #------------------------------------------------------------------------------
1743 sub get_other_subfeature
1744 {
1745 my $self = shift;
1746 my $tagset = shift;
1747 my $subfeature = shift;
1748 my $other = $self->other();
1749 if(defined($self->tagset()) && $tagset eq $self->tagset() && defined($other) && ref($other) eq 'HASH' && exists($other->{$subfeature}))
1750 {
1751 # This method was created because of simple hashes whose values are strings.
1752 # But there is no guarantee that the value is not a reference and thus we must return a deep copy.
1753 return _duplicate_recursive($other->{$subfeature});
1754 }
1755 return '';
1756 }
1757  
1758  
1759  
1760 #------------------------------------------------------------------------------
1761 # Tests tagset + other features. Instead of returning a deep copy of the
1762 # possibly structured value (see get_other_for_tagset()), this method only
1763 # queries whether the other feature has/contains one particular scalar value.
1764 #------------------------------------------------------------------------------
1765 sub is_other
1766 {
1767 my $self = shift;
1768 my $tagset = shift;
1769 my $arg1 = shift;
1770 my $arg2 = shift;
1771 $arg1 = '' if(!defined($arg1));
1772 $arg2 = '' if(!defined($arg2));
1773 if($self->tagset() eq $tagset)
1774 {
1775 my $other = $self->other();
1776 $other = '' if(!defined($other));
1777 my $ref = ref($other);
1778 if($ref eq '' && $other eq $arg1)
1779 {
1780 return 1;
1781 }
1782 elsif($ref eq 'ARRAY' && any { $_ eq $arg1 } (@{$other}))
1783 {
1784 return 1;
1785 }
1786 elsif($ref eq 'HASH' && exists($other->{$arg1}))
1787 {
1788 my $value = $other->{$arg1};
1789 $value = '' if(!defined($value));
1790 if($value eq $arg2)
1791 {
1792 return 1;
1793 }
1794 else
1795 {
1796 return 0;
1797 }
1798 }
1799 else
1800 {
1801 return 0;
1802 }
1803 }
1804 else
1805 {
1806 return 0;
1807 }
1808 }
1809  
1810  
1811  
1812 #------------------------------------------------------------------------------
1813 # Tests whether values of a feature contain one particular value. Useful if we
1814 # know that a feature may have array of values.
1815 #------------------------------------------------------------------------------
1816 sub contains
1817 {
1818 my $self = shift;
1819 my $feature = shift;
1820 my $value = shift;
1821 return any { $_ eq $value } ($self->get_list($feature));
1822 }
1823  
1824  
1825  
1826 #==============================================================================
1827 # Methods for the universal part-of-speech tags and the universal features as
1828 # defined in October 2014 for the Universal Dependencies
1829 # (http://universaldependencies.github.io/docs/).
1830 #==============================================================================
1831 use Lingua::Interset::Tagset::MUL::Upos;
1832 has '_upos_driver' => ( isa => 'Lingua::Interset::Tagset::MUL::Upos', is => 'ro', builder => '_create_upos_driver', lazy => 1 );
1833 sub _create_upos_driver { return new Lingua::Interset::Tagset::MUL::Upos; }
1834 has '_map_from_uf' => ( isa => 'HashRef', is => 'ro', builder => '_create_map_from_uf', lazy => 1 );
1835 sub _create_map_from_uf
1836 {
1837 # A reverse hash will help translate Universal Features to Interset features.
1838 my %map_from_uf;
1839 foreach my $feature (keys(%matrix))
1840 {
1841 if(defined($matrix{$feature}{uname}) && !defined($map_from_uf{$matrix{$feature}{uname}}))
1842 {
1843 $map_from_uf{$matrix{$feature}{uname}} = $feature;
1844 }
1845 }
1846 # Backward compatibility with UD v1:
1847 $map_from_uf{Negative} = 'polarity';
1848 return \%map_from_uf;
1849 }
1850  
1851  
1852  
1853 #------------------------------------------------------------------------------
1854 # Sets feature values according to a universal part-of-speech tag as defined in
1855 # 2014 for the Universal Dependencies
1856 # (http://universaldependencies.github.io/docs/).
1857 #------------------------------------------------------------------------------
1858 sub set_upos
1859 {
1860 my $self = shift;
1861 my $upos = shift;
1862 # We will use the mul::upos driver to decode the tag. However, the driver
1863 # may destroy the current value of prontype: it may replace it by a generic
1864 # value 'prn'. Save the current prontype if the new one is 'prn'.
1865 my $old_prontype = $self->prontype();
1866 my $driver = $self->_upos_driver();
1867 $driver->decode_and_merge_hard($upos, $self);
1868 my $new_prontype = $self->prontype();
1869 if($new_prontype eq 'prn' && $old_prontype ne '' && $old_prontype ne 'prn')
1870 {
1871 $self->set('prontype', $old_prontype);
1872 }
1873 }
1874  
1875  
1876  
1877 #------------------------------------------------------------------------------
1878 # Returns the universal part-of-speech tag as defined in 2014 for the Universal
1879 # Dependencies (http://universaldependencies.github.io/docs/).
1880 #------------------------------------------------------------------------------
1881 sub get_upos
1882 {
1883 my $self = shift;
1884 my $driver = $self->_upos_driver();
1885 return $driver->encode($self);
1886 }
1887 sub upos { return get_upos(@_); }
1888  
1889  
1890  
1891 #------------------------------------------------------------------------------
1892 # Takes a list of feature=value pairs in the format prescribed by the
1893 # Universal Dependencies (http://universaldependencies.github.io/docs/), i.e.
1894 # identifiers are capitalized, some features are renamed and all pairs are
1895 # ordered alphabetically. Sets our feature values accordingly.
1896 #------------------------------------------------------------------------------
1897 sub add_ufeatures
1898 {
1899 my $self = shift;
1900 my @pairs = @_;
1901 my $map = $self->_map_from_uf();
1902 foreach my $pair (@pairs)
1903 {
1904 if($pair =~ m/^(.+)=(.+)$/)
1905 {
1906 my $ufeature = $1;
1907 my $uvalue = $2;
1908 my $feature = $map->{$ufeature};
1909 if(defined($feature))
1910 {
1911 my $value = lc($uvalue);
1912 # Universal Features use comma to join multi-values but we use the vertical bar.
1913 $value =~ s/,/\|/g;
1914 # Backward compatibility: if this is an old value that has been renamed, get the new value.
1915 # If the value has never been known, we will get back an undefined value.
1916 $value = $self->_get_compatible_value($feature, $value);
1917 if(defined($feature) && defined($value))
1918 {
1919 $self->set($feature, $value);
1920 }
1921 else # other
1922 {
1923 $self->set_tagset('mul::uposf');
1924 $self->set_other_subfeature($ufeature, $uvalue);
1925 }
1926 }
1927 else # other
1928 {
1929 $self->set_tagset('mul::uposf');
1930 $self->set_other_subfeature($ufeature, $uvalue);
1931 }
1932 }
1933 }
1934 }
1935  
1936  
1937  
1938 #------------------------------------------------------------------------------
1939 # Returns the list of feature=value pairs in the format prescribed by the
1940 # Universal Dependencies (http://universaldependencies.github.io/docs/), i.e.
1941 # identifiers are capitalized, some features are renamed and all pairs are
1942 # ordered alphabetically.
1943 #------------------------------------------------------------------------------
1944 sub get_ufeatures
1945 {
1946 my $self = shift;
1947 my $fh = $self->get_hash();
1948 # The Universal Features must be sorted alphabetically, that is, lowercase letters equal to uppercase
1949 # and 'Number' < 'NumType'.
1950 my @features = sort {lc($matrix{$a}{uname}) cmp lc($matrix{$b}{uname})} (grep {defined($matrix{$_}{uname})} (keys(%{$fh})));
1951 my @pairs;
1952 foreach my $feature (@features)
1953 {
1954 my $uname = $matrix{$feature}{uname};
1955 my @values = grep {defined($_) && $_ ne ''} $self->get_list($feature);
1956 next unless(@values);
1957 # Sort multivalues alphabetically and capitalize them.
1958 @values = sort {lc($a) cmp lc($b)} (map {my $x = $_; $x =~ s/^(.)/\u$1/; $x} (@values));
1959 # Join values using comma (unlike in get_joined(), with Universal Features we cannot use the vertical bar).
1960 my $value = join(',', @values);
1961 my $pair = "$uname=$value";
1962 # Some values of some features became obsolete because the distinction was moved to the POS tag level.
1963 next if($pair =~ m/^(PronType=Prn|ConjType=(Coor|Sub)|NounType=(Com|Prop)|VerbType=Aux|Variant=[0-9A-C])$/);
1964 push(@pairs, $pair);
1965 }
1966 # Check whether there are language-specific features or values that Interset
1967 # does not know and that thus had to be stored in the 'other' feature.
1968 my $other = $self->get_other_for_tagset ('mul::uposf');
1969 if(defined($other) && ref($other) eq 'HASH')
1970 {
1971 my @ufeatures = keys(%{$other});
1972 my @upairs = map {"$_=$other->{$_}"} (@ufeatures);
1973 push(@pairs, @upairs);
1974 @pairs = sort {lc($a) cmp lc($b)} (@pairs);
1975 }
1976 return @pairs;
1977 }
1978  
1979  
1980  
1981 #------------------------------------------------------------------------------
1982 # Tests multiple Interset features simultaneously. Input is a list of feature-
1983 # value pairs, return value is 1 if the node matches all these values. This
1984 # function is an abbreviation for a series of get_iset() calls in an if
1985 # statement:
1986 #
1987 # if($node->match_iset('pos' => 'noun', 'gender' => 'masc')) { ... }
1988 #------------------------------------------------------------------------------
1989 sub matches
1990 {
1991 my $self = shift;
1992 my @req = @_;
1993 for ( my $i = 0; $i <= $#req; $i += 2 )
1994 {
1995 my $feature = $req[$i];
1996 my $expected = $req[$i+1];
1997 confess("Undefined feature") unless ($feature);
1998 my $value = $self->get_joined($feature);
1999 my $comp =
2000 $expected =~ s/^\!\~// ? 'nr' :
2001 $expected =~ s/^\!// ? 'ne' :
2002 $expected =~ s/^\~// ? 're' : 'eq';
2003 if (
2004 $comp eq 'eq' && $value ne $expected ||
2005 $comp eq 'ne' && $value eq $expected ||
2006 $comp eq 're' && $value !~ m/$expected/ ||
2007 $comp eq 'nr' && $value =~ m/$expected/
2008 )
2009 {
2010 return 0;
2011 }
2012 }
2013 return 1;
2014 }
2015  
2016  
2017  
2018 #------------------------------------------------------------------------------
2019 # Generates text from contents of feature structure so it can be printed.
2020 #------------------------------------------------------------------------------
2021 sub as_string
2022 {
2023 my $self = shift;
2024 my @features = $self->get_nonempty_features();
2025 my @assignments = map
2026 {
2027 my $f = $_;
2028 my $v;
2029 if($f eq 'other')
2030 {
2031 $v = structure_to_string($self->{$f});
2032 }
2033 else
2034 {
2035 $v = '"'.$self->get_joined($f).'"';
2036 }
2037 "$f=$v";
2038 }
2039 (@features);
2040 return "[".join(', ', @assignments)."]";
2041 }
2042  
2043  
2044  
2045 #------------------------------------------------------------------------------
2046 # Generates text from contents of feature structure in the form used in the
2047 # FEATS column of the CoNLL-X file format. The tagset and other features will
2048 # be omitted. Multivalues will be separated by a comma because the vertical bar
2049 # separates features.
2050 #------------------------------------------------------------------------------
2051 sub as_string_conllx
2052 {
2053 my $self = shift;
2054 my @features = grep {$_ ne 'tagset' && $_ ne 'other' && defined($self->{$_}) && $self->{$_} ne ''} known_features();
2055 my @assignments = map
2056 {
2057 my $f = $_;
2058 my $v = $self->get_joined($f);
2059 $v =~ s/\|/,/g;
2060 "$f=$v";
2061 }
2062 (@features);
2063 return (scalar(@assignments) > 0) ? join('|', @assignments) : '_';
2064 }
2065  
2066  
2067  
2068 #------------------------------------------------------------------------------
2069 # Recursively converts a structure to string describing a Perl constant.
2070 # Useful for using eval.
2071 #------------------------------------------------------------------------------
2072 sub structure_to_string
2073 {
2074 my $source = shift;
2075 my $string;
2076 my $ref = ref($source);
2077 if($ref eq 'ARRAY')
2078 {
2079 $string = "[".join(", ", map{structure_to_string($_)}(@{$source}))."]";
2080 }
2081 elsif($ref eq 'HASH')
2082 {
2083 $string = "{".join(", ", map{structure_to_string($_)." => ".structure_to_string($source->{$_})}(keys(%{$source})))."}";
2084 }
2085 else
2086 {
2087 $string = $source;
2088 $string = '' if(!defined($string));
2089 $string =~ s/([\\"\$\@])/\\$1/g; # "
2090 $string = "\"$string\"";
2091 }
2092 return $string;
2093 }
2094  
2095  
2096  
2097 ###############################################################################
2098 # Shortcuts for some frequent tests people want to do against Interset.
2099 ###############################################################################
2100  
2101 #------------------------------------------------------------------------------
2102 sub is_noun {my $self = shift; return $self->contains('pos', 'noun');}
2103  
2104 #------------------------------------------------------------------------------
2105 sub is_abbreviation {my $self = shift; return $self->abbr() eq 'yes';}
2106  
2107 #------------------------------------------------------------------------------
2108 sub is_abessive {my $self = shift; return $self->contains('case', 'abe');}
2109  
2110 #------------------------------------------------------------------------------
2111 sub is_ablative {my $self = shift; return $self->contains('case', 'abl');}
2112  
2113 #------------------------------------------------------------------------------
2114 sub is_absolute_superlative {my $self = shift; return $self->contains('degree', 'abs');}
2115  
2116 #------------------------------------------------------------------------------
2117 sub is_absolutive {my $self = shift; return $self->contains('case', 'abs');}
2118  
2119 #------------------------------------------------------------------------------
2120 sub is_accusative {my $self = shift; return $self->contains('case', 'acc');}
2121  
2122 #------------------------------------------------------------------------------
2123 sub is_active {my $self = shift; return $self->contains('voice', 'act');}
2124  
2125 #------------------------------------------------------------------------------
2126 sub is_additive {my $self = shift; return $self->contains('case', 'add');}
2127  
2128 #------------------------------------------------------------------------------
2129 sub is_adessive {my $self = shift; return $self->contains('case', 'ade');}
2130  
2131 #------------------------------------------------------------------------------
2132 sub is_adjective {my $self = shift; return $self->contains('pos', 'adj');}
2133  
2134 #------------------------------------------------------------------------------
2135 sub is_admirative {my $self = shift; return $self->contains('mood', 'adm');}
2136  
2137 #------------------------------------------------------------------------------
2138 sub is_adposition {my $self = shift; return $self->contains('pos', 'adp');}
2139  
2140 #------------------------------------------------------------------------------
2141 sub is_adverb {my $self = shift; return $self->contains('pos', 'adv');}
2142  
2143 #------------------------------------------------------------------------------
2144 sub is_affirmative {my $self = shift; return $self->contains('negativeness', 'pos');}
2145  
2146 #------------------------------------------------------------------------------
2147 sub is_allative {my $self = shift; return $self->contains('case', 'all');}
2148  
2149 #------------------------------------------------------------------------------
2150 sub is_animate {my $self = shift; return $self->contains('animacy', 'anim');}
2151  
2152 #------------------------------------------------------------------------------
2153 sub is_antipassive {my $self = shift; return $self->contains('voice', 'antip');}
2154  
2155 #------------------------------------------------------------------------------
2156 sub is_aorist {my $self = shift; return $self->contains('tense', 'aor');}
2157  
2158 #------------------------------------------------------------------------------
2159 sub is_archaic {my $self = shift; return $self->contains('style', 'arch');}
2160  
2161 #------------------------------------------------------------------------------
2162 sub is_article {my $self = shift; return $self->contains('prontype', 'art');}
2163  
2164 #------------------------------------------------------------------------------
2165 sub is_associative {my $self = shift; return $self->contains('case', 'com');}
2166  
2167 #------------------------------------------------------------------------------
2168 sub is_augmentative {my $self = shift; return $self->contains('degree', 'aug');}
2169  
2170 #------------------------------------------------------------------------------
2171 sub is_auxiliary {my $self = shift; return $self->contains('verbtype', 'aux');}
2172  
2173 #------------------------------------------------------------------------------
2174 sub is_benefactive {my $self = shift; return $self->contains('case', 'ben');}
2175  
2176 #------------------------------------------------------------------------------
2177 sub is_cardinal {my $self = shift; return $self->contains('numtype', 'card');}
2178  
2179 #------------------------------------------------------------------------------
2180 sub is_colloquial {my $self = shift; return $self->contains('style', 'coll');}
2181  
2182 #------------------------------------------------------------------------------
2183 sub is_comitative {my $self = shift; return $self->contains('case', 'com');}
2184  
2185 #------------------------------------------------------------------------------
2186 sub is_common_gender {my $self = shift; return $self->contains('gender', 'com');}
2187  
2188 #------------------------------------------------------------------------------
2189 sub is_comparative {my $self = shift; return $self->contains('degree', 'cmp') || $self->contains('case', 'cmp');}
2190  
2191 #------------------------------------------------------------------------------
2192 sub is_conditional {my $self = shift; return $self->contains('mood', 'cnd');}
2193  
2194 #------------------------------------------------------------------------------
2195 sub is_conjunction {my $self = shift; return $self->contains('pos', 'conj');}
2196  
2197 #------------------------------------------------------------------------------
2198 sub is_conjunctive {my $self = shift; return $self->contains('mood', 'sub');}
2199  
2200 #------------------------------------------------------------------------------
2201 sub is_construct {my $self = shift; return $self->contains('definite', 'cons');}
2202  
2203 #------------------------------------------------------------------------------
2204 sub is_converb {my $self = shift; return $self->contains('verbform', 'conv');}
2205  
2206 #------------------------------------------------------------------------------
2207 sub is_coordinator {my $self = shift; return $self->is_conjunction() && $self->conjtype() eq 'coor';}
2208  
2209 #------------------------------------------------------------------------------
2210 sub is_count_plural {my $self = shift; return $self->contains('number', 'count');}
2211  
2212 #------------------------------------------------------------------------------
2213 sub is_dative {my $self = shift; return $self->contains('case', 'dat');}
2214  
2215 #------------------------------------------------------------------------------
2216 sub is_definite {my $self = shift; return $self->contains('definite', 'def');}
2217  
2218 #------------------------------------------------------------------------------
2219 sub is_delative {my $self = shift; return $self->contains('case', 'del');}
2220  
2221 #------------------------------------------------------------------------------
2222 sub is_demonstrative {my $self = shift; return $self->contains('prontype', 'dem');}
2223  
2224 #------------------------------------------------------------------------------
2225 sub is_desiderative {my $self = shift; return $self->contains('mood', 'des');}
2226  
2227 #------------------------------------------------------------------------------
2228 sub is_destinative {my $self = shift; return $self->contains('case', 'ben');}
2229  
2230 #------------------------------------------------------------------------------
2231 sub is_determiner {my $self = shift; return $self->is_pronominal() && $self->is_adjective();}
2232  
2233 #------------------------------------------------------------------------------
2234 sub is_diminutive {my $self = shift; return $self->contains('degree', 'dim');}
2235  
2236 #------------------------------------------------------------------------------
2237 sub is_direct_voice {my $self = shift; return $self->contains('voice', 'dir');}
2238  
2239 #------------------------------------------------------------------------------
2240 sub is_distributive {my $self = shift; return $self->contains('case', 'dis');}
2241  
2242 #------------------------------------------------------------------------------
2243 sub is_dual {my $self = shift; return $self->contains('number', 'dual');}
2244  
2245 #------------------------------------------------------------------------------
2246 sub is_elative {my $self = shift; return $self->contains('case', 'ela');}
2247  
2248 #------------------------------------------------------------------------------
2249 sub is_elevating {my $self = shift; return $self->contains('polite', 'elev');}
2250  
2251 #------------------------------------------------------------------------------
2252 sub is_equative {my $self = shift; return $self->contains('degree', 'equ') || $self->contains('case', 'equ');}
2253  
2254 #------------------------------------------------------------------------------
2255 sub is_ergative {my $self = shift; return $self->contains('case', 'erg');}
2256  
2257 #------------------------------------------------------------------------------
2258 sub is_essive {my $self = shift; return $self->contains('case', 'ess');}
2259  
2260 #------------------------------------------------------------------------------
2261 sub is_exclamative {my $self = shift; return $self->contains('prontype', 'exc');}
2262  
2263 #------------------------------------------------------------------------------
2264 sub is_exclusive {my $self = shift; return $self->contains('clusivity', 'ex');}
2265  
2266 #------------------------------------------------------------------------------
2267 sub is_factive {my $self = shift; return $self->contains('case', 'tra');}
2268  
2269 #------------------------------------------------------------------------------
2270 sub is_feminine {my $self = shift; return $self->contains('gender', 'fem');}
2271  
2272 #------------------------------------------------------------------------------
2273 sub is_finite_verb {my $self = shift; return $self->contains('verbform', 'fin');}
2274  
2275 #------------------------------------------------------------------------------
2276 sub is_first_hand {my $self = shift; return $self->contains('evident', 'fh');}
2277  
2278 #------------------------------------------------------------------------------
2279 sub is_first_person {my $self = shift; return $self->contains('person', '1');}
2280  
2281 #------------------------------------------------------------------------------
2282 sub is_foreign {my $self = shift; return $self->foreign() eq 'yes';}
2283  
2284 #------------------------------------------------------------------------------
2285 sub is_formal {my $self = shift; return $self->contains('polite', 'form');}
2286  
2287 #------------------------------------------------------------------------------
2288 sub is_fourth_person {my $self = shift; return $self->contains('person', '4');}
2289  
2290 #------------------------------------------------------------------------------
2291 sub is_future {my $self = shift; return $self->contains('tense', 'fut');}
2292  
2293 #------------------------------------------------------------------------------
2294 sub is_genitive {my $self = shift; return $self->contains('case', 'gen');}
2295  
2296 #------------------------------------------------------------------------------
2297 sub is_gerund {my $self = shift; return $self->contains('verbform', 'ger');}
2298  
2299 #------------------------------------------------------------------------------
2300 sub is_gerundive {my $self = shift; return $self->contains('verbform', 'gdv');}
2301  
2302 #------------------------------------------------------------------------------
2303 sub is_greater_paucal {my $self = shift; return $self->contains('number', 'grpa');}
2304  
2305 #------------------------------------------------------------------------------
2306 sub is_greater_plural {my $self = shift; return $self->contains('number', 'grpl');}
2307  
2308 #------------------------------------------------------------------------------
2309 sub is_habitual {my $self = shift; return $self->contains('aspect', 'hab');}
2310  
2311 #------------------------------------------------------------------------------
2312 sub is_human {my $self = shift; return $self->contains('animacy', 'hum');}
2313  
2314 #------------------------------------------------------------------------------
2315 sub is_humbling {my $self = shift; return $self->contains('polite', 'humb');}
2316  
2317 #------------------------------------------------------------------------------
2318 sub is_hyph {my $self = shift; return $self->hyph() eq 'yes';}
2319  
2320 #------------------------------------------------------------------------------
2321 sub is_illative {my $self = shift; return $self->contains('case', 'ill');}
2322  
2323 #------------------------------------------------------------------------------
2324 sub is_imperative {my $self = shift; return $self->contains('mood', 'imp');}
2325  
2326 #------------------------------------------------------------------------------
2327 sub is_imperfect {my $self = shift; return $self->contains('tense', 'imp') || $self->contains('aspect', 'imp');}
2328  
2329 #------------------------------------------------------------------------------
2330 sub is_impersonal {my $self = shift; return $self->contains('person', '0');}
2331  
2332 #------------------------------------------------------------------------------
2333 sub is_inanimate {my $self = shift; return $self->contains('animacy', 'inan');}
2334  
2335 #------------------------------------------------------------------------------
2336 sub is_inclusive {my $self = shift; return $self->contains('clusivity', 'in');}
2337  
2338 #------------------------------------------------------------------------------
2339 sub is_indefinite {my $self = shift; return $self->contains('prontype', 'ind') || $self->contains('definite', 'ind');}
2340  
2341 #------------------------------------------------------------------------------
2342 sub is_indicative {my $self = shift; return $self->contains('mood', 'ind');}
2343  
2344 #------------------------------------------------------------------------------
2345 sub is_inessive {my $self = shift; return $self->contains('case', 'ine');}
2346  
2347 #------------------------------------------------------------------------------
2348 sub is_infinitive {my $self = shift; return $self->contains('verbform', 'inf');}
2349  
2350 #------------------------------------------------------------------------------
2351 sub is_informal {my $self = shift; return $self->contains('polite', 'infm');}
2352  
2353 #------------------------------------------------------------------------------
2354 sub is_instructive {my $self = shift; return $self->contains('case', 'ins');}
2355  
2356 #------------------------------------------------------------------------------
2357 sub is_instrumental {my $self = shift; return $self->contains('case', 'ins');}
2358  
2359 #------------------------------------------------------------------------------
2360 sub is_interjection {my $self = shift; $self->contains('pos', 'int');}
2361  
2362 #------------------------------------------------------------------------------
2363 sub is_interrogative {my $self = shift; $self->contains('prontype', 'int');}
2364  
2365 #------------------------------------------------------------------------------
2366 sub is_intransitive {my $self = shift; return $self->contains('subcat', 'intr');}
2367  
2368 #------------------------------------------------------------------------------
2369 sub is_inverse_number {my $self = shift; return $self->contains('number', 'inv');}
2370  
2371 #------------------------------------------------------------------------------
2372 sub is_inverse_voice {my $self = shift; return $self->contains('voice', 'inv');}
2373  
2374 #------------------------------------------------------------------------------
2375 sub is_iterative {my $self = shift; return $self->contains('aspect', 'iter');}
2376  
2377 #------------------------------------------------------------------------------
2378 sub is_jussive {my $self = shift; return $self->contains('mood', 'jus');}
2379  
2380 #------------------------------------------------------------------------------
2381 sub is_lative {my $self = shift; return $self->contains('case', 'lat');}
2382  
2383 #------------------------------------------------------------------------------
2384 sub is_locative {my $self = shift; return $self->contains('case', 'loc');}
2385  
2386 #------------------------------------------------------------------------------
2387 sub is_masculine {my $self = shift; return $self->contains('gender', 'masc');}
2388  
2389 #------------------------------------------------------------------------------
2390 sub is_mediopassive {my $self = shift; return $self->is_middle_voice() && $self->is_passive();}
2391  
2392 #------------------------------------------------------------------------------
2393 sub is_middle_voice {my $self = shift; return $self->contains('voice', 'mid');}
2394  
2395 #------------------------------------------------------------------------------
2396 sub is_modal {my $self = shift; return $self->contains('verbtype', 'mod');}
2397  
2398 #------------------------------------------------------------------------------
2399 sub is_motivative {my $self = shift; return $self->contains('case', 'cau');}
2400  
2401 #------------------------------------------------------------------------------
2402 sub is_multiplicative {my $self = shift; return $self->contains('numtype', 'mult');}
2403  
2404 #------------------------------------------------------------------------------
2405 sub is_narrative {my $self = shift; return $self->contains('tense', 'past') && $self->contains('evident', 'nfh');}
2406  
2407 #------------------------------------------------------------------------------
2408 sub is_necessitative {my $self = shift; return $self->contains('mood', 'nec');}
2409  
2410 #------------------------------------------------------------------------------
2411 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'
2412  
2413 #------------------------------------------------------------------------------
2414 sub is_nominative {my $self = shift; return $self->contains('case', 'nom');}
2415  
2416 #------------------------------------------------------------------------------
2417 sub is_non_first_hand {my $self = shift; return $self->contains('evident', 'nfh');}
2418  
2419 #------------------------------------------------------------------------------
2420 sub is_nonhuman {my $self = shift; return $self->contains('animacy', 'nhum');}
2421  
2422 #------------------------------------------------------------------------------
2423 sub is_neuter {my $self = shift; return $self->contains('gender', 'neut');}
2424  
2425 #------------------------------------------------------------------------------
2426 sub is_numeral {my $self = shift; return $self->contains('pos', 'num') || $self->numtype() ne '';}
2427  
2428 #------------------------------------------------------------------------------
2429 sub is_optative {my $self = shift; return $self->contains('mood', 'opt');}
2430  
2431 #------------------------------------------------------------------------------
2432 sub is_ordinal {my $self = shift; return $self->contains('numtype', 'ord');}
2433  
2434 #------------------------------------------------------------------------------
2435 sub is_participle {my $self = shift; return $self->contains('verbform', 'part');}
2436  
2437 #------------------------------------------------------------------------------
2438 sub is_particle {my $self = shift; return $self->contains('pos', 'part');}
2439  
2440 #------------------------------------------------------------------------------
2441 sub is_partitive {my $self = shift; return $self->contains('case', 'par');}
2442  
2443 #------------------------------------------------------------------------------
2444 sub is_passive {my $self = shift; return $self->contains('voice', 'pass');}
2445  
2446 #------------------------------------------------------------------------------
2447 sub is_past {my $self = shift; return $self->contains('tense', 'past');}
2448  
2449 #------------------------------------------------------------------------------
2450 sub is_paucal {my $self = shift; return $self->contains('number', 'pauc');}
2451  
2452 #------------------------------------------------------------------------------
2453 sub is_perfect {my $self = shift; return $self->contains('aspect', 'perf');}
2454  
2455 #------------------------------------------------------------------------------
2456 sub is_personal {my $self = shift; return $self->contains('prontype', 'prs');}
2457  
2458 #------------------------------------------------------------------------------
2459 sub is_personal_pronoun {my $self = shift; return $self->contains('prontype', 'prs');}
2460  
2461 #------------------------------------------------------------------------------
2462 sub is_pluperfect {my $self = shift; return $self->contains('tense', 'pqp');}
2463  
2464 #------------------------------------------------------------------------------
2465 sub is_plural {my $self = shift; return $self->contains('number', 'plur');}
2466  
2467 #------------------------------------------------------------------------------
2468 sub is_polite {my $self = shift; return $self->contains('polite', 'form');}
2469  
2470 #------------------------------------------------------------------------------
2471 sub is_positive {my $self = shift; return $self->contains('degree', 'pos');}
2472  
2473 #------------------------------------------------------------------------------
2474 sub is_possessive {my $self = shift; return $self->poss() eq 'yes';}
2475  
2476 #------------------------------------------------------------------------------
2477 sub is_potential {my $self = shift; return $self->contains('mood', 'pot');}
2478  
2479 #------------------------------------------------------------------------------
2480 sub is_present {my $self = shift; return $self->contains('tense', 'pres');}
2481  
2482 #------------------------------------------------------------------------------
2483 sub is_prolative {my $self = shift; return $self->contains('case', 'ess');}
2484  
2485 #------------------------------------------------------------------------------
2486 sub is_pronominal {my $self = shift; return $self->prontype() ne '';}
2487  
2488 #------------------------------------------------------------------------------
2489 sub is_pronoun {my $self = shift; return $self->is_pronominal() && $self->is_noun();}
2490  
2491 #------------------------------------------------------------------------------
2492 sub is_proper_noun {my $self = shift; return $self->contains('nountype', 'prop');}
2493  
2494 #------------------------------------------------------------------------------
2495 sub is_progressive {my $self = shift; return $self->contains('aspect', 'prog');}
2496  
2497 #------------------------------------------------------------------------------
2498 sub is_prospective {my $self = shift; return $self->contains('aspect', 'prosp');}
2499  
2500 #------------------------------------------------------------------------------
2501 sub is_punctuation {my $self = shift; return $self->contains('pos', 'punc');}
2502  
2503 #------------------------------------------------------------------------------
2504 sub is_purposive {my $self = shift; return $self->contains('mood', 'prp');}
2505  
2506 #------------------------------------------------------------------------------
2507 sub is_quotative {my $self = shift; return $self->contains('mood', 'qot');}
2508  
2509 #------------------------------------------------------------------------------
2510 sub is_rare {my $self = shift; return $self->contains('style', 'rare');}
2511  
2512 #------------------------------------------------------------------------------
2513 sub is_reciprocal {my $self = shift; return $self->contains('prontype', 'rcp') || $self->contains('voice', 'rcp');}
2514  
2515 #------------------------------------------------------------------------------
2516 sub is_reflexive {my $self = shift; return $self->reflex() eq 'yes';}
2517  
2518 #------------------------------------------------------------------------------
2519 sub is_relative {my $self = shift; $self->contains('prontype', 'rel');}
2520  
2521 #------------------------------------------------------------------------------
2522 sub is_second_person {my $self = shift; return $self->contains('person', '2');}
2523  
2524 #------------------------------------------------------------------------------
2525 sub is_singular {my $self = shift; return $self->contains('number', 'sing');}
2526  
2527 #------------------------------------------------------------------------------
2528 sub is_specific {my $self = shift; return $self->contains('definite', 'spec');}
2529  
2530 #------------------------------------------------------------------------------
2531 sub is_subjunctive {my $self = shift; return $self->contains('mood', 'sub');}
2532  
2533 #------------------------------------------------------------------------------
2534 sub is_sublative {my $self = shift; return $self->contains('case', 'sub');}
2535  
2536 #------------------------------------------------------------------------------
2537 sub is_subordinator {my $self = shift; return $self->is_conjunction() && $self->conjtype() eq 'sub';}
2538  
2539 #------------------------------------------------------------------------------
2540 sub is_superessive {my $self = shift; return $self->contains('case', 'sup');}
2541  
2542 #------------------------------------------------------------------------------
2543 sub is_superlative {my $self = shift; return $self->contains('degree', 'sup');}
2544  
2545 #------------------------------------------------------------------------------
2546 sub is_supine {my $self = shift; return $self->contains('verbform', 'sup');}
2547  
2548 #------------------------------------------------------------------------------
2549 sub is_symbol {my $self = shift; return $self->contains('pos', 'sym');}
2550  
2551 #------------------------------------------------------------------------------
2552 sub is_temporal {my $self = shift; return $self->contains('case', 'tem');}
2553  
2554 #------------------------------------------------------------------------------
2555 sub is_terminative {my $self = shift; return $self->contains('case', 'ter');}
2556  
2557 #------------------------------------------------------------------------------
2558 sub is_third_person {my $self = shift; return $self->contains('person', '3');}
2559  
2560 #------------------------------------------------------------------------------
2561 sub is_total {my $self = shift; return $self->contains('prontype', 'tot');}
2562  
2563 #------------------------------------------------------------------------------
2564 sub is_transgressive {my $self = shift; return $self->contains('verbform', 'conv');}
2565  
2566 #------------------------------------------------------------------------------
2567 sub is_transitive {my $self = shift; return $self->contains('subcat', 'tran');}
2568  
2569 #------------------------------------------------------------------------------
2570 sub is_translative {my $self = shift; return $self->contains('case', 'tra');}
2571  
2572 #------------------------------------------------------------------------------
2573 sub is_trial {my $self = shift; return $self->contains('number', 'tri');}
2574  
2575 #------------------------------------------------------------------------------
2576 sub is_typo {my $self = shift; return $self->typo() eq 'yes';}
2577  
2578 #------------------------------------------------------------------------------
2579 sub is_verb {my $self = shift; return $self->contains('pos', 'verb');}
2580  
2581 #------------------------------------------------------------------------------
2582 sub is_verbal_noun {my $self = shift; return $self->contains('verbform', 'vnoun');}
2583  
2584 #------------------------------------------------------------------------------
2585 sub is_vocative {my $self = shift; return $self->contains('case', 'voc');}
2586  
2587 #------------------------------------------------------------------------------
2588 sub is_wh {my $self = shift; return any {m/^(int|rel)$/} ($self->get_list('prontype'));}
2589  
2590 #------------------------------------------------------------------------------
2591 sub is_zero_person {my $self = shift; return $self->contains('person', '0');}
2592  
2593  
2594  
2595 ###############################################################################
2596 # ENFORCING PERMITTED (EXPECTED) VALUES IN FEATURE STRUCTURES
2597 ###############################################################################
2598  
2599  
2600  
2601 #------------------------------------------------------------------------------
2602 # Returns the set of replacement values for the case a feature value is not
2603 # permitted in a given context. The set is derived from the feature matrix
2604 # above. It is a hash{feature}{value0}, leading to a list of values that can be
2605 # used to replace the value0, ordered by priority.
2606 #------------------------------------------------------------------------------
2607 sub get_replacements
2608 {
2609 if(!defined($replacements))
2610 {
2611 $replacements = _preprocess_list_of_replacements();
2612 }
2613 return $replacements;
2614 }
2615  
2616  
2617  
2618 #------------------------------------------------------------------------------
2619 # Preprocesses the lists of replacement values defined above in the %matrix.
2620 # In the original tagset::common module, this code was in the BEGIN block and
2621 # it created the global hash %defaults1 from %defaults.
2622 #------------------------------------------------------------------------------
2623 sub _preprocess_list_of_replacements
2624 {
2625 # This is a lazy attribute and the builder can be called anytime, even
2626 # from map() or grep(). Avoid damaging the caller's $_!
2627 local $_;
2628 my %defaults1;
2629 # Loop over features.
2630 my @keys = keys(%matrix);
2631 foreach my $feature (@keys)
2632 {
2633 # For each feature, there is an array of arrays.
2634 # The first member of each second-order array is the value to replace.
2635 # The rest (if any) are the preferred replacements for this particular value.
2636 # First of all, collect preferred replacements for all values of this feature.
2637 my %map;
2638 foreach my $valarray (@{$matrix{$feature}{replacements}})
2639 {
2640 my $value = $valarray->[0];
2641 $map{$value}{$value}++;
2642 my @backoff;
2643 # Add all preferred replacements (if any) to the list.
2644 for(my $i = 1; $i<=$#{$valarray}; $i++)
2645 {
2646 push(@backoff, $valarray->[$i]);
2647 # Remember all values that have been added as replacements of $value.
2648 $map{$value}{$valarray->[$i]}++;
2649 }
2650 $defaults1{$feature}{$value} = \@backoff;
2651 }
2652 # The primary list of values constitutes the sequence of replacements for the empty value.
2653 foreach my $valarray (@{$matrix{$feature}{replacements}})
2654 {
2655 my $replacement = $valarray->[0];
2656 unless($map{''}{$replacement} || $replacement eq '')
2657 {
2658 push(@{$defaults1{$feature}{''}}, $replacement);
2659 $map{''}{$replacement}++;
2660 }
2661 }
2662 # If a value had preferred replacements, add replacements of the last preferred replacement. Check loops!
2663 # Loop over values again.
2664 foreach my $value (keys(%{$defaults1{$feature}}))
2665 {
2666 # Remember all visited values to prevent loops!
2667 my %visited;
2668 $visited{$value}++;
2669 # Find the last preferred replacement, if any.
2670 my $last;
2671 for(;;)
2672 {
2673 my $new_last;
2674 if(scalar(@{$defaults1{$feature}{$value}}))
2675 {
2676 $last = $defaults1{$feature}{$value}[$#{$defaults1{$feature}{$value}}];
2677 }
2678 # Unless the last preferred replacement has been visited, try to find its replacements.
2679 if($last)
2680 {
2681 unless($visited{$last})
2682 {
2683 $visited{$last}++;
2684 if(ref($defaults1{$feature}{$last}) ne 'ARRAY')
2685 {
2686 confess("Something went wrong when preparing replacement values for feature '$feature' and value '$last'");
2687 }
2688 my @replacements_of_last = @{$defaults1{$feature}{$last}};
2689 # If $last has replacements that $value does not have, add them to $value.
2690 foreach my $replacement (@replacements_of_last)
2691 {
2692 unless($map{$value}{$replacement} || $replacement eq $value)
2693 {
2694 push(@{$defaults1{$feature}{$value}}, $replacement);
2695 $map{$value}{$replacement}++;
2696 $new_last++;
2697 }
2698 }
2699 }
2700 }
2701 # If no $last has been found or if it has been visited, break the loop.
2702 last unless($new_last);
2703 }
2704 # The empty value and all other unvisited values are the next replacements to consider.
2705 foreach my $valarray ([''], @{$matrix{$feature}{replacements}})
2706 {
2707 my $replacement = $valarray->[0];
2708 unless($map{$value}{$replacement} || $replacement eq $value)
2709 {
2710 push(@{$defaults1{$feature}{$value}}, $replacement);
2711 $map{$value}{$replacement}++;
2712 }
2713 }
2714 # Debugging: print the complete list of replacements.
2715 # print STDERR ("$feature: $value:\t", join(', ', @{$defaults1{$feature}{$value}}), "\n");
2716 }
2717 }
2718 return \%defaults1;
2719 }
2720  
2721  
2722  
2723 #------------------------------------------------------------------------------
2724 # Compares two arrays of values. Prefers precision over recall. Accepts that
2725 # value X can serve as replacement of value Y, and counts it as 1/N occurrences
2726 # of Y. Replacements are retrieved from the global %matrix.
2727 #------------------------------------------------------------------------------
2728 sub get_similarity_of_arrays
2729 {
2730 my $feature = shift; # feature name needed to find default values
2731 my $srch = shift; # array reference
2732 my $eval = shift; # array reference
2733 my $defaults = get_replacements();
2734 # For each scalar searched, get replacement array (beginning with the scalar itself).
2735 my @menu; # 2-dimensional matrix
2736 for(my $i = 0; $i<=$#{$srch}; $i++)
2737 {
2738 push(@{$menu[$i]}, $srch->[$i]);
2739 push(@{$menu[$i]}, @{$defaults->{$feature}{$srch->[$i]}});
2740 }
2741 # Look for menu values in array being evaluated. If not found, look for replacements.
2742 my @found; # srch values matched to something in eval
2743 my @used; # eval values identified as something searched for
2744 my $n_found = 0; # how many srch values have been found
2745 my $n_used = 0; # how many eval values have been used
2746 my $n_srch = scalar(@{$srch});
2747 my $n_eval = scalar(@{$eval});
2748 my $score = 0; # number of hits, weighed (replacement is not a full hit, original value is)
2749 if(@menu)
2750 {
2751 # Loop over levels of replacement.
2752 for(my $i = 0; $i<=$#{$menu[0]} && $n_found<$n_srch && $n_used<$n_eval; $i++)
2753 {
2754 # Loop over searched values.
2755 for(my $j = 0; $j<=$#menu && $n_found<$n_srch && $n_used<$n_eval; $j++)
2756 {
2757 next if($found[$j]);
2758 # Look for i-th replacement of j-th value in the evaluated array.
2759 for(my $k = 0; $k<=$#{$eval}; $k++)
2760 {
2761 if(!$used[$k] && $eval->[$k] eq $menu[$j][$i])
2762 {
2763 $found[$j]++;
2764 $used[$k]++;
2765 $n_found++;
2766 $n_used++;
2767 # Add reward for this level of replacement.
2768 # (What fraction of an occurrence are we going to count for this?)
2769 $score += 1/($i+1);
2770 last;
2771 }
2772 }
2773 }
2774 }
2775 }
2776 # Use the score to compute precision and recall.
2777 my ($p, $r);
2778 $p = $score/$n_srch if($n_srch);
2779 $r = $score/$n_eval if($n_eval);
2780 # Prefer precision over recall.
2781 my $result = (2*$p+$r)/3;
2782 return $result;
2783 }
2784  
2785  
2786  
2787 #------------------------------------------------------------------------------
2788 # Selects the most suitable replacement. Can deal with arrays of values.
2789 #------------------------------------------------------------------------------
2790 sub select_replacement
2791 {
2792 my $feature = shift; # feature name needed to get default replacements of a value
2793 my $value = shift; # scalar or array reference
2794 my $permitted = shift; # hash reference; keys are permitted values; array values joint
2795 # The "tagset" and "other" features are special. All values are permitted.
2796 if($feature =~ m/^(tagset|other)$/)
2797 {
2798 return $value;
2799 }
2800 # If value is not an array, make it an array.
2801 my @values = ref($value) eq 'ARRAY' ? @{$value} : ($value);
2802 # Convert every permitted value to an array as well.
2803 my @permitted = keys(%{$permitted});
2804 if(!scalar(@permitted))
2805 {
2806 print STDERR ("Feature = $feature\n");
2807 print STDERR ("Value to replace = ", array_to_scalar_value($value), "\n");
2808 confess("Cannot select a replacement if no values are permitted.\n");
2809 }
2810 my %suitability;
2811 foreach my $p (@permitted)
2812 {
2813 # Warning: split converts empty values to empty array but we want array with one empty element.
2814 my @pvalues = split(/\|/, $p);
2815 $pvalues[0] = '' unless(@pvalues);
2816 # Get suitability evaluation for $p.
2817 $suitability{$p} = get_similarity_of_arrays($feature, \@values, \@pvalues);
2818 }
2819 # Return the most suitable permitted value.
2820 @permitted = sort {$suitability{$b} <=> $suitability{$a}} (@permitted);
2821 # If the replacement is an array, return a reference to it.
2822 my @repl = split(/\|/, $permitted[0]);
2823 if(scalar(@repl)==0)
2824 {
2825 return '';
2826 }
2827 elsif(scalar(@repl)==1)
2828 {
2829 return $repl[0];
2830 }
2831 else
2832 {
2833 return \@repl;
2834 }
2835 }
2836  
2837  
2838  
2839 #------------------------------------------------------------------------------
2840 # Makes sure that a feature structure complies with the permitted combinations
2841 # recorded in a trie. Replaces feature values if needed.
2842 #------------------------------------------------------------------------------
2843 sub enforce_permitted_values
2844 {
2845 my $self = shift;
2846 my $trie = shift; # Lingua::Interset::Trie
2847 my $pointer = shift; # reference to a hash inside the trie, not necessarily the root hash
2848 if(!defined($pointer))
2849 {
2850 $pointer = $trie->root_hash();
2851 }
2852 my $debug = 0;
2853 my $features = $trie->features();
2854 my @features = @{$features};
2855 foreach my $feature (@features)
2856 {
2857 my $value = $self->get($feature);
2858 print("$feature: ", join(', ', map {"'$_' => '$pointer->{$_}'"} sort keys(%{$pointer})), "\n") if($debug);
2859 unless(exists($pointer->{$value}))
2860 {
2861 my $replacement = select_replacement($feature, $value, $pointer);
2862 print("$feature: '$value' => '$replacement'\n") if($debug);
2863 $self->set($feature, $replacement);
2864 $value = $replacement;
2865 }
2866 elsif($debug)
2867 {
2868 print("$feature: '$value' is ok\n");
2869 }
2870 $pointer = $trie->advance_pointer($pointer, $feature, $value);
2871 }
2872 }
2873  
2874  
2875  
2876 ###############################################################################
2877 # GENERIC FEATURE STRUCTURE MANIPULATION
2878 # The following section contains feature-structure-related static functions,
2879 # not just methods (no $self parameter is expected).
2880 ###############################################################################
2881  
2882  
2883  
2884 #------------------------------------------------------------------------------
2885 # Compares two values, scalars or arrays, whether they are equal or not.
2886 #------------------------------------------------------------------------------
2887 sub iseq
2888 {
2889 my $a = shift;
2890 my $b = shift;
2891 if(ref($a) ne ref($b))
2892 {
2893 return 0;
2894 }
2895 elsif(ref($a) eq 'ARRAY')
2896 {
2897 return array_to_scalar_value($a) eq array_to_scalar_value($b);
2898 }
2899 else
2900 {
2901 return $a eq $b;
2902 }
2903 }
2904  
2905  
2906  
2907 #------------------------------------------------------------------------------
2908 # Converts array values to scalars. Sorts the array and combines all elements
2909 # in one string, using the vertical bar as delimiter. Does not care about
2910 # occurrences of vertical bars inside the elements (there should be none
2911 # anyway).
2912 #------------------------------------------------------------------------------
2913 sub array_to_scalar_value
2914 {
2915 my $value = shift;
2916 if(ref($value) eq 'ARRAY')
2917 {
2918 # The sorting helps to ensure that values from two arrays with the same
2919 # elements will be stringwise identical.
2920 $value = join('|', sort(@{$value}));
2921 }
2922 return $value;
2923 }
2924  
2925  
2926  
2927 #------------------------------------------------------------------------------
2928 # Creates a deep copy of a feature structure. If there is a reference to an
2929 # array of values, a copy of the array is created and the copy is referenced
2930 # from the new structure, rather than just copying the reference to the old
2931 # array. The same holds for the "other" feature, which can contain references
2932 # to arrays and / or hashes nested in unlimited number of levels. In fact, this
2933 # function could be used for any nested structures, not just feature
2934 # structures.
2935 #------------------------------------------------------------------------------
2936 sub duplicate
2937 {
2938 my $self = shift;
2939 my $srchash = $self->get_hash();
2940 my $tgthash = _duplicate_recursive($srchash);
2941 my $duplicate = new Lingua::Interset::FeatureStructure();
2942 $duplicate->set_hash($tgthash);
2943 return $duplicate;
2944 }
2945 sub _duplicate_recursive
2946 {
2947 my $source = shift;
2948 my $duplicate;
2949 my $ref = ref($source);
2950 if($ref eq 'ARRAY')
2951 {
2952 my @new_array;
2953 foreach my $element (@{$source})
2954 {
2955 push(@new_array, _duplicate_recursive($element));
2956 }
2957 $duplicate = \@new_array;
2958 }
2959 elsif($ref eq 'HASH')
2960 {
2961 my %new_hash;
2962 foreach my $key (keys(%{$source}))
2963 {
2964 $new_hash{$key} = _duplicate_recursive($source->{$key});
2965 }
2966 $duplicate = \%new_hash;
2967 }
2968 else
2969 {
2970 $duplicate = $source;
2971 }
2972 return $duplicate;
2973 }
2974  
2975  
2976  
2977 1;
2978  
2979 __END__
2980  
2981 =pod
2982  
2983 =encoding UTF-8
2984  
2985 =head1 NAME
2986  
2987 Lingua::Interset::FeatureStructure - Definition of morphosyntactic features and their values.
2988  
2989 =head1 VERSION
2990  
2991 version 3.015
2992  
2993 =head1 SYNOPSIS
2994  
2995 use Lingua::Interset::FeatureStructure;
2996  
2997 print(Lingua::Interset::FeatureStructure->known_features(), "\n");
2998  
2999 =head1 DESCRIPTION
3000  
3001 DZ Interset is a universal framework for reading, writing, converting and
3002 interpreting part-of-speech and morphosyntactic tags from multiple tagsets
3003 of many different natural languages.
3004  
3005 The C<FeatureStructure> class defines all morphosyntactic features and their values used
3006 in DZ Interset. An object of this class represents a morphosyntactic tag
3007 for a natural language word.
3008  
3009 More information is given at the DZ Interset project page,
3010 L<https://wiki.ufal.ms.mff.cuni.cz/user:zeman:interset:features>.
3011  
3012 =head1 METHODS
3013  
3014 =head2 set()
3015  
3016 A generic setter for any feature. These two statements do the same thing:
3017  
3018 $fs->set ('pos', 'noun');
3019 $fs->set_pos ('noun');
3020  
3021 If you want to set multiple values of a feature, there are several ways to do it:
3022  
3023 $fs->set ('tense', ['pres', 'fut']);
3024 $fs->set ('tense', 'pres', 'fut');
3025 $fs->set ('tense', 'pres|fut');
3026  
3027 All of the above mean that the word is either in present or in future tense.
3028  
3029 Note that the 'other' feature behaves differently.
3030 Its value can be structured, C<set()> will keep the structure and will not try to interpret it.
3031  
3032 Using generic C<set()> is more flexible than using specialized setters such as C<set_pos()>.
3033 Even if flexibility is not needed it is recommended to avoid the specialized setters and
3034 use the generic C<set()> method. If multiple values are set using an array reference,
3035 the specialized setters will not create a deep copy of the array, they will only copy
3036 the reference. Generic C<set()> will create a deep copy. The array will thus not be shared
3037 among several feature structures. If someone later retrieves the array reference
3038 via C<get()>, and decides to modify the array, they will probably expect to
3039 change only that particular feature structure and not others that happen to
3040 use the same array.
3041  
3042 =head2 add()
3043  
3044 $fs->add ('pos' => 'conj', 'conjtype' => 'coor');
3045  
3046 Sets several features at once.
3047 Takes a list of value assignments, i.e. an array of an even number of elements
3048 (feature1, value1, feature2, value2, ...)
3049 This is useful when defining decoders from physical tagsets.
3050 Typically, one wants to define a table of assignments for each part of speech or input feature:
3051  
3052 'CC' => ['pos' => 'conj', 'conjtype' => 'coor']
3053  
3054 =head2 set_hash()
3055  
3056 my %hash = ('pos' => 'noun', 'number' => 'plur');
3057 $fs->set_hash (\%hash);
3058  
3059 Takes a reference to a hash of features and their values.
3060 Sets the values of the features in this C<FeatureStructure>.
3061 Unknown features are ignored.
3062 Known features that are not set in the hash will be (re-)set to empty values.
3063  
3064 =head2 merge_hash_hard()
3065  
3066 my %hash = ('pos' => 'noun', 'number' => 'plur');
3067 $fs->merge_hash_hard (\%hash);
3068  
3069 Takes a reference to a hash of features and their values.
3070 Sets the values of the features in this C<FeatureStructure>.
3071 Unknown features are ignored.
3072 Known features that are not set in the hash will be left untouched;
3073 this is the difference from C<set_hash()>.
3074 However, if the current value of a feature is non-empty and the hash contains
3075 a different non-empty value, the current value will be replaced by the one from
3076 the hash.
3077  
3078 =head2 merge_hash_soft()
3079  
3080 my %hash = ('pos' => 'noun', 'number' => 'plur');
3081 $fs->merge_hash_soft (\%hash);
3082  
3083 Takes a reference to a hash of features and their values.
3084 Sets the values of the features in this C<FeatureStructure>.
3085 Unknown features are ignored.
3086 Known features that are not set in the hash will be left untouched;
3087 this is the difference from C<set_hash()>.
3088 Known features that are set both in the hash and in this feature structure
3089 will be merged into a list of values (any single value will occur at most once).
3090 This is the difference from C<merge_hash_hard()>.
3091  
3092 =head2 clear()
3093  
3094 A generic setter that clears the value of a feature, i.e. removes the feature
3095 from the feature structure. All the following statements do the same thing:
3096  
3097 $fs->clear ('pos');
3098 $fs->clear_pos();
3099 $fs->set ('pos', '');
3100 $fs->set ('pos', undef);
3101 $fs->set_pos ('');
3102 $fs->set_pos (undef);
3103  
3104 We can also clear several features at once:
3105  
3106 $fs->clear ('pos', 'prontype', 'gender');
3107  
3108 =head2 get_nonempty_features()
3109  
3110 Returns a list of names of features whose values are not empty.
3111  
3112 my @features = $fs->get_nonempty_features();
3113 my @values = map { $fs->get_joined ($_) } @features;
3114  
3115 The features are returned in a pre-defined (but not alphabetical) order.
3116  
3117 =head2 get()
3118  
3119 A generic getter for any feature. These two statements do the same thing:
3120  
3121 $pos = $fs->get ('pos');
3122 $pos = $fs->pos();
3123  
3124 Be warned that B<you can get an array reference> if the feature has multiple
3125 values. It is probably better to use one of the alternative C<get...()> functions
3126 where it is better defined what you can get.
3127  
3128 =head2 get_joined()
3129  
3130 Similar to C<get()> but always returns scalar.
3131 If there is an array of disjoint values, it sorts them alphabetically and
3132 joins them using the vertical bar. Example: C<'fem|masc'>.
3133 The sorting makes comparisons easier;
3134 it is assumed that the actual ordering is not significant
3135 and that C<'fem|masc'> is identical to C<'masc|fem'>.
3136  
3137 =head2 get_list()
3138  
3139 Similar to get but always returns list of values.
3140 If there is an array of disjoint values, this is the list.
3141 If there is a single value (empty or not), this value will be the only member of the list.
3142  
3143 Unlike in C<get_joined()>, this method does I<not sort> the list before returning it.
3144  
3145 =head2 get_hash()
3146  
3147 my $hashref = $fs->get_hash();
3148  
3149 Creates a hash of all non-empty features and their values.
3150 The values are identical to what the C<get($feature)> method would return;
3151 in particular, the value may be a reference to an array.
3152  
3153 Returns a reference to the hash.
3154  
3155 =head2 get_other_for_tagset()
3156  
3157 my $other = $fs->get_other_for_tagset ('cs::pdt');
3158  
3159 Takes a tagset id.
3160 If it matches the value of the C<tagset> feature, returns the value of the
3161 C<other> feature (it returns a deep copy, which the caller may freely modify).
3162 If the tagset id does not match, the method returns an empty string.
3163  
3164 =head2 set_other_subfeature()
3165  
3166 $fs->set_other_subfeature ('my_weird_feature', 'my_weird_value');
3167  
3168 Takes a non-Interset feature and its value and stores it as a subfeature of
3169 the feature C<other>. If C<other> is currently undefined, empty or anything
3170 else than a hash reference, the method will first create a new hash and store
3171 its reference in C<other>, overwriting its previous value (if any).
3172  
3173 If C<other> is a reference to a hash of subfeatures, the method will add the
3174 new subfeature and its value to the hash. If there has been a subfeature of the
3175 same name, its value will be overwritten.
3176  
3177 Only simple scalar values of subfeatures are assumed. It is not verified but
3178 no deep copy will be made if the value is a reference. Both the feature name
3179 and the value must be defined and non-empty, otherwise the method will do
3180 nothing.
3181  
3182 Note that the function does not check the current value of the C<tagset>
3183 feature. It is silently assumed that if you put anything in C<other>, you know
3184 that this is “your” feature structure.
3185  
3186 =head2 get_other_subfeature()
3187  
3188 my $value = $fs->get_other_subfeature ('cs::pdt', 'my_weird_feature');
3189  
3190 Takes a tagset id and name of a non-Interset feature, stored as a subfeature of
3191 C<other>. The C<other> feature may have arbitrary values ranging from plain
3192 scalars to references to multi-level nested structures of hashes and arrays.
3193 This method focuses on the case that C<other> contains a single-level hash.
3194 The hash keys can be seen as names of additional features that are otherwise
3195 not available in Interset. These additional features are subfeatures of
3196 C<other> and their values are strings. This is one of the most useful ways of
3197 deploying the C<other> feature.
3198  
3199 If the given tagset id matches the value of the C<tagset> feature,
3200 and the value of C<other> is a hash reference,
3201 the method uses the subfeature name as a key to the hash.
3202 If there is a value stored under the hash, it returns the value.
3203 (In case the value is not a string but a reference, a deep copy is created.)
3204 Otherwise it returns the empty string.
3205  
3206 =head2 is_other()
3207  
3208 if ($fs->is_other ('cs::pdt', 'my_weird_tag') ||
3209 $fs->is_other ('cs::pdt', 'my_weird_feature', 'much_weirder_value'))
3210 {
3211 ...
3212 }
3213  
3214 Takes a tagset id.
3215 If it does not match the value of the C<tagset> feature, returns an empty string.
3216 If the tagset ids do match, the method queries the value of the C<other>
3217 feature. Unlike C<get_other_for_tagset()>, it does not create a deep copy of the
3218 possibly structured value. Instead, it only checks whether the feature has or
3219 contains one particular scalar value.
3220  
3221 There are no a priori restrictions on the values of the C<other> feature.
3222 The value can be a multi-level nested structure of hashes and arrays if necessary.
3223 However, most of the time it will be either a scalar value, or a flat (one-level)
3224 hash of feature-value pairs that cannot be stored using standard Interset features.
3225  
3226 Besides the tagset id, this method takes one or two additional arguments.
3227 If the current value of C<other> is scalar, the method checks whether it equals
3228 to Argument 1. If the current value is an array reference, the method checks
3229 whether the array contains Argument 1. If the current value is a hash reference,
3230 the method interprets Argument 1 as hash key and checks whether the value
3231 stored under that key equals to Argument 2.
3232  
3233 It returns 1 when a match is found and 0 otherwise.
3234  
3235 =head2 contains()
3236  
3237 $fs->set ('prontype', 'int|rel');
3238 if($fs->contains ('prontype', 'int'))
3239 {
3240 print("One of the possible pronominal classes for this word is 'interrogative'.\n");
3241 }
3242  
3243 Takes a feature and a value.
3244 Tests whether the given value is one of the current values of the feature.
3245 This function can be used instead of simple C<< if($fs->prontype() eq 'int') >>
3246 whenever we believe that arrays of values could occur.
3247  
3248 =head2 set_upos()
3249  
3250 Sets feature values according to a universal part-of-speech tag as defined in 2014
3251 for the Universal Dependencies
3252 (L<http://universaldependencies.github.io/docs/>).
3253  
3254 =head2 get_upos(), upos()
3255  
3256 Returns the universal part-of-speech tag as defined in 2014
3257 for the Universal Dependencies
3258 (L<http://universaldependencies.github.io/docs/>).
3259  
3260 =head2 add_ufeatures()
3261  
3262 $fs->add_ufeatures ('Case=Nom', 'Gender=Masc,Neut');
3263  
3264 Takes a list of feature-value pairs in the format prescribed by the
3265 Universal Dependencies (L<http://universaldependencies.org/>), i.e.
3266 all features and values are capitalized, some features are renamed and all
3267 feature-value pairs are ordered alphabetically.
3268 Sets our feature values accordingly.
3269 Values of our features that are not mentioned in the input list will be left untouched.
3270  
3271 This method does not complain about unknown features or values.
3272 They will be stored as subfeatures of the C<other> feature.
3273 Hence it is possible to read the input even if it contains language-specific
3274 extensions that are not yet known to Interset.
3275  
3276 =head2 get_ufeatures()
3277  
3278 my @ufpairs = $fs->get_ufeatures();
3279 print (join ('|', @ufpairs));
3280  
3281 Returns the list of feature-value pairs in the format prescribed by the
3282 Universal Dependencies (L<http://universaldependencies.github.io/docs/>), i.e.
3283 all features and values are capitalized, some features are renamed and all
3284 feature-value pairs are ordered alphabetically.
3285  
3286 =head2 matches()
3287  
3288 if ($fs->matches ('pos' => 'noun', 'gender' => '!masc', 'number' => '~(dual|plur)'))
3289 {
3290 ...
3291 }
3292  
3293 Tests multiple features simultaneously.
3294 Input is a list of feature-value pairs, return value is 1 if the structure matches all these values.
3295 This function is an abbreviation for a series of C<get_joined()> calls in an if statement.
3296  
3297 If the expected value is preceded by "!", the actual value must not be equal to the expected value.
3298 If the expected value is preceded by "~", then it is a regular expression which the actual value must match.
3299 If the expected value is preceded by "!~", then it is a regular expression which the actual value must not match.
3300  
3301 =head2 as_string()
3302  
3303 Generates a textual representation of the feature structure so it can be printed.
3304 Features are in a predefined (but not alphabetical) order. Complex values of
3305 the C<other> feature are serialized in depth. If a feature has multiple values,
3306 they are sorted alphabetically and delimited by the vertical bar character.
3307 What follows is a sample output for the C<cs::pdt> tags C<NNMS1-----A---->,
3308 C<Ck-P1----------> and C<VpQW---XR-AA--->:
3309  
3310 [pos="noun", polarity="pos", gender="masc", animacy="anim", number="sing", case="nom", tagset="cs::pdt"]
3311 [pos="adj", numtype="ord", number="plur", case="nom", tagset="cs::pdt", other={"numtype" => "suffix"}]
3312 [pos="verb", polarity="pos", gender="fem|neut", number="plur|sing", verbform="part", tense="past", voice="act", tagset="cs::pdt"]
3313  
3314 =head2 as_string_conllx()
3315  
3316 Generates a textual representation of the feature structure in the form used in
3317 the FEATS column of the CoNLL-X file format. The C<tagset> and C<other>
3318 features are omitted. Features are in predefined (but not alphabetical) order.
3319 If a feature has multiple values, they are sorted alphabetically and delimited
3320 by comma (because the vertical bar is used to separate features).
3321 What follows is a sample output for the C<cs::pdt> tags C<NNMS1-----A---->,
3322 C<Ck-P1----------> and C<VpQW---XR-AA--->:
3323  
3324 pos=noun|polarity=pos|gender=masc|animacy=anim|number=sing|case=nom
3325 pos=adj|numtype=ord|number=plur|case=nom
3326 pos=verb|polarity=pos|gender=fem,neut|number=plur,sing|verbform=part|tense=past|voice=act
3327  
3328 If the values of all features (including C<pos>) are empty, the method
3329 returns the underscore character. Thus the result is never undefined or empty.
3330  
3331 =head2 is_noun()
3332 Also returns 1 if the C<pos> feature has multiple values and one of them is C<noun>, e.g.
3333 if C<get_joined('pos') eq 'noun|adj'>. Note that pronouns also have C<pos=noun>.
3334 If you want to exclude pronouns, test C<is_noun() && !is_pronominal()>.
3335  
3336 =head2 is_abbreviation()
3337  
3338 =head2 is_abessive()
3339  
3340 =head2 is_ablative()
3341  
3342 =head2 is_absolute_superlative()
3343  
3344 =head2 is_absolutive()
3345  
3346 =head2 is_accusative()
3347  
3348 =head2 is_active()
3349  
3350 =head2 is_additive()
3351  
3352 =head2 is_adessive()
3353  
3354 =head2 is_adjective()
3355  
3356 =head2 is_admirative()
3357  
3358 =head2 is_adposition()
3359  
3360 =head2 is_adverb()
3361  
3362 =head2 is_affirmative()
3363  
3364 =head2 is_allative()
3365  
3366 =head2 is_animate()
3367  
3368 =head2 is_antipassive()
3369  
3370 =head2 is_aorist()
3371  
3372 =head2 is_archaic()
3373  
3374 =head2 is_article()
3375  
3376 =head2 is_associative()
3377  
3378 =head2 is_augmentative()
3379  
3380 =head2 is_auxiliary()
3381  
3382 =head2 is_benefactive()
3383  
3384 =head2 is_cardinal()
3385  
3386 =head2 is_colloquial()
3387  
3388 =head2 is_comitative()
3389  
3390 =head2 is_common_gender()
3391  
3392 =head2 is_comparative()
3393  
3394 =head2 is_conditional()
3395  
3396 =head2 is_conjunction()
3397  
3398 =head2 is_conjunctive()
3399  
3400 =head2 is_construct()
3401  
3402 =head2 is_converb()
3403  
3404 =head2 is_coordinator()
3405  
3406 =head2 is_count_plural()
3407  
3408 =head2 is_dative()
3409  
3410 =head2 is_definite()
3411  
3412 =head2 is_delative()
3413  
3414 =head2 is_demonstrative()
3415  
3416 =head2 is_desiderative()
3417  
3418 =head2 is_destinative()
3419  
3420 =head2 is_determiner()
3421  
3422 =head2 is_diminutive()
3423  
3424 =head2 is_direct_voice()
3425  
3426 =head2 is_distributive()
3427  
3428 =head2 is_dual()
3429  
3430 =head2 is_elative()
3431  
3432 =head2 is_elevating()
3433  
3434 =head2 is_equative()
3435  
3436 =head2 is_ergative()
3437  
3438 =head2 is_essive()
3439  
3440 =head2 is_exclamative()
3441  
3442 =head2 is_exclusive()
3443  
3444 =head2 is_factive()
3445  
3446 =head2 is_feminine()
3447  
3448 =head2 is_finite_verb()
3449  
3450 =head2 is_first_hand()
3451  
3452 =head2 is_first_person()
3453  
3454 =head2 is_foreign()
3455  
3456 =head2 is_formal()
3457  
3458 =head2 is_fourth_person()
3459  
3460 =head2 is_future()
3461  
3462 =head2 is_genitive()
3463  
3464 =head2 is_gerund()
3465  
3466 =head2 is_gerundive()
3467  
3468 =head2 is_greater_paucal()
3469  
3470 =head2 is_greater_plural()
3471  
3472 =head2 is_habitual()
3473  
3474 =head2 is_human()
3475  
3476 =head2 is_humbling()
3477  
3478 =head2 is_hyph()
3479  
3480 =head2 is_illative()
3481  
3482 =head2 is_imperative()
3483  
3484 =head2 is_imperfect()
3485  
3486 =head2 is_impersonal()
3487  
3488 =head2 is_inanimate()
3489  
3490 =head2 is_inclusive()
3491  
3492 =head2 is_indefinite()
3493  
3494 =head2 is_indicative()
3495  
3496 =head2 is_inessive()
3497  
3498 =head2 is_infinitive()
3499  
3500 =head2 is_informal()
3501  
3502 =head2 is_instructive()
3503  
3504 =head2 is_instrumental()
3505  
3506 =head2 is_interjection()
3507  
3508 =head2 is_interrogative()
3509  
3510 =head2 is_intransitive()
3511  
3512 =head2 is_inverse_number()
3513  
3514 =head2 is_inverse_voice()
3515  
3516 =head2 is_iterative()
3517  
3518 =head2 is_jussive()
3519  
3520 =head2 is_lative()
3521  
3522 =head2 is_locative()
3523  
3524 =head2 is_masculine()
3525  
3526 =head2 is_mediopassive()
3527  
3528 =head2 is_middle_voice()
3529  
3530 =head2 is_modal()
3531  
3532 =head2 is_motivative()
3533  
3534 =head2 is_multiplicative()
3535  
3536 =head2 is_narrative()
3537  
3538 =head2 is_necessitative()
3539  
3540 =head2 is_negative()
3541  
3542 =head2 is_nominative()
3543  
3544 =head2 is_non_first_hand()
3545  
3546 =head2 is_nonhuman()
3547  
3548 =head2 is_neuter()
3549  
3550 =head2 is_numeral()
3551  
3552 =head2 is_optative()
3553  
3554 =head2 is_ordinal()
3555  
3556 =head2 is_participle()
3557  
3558 =head2 is_particle()
3559  
3560 =head2 is_partitive()
3561  
3562 =head2 is_passive()
3563  
3564 =head2 is_past()
3565  
3566 =head2 is_paucal()
3567  
3568 =head2 is_perfect()
3569  
3570 =head2 is_personal()
3571  
3572 =head2 is_personal_pronoun()
3573  
3574 =head2 is_pluperfect()
3575  
3576 =head2 is_plural()
3577  
3578 =head2 is_polite()
3579  
3580 =head2 is_positive()
3581  
3582 =head2 is_possessive()
3583  
3584 =head2 is_potential()
3585  
3586 =head2 is_present()
3587  
3588 =head2 is_prolative()
3589  
3590 =head2 is_pronominal()
3591  
3592 =head2 is_pronoun()
3593  
3594 =head2 is_proper_noun()
3595  
3596 =head2 is_progressive()
3597  
3598 =head2 is_prospective()
3599  
3600 =head2 is_punctuation()
3601  
3602 =head2 is_purposive()
3603  
3604 =head2 is_quotative()
3605  
3606 =head2 is_rare()
3607  
3608 =head2 is_reciprocal()
3609  
3610 =head2 is_reflexive()
3611  
3612 =head2 is_relative()
3613  
3614 =head2 is_second_person()
3615  
3616 =head2 is_singular()
3617  
3618 =head2 is_specific()
3619  
3620 =head2 is_subjunctive()
3621  
3622 =head2 is_sublative()
3623  
3624 =head2 is_subordinator()
3625  
3626 =head2 is_superessive()
3627  
3628 =head2 is_superlative()
3629  
3630 =head2 is_supine()
3631  
3632 =head2 is_symbol()
3633  
3634 =head2 is_temporal()
3635  
3636 =head2 is_terminative()
3637  
3638 =head2 is_third_person()
3639  
3640 =head2 is_total()
3641  
3642 =head2 is_transgressive()
3643  
3644 =head2 is_transitive()
3645  
3646 =head2 is_translative()
3647  
3648 =head2 is_trial()
3649  
3650 =head2 is_typo()
3651  
3652 =head2 is_verb()
3653  
3654 =head2 is_verbal_noun()
3655  
3656 =head2 is_vocative()
3657  
3658 =head2 is_wh()
3659  
3660 =head2 is_zero_person()
3661  
3662 =head2 enforce_permitted_values()
3663  
3664 $fs->enforce_permitted_values ($permitted_trie);
3665  
3666 Makes sure that a feature structure complies with the permitted combinations
3667 recorded in a trie.
3668 Takes a L<Lingua::Interset::Trie> object as a parameter.
3669 Replaces feature values if needed.
3670 (Note that even the empty value may or may not be permitted.)
3671  
3672 =head2 duplicate()
3673  
3674 Returns a new C<Lingua::Interset::FeatureStructure> object that is
3675 a duplicate of the current structure.
3676 Makes sure that a deep copy is constructed if there are any complex feature values.
3677  
3678 =head1 FUNCTIONS
3679  
3680 =head2 known_features()
3681  
3682 Returns the list of known feature names in print order.
3683  
3684 =head2 priority_features()
3685  
3686 Returns the list of known features ordered according to their default priority.
3687 The priority is used in L<Lingua::Interset::Trie> when one looks for the closest
3688 matching permitted structure.
3689  
3690 =head2 known_values()
3691  
3692 Returns the list of known values of a feature, in print order.
3693 Dies if asked about an unknown feature.
3694  
3695 =head2 feature_valid()
3696  
3697 Takes a string and returns a nonzero value if the string is a name of a known
3698 feature.
3699  
3700 =head2 value_valid()
3701  
3702 Takes two scalars, C<$feature> and C<$value>. Tells whether they are a valid
3703 (known) pair of feature name and value. A reference to a list of valid values
3704 is also a valid value. This function does not die when the feature is not valid.
3705  
3706 =head2 structure_to_string()
3707  
3708 Recursively converts a structure to a string.
3709 The string uses Perl syntax for constant structures, so it can be used in eval.
3710  
3711 =head2 get_replacements()
3712  
3713 my $replacements = Lingua::Interset::FeatureStructure->get_replacements();
3714 my $rep_adverb = $replacements->{pos}{adv};
3715 foreach my $r (@{$rep_adverb})
3716 {
3717 if(...)
3718 {
3719 # This replacement matches our constraints, let's use it.
3720 return $r;
3721 }
3722 }
3723  
3724 Returns the set of replacement values for the case a feature value is not
3725 permitted in a given context.
3726 It is a hash{feature}{value0}, leading to a list of values that can be
3727 used to replace the value0, ordered by priority.
3728  
3729 =head2 iseq()
3730  
3731 if (Lingua::Interset::FeatureStructure->iseq ($a, $b)) { ... }
3732  
3733 Compares two values, scalars or arrays, whether they are equal or not.
3734 Takes two parameters.
3735 Each of them can be a scalar or an array reference.
3736  
3737 =head2 array_to_scalar_value()
3738  
3739 Converts array values to scalars. Sorts the array and combines all elements
3740 in one string, using the vertical bar as delimiter. Does not care about
3741 occurrences of vertical bars inside the elements (there should be none
3742 anyway).
3743  
3744 Takes an array reference as parameter.
3745 If the parameter turns out to be a plain scalar, the function just returns it.
3746  
3747 =head1 SEE ALSO
3748  
3749 L<Lingua::Interset>,
3750 L<Lingua::Interset::Tagset>
3751  
3752 =head1 AUTHOR
3753  
3754 Dan Zeman <zeman@ufal.mff.cuni.cz>
3755  
3756 =head1 COPYRIGHT AND LICENSE
3757  
3758 This software is copyright (c) 2019 by Univerzita Karlova (Charles University).
3759  
3760 This is free software; you can redistribute it and/or modify it under
3761 the same terms as the Perl 5 programming language system itself.
3762  
3763 =cut