Class Nysiis

  • All Implemented Interfaces:
    Encoder, StringEncoder

    public class Nysiis
    extends Object
    implements StringEncoder
    Encodes a string into a NYSIIS value. NYSIIS is an encoding used to relate similar names, but can also be used as a general purpose scheme to find word with similar phonemes.

    NYSIIS features an accuracy increase of 2.7% over the traditional Soundex algorithm.

    Algorithm description:

     1. Transcode first characters of name
       1a. MAC ->   MCC
       1b. KN  ->   NN
       1c. K   ->   C
       1d. PH  ->   FF
       1e. PF  ->   FF
       1f. SCH ->   SSS
     2. Transcode last characters of name
       2a. EE, IE          ->   Y
       2b. DT,RT,RD,NT,ND  ->   D
     3. First character of key = first character of name
     4. Transcode remaining characters by following these rules, incrementing by one character each time
       4a. EV  ->   AF  else A,E,I,O,U -> A
       4b. Q   ->   G
       4c. Z   ->   S
       4d. M   ->   N
       4e. KN  ->   N   else K -> C
       4f. SCH ->   SSS
       4g. PH  ->   FF
       4h. H   ->   If previous or next is nonvowel, previous
       4i. W   ->   If previous is vowel, previous
       4j. Add current to key if current != last key character
     5. If last character is S, remove it
     6. If last characters are AY, replace with Y
     7. If last character is A, remove it
     8. Collapse all strings of repeated characters
     9. Add original first character of name as first character of key

    This class is immutable and thread-safe.

    See Also:
    NYSIIS on Wikipedia, NYSIIS on, Soundex
    • Constructor Detail

      • Nysiis

        public Nysiis()
        Creates an instance of the Nysiis encoder with strict mode (original form), i.e. encoded strings have a maximum length of 6.
      • Nysiis

        public Nysiis(boolean strict)
        Create an instance of the Nysiis encoder with the specified strict mode:
        • true: encoded strings have a maximum length of 6
        • false: encoded strings may have arbitrary length
        strict - the strict mode
    • Method Detail

      • encode

        public Object encode(Object obj)
                      throws EncoderException
        Encodes an Object using the NYSIIS algorithm. This method is provided in order to satisfy the requirements of the Encoder interface, and will throw an EncoderException if the supplied object is not of type String.
        Specified by:
        encode in interface  Encoder
        obj - Object to encode
        An object (or a String) containing the NYSIIS code which corresponds to the given String.
        EncoderException - if the parameter supplied is not of a String
        IllegalArgumentException - if a character is not mapped
      • encode

        public String encode(String str)
        Encodes a String using the NYSIIS algorithm.
        Specified by:
        encode in interface  StringEncoder
        str - A String object to encode
        A Nysiis code corresponding to the String supplied
        IllegalArgumentException - if a character is not mapped
      • isStrict

        public boolean isStrict()
        Indicates the strict mode for this Nysiis encoder.
        true if the encoder is configured for strict mode, false otherwise
      • nysiis

        public String nysiis(String str)
        Retrieves the NYSIIS code for a given String object.
        str - String to encode using the NYSIIS algorithm
        A NYSIIS code for the String supplied

Copyright © 2002–2020 The Apache Software Foundation. All rights reserved.