Skip to content

Commit

Permalink
Merge 706e50f into 473d084
Browse files Browse the repository at this point in the history
  • Loading branch information
nielstron committed Sep 27, 2018
2 parents 473d084 + 706e50f commit 816391a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 22 deletions.
36 changes: 25 additions & 11 deletions inflect.py
Expand Up @@ -1524,19 +1524,21 @@ def inflect(self, text):
# ## PLURAL SUBROUTINES

def postprocess(self, orig, inflected):
"""
FIX PEDANTRY AND CAPITALIZATION :-)
"""
if '|' in inflected:
inflected = inflected.split('|')[self.classical_dict['all']]
if orig == "I":
return inflected
if orig == orig.upper():
return inflected.upper()
if orig[0] == orig[0].upper():
return '{}{}'.format(inflected[0].upper(),
inflected[1:])
return inflected
result = inflected.split(' ')
# Try to fix word wise capitalization
for index, word in enumerate(orig.split(' ')):
if word == 'I':
# Is this the only word for exceptions like this
# Where the original is fully capitalized without 'meaning' capitalization?
# Also this fails to handle a capitalizaion in context
continue
if word.capitalize() == word:
result[index] = result[index].capitalize()
if word == word.upper():
result[index] = result[index].upper()
return ' '.join(result)

def partition_word(self, text):
mo = search(r'\A(\s*)(.+?)(\s*)\Z', text)
Expand Down Expand Up @@ -1894,6 +1896,18 @@ def _plnoun(self, word, count=None):
lowersplit[:numword - 1] +
[self._plnoun(lowersplit[numword - 1], 2)] + lowersplit[numword:])

# Handle units given in degrees
if len(lowersplit) >= 2 and lowersplit[0] in ['degree']:
return ' '.join(
[self._plnoun(lowersplit[0])] + lowersplit[1:]
)
# only pluralize denominators in units
mo = search(r'(?P<denominator>.+)( (%s) .+)' % '|'.join(['per', 'a']), lowerword)
if mo:
index = len(mo.group('denominator'))
return '{}{}'.format(self._plnoun(lowerword[:index]),
lowerword[index:])

lowersplit = lowerword.split('-')
if len(lowersplit) >= 3:
for numword in range(1, len(lowersplit) - 1):
Expand Down
47 changes: 36 additions & 11 deletions tests/test_compounds.py
Expand Up @@ -2,19 +2,44 @@

import inflect

p = inflect.engine()

class TestCompounds(object):
def setup(self):
self.p = inflect.engine()

def test_compound_1(self):
eq_(self.p.singular_noun('hello-out-there'), 'hello-out-there')
def test_compound_1():
eq_(p.singular_noun('hello-out-there'), 'hello-out-there')

def test_compound_2(self):
eq_(self.p.singular_noun('hello out there'), 'hello out there')

def test_compound_3(self):
eq_(self.p.singular_noun('continue-to-operate'), 'continue-to-operate')
def test_compound_2():
eq_(p.singular_noun('hello out there'), 'hello out there')

def test_compound_4(self):
eq_(self.p.singular_noun('case of diapers'), 'case of diapers')

def test_compound_3():
eq_(p.singular_noun('continue-to-operate'), 'continue-to-operate')


def test_compound_4():
eq_(p.singular_noun('case of diapers'), 'case of diapers')


def test_unit_handling_degree():
test_cases = {
'degree celsius': 'degrees celsius',
'degree fahrenheit': 'degrees fahrenheit',
'degree rankine': 'degrees rankine'
}
for singular, plural in test_cases.items():
eq_(p.plural(singular), plural)


def test_unit_handling_fractional():
test_cases = {
'pound per square inch': 'pounds per square inch',
'pound-force per square inch': 'pound-forces per square inch',
'metre per second': 'metres per second',
'kilometre per hour': 'kilometres per hour',
'foot per square second': 'feet per square second',
'cubic metre per second': 'cubic metres per second',
'dollar a year': 'dollars a year'
}
for singular, plural in test_cases.items():
eq_(p.plural(singular), plural)
2 changes: 2 additions & 0 deletions tests/test_pwd.py
Expand Up @@ -214,6 +214,8 @@ def test_postprocess(self):
('COW', 'cows', 'COWS'),
('Cow', 'cows', 'Cows'),
('cow', 'cows|kine', 'cows'),
('Entry', 'entries', 'Entries'),
('can of Coke', 'cans of coke', 'cans of Coke')
):
self.assertEqual(p.postprocess(orig, infl), txt)

Expand Down

0 comments on commit 816391a

Please sign in to comment.