Spaces:
Runtime error
Runtime error
translate efficient
Browse files- multi_language.py +34 -0
multi_language.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
def extract_chinese_characters(file_path):
|
| 4 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 5 |
+
content = f.read()
|
| 6 |
+
chinese_characters = []
|
| 7 |
+
sentence = {'file':file_path, 'begin':-1, 'end':-1, 'word': ""}
|
| 8 |
+
for index, char in enumerate(content):
|
| 9 |
+
if 0x4e00 <= ord(char) <= 0x9fff:
|
| 10 |
+
sentence['word'] += char
|
| 11 |
+
if sentence['begin'] == -1: sentence['begin'] = index
|
| 12 |
+
sentence['end'] = index
|
| 13 |
+
else:
|
| 14 |
+
if len(sentence['word'])>0:
|
| 15 |
+
chinese_characters.append(sentence)
|
| 16 |
+
sentence = {'file':file_path, 'begin':-1, 'end':-1, 'word': ""}
|
| 17 |
+
return chinese_characters
|
| 18 |
+
|
| 19 |
+
def extract_chinese_characters_from_directory(directory_path):
|
| 20 |
+
chinese_characters = []
|
| 21 |
+
for root, dirs, files in os.walk(directory_path):
|
| 22 |
+
for file in files:
|
| 23 |
+
if file.endswith('.py'):
|
| 24 |
+
file_path = os.path.join(root, file)
|
| 25 |
+
chinese_characters.extend(extract_chinese_characters(file_path))
|
| 26 |
+
return chinese_characters
|
| 27 |
+
|
| 28 |
+
directory_path = './'
|
| 29 |
+
chinese_characters = extract_chinese_characters_from_directory(directory_path)
|
| 30 |
+
word_to_translate = {}
|
| 31 |
+
for d in chinese_characters:
|
| 32 |
+
word_to_translate[d['word']] = "Translation"
|
| 33 |
+
|
| 34 |
+
print('All Chinese characters:', chinese_characters)
|