Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cleantext/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ def replace_urls(text, replace_with="<URL>"):
"""
Replace all URLs in ``text`` str with ``replace_with`` str.
"""
CJK = r'[\u4e00-\u9fff\u3040-\u30ff]' # CJK Unicode ranges: \u4e00-\u9fff (Chinese/Japanese Kanji), \u3040-\u30ff (Hiragana/Katakana)
B = r'[^\s,。?!;:、()【】「」“”‘’\)\]\}]'
text = re.sub(
r'(' + B + r'*?' + CJK + B + r'*?https?://' + B + r'*|' +
B + r'*?https?://' + B + r'*?' + CJK + B + r'*)',
lambda m: ''.join(re.findall(CJK, m.group(1))) + replace_with,
text
)
return constants.URL_REGEX.sub(replace_with, text)


Expand Down
24 changes: 24 additions & 0 deletions tests/test_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@ def test_replace_urls():
assert cleantext.replace_urls(text, "*URL*") == proc_text


def test_replace_CJK_urls():
texts = [
[
"请点击http://www.example.com查看内容",
"请点击查看内容*URL*",
],
[
"http://www.tie你好ba.com",
"你好*URL*",
],
[
"こんにちはhttp://example.com/テスト",
"こんにちはテスト*URL*",
],
[
"前部http://leetcode.com後ろにある",
"前部後ろにある*URL*",
],
]

for text, proc_text in texts:
assert cleantext.replace_urls(text, "*URL*") == proc_text


email_addresses = [
"mustermann@fh-aachen.de",
"mustermann(at)fh-aachen.de",
Expand Down