Fab*_*ble 11 python turkish built-in python-3.x cyrillic
我正在使用多语言文本数据,其中包括使用西里尔字母和土耳其语的俄语.我基本上要的话在比较两个文件my_file和check_file,如果在的话my_file可以发现check_file,把它们写在输出文件中保留约从两个输入文件这些词的元信息.
有些单词是小写的,而其他单词是大写的,所以我必须小写所有单词来比较它们.当我使用Python 3.6.5并且Python 3使用unicode作为默认值时,它会处理小写,然后为Cyrillic正确地大写单词.但是对于土耳其语,有些字母处理不正确.大写'?'应对应小写'i',大写'I'应对应小写'?',小写'i'应对应大写'?',如果我在控制台中键入以下内容则不是这种情况:
>>> print('?'.lower())
i? # somewhat not rendered correctly, corresponds to unicode 'i\u0307'
>>> print('I'.lower())
i
>>> print('i'.upper())
I
Run Code Online (Sandbox Code Playgroud)
我正在做如下(简化的示例代码):
# python my_file check_file language
import sys
language = sys.argv[3]
# code to get the files as lists
my_file_list = [['?spanak', 'N'], ['?s?r', 'N'], ['ac?k', 'V']]
check_file_list = [['109', 'Ispanak', 'food_drink'], ['470', 'Is?r', 'action_words'], [409, 'Ac?k', 'action_words']]
# get the lists as dict
my_dict = {}
check_dict = {}
for l in my_file_list:
word = l[0].lower()
pos = l[1]
my_dict[word] = pos
for l in check_file_list:
word_id = l[0]
word = l[1].lower()
word_cat = l[2]
check_dict[word] = [word_id, word_cat]
# compare the two dicts
for word, pos in my_dict.items():
if word in check_dict:
word_id = check_dict[word][0]
word_cat = check_dict[word][1]
print(word, pos, word_id, word_cat)
Run Code Online (Sandbox Code Playgroud)
这只给我一个结果,但它应该给我三个字作为结果:
ac?k V 409 action_words
Run Code Online (Sandbox Code Playgroud)
到目前为止我基于这个问题做了什么:
import locale并locale.setlocale(locale.LC_ALL, 'tr_TR.UTF-8')在问题中提到,但它没有改变任何东西.实现两个函数turkish_lower(self)和turkish_upper(self)三个有问题的字母,如第二个答案中所述,这似乎是唯一的解决方案:
def turkish_lower(self):
self = re.sub(r'?', 'i', self)
self = re.sub(r'I', '?', self)
self = self.lower()
return self
def turkish_upper(self):
self = re.sub(r'i', '?', self)
self = self.upper()
return self
Run Code Online (Sandbox Code Playgroud)但是如何在不必if language == 'Turkish'每次检查的情况下使用这两个功能呢?我应该重写内置的功能lower()和upper()?如果是的话,pythonic的做法是什么?我应该为我正在使用的各种语言实现类,并覆盖类内的土耳其语内置函数吗?
我建议尝试安装区域设置的土耳其语语言包:
sudo apt-get install language-pack-tr
sudo dpkg-reconfigure locales # *
Run Code Online (Sandbox Code Playgroud)
您还可以使用终端命令检查区域设置中的语言:$ locale -a
| 归档时间: |
|
| 查看次数: |
413 次 |
| 最近记录: |