2017-03-12 12:07:28 +00:00
|
|
|
|
# coding: utf8
|
2016-12-08 18:46:43 +00:00
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
2017-04-19 23:22:52 +00:00
|
|
|
|
import regex as re
|
|
|
|
|
re.DEFAULT_VERSION = re.VERSION1
|
2017-01-08 19:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_UNITS = """
|
|
|
|
|
km km² km³ m m² m³ dm dm² dm³ cm cm² cm³ mm mm² mm³ ha µm nm yd in ft kg g mg
|
|
|
|
|
µg t lb oz m/s km/h kmh mph hPa Pa mbar mb MB kb KB gb GB tb
|
|
|
|
|
TB T G M K
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_CURRENCY = r"""
|
|
|
|
|
\$ £ € ¥ ฿ US\$ C\$ A\$
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_QUOTES = r"""
|
|
|
|
|
' '' " ” “ `` ` ‘ ´ ‚ , „ » «
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_PUNCT = r"""
|
|
|
|
|
… , : ; \! \? ¿ ¡ \( \) \[ \] \{ \} < > _ # \* &
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_HYPHENS = r"""
|
|
|
|
|
- – — -- ---
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LIST_ELLIPSES = [
|
|
|
|
|
r'\.\.+',
|
|
|
|
|
"…"
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LIST_CURRENCY = list(_CURRENCY.strip().split())
|
|
|
|
|
LIST_QUOTES = list(_QUOTES.strip().split())
|
|
|
|
|
LIST_PUNCT = list(_PUNCT.strip().split())
|
|
|
|
|
LIST_HYPHENS = list(_HYPHENS.strip().split())
|
|
|
|
|
|
|
|
|
|
|
2017-04-19 23:25:02 +00:00
|
|
|
|
BENGALI = r'[\p{L}&&\p{Bengali}]'
|
|
|
|
|
HEBREW = r'[\p{L}&&\p{Hebrew}]'
|
|
|
|
|
LATIN_LOWER = r'[\p{Ll}&&\p{Latin}]'
|
|
|
|
|
LATIN_UPPER = r'[\p{Lu}&&\p{Latin}]'
|
|
|
|
|
LATIN = r'[[\p{Ll}||\p{Lu}]&&\p{Latin}]'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ALPHA_LOWER = '[{}]'.format('||'.join([BENGALI, HEBREW, LATIN_LOWER]))
|
|
|
|
|
ALPHA_UPPER = '[{}]'.format('||'.join([BENGALI, HEBREW, LATIN_UPPER]))
|
|
|
|
|
ALPHA = '[{}]'.format('||'.join([BENGALI, HEBREW, LATIN]))
|
2017-01-08 19:37:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QUOTES = _QUOTES.strip().replace(' ', '|')
|
|
|
|
|
CURRENCY = _CURRENCY.strip().replace(' ', '|')
|
2017-01-14 14:51:59 +00:00
|
|
|
|
UNITS = _UNITS.strip().replace(' ', '|').replace('\n', '|')
|
2017-01-08 19:37:39 +00:00
|
|
|
|
HYPHENS = _HYPHENS.strip().replace(' ', '|')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Prefixes
|
|
|
|
|
|
|
|
|
|
TOKENIZER_PREFIXES = (
|
2017-02-02 15:21:11 +00:00
|
|
|
|
['§', '%', '=', r'\+'] +
|
2017-01-08 19:37:39 +00:00
|
|
|
|
LIST_PUNCT +
|
|
|
|
|
LIST_ELLIPSES +
|
|
|
|
|
LIST_QUOTES +
|
|
|
|
|
LIST_CURRENCY
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Suffixes
|
|
|
|
|
|
|
|
|
|
TOKENIZER_SUFFIXES = (
|
|
|
|
|
LIST_PUNCT +
|
|
|
|
|
LIST_ELLIPSES +
|
|
|
|
|
LIST_QUOTES +
|
|
|
|
|
[
|
|
|
|
|
r'(?<=[0-9])\+',
|
|
|
|
|
r'(?<=°[FfCcKk])\.',
|
|
|
|
|
r'(?<=[0-9])(?:{c})'.format(c=CURRENCY),
|
|
|
|
|
r'(?<=[0-9])(?:{u})'.format(u=UNITS),
|
|
|
|
|
r'(?<=[0-9{al}{p}(?:{q})])\.'.format(al=ALPHA_LOWER, p=r'%²\-\)\]\+', q=QUOTES),
|
2017-01-08 21:28:25 +00:00
|
|
|
|
r'(?<=[{au}][{au}])\.'.format(au=ALPHA_UPPER),
|
2017-01-08 19:37:39 +00:00
|
|
|
|
"'s", "'S", "’s", "’S"
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Infixes
|
|
|
|
|
|
|
|
|
|
TOKENIZER_INFIXES = (
|
|
|
|
|
LIST_ELLIPSES +
|
|
|
|
|
[
|
2017-01-12 21:58:26 +00:00
|
|
|
|
r'(?<=[0-9])[+\-\*^](?=[0-9-])',
|
2017-01-08 19:37:39 +00:00
|
|
|
|
r'(?<=[{al}])\.(?=[{au}])'.format(al=ALPHA_LOWER, au=ALPHA_UPPER),
|
|
|
|
|
r'(?<=[{a}]),(?=[{a}])'.format(a=ALPHA),
|
2017-02-02 15:22:40 +00:00
|
|
|
|
r'(?<=[{a}])[?";:=,.]*(?:{h})(?=[{a}])'.format(a=ALPHA, h=HYPHENS),
|
2017-04-07 15:30:44 +00:00
|
|
|
|
r'(?<=[{a}"])[:<>=/](?=[{a}])'.format(a=ALPHA)
|
2017-01-08 19:37:39 +00:00
|
|
|
|
]
|
|
|
|
|
)
|
2016-12-08 18:46:43 +00:00
|
|
|
|
|
|
|
|
|
|
2016-12-18 14:35:56 +00:00
|
|
|
|
__all__ = ["TOKENIZER_PREFIXES", "TOKENIZER_SUFFIXES", "TOKENIZER_INFIXES"]
|