This function takes an English name and derives a bird code from it using the rules of the six-letter system.
#================================================================
# Specification functions
#----------------------------------------------------------------
# one-word-rule(word) == word, truncated or blank-padded to
# size ABBR_L
#----------------------------------------------------------------
# two-word-rule(w1,w2) ==
# if w1 matches one of the confusing color names ->
# (the substitute 3-letter color code) + w2[:3]
# else ->
# (w1[:3], padded to 3 with hyphens) +
# (w2[:3], padded to 3 with spaces)
#----------------------------------------------------------------
# three-word-rule(w1,w2,w3) ==
# if w1 matches one of the confusing color names ->
# (the substitute 2-letter color code) +
# w2[0] + w3[:3]
# else ->
# w1[:2]+w2[0]+(w3[:3], padded to 3 with spaces)
#----------------------------------------------------------------
# four-word-rule(w1,w2,w3,wLast) ==
# w1[0]+w2[0]+w3[0]+(wLast[:3], padded to 3 with spaces)
#----------------------------------------------------------------
# - - - a b b r e v i a t e - - -
def abbreviate ( eng ):
"Form the canonical abbreviation from an English name."
#-- 1 --
# [ words := (eng), uppercased and trimmed of whitespace
# at both ends, with all hyphens changed to spaces ]
words = eng.upper().strip().replace ( "-", " " )
#-- 2 --
# [ wordList := words, broken into a list on spaces ]
wordList = words.split()
#-- 3 --
# if wordList has 1 element ->
# result := one-word-rule ( wordList[0] )
# else if wordList has 2 elements ->
# result := two-word-rule ( wordList[0], wordList[1] )
# else if wordList has 3 elements ->
# result := three-word-rule ( wordList[0], wordList[1],
# wordList[2] )
# else ->
# result := four-word-rule ( wordList[0], wordList[1],
# wordList[2], wordList[-1] ) ]
if len(wordList) > 3:
result = rule4 ( wordList[0], wordList[1], wordList[2],
wordList[-1] )
elif len(wordList) == 3:
result = rule3 ( wordList[0], wordList[1], wordList[2] )
elif len(wordList) == 2:
result = rule2 ( wordList[0], wordList[1] )
else:
result = wordList[0][:ABBR_L].ljust(ABBR_L)
#-- 4 --
return result
# - - - r u l e 2 - - -
def rule2 ( w1, w2 ):
"""Implements the two-word rule.
[ w1 and w2 are strings ->
return two-word-rule(w1,w2) ]
"""
#-- 1 --
# [ head := first three letters of w1, right-padded to
# length 3 with hyphens ]
head = w1[:3].ljust(3)
#-- 2 --
# [ if w1 matches a confusing color name ->
# head := the corresponding 3-letter substitute
# else -> I ]
if COLOR_SUB_3.has_key ( w1 ):
head = COLOR_SUB_3[w1]
#-- 3 --
# [ return (head + (first three letters of w2)), right-padded
# to ABBR_L with spaces ]
return (head+w2[:3]).ljust(ABBR_L)
# - - - r u l e 3 - - -
def rule3 ( w1, w2, w3 ):
"""Implements the three-word rule.
[ w1, w2 and w3 are strings ->
return three-word-rule(w1,w2,w3) ]
"""
#-- 1 --
# [ head := first 2 characters of w1
# tail := (first character of w2) + (first three characters
# of w3) ]
head = w1[:2]
tail = w2[0] + w3[:3]
#-- 2 --
# [ if w1 matches a confusing color name ->
# head := the corresponding 2-letter substitute
# else -> I ]
if COLOR_SUB_2.has_key ( w1 ):
head = COLOR_SUB_2[w1]
#-- 3 --
return head + tail
# - - - r u l e 4 - - -
def rule4 ( w1, w2, w3, wLast ):
"""Implements the four-word rule.
[ if all arguments are strings ->
return four-word-rule(w1,w2,w3,wLast) ]
"""
return ("%s%s%s%s" %
( w1[0], w2[0], w3[0], wLast[:3] ) ).ljust(ABBR_L)