#NEWLINE# def singularize( word , pos = NOUN , custom = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) : #NEWLINE# #INDENT# ' Returns the singular of a given word.\n    ' #NEWLINE# if ( word in custom ) : #NEWLINE# #INDENT# return custom [ word ] #NEWLINE# #UNINDENT# if ( '-' in word ) : #NEWLINE# #INDENT# w = word . split ( '-' ) #NEWLINE# if ( ( len ( g ) > 1 ) and ( w [ 1 ] in plural_prepositions ) ) : #NEWLINE# #INDENT# return ( ( singularize ( w [ 0 ] , pos , custom ) + '-' ) + '-' . join ( w [ 1 : ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# if word . endswith ( "'" ) : #NEWLINE# #INDENT# return ( singularize ( word [ : ( - 1 ) ] ) + "'s" ) #NEWLINE# #UNINDENT# w = word . lower ( ) #NEWLINE# for x in singular_uninflected : #NEWLINE# #INDENT# if x . endswith ( w ) : #NEWLINE# #INDENT# return word #NEWLINE# #UNINDENT# #UNINDENT# for x in singular_uncountable : #NEWLINE# #INDENT# if x . endswith ( w ) : #NEWLINE# #INDENT# return word #NEWLINE# #UNINDENT# #UNINDENT# for x in singular_ie : #NEWLINE# #INDENT# if w . endswith ( ( x + 's' ) ) : #NEWLINE# #INDENT# return w #NEWLINE# #UNINDENT# #UNINDENT# for x in singular_irregular : #NEWLINE# #INDENT# if w . endswith ( x ) : #NEWLINE# #INDENT# return re . sub ( ( ( '(?i)' + x ) + '$' ) , singular_irregular [ x ] , word ) #NEWLINE# #UNINDENT# #UNINDENT# for ( suffix , inflection ) in singular_rules : #NEWLINE# #INDENT# m = suffix . search ( word ) #NEWLINE# g = ( ( m and m . groups ( ) ) or [ ] ) #NEWLINE# if m : #NEWLINE# #INDENT# for k in range ( len ( g ) ) : #NEWLINE# #INDENT# if ( g [ k ] is None ) : #NEWLINE# #INDENT# inflection = inflection . replace ( ( '\\' + str ( ( k + 1 ) ) ) , '' ) #NEWLINE# #UNINDENT# #UNINDENT# return suffix . sub ( inflection , word ) #NEWLINE# #UNINDENT# #UNINDENT# return word