1 | # coding=utf-8 |
---|
2 | |
---|
3 | import re |
---|
4 | |
---|
5 | SGML_TAG = ur""" |
---|
6 | <!-- .*? --> # XML/SGML comment |
---|
7 | | # -- OR -- |
---|
8 | <[!?/]?(?!\d)\w[-\.:\w]* # Start of tag/directive |
---|
9 | ( # Attributes |
---|
10 | [^>'"]* # - attribute name (+whitespace +equal sign) |
---|
11 | ('[^']*'|"[^"]*") # - attribute value |
---|
12 | )* |
---|
13 | \s* # Spaces at the end |
---|
14 | /? # Forward slash at the end of singleton tags |
---|
15 | \s* # More spaces at the end |
---|
16 | > # +End of tag/directive |
---|
17 | """ |
---|
18 | SGML_TAG_RE = re.compile(SGML_TAG, re.UNICODE | re.VERBOSE | re.DOTALL) |
---|
19 | |
---|
20 | WHITESPACE = ur"\s+" |
---|
21 | WHITESPACE_RE = re.compile(WHITESPACE) |
---|
22 | |
---|
23 | DNS_HOST = ur"(([-a-z0-9]+\.)+[a-z]{2,})" |
---|
24 | |
---|
25 | URL = ur""" |
---|
26 | ( |
---|
27 | # scheme://[user:password] |
---|
28 | (ftps?|https?|file)://([-a-z0-9_;?&=](:[-a-z0-9_;?&=]*)?@)? |
---|
29 | # or "www" without the scheme part |
---|
30 | |www\. |
---|
31 | ) |
---|
32 | # DNS host / localhost / IP |
---|
33 | (""" + DNS_HOST + """ |
---|
34 | | localhost | |
---|
35 | ([0-9]{1,3}\.){3}[0-9]{1,3}) |
---|
36 | # Port specification (optional) |
---|
37 | (:[0-9]+)? |
---|
38 | # Scheme specific extension (optional) |
---|
39 | (/[-\w;/?:@=&\$_.+!*'(~#%,]*)? |
---|
40 | """ |
---|
41 | URL_RE = re.compile(URL, re.VERBOSE | re.IGNORECASE | re.UNICODE) |
---|
42 | |
---|
43 | EMAIL = ur"[-a-z0-9._']+@" + DNS_HOST |
---|
44 | EMAIL_RE = re.compile(EMAIL, re.IGNORECASE) |
---|
45 | |
---|
46 | HTMLENTITY = ur"&(#x?[0-9A-F]+|\w+);" |
---|
47 | HTMLENTITY_RE = re.compile(HTMLENTITY) |
---|
48 | |
---|
49 | HASHTAG = ur"(?<!\w)#[A-Za-z]\w+" |
---|
50 | HASHTAG_RE = re.compile(HASHTAG) |
---|
51 | |
---|
52 | DOTCOM = ur""" |
---|
53 | (?<!\w) |
---|
54 | ([-a-z0-9]+\.){1,2}(com|org|eu|no|nu) |
---|
55 | (?!\w) |
---|
56 | """ |
---|
57 | DOTCOM_RE = re.compile(DOTCOM, re.IGNORECASE | re.VERBOSE) |
---|
58 | |
---|
59 | ABBREVIATION = ur""" |
---|
60 | (?<!\w) |
---|
61 | (?: |
---|
62 | #general |
---|
63 | co\.|inc\.|ltd\.|dr\.|prof\.|jr\. |
---|
64 | | |
---|
65 | #http://en.wiktionary.org/wiki/Category:Swedish_abbreviations |
---|
66 | 1:a|2:a|3:a|4:a|5:a|6:a|7:a|8:a|9:a|10:a|11:a|12:a|13:a|14:a|15:a|16:a|17:a|18:a|19:a|20:a|21:a|22:a|23:a|24:a|25:a|26:a|27:a|28:a|29:a|30:a|31:a|1:e|2:e|3:e|4:e|5:e|6:e|7:e|8:e|9:e|10:e|11:e|12:e|13:e|14:e|15:e|16:e|17:e|18:e|19:e|20:e|21:e|22:e|23:e|24:e|25:e|26:e|27:e|28:e|29:e|30:e|31:e|ack\.|adj\.|adv\.|amer\.|anat\.|anv\.|Apg\.|arab\.|aram\.|arkeol\.|arkit\.|astr\.|bankv\.|bet\.|betyd\.|bibl\.|bildl\.|biol\.|bl\.a\.|bokf\.|boktr\.|bot\.|d\.|d\.v\.s\.|d\.y\.|d\.ä\.|da\.|data\.|cont\.|dets\.|dial\.|dim\.|Dr\.|dvs\.|e\.d\.|e\.dyl\.|e\.Kr\.|e\.m\.|eg\.|ekon\.|el\.|eng\.|etc\.|ev\.|ex\.|exkl\.|f\.|f\.d\.|f\.Kr\.|f\.m\.|f\.v\.t\.|fam\.|fem\.|fig\.|fil\.|filos\.|fonet\.|forneng\.|fornfra\.|fornhögty\.|fr\.|fr\.o\.m\.|fra\.|fsv\.|fys\.|förk\.|geogr\.|geol\.|geom\.|germ\.|got\.|grek\.|hand\.|hebr\.|hist\.|holl\.|ibl\.|cont\.|imperf\.|inf\.|ink\.|inkl\.|inst\.|interj\.|it\.|jap\.|jmf\.|jur\.|kem\.|kl\.|komp\.|konst\.|l\.|lat\.|litt\.|log\.|m\.fl\.|m\.m\.|mask\.|mat\.|med\.|medeleng\.|medelholl\.|medelhögty\.|medellågty\.|medeltidslat\.|meteor\.|mil\.|miner\.|mus\.|myt\.|N\.N\.|neds\.|neutr\.|no\.|nr\.|o\.d\.|o\.dyl\.|o\.s\.v\.|oböjl\.|omkr\.|osv\.|p\.g\.a\.|p\.m\.s\.|p\.s\.s\.|part\.|pedag\.|perf\.part\.|pers\.|plur\.|polit\.|port\.|prep\.|pres\.part\.|pron\.|psykol\.|real\.|resp\.|runsv\.|ry\.|s\.a\.s\.|s\.k\.|s\.ö\.|senlat\.|sing\.|sjö\.|skämts\.|sl\.|spa\.|sport\.|språkv\.|subst\.|särsk\.|t\.|t\. ex\.|t\.ex\.|t\.o\.m\.|tekn\.|teol\.|tex\.|cont\.|tr\.|ty\.|v\.t\.|vanl\.|vard\.|vers\.|vulgärlat\.|y\.|zool\.|ä\.|äv\.|åld\. |
---|
67 | | |
---|
68 | #https://en.wiktionary.org/wiki/Category:Norwegian_abbreviations |
---|
69 | adr\.|ans\.|bm\.|bto\.|d\.d\.|d\.m\.|d\.y\.|div\.|dvs\.|e\.Kr\.|eg\.|el\.|f\.eks\.|m\.m\.|ma\.|mva\.|n\.å\.|ndf\.|nkr|nov\.|nr\.|nto\.|nyno\.|osv\.|o\.s\.v\.|pga\.|rv\. |
---|
70 | ) |
---|
71 | (?!\w) |
---|
72 | """ |
---|
73 | ABBREVIATION_RE = re.compile(ABBREVIATION, re.UNICODE | re.VERBOSE | re.IGNORECASE) |
---|
74 | |
---|
75 | NUMBER = ur""" |
---|
76 | (?<!\S) |
---|
77 | [-+]? |
---|
78 | (\d[\d,.]*\d | \d) |
---|
79 | ([eE][-+][0-9]+)? |
---|
80 | (?![-\w]) |
---|
81 | """ |
---|
82 | NUMBER_RE = re.compile(NUMBER, re.UNICODE | re.VERBOSE) |
---|
83 | |
---|
84 | USA = ur""" |
---|
85 | (?<!\w) |
---|
86 | ([A-Z]\.){2,} |
---|
87 | (?![\w.]) |
---|
88 | """ |
---|
89 | USA_RE = re.compile(USA, re.UNICODE | re.VERBOSE) |
---|
90 | |
---|
91 | WORD = ur"\w[\w-]*\w|\w" |
---|
92 | WORD_RE = re.compile(WORD, re.UNICODE) |
---|
93 | |
---|
94 | MULTICHAR_PUNCTUATION = ur"([?!]+|'')" |
---|
95 | MULTICHAR_PUNCTUATION_RE = re.compile(MULTICHAR_PUNCTUATION) |
---|
96 | |
---|
97 | SINGLECHAR_PUNCTUATION = ur"[\u0028\u005b\u007b\u0029\u005d\u007d\u2985\u2989\u3008\u298d\u300c\u2991\u3010\u2995\u3014\u2018\u169b\u201c\xab\u23b5\xbb\u0f3a\ufd3e\u29d9\u27e9\u276a\u276e\u2772\u29fd\u2986\u300b\u298a\u300f\u298e\u2992\u3017\u3018\u301b\u169c\u301f\u0f3d\u29da\u27e6\u2769\u27ea\u276d\u2771\u2775\u2983\u2987\u298b\u300a\u298f\u300e\u2993\u2997\u3016\u301a\u301e\u203a\u0f3c\u2046\u29db\u27e7\u2768\u27eb\u276c\u2770\u2774\u3019\u2984\u3009\u2988\u2996\u300d\u298c\u3011\u2990\u3015\u2994\u2019\u2998\u201d\u301d\u23b4\u2039\u0f3b\ufd3f\u2045\u29d8\u27e8\u276b\u276f\u2773\u29fc]" |
---|
98 | SINGLECHAR_PUNCTUATION_RE = re.compile(SINGLECHAR_PUNCTUATION, re.UNICODE) |
---|
99 | |
---|
100 | ANY_SEQUENCE = ur"(.)\1*" |
---|
101 | ANY_SEQUENCE_RE = re.compile(ANY_SEQUENCE) |
---|
102 | |
---|
103 | re_list = [ |
---|
104 | ('SGML_TAG', SGML_TAG_RE), |
---|
105 | ('WHITESPACE', WHITESPACE_RE), |
---|
106 | ('URL', URL_RE), |
---|
107 | ('EMAIL', EMAIL_RE), |
---|
108 | ('HTMLENTITY', HTMLENTITY_RE), |
---|
109 | ('HASHTAG', HASHTAG_RE), |
---|
110 | ('DOTCOM', DOTCOM_RE), |
---|
111 | ('ABBREVIATION', ABBREVIATION_RE), |
---|
112 | ('NUMBER', NUMBER_RE), |
---|
113 | ('USA', USA_RE), |
---|
114 | ('WORD', WORD_RE), |
---|
115 | ('MULTICHAR_PUNCTUATION', MULTICHAR_PUNCTUATION_RE), |
---|
116 | ('SINGLECHAR_PUNCTUATION', SINGLECHAR_PUNCTUATION_RE), |
---|
117 | ('ANY_SEQUENCE', ANY_SEQUENCE_RE), |
---|
118 | ] |
---|