Soundex Algorithm in AWK

Liste des GroupesRevenir à cl awk 
Sujet : Soundex Algorithm in AWK
De : porkchop (at) *nospam* invalid.foo (Mike Sanders)
Groupes : comp.lang.awk
Date : 23. Aug 2024, 06:11:33
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <va95m5$q367$1@dont-email.me>
User-Agent : tin/2.6.2-20221225 ("Pittyvaich") (NetBSD/9.3 (amd64))
# Soundex Algorithm in AWK: Michael Sanders 2024
# example usage: awk -f soundex.awk < words.txt
# see also: https://en.wikipedia.org/wiki/Soundex

{ print $0 " : " soundex($0) }

function soundex(word, i, code, c, firstLetter, lastCode, buf) {
  word = toupper(word) # convert word to uppercase
  firstLetter = substr(word, 1, 1)
  code = buf = ""

  # map of letters to soundex digits
  for (i = 2; i <= length(word); i++) {
    c = substr(word, i, 1)
    if (c ~ /[BFPV]/)          code = "1"
    else if (c ~ /[CGJKQSXZ]/) code = "2"
    else if (c ~ /[DT]/)       code = "3"
    else if (c ~ /[L]/)        code = "4"
    else if (c ~ /[MN]/)       code = "5"
    else if (c ~ /[R]/)        code = "6"
    else code = "" # skip A, E, I, O, U, H, W, Y

    # ignore consecutive identical codes
    if (code != lastCode && code != "") {
      buf = buf code
      lastCode = code
    }
  }

  # combine 1st letter with buf, pad with zeros or truncate to 4 characters
  return substr(firstLetter buf "000", 1, 4)
}

# eof

--
:wq
Mike Sanders


Date Sujet#  Auteur
23 Aug 24 o Soundex Algorithm in AWK1Mike Sanders

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal