#NEWLINE# def pluralize( word , pos = NOUN , custom = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } , classical = True ) : #NEWLINE# #INDENT# ' Returns the plural of a given word.\n        For example: child -> children.\n        Handles nouns and adjectives, using classical inflection by default\n        (e.g. where "matrix" pluralizes to "matrices" instead of "matrixes").\n        The custom dictionary is for user-defined replacements.\n    ' #NEWLINE# if ( word in custom ) : #NEWLINE# #INDENT# return custom [ word ] #NEWLINE# #UNINDENT# if ( word . endswith ( "'" ) or word . endswith ( "'s" ) ) : #NEWLINE# #INDENT# owner = word . rstrip ( "'s" ) #NEWLINE# owners = pluralize ( owner , pos , custom , classical ) #NEWLINE# if owners . endswith ( 's' ) : #NEWLINE# #INDENT# return ( owners + "'" ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return ( owners + "'s" ) #NEWLINE# #UNINDENT# #UNINDENT# words = word . replace ( '-' , ' ' ) . split ( ' ' ) #NEWLINE# if ( len ( words ) > 1 ) : #NEWLINE# #INDENT# if ( ( words [ 1 ] == 'general' ) or ( ( words [ 1 ] == 'General' ) and ( words [ 0 ] not in plural_categories [ 'general-generals' ] ) ) ) : #NEWLINE# #INDENT# return word . replace ( words [ 0 ] , pluralize ( words [ 0 ] , pos , custom , classical ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( words [ 1 ] in plural_prepositions ) : #NEWLINE# #INDENT# return word . replace ( words [ 0 ] , pluralize ( words [ 0 ] , pos , custom , classical ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return word . replace ( words [ ( - 1 ) ] , pluralize ( words [ ( - 1 ) ] , pos , custom , classical ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# n = list ( range ( len ( plural_rules ) ) ) #NEWLINE# if pos . startswith ( ADJECTIVE ) : #NEWLINE# #INDENT# n = [ 0 , 1 ] #NEWLINE# #UNINDENT# for i in n : #NEWLINE# #INDENT# ruleset = plural_rules [ i ] #NEWLINE# for rule in ruleset : #NEWLINE# #INDENT# ( suffix , inflection , category , classic ) = rule #NEWLINE# if ( category == None ) : #NEWLINE# #INDENT# if ( ( not classic ) or ( classic and classical ) ) : #NEWLINE# #INDENT# if ( suffix . search ( word ) is not None ) : #NEWLINE# #INDENT# return suffix . sub ( inflection , word ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( category != None ) : #NEWLINE# #INDENT# if ( ( word in plural_categories [ category ] ) and ( ( not classic ) or ( classic and classical ) ) ) : #NEWLINE# #INDENT# if ( suffix . search ( word ) is not None ) : #NEWLINE# #INDENT# return suffix . sub ( inflection , word )