Kinematics

grammar

Feb 23rd, 2013
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.38 KB | None | 0 0
  1. # coding=utf-8
  2. ## Grammar utilities
  3.  
  4.  
  5. # Constants:
  6.  
  7. ## Not all these letters are actually in the English language, but what with all the borrowed words...
  8. englishVowels = u'aeiouàáâãäåæèéêëìíîïðòóôõöøùúûüāăąēĕėęěĩīĭįıijōŏőœũūŭůűų'
  9.  
  10.  
  11. ## Generate the appropriate plural form of the given word, based on the quantity.
  12. # Formatting for names to account for plurality:
  13. #
  14. # sheep
  15. # horse~
  16. # knife:knives
  17. #
  18. # 'sheep' doesn't get modified when plural.  Returns same value regardless of quantity.
  19. # 'horse~' appends an 's' in place of the ~ when plural.  1 = 'horse', 2+ = 'horses'
  20. # 'knife:knives' explicity states the plural form.  Anything before the colon for 1, anything after for 2+.
  21. def makePlural(basename, quantity):
  22.     if u'~' in basename:
  23.         if quantity == 1:
  24.             return basename.replace(u'~', u'')
  25.         return basename.replace(u'~', u's')
  26.     elif u':' in basename:
  27.         parts = basename.split(sep = u':')
  28.         if quantity == 1 or len(parts) == 1:
  29.             return parts[0]
  30.         return parts[1]
  31.     else:
  32.         return basename
  33.  
  34.  
  35. ## Get the singular indefinite article for a name that may be prefixed by
  36. #  a 'flavor' (eg: a 'red' potion)
  37. # \param basename - the word being queried
  38. # \param prefixList - potential prefixes (eg: flavor) that come before the main word
  39. # \param definiteArticle - indicate whether to use a definite rather than
  40. #       indefinite article
  41. def getSingularArticle(basename, prefixList = None, useDefiniteArticle = False):
  42.     if useDefiniteArticle:
  43.         return u'the'
  44.  
  45.     # Take the first prefix word, if any.
  46.     if prefixList:
  47.         depend = prefixList[0]
  48.     else:
  49.         depend = basename
  50.  
  51.     # \todo Assuming 'an' when the name starts with a vowel isn't
  52.     # perfectly reliable (c.f. "uranium").
  53.     if depend[0].lower() in englishVowels:
  54.         return u'an'
  55.     else:
  56.         return u'a'
  57.  
  58.  
  59. ## Generate a complete phrase to describe a given item.
  60. # \param basename - The base name of the item, formatted to allow for pluralisation.
  61. # \param quantity - The number of items (defaults to 1)
  62. # \param prefixList - A list of strings that describes the item and should be placed before the
  63. #                 item name in sequential order.  EG: "Red" in "Red Potion"; "Spiked", "Mythril"
  64. #                 in "Spiked Mythril Small Shield".
  65. # \param suffixList - A list of strings that describes the item and should be placed after the
  66. #                 item name, in sequential order. (EG: "Slay Giant", "Cure Light Wounds")
  67. #               - If the first detail descriptor starts with a non-alphabetical character,
  68. #                 it's added as-is (eg: "(Holy Avenger)"). Any additional details are added
  69. #                 in a simple comma-delimited list (eg: (Holy Avenger), Speed, Resist Sound).
  70. #               - If the first descriptor starts with alphabetical text, it's formatted using
  71. #                 "of", additional details are separated with commas, and the last one uses "and".
  72. #                 (eg: a ring of resist fire, resist cold and resist lightning)
  73. def getItemPhrase(basename, quantity = 1, useDefiniteArticle = False, prefixList = None, suffixList = None):
  74.     # Trim any empty/None values from either of the lists.
  75.     prefixList = [x for x in prefixList if x]
  76.     suffixList = [x for x in suffixList if x]
  77.  
  78.     # Use title() to make sure words are capitalized.
  79.     name = makePlural(basename.title(), quantity)
  80.  
  81.     if quantity == 0:
  82.         article = u'no'
  83.     elif quantity == 1:
  84.         article = getSingularArticle(name, prefixList, useDefiniteArticle)
  85.     else:
  86.         article = unicode(quantity);
  87.  
  88.     phrase = u'%s ' % article
  89.  
  90.     # Add prefix values if we're given any non-empty list.
  91.     for prefix in prefixList:
  92.         phrase +=  u'%s ' % prefix
  93.  
  94.     phrase += name
  95.  
  96.     # Add suffix values if we're given any non-empty list.
  97.     if suffixList:
  98.         # Check to see if the first detail entry starts with an alphabetic character.
  99.         # If it does, use "of ~~"; otherwise just use a comma-separated list.
  100.         if suffixList[0][0].isalpha():
  101.             suffixStr = u"of %s" % suffixList[0]
  102.             for suffix in suffixList[1:-1]:
  103.                 if suffix:
  104.                     suffixStr += u", %s" % suffix
  105.             if len(suffixList) > 1:
  106.                 suffixStr += u" and %s" % suffixList[-1]
  107.         else:
  108.             suffixStr = u"%s" % suffixList[0]
  109.             for suffix in suffixList[1:]:
  110.                 if suffix:
  111.                     suffixStr += u", %s" % suffix
  112.  
  113.         phrase += u" %s" % suffixStr
  114.  
  115.     return phrase
  116.  
  117.  
  118.  
  119. ## Get the article to use with the provided word phrase, based on quantity and
  120. #  whether we're using the definite article or not.
  121. # \param phrase - the word phrase the article is being applied to
  122. # \param quantity - the quantity of the word in question
  123. # \param useDefiniteArticle - indicate whether to use a definite rather than
  124. #       indefinite article
  125. def getArticle(phrase, quantity = 1, useDefiniteArticle = False):
  126.     if quantity == 0:
  127.         return u'no more'
  128.     elif quantity > 1:
  129.         return unicode(quantity);
  130.  
  131.     if useDefiniteArticle:
  132.         return u'the'
  133.  
  134.     if phrase[0].lower() in englishVowels:
  135.         return u'an'
  136.     else:
  137.         return u'a'
  138.  
  139.  
  140. ##Get a fully constructed item name based on the provided parts of speech.
  141. # \param baseNoun - The fundamental noun of the item
  142. # \param baseAdjectives - List of adjectives to apply to the baseNoun
  143. # \param modifierNoun - A noun that can modify the base noun (and should
  144. #        be placed before the baseAdjectives)
  145. # \param modAdjectives - List of adjectives to apply to the modifierNoun
  146. #        (or just extend the number being applied to the base noun)
  147. # \param itemName - The proper 'name' of the item. Placed after the base noun
  148. # \param suffixList - List of suffixes to add after the base noun, if
  149. #        the item doesn't have a proper name.
  150. # \param quantity - The quantity of the item, which determines the article
  151. def getFullItemName(baseNoun, baseAdjectives = None,
  152.                     modifierNoun = None, modAdjectives = None,
  153.                     itemName = None, suffixList = None,
  154.                     quantity = 1):
  155.     phrase = u""
  156.  
  157.     if modAdjectives:
  158.         for adj in modAdjectives:
  159.             if adj:
  160.                 phrase += u"%s " % adj
  161.  
  162.     if modifierNoun:
  163.         pluralModNoun = makePlural(modifierNoun, quantity)
  164.     else:
  165.         pluralModNoun = None
  166.  
  167.     if pluralModNoun:
  168.         phrase += u"%s " % pluralModNoun
  169.  
  170.     if baseAdjectives:
  171.         for adj in baseAdjectives:
  172.             if adj:
  173.                 phrase += u"%s " % adj
  174.  
  175.     phrase += u"%s" % makePlural(baseNoun.title(), quantity)
  176.  
  177.     if itemName:
  178.         if itemName[0].isalpha():
  179.             phrase += u" of"
  180.         phrase += u" %s" % itemName
  181.     elif suffixList:
  182.         if suffixList[0][0].isalpha():
  183.             phrase += u" of"
  184.         phrase += u" %s" % suffixList[0]
  185.         for suffix in suffixList[1:-1]:
  186.             phrase += u", %s" % suffix
  187.         if len(suffixList) > 1:
  188.             phrase += u" and %s" % suffixList[-1]
  189.  
  190.     if itemName:
  191.         useDefiniteArticle = True
  192.     else:
  193.         useDefiniteArticle = False
  194.  
  195.     article = getArticle(phrase, quantity, useDefiniteArticle)
  196.  
  197.     phrase = u"%s %s" % (article, phrase)
  198.  
  199.     return phrase
Advertisement
Add Comment
Please, Sign In to add comment