potluck.phrasing
Functions for phrasing feedback (e.g., pluralization).
phrasing.py
1""" 2Functions for phrasing feedback (e.g., pluralization). 3 4phrasing.py 5""" 6 7 8#--------------------------# 9# Basic English management # 10#--------------------------# 11 12def a_an(string): 13 """ 14 Returns the given string it 'a ' prepended, unless it starts with a 15 vowel in which case 'an ' will be prepended. 16 """ 17 if string[0].lower() in 'aeiou': 18 return 'an ' + string 19 else: 20 return 'a ' + string 21 22 23def plural(n, singular, plural=None): 24 """ 25 Uses a number n to determine whether to use the given singular or 26 plural phrasing. If the plural phrasing is omitted, it is assumed to 27 be just the singular phrasing plus 's'. 28 """ 29 if plural is None: 30 plural = singular + 's' 31 32 if n == 0: 33 return plural 34 elif n == 1: 35 return singular 36 else: 37 return plural 38 39 40def obj_num(n, singular, plural=None): 41 """ 42 Translates a number n into a string describing that many objects with 43 the given singular and plural phrasing. If the plural phrasing is 44 omitted, it is assumed to be just the singular phrasing plus 's'. 45 """ 46 if plural is None: 47 plural = singular + 's' 48 49 if n == 0: 50 return "zero " + plural 51 elif n == 1: 52 return "one " + singular 53 else: 54 return "{} {}".format(n, plural) 55 56 57def comma_list(strings, junction="and"): 58 """ 59 Turns a list of strings into a comma-separated list using the word 60 'and' (or the given junction word) before the final item and an 61 Oxford comma, but also does the correct thing for 1- or 2-element 62 lists. Returns an empty string if given an empty list. 63 """ 64 strings = list(strings) 65 if len(strings) == 0: 66 return "" 67 if len(strings) == 1: 68 return strings[0] 69 elif len(strings) == 2: 70 return strings[0] + " " + junction + " " + strings[1] 71 else: 72 return ', '.join(strings[:-1]) + ", " + junction + " " + strings[-1] 73 74 75def ordinal(n): 76 """ 77 Returns the ordinal string for the number n (i.e., 0th, 1st, 2nd, 78 etc.) 79 """ 80 digits = str(n) 81 if digits.endswith('11'): 82 return digits + 'th' 83 elif digits.endswith('12'): 84 return digits + 'th' 85 elif digits.endswith('13'): 86 return digits + 'th' 87 elif digits.endswith('1'): 88 return digits + 'st' 89 elif digits.endswith('2'): 90 return digits + 'nd' 91 elif digits.endswith('3'): 92 return digits + 'rd' 93 else: 94 return digits + 'th'
13def a_an(string): 14 """ 15 Returns the given string it 'a ' prepended, unless it starts with a 16 vowel in which case 'an ' will be prepended. 17 """ 18 if string[0].lower() in 'aeiou': 19 return 'an ' + string 20 else: 21 return 'a ' + string
Returns the given string it 'a ' prepended, unless it starts with a vowel in which case 'an ' will be prepended.
24def plural(n, singular, plural=None): 25 """ 26 Uses a number n to determine whether to use the given singular or 27 plural phrasing. If the plural phrasing is omitted, it is assumed to 28 be just the singular phrasing plus 's'. 29 """ 30 if plural is None: 31 plural = singular + 's' 32 33 if n == 0: 34 return plural 35 elif n == 1: 36 return singular 37 else: 38 return plural
Uses a number n to determine whether to use the given singular or plural phrasing. If the plural phrasing is omitted, it is assumed to be just the singular phrasing plus 's'.
41def obj_num(n, singular, plural=None): 42 """ 43 Translates a number n into a string describing that many objects with 44 the given singular and plural phrasing. If the plural phrasing is 45 omitted, it is assumed to be just the singular phrasing plus 's'. 46 """ 47 if plural is None: 48 plural = singular + 's' 49 50 if n == 0: 51 return "zero " + plural 52 elif n == 1: 53 return "one " + singular 54 else: 55 return "{} {}".format(n, plural)
Translates a number n into a string describing that many objects with the given singular and plural phrasing. If the plural phrasing is omitted, it is assumed to be just the singular phrasing plus 's'.
58def comma_list(strings, junction="and"): 59 """ 60 Turns a list of strings into a comma-separated list using the word 61 'and' (or the given junction word) before the final item and an 62 Oxford comma, but also does the correct thing for 1- or 2-element 63 lists. Returns an empty string if given an empty list. 64 """ 65 strings = list(strings) 66 if len(strings) == 0: 67 return "" 68 if len(strings) == 1: 69 return strings[0] 70 elif len(strings) == 2: 71 return strings[0] + " " + junction + " " + strings[1] 72 else: 73 return ', '.join(strings[:-1]) + ", " + junction + " " + strings[-1]
Turns a list of strings into a comma-separated list using the word 'and' (or the given junction word) before the final item and an Oxford comma, but also does the correct thing for 1- or 2-element lists. Returns an empty string if given an empty list.
76def ordinal(n): 77 """ 78 Returns the ordinal string for the number n (i.e., 0th, 1st, 2nd, 79 etc.) 80 """ 81 digits = str(n) 82 if digits.endswith('11'): 83 return digits + 'th' 84 elif digits.endswith('12'): 85 return digits + 'th' 86 elif digits.endswith('13'): 87 return digits + 'th' 88 elif digits.endswith('1'): 89 return digits + 'st' 90 elif digits.endswith('2'): 91 return digits + 'nd' 92 elif digits.endswith('3'): 93 return digits + 'rd' 94 else: 95 return digits + 'th'
Returns the ordinal string for the number n (i.e., 0th, 1st, 2nd, etc.)