Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding=utf-8
- ## Grammar utilities
- # Constants:
- ## Not all these letters are actually in the English language, but what with all the borrowed words...
- englishVowels = u'aeiouàáâãäåæèéêëìíîïðòóôõöøùúûüāăąēĕėęěĩīĭįıijōŏőœũūŭůűų'
- ## Generate the appropriate plural form of the given word, based on the quantity.
- # Formatting for names to account for plurality:
- #
- # sheep
- # horse~
- # knife:knives
- #
- # 'sheep' doesn't get modified when plural. Returns same value regardless of quantity.
- # 'horse~' appends an 's' in place of the ~ when plural. 1 = 'horse', 2+ = 'horses'
- # 'knife:knives' explicity states the plural form. Anything before the colon for 1, anything after for 2+.
- def makePlural(basename, quantity):
- if u'~' in basename:
- if quantity == 1:
- return basename.replace(u'~', u'')
- return basename.replace(u'~', u's')
- elif u':' in basename:
- parts = basename.split(sep = u':')
- if quantity == 1 or len(parts) == 1:
- return parts[0]
- return parts[1]
- else:
- return basename
- ## Get the singular indefinite article for a name that may be prefixed by
- # a 'flavor' (eg: a 'red' potion)
- # \param basename - the word being queried
- # \param prefixList - potential prefixes (eg: flavor) that come before the main word
- # \param definiteArticle - indicate whether to use a definite rather than
- # indefinite article
- def getSingularArticle(basename, prefixList = None, useDefiniteArticle = False):
- if useDefiniteArticle:
- return u'the'
- # Take the first prefix word, if any.
- if prefixList:
- depend = prefixList[0]
- else:
- depend = basename
- # \todo Assuming 'an' when the name starts with a vowel isn't
- # perfectly reliable (c.f. "uranium").
- if depend[0].lower() in englishVowels:
- return u'an'
- else:
- return u'a'
- ## Generate a complete phrase to describe a given item.
- # \param basename - The base name of the item, formatted to allow for pluralisation.
- # \param quantity - The number of items (defaults to 1)
- # \param prefixList - A list of strings that describes the item and should be placed before the
- # item name in sequential order. EG: "Red" in "Red Potion"; "Spiked", "Mythril"
- # in "Spiked Mythril Small Shield".
- # \param suffixList - A list of strings that describes the item and should be placed after the
- # item name, in sequential order. (EG: "Slay Giant", "Cure Light Wounds")
- # - If the first detail descriptor starts with a non-alphabetical character,
- # it's added as-is (eg: "(Holy Avenger)"). Any additional details are added
- # in a simple comma-delimited list (eg: (Holy Avenger), Speed, Resist Sound).
- # - If the first descriptor starts with alphabetical text, it's formatted using
- # "of", additional details are separated with commas, and the last one uses "and".
- # (eg: a ring of resist fire, resist cold and resist lightning)
- def getItemPhrase(basename, quantity = 1, useDefiniteArticle = False, prefixList = None, suffixList = None):
- # Trim any empty/None values from either of the lists.
- prefixList = [x for x in prefixList if x]
- suffixList = [x for x in suffixList if x]
- # Use title() to make sure words are capitalized.
- name = makePlural(basename.title(), quantity)
- if quantity == 0:
- article = u'no'
- elif quantity == 1:
- article = getSingularArticle(name, prefixList, useDefiniteArticle)
- else:
- article = unicode(quantity);
- phrase = u'%s ' % article
- # Add prefix values if we're given any non-empty list.
- for prefix in prefixList:
- phrase += u'%s ' % prefix
- phrase += name
- # Add suffix values if we're given any non-empty list.
- if suffixList:
- # Check to see if the first detail entry starts with an alphabetic character.
- # If it does, use "of ~~"; otherwise just use a comma-separated list.
- if suffixList[0][0].isalpha():
- suffixStr = u"of %s" % suffixList[0]
- for suffix in suffixList[1:-1]:
- if suffix:
- suffixStr += u", %s" % suffix
- if len(suffixList) > 1:
- suffixStr += u" and %s" % suffixList[-1]
- else:
- suffixStr = u"%s" % suffixList[0]
- for suffix in suffixList[1:]:
- if suffix:
- suffixStr += u", %s" % suffix
- phrase += u" %s" % suffixStr
- return phrase
- ## Get the article to use with the provided word phrase, based on quantity and
- # whether we're using the definite article or not.
- # \param phrase - the word phrase the article is being applied to
- # \param quantity - the quantity of the word in question
- # \param useDefiniteArticle - indicate whether to use a definite rather than
- # indefinite article
- def getArticle(phrase, quantity = 1, useDefiniteArticle = False):
- if quantity == 0:
- return u'no more'
- elif quantity > 1:
- return unicode(quantity);
- if useDefiniteArticle:
- return u'the'
- if phrase[0].lower() in englishVowels:
- return u'an'
- else:
- return u'a'
- ##Get a fully constructed item name based on the provided parts of speech.
- # \param baseNoun - The fundamental noun of the item
- # \param baseAdjectives - List of adjectives to apply to the baseNoun
- # \param modifierNoun - A noun that can modify the base noun (and should
- # be placed before the baseAdjectives)
- # \param modAdjectives - List of adjectives to apply to the modifierNoun
- # (or just extend the number being applied to the base noun)
- # \param itemName - The proper 'name' of the item. Placed after the base noun
- # \param suffixList - List of suffixes to add after the base noun, if
- # the item doesn't have a proper name.
- # \param quantity - The quantity of the item, which determines the article
- def getFullItemName(baseNoun, baseAdjectives = None,
- modifierNoun = None, modAdjectives = None,
- itemName = None, suffixList = None,
- quantity = 1):
- phrase = u""
- if modAdjectives:
- for adj in modAdjectives:
- if adj:
- phrase += u"%s " % adj
- if modifierNoun:
- pluralModNoun = makePlural(modifierNoun, quantity)
- else:
- pluralModNoun = None
- if pluralModNoun:
- phrase += u"%s " % pluralModNoun
- if baseAdjectives:
- for adj in baseAdjectives:
- if adj:
- phrase += u"%s " % adj
- phrase += u"%s" % makePlural(baseNoun.title(), quantity)
- if itemName:
- if itemName[0].isalpha():
- phrase += u" of"
- phrase += u" %s" % itemName
- elif suffixList:
- if suffixList[0][0].isalpha():
- phrase += u" of"
- phrase += u" %s" % suffixList[0]
- for suffix in suffixList[1:-1]:
- phrase += u", %s" % suffix
- if len(suffixList) > 1:
- phrase += u" and %s" % suffixList[-1]
- if itemName:
- useDefiniteArticle = True
- else:
- useDefiniteArticle = False
- article = getArticle(phrase, quantity, useDefiniteArticle)
- phrase = u"%s %s" % (article, phrase)
- return phrase
Advertisement
Add Comment
Please, Sign In to add comment