Class UrlCharSequenceNormalizer

java.lang.Object
opennlp.tools.util.normalizer.UrlCharSequenceNormalizer
All Implemented Interfaces:
Serializable, CharSequenceNormalizer

public class UrlCharSequenceNormalizer extends Object implements CharSequenceNormalizer
A CharSequenceNormalizer implementation that replaces each http or https URL and each email address with one space.

A URL is a scheme, ://, and a body. The scheme is http or https in any letter case and starts the text or follows a code point that is neither a letter, a digit, nor one of _ + - . :, so the https inside git+https:// or blob:https:// is not a URL of its own. The body runs to the end of the text or to the first Unicode whitespace, control character, unpaired surrogate, typographic quotation mark, or one of < > "; it therefore includes a port, userinfo, percent escapes, a bracketed IPv6 host, and non-ASCII host names and paths. Trailing sentence punctuation (. , ; : ! ?), a trailing apostrophe, and a closing bracket without its opening bracket inside the body are not part of the URL. A URL with another scheme is kept as it is, and no email address is matched inside it.

An email address is a maximal run of the local-part set [-+_.0-9A-Za-z] whose left neighbor is outside that set, an @, and a domain run out of [-.0-9A-Za-z] that must not start with a dot and must span at least two chars.

Since 3.0.0 URLs are removed as a whole. Under CompatibilityMode.LEGACY the earlier output is produced for language detector models trained with it: a URL is a lowercase http:// or https:// followed by a run of [-_.?&~;+=/#0-9A-Za-z], and email addresses are then matched in the result, also inside URLs with another scheme.

See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • SCHEME_SEPARATOR

      private static final String SCHEME_SEPARATOR
      See Also:
    • HTTP

      private static final String HTTP
      See Also:
    • HTTPS

      private static final String HTTPS
      See Also:
    • SCHEME_BODY

      private static final CodePointSet SCHEME_BODY
      The characters a scheme continues with after its first letter, per RFC 3986.
    • SCHEME_NEIGHBORS

      private static final CodePointSet SCHEME_NEIGHBORS
      Code points that, before the scheme, make it part of a larger token.
    • BODY_DELIMITERS

      private static final CodePointSet BODY_DELIMITERS
      Delimiters that end a URL body besides whitespace and control characters.
    • TRAILING_PUNCTUATION

      private static final CodePointSet TRAILING_PUNCTUATION
      Punctuation given back when it trails a URL.
    • LEGACY_URL_BODY

      private static final CodePointSet LEGACY_URL_BODY
      The URL body set of the legacy output: [-_.?&~;+=/#0-9A-Za-z].
    • INSTANCE

      private static final UrlCharSequenceNormalizer INSTANCE
  • Constructor Details

    • UrlCharSequenceNormalizer

      public UrlCharSequenceNormalizer()
  • Method Details

    • getInstance

      public static UrlCharSequenceNormalizer getInstance()
      Returns the shared, stateless instance.
      Returns:
      the shared, stateless instance
    • normalize

      public CharSequence normalize(CharSequence text)
      Normalizes a sequence of characters. URLs and email addresses are found in one pass, so an address inside a URL of any scheme is never matched on its own.
      Specified by:
      normalize in interface CharSequenceNormalizer
      Parameters:
      text - The CharSequence to normalize. Must not be null.
      Returns:
      The normalized CharSequence.
    • schemeEnd

      private int schemeEnd(CharSequence text, int start)
      Finds the end of a scheme and its separator that start at an index.
      Parameters:
      text - The text. Must not be null.
      start - The index the scheme must start at, inside the text.
      Returns:
      The index after ://, or -1 if no scheme with a left boundary starts there.
    • bodyEnd

      private int bodyEnd(CharSequence text, int bodyStart)
      Finds the end of a URL body that starts at an index.
      Parameters:
      text - The text. Must not be null.
      bodyStart - The index after the scheme separator.
      Returns:
      The exclusive end of the body, or bodyStart if the body is empty.
    • endsBody

      private boolean endsBody(int codePoint)
      Tests whether a code point ends a URL body.
      Parameters:
      codePoint - The code point; an unpaired surrogate is passed as its own value.
      Returns:
      true for whitespace, a control character, an unpaired surrogate, a typographic quotation mark, or a delimiter.
    • trimTrailing

      private int trimTrailing(CharSequence text, int bodyStart, int end)
      Gives back trailing punctuation and closing brackets that have no opening bracket inside the body.
      Parameters:
      text - The text. Must not be null.
      bodyStart - The index the body starts at.
      end - The exclusive end of the body as scanned.
      Returns:
      The exclusive end of the URL.
    • isHttpScheme

      private boolean isHttpScheme(CharSequence text, int start, int end)
      Tests whether the scheme between two indexes is http or https in any letter case.
      Parameters:
      text - The text. Must not be null.
      start - The index the scheme starts at.
      end - The index after the scheme, where :// starts.
      Returns:
      true if the scheme is http or https.
    • removeLegacyUrls

      private CharSequence removeLegacyUrls(CharSequence text)
      Replaces each URL of the legacy output with one space.
      Parameters:
      text - The text to scan; never null.
      Returns:
      The input itself when nothing matched, otherwise the normalized copy.
    • matchLegacyUrlEnd

      private int matchLegacyUrlEnd(CharSequence text, int start)
      Returns the exclusive end of a legacy URL match starting at start, or -1 if there is none.
      Parameters:
      text - The text to look into; never null.
      start - The index the match must start at.
      Returns:
      the exclusive end of a legacy URL match starting at start, or -1 if there is none