comparison env/lib/python3.9/site-packages/galaxy/util/inflection.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 #!/usr/bin/env python
2
3 # Copyright (c) 2006 Bermi Ferrer Martinez
4 #
5 # bermi a-t bermilabs - com
6 # See the end of this file for the free software, open source license (BSD-style).
7 #
8 # Modified by the Galaxy team.
9
10 import re
11
12
13 class Inflector:
14 """
15 Inflector for pluralizing and singularizing English nouns.
16 """
17 # This is a small subset of words that either have the same singular and plural form, or have no singular form
18 NONCHANGING_WORDS = {
19 'equipment',
20 'information',
21 'rice',
22 'money',
23 'species',
24 'series',
25 'sheep',
26 'sms',
27 }
28
29 IRREGULAR_WORDS = {
30 'person': 'people',
31 'man': 'men',
32 'child': 'children',
33 'sex': 'sexes',
34 'move': 'moves',
35 'octopus': 'octopi',
36 }
37
38 PLURALIZE_RULES = (
39 ('(?i)(quiz)$', '\\1zes'),
40 ('(?i)^(ox)$', '\\1en'),
41 ('(?i)([m|l])ouse$', '\\1ice'),
42 ('(?i)(matr|vert|ind)ix|ex$', '\\1ices'),
43 ('(?i)(x|ch|ss|sh)$', '\\1es'),
44 ('(?i)([^aeiouy]|qu)ies$', '\\1y'),
45 ('(?i)([^aeiouy]|qu)y$', '\\1ies'),
46 ('(?i)(hive)$', '\\1s'),
47 ('(?i)(?:([^f])fe|([lr])f)$', '\\1\\2ves'),
48 ('(?i)sis$', 'ses'),
49 ('(?i)([ti])um$', '\\1a'),
50 ('(?i)(buffal|tomat)o$', '\\1oes'),
51 ('(?i)(bu)s$', '\\1ses'),
52 ('(?i)(alias|status|virus)', '\\1es'),
53 ('(?i)(ax|test)is$', '\\1es'),
54 ('(?i)s$', 's'),
55 ('(?i)$', 's')
56 )
57
58 SINGULARIZE_RULES = (
59 ('(?i)(quiz)zes$', '\\1'),
60 ('(?i)(matr)ices$', '\\1ix'),
61 ('(?i)(vert|ind)ices$', '\\1ex'),
62 ('(?i)^(ox)en', '\\1'),
63 ('(?i)(alias|status|virus)es$', '\\1'),
64 ('(?i)(cris|ax|test)es$', '\\1is'),
65 ('(?i)(shoe)s$', '\\1'),
66 ('(?i)(o)es$', '\\1'),
67 ('(?i)(bus)es$', '\\1'),
68 ('(?i)([m|l])ice$', '\\1ouse'),
69 ('(?i)(x|ch|ss|sh)es$', '\\1'),
70 ('(?i)(m)ovies$', '\\1ovie'),
71 ('(?i)(s)eries$', '\\1eries'),
72 ('(?i)([^aeiouy]|qu)ies$', '\\1y'),
73 ('(?i)([lr])ves$', '\\1f'),
74 ('(?i)(tive)s$', '\\1'),
75 ('(?i)(hive)s$', '\\1'),
76 ('(?i)([^f])ves$', '\\1fe'),
77 ('(?i)(^analy)ses$', '\\1sis'),
78 ('(?i)((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$', '\\1\\2sis'),
79 ('(?i)([ti])a$', '\\1um'),
80 ('(?i)(n)ews$', '\\1ews'),
81 ('(?i)s$', ''),
82 )
83
84 def pluralize(self, word):
85 '''Pluralizes nouns.'''
86 return self._transform(self.PLURALIZE_RULES, word)
87
88 def singularize(self, word):
89 '''Singularizes nouns.'''
90 return self._transform(self.SINGULARIZE_RULES, word, pluralize=False)
91
92 def cond_plural(self, number_of_records, word):
93 '''Returns the plural form of a word if first parameter is greater than 1'''
94 if number_of_records != 1:
95 return self.pluralize(word)
96 return word
97
98 def _transform(self, rules, word, pluralize=True):
99 return self._handle_nonchanging(word) or self._handle_irregular(word, pluralize=pluralize) or self._apply_rules(rules, word) or word
100
101 def _handle_nonchanging(self, word):
102 lower_cased_word = word.lower()
103 # Check if word is an item or the suffix of any item in NONCHANGING_WORDS
104 for nonchanging_word in self.NONCHANGING_WORDS:
105 if lower_cased_word.endswith(nonchanging_word):
106 return word
107
108 def _handle_irregular(self, word, pluralize=True):
109 for form_a, form_b in self.IRREGULAR_WORDS.items():
110 if not pluralize:
111 form_a, form_b = form_b, form_a
112 match = re.search('(' + form_a + ')$', word, re.IGNORECASE)
113 if match:
114 return re.sub('(?i)' + form_a + '$', match.expand('\\1')[0] + form_b[1:], word)
115
116 def _apply_rules(self, rules, word):
117 for pattern, replacement in rules:
118 if re.search(pattern, word):
119 return re.sub(pattern, replacement, word)
120
121
122 # Copyright (c) 2006 Bermi Ferrer Martinez
123 # Permission is hereby granted, free of charge, to any person obtaining a copy
124 # of this software to deal in this software without restriction, including
125 # without limitation the rights to use, copy, modify, merge, publish,
126 # distribute, sublicense, and/or sell copies of this software, and to permit
127 # persons to whom this software is furnished to do so, subject to the following
128 # condition:
129 #
130 # THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
131 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
132 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
133 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
134 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
135 # OUT OF OR IN CONNECTION WITH THIS SOFTWARE OR THE USE OR OTHER DEALINGS IN
136 # THIS SOFTWARE.