code: def nysiis(term):
      "returns New York State Identification and Intelligence Algorithm (NYSIIS) code for the given term"
  
      code = ""
  
      i = 0
      term_length = len(term)
  
      if term_length == 0:
          # empty string ?
          return code
      # end if
  
      # build translation table for the first characters
      table = {
          "mac": "mcc",
          "ph": "ff",
          "kn": "nn",
          "pf": "ff",
          "k": "c",
          "sch": "sss",
      }
  
      for table_entry in table.keys():
          table_value = table[table_entry]  # get table value
          table_value_len = len(table_value)  # calculate its length
          first_chars = term[0:table_value_len]
          if first_chars == table_entry:
              term = table_value + term[table_value_len:]
              break
          # end if
      # end for
  
      # build translation table for the last characters
      table = {
          "ee": "y",
          "ie": "y",
          "dt": "d",
          "rt": "d",
          "rd": "d",
          "nt": "d",
          "nd": "d",
      }
  
      for table_entry in table.keys():
          table_value = table[table_entry]  # get table value
          table_entry_len = len(table_entry)  # calculate its length
          last_chars = term[(0 - table_entry_len) :]
          # print last_chars, ", ", table_entry, ", ", table_value
          if last_chars == table_entry:
              term = term[: (0 - table_value_len + 1)] + table_value
              break
          # end if
      # end for
  
      # initialize code
      code = term
  
      # transform ev->af
      code = re.sub(r"ev", r"af", code)
  
      # transform a,e,i,o,u->a
      code = re.sub(r"[aeiouy]", r"a", code)
  
      # transform q->g
      code = re.sub(r"q", r"g", code)
  
      # transform z->s
      code = re.sub(r"z", r"s", code)
  
      # transform m->n
      code = re.sub(r"m", r"n", code)
  
      # transform kn->n
      code = re.sub(r"kn", r"n", code)
  
      # transform k->c
      code = re.sub(r"k", r"c", code)
  
      # transform sch->sss
      code = re.sub(r"sch", r"sss", code)
  
      # transform ph->ff
      code = re.sub(r"ph", r"ff", code)
  
      # transform h-> if previous or next is nonvowel -> previous
      occur = re.findall(r"([a-z]{0,1}?)h([a-z]{0,1}?)", code)
      # print occur
      for occur_group in occur:
          occur_item_previous = occur_group[0]
          occur_item_next = occur_group[1]
  
          if (re.match(r"[^aeiouy]", occur_item_previous)) or (
              re.match(r"[^aeiouy]", occur_item_next)
          ):
              if occur_item_previous != "":
                  # make substitution
                  code = re.sub(
                      occur_item_previous + "h",
                      occur_item_previous * 2,
                      code,
                      1,
                  )
              # end if
          # end if
      # end for
  
      # transform w-> if previous is vowel -> previous
      occur = re.findall(r"([aeiouy]{1}?)w", code)
      # print occur
      for occur_group in occur:
          occur_item_previous = occur_group[0]
          # make substitution
          code = re.sub(
              occur_item_previous + "w",
              occur_item_previous * 2,
              code,
              1,
          )
      # end for
  
      # check last character
      # -s, remove
      code = re.sub(r"s$", r"", code)
      # -ay, replace by -y
      code = re.sub(r"ay$", r"y", code)
      # -a, remove
      code = re.sub(r"a$", r"", code)
  
      # return nysiis code
      return code
  
imports: import string
  import re
label_desc: Use image node_id 3 for calling a global function with example usage: nysiis(term) and returns: code, code