Class UrlCharSequenceNormalizer
- All Implemented Interfaces:
Serializable,CharSequenceNormalizer
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 Summary
FieldsModifier and TypeFieldDescriptionprivate static final CodePointSetDelimiters that end a URL body besides whitespace and control characters.private static final Stringprivate static final Stringprivate static final UrlCharSequenceNormalizerprivate static final CodePointSetThe URL body set of the legacy output:[-_.?&~;+=/#0-9A-Za-z].private static final CodePointSetThe characters a scheme continues with after its first letter, per RFC 3986.private static final CodePointSetCode points that, before the scheme, make it part of a larger token.private static final Stringprivate static final longprivate static final CodePointSetPunctuation given back when it trails a URL. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate intbodyEnd(CharSequence text, int bodyStart) Finds the end of a URL body that starts at an index.private booleanendsBody(int codePoint) Tests whether a code point ends a URL body.static UrlCharSequenceNormalizerReturns the shared, stateless instance.private booleanisHttpScheme(CharSequence text, int start, int end) Tests whether the scheme between two indexes ishttporhttpsin any letter case.private intmatchLegacyUrlEnd(CharSequence text, int start) Returns the exclusive end of a legacy URL match starting atstart, or-1if there is none.normalize(CharSequence text) Normalizes a sequence of characters.private CharSequenceremoveLegacyUrls(CharSequence text) Replaces each URL of the legacy output with one space.private intschemeEnd(CharSequence text, int start) Finds the end of a scheme and its separator that start at an index.private inttrimTrailing(CharSequence text, int bodyStart, int end) Gives back trailing punctuation and closing brackets that have no opening bracket inside the body.
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
SCHEME_SEPARATOR
- See Also:
-
HTTP
- See Also:
-
HTTPS
- See Also:
-
SCHEME_BODY
The characters a scheme continues with after its first letter, per RFC 3986. -
SCHEME_NEIGHBORS
Code points that, before the scheme, make it part of a larger token. -
BODY_DELIMITERS
Delimiters that end a URL body besides whitespace and control characters. -
TRAILING_PUNCTUATION
Punctuation given back when it trails a URL. -
LEGACY_URL_BODY
The URL body set of the legacy output:[-_.?&~;+=/#0-9A-Za-z]. -
INSTANCE
-
-
Constructor Details
-
UrlCharSequenceNormalizer
public UrlCharSequenceNormalizer()
-
-
Method Details
-
getInstance
Returns the shared, stateless instance.- Returns:
- the shared, stateless instance
-
normalize
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:
normalizein interfaceCharSequenceNormalizer- Parameters:
text- TheCharSequenceto normalize. Must not benull.- Returns:
- The normalized
CharSequence.
-
schemeEnd
Finds the end of a scheme and its separator that start at an index.- Parameters:
text- The text. Must not benull.start- The index the scheme must start at, inside the text.- Returns:
- The index after
://, or-1if no scheme with a left boundary starts there.
-
bodyEnd
Finds the end of a URL body that starts at an index.- Parameters:
text- The text. Must not benull.bodyStart- The index after the scheme separator.- Returns:
- The exclusive end of the body, or
bodyStartif 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:
truefor whitespace, a control character, an unpaired surrogate, a typographic quotation mark, or a delimiter.
-
trimTrailing
Gives back trailing punctuation and closing brackets that have no opening bracket inside the body.- Parameters:
text- The text. Must not benull.bodyStart- The index the body starts at.end- The exclusive end of the body as scanned.- Returns:
- The exclusive end of the URL.
-
isHttpScheme
Tests whether the scheme between two indexes ishttporhttpsin any letter case.- Parameters:
text- The text. Must not benull.start- The index the scheme starts at.end- The index after the scheme, where://starts.- Returns:
trueif the scheme ishttporhttps.
-
removeLegacyUrls
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
Returns the exclusive end of a legacy URL match starting atstart, or-1if 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-1if there is none
-