2013-11-16 19:21:35 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
# Copyright (C) 2013 Alex Yatskov
|
|
|
|
|
# This module is based on Rikaichan code written by Jonathan Zarate
|
|
|
|
|
#
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def isHiragana(c):
|
|
|
|
|
return 0x3040 <= ord(c) < 0x30a0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def isKatakana(c):
|
|
|
|
|
return 0x30a0 <= ord(c) < 0x3100
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def isKana(c):
|
|
|
|
|
return isHiragana(c) or isKatakana(c)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def isKanji(c):
|
|
|
|
|
return 0x4e00 <= ord(c) < 0x9fb0 or 0x3400 <= ord(c) < 0x4dc0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def isJapanese(c):
|
2016-05-29 06:29:54 +00:00
|
|
|
|
return isKana(c) or isKanji(c) or c == u'々'
|
2013-11-16 19:21:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sanitize(text, kana=True, wildcards=False):
|
|
|
|
|
if kana:
|
|
|
|
|
checker = isJapanese
|
|
|
|
|
else:
|
|
|
|
|
checker = isKanji
|
|
|
|
|
|
|
|
|
|
if wildcards:
|
|
|
|
|
text = re.sub(u'[\**]', u'%', text)
|
|
|
|
|
text = re.sub(u'[\??]', u'_', text)
|
|
|
|
|
overrides = [u'%', u'_']
|
|
|
|
|
else:
|
2016-05-18 16:30:57 +00:00
|
|
|
|
overrides = []
|
2013-11-16 19:21:35 +00:00
|
|
|
|
|
2016-05-18 05:12:05 +00:00
|
|
|
|
result = u''
|
2013-11-16 19:21:35 +00:00
|
|
|
|
for c in text:
|
|
|
|
|
if checker(c) or c in overrides:
|
|
|
|
|
result += c
|
|
|
|
|
|
|
|
|
|
return result
|