-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented translation of text in directories
- Loading branch information
Showing
4 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
.openai.env | ||
.DS_Store | ||
data | ||
gcp_key.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ dependencies: | |
- pandas==1.5.3 | ||
- tiktoken==0.2.0 | ||
- beautifulsoup4==4.11.2 | ||
- google-cloud-translate==3.11.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import os | ||
from google.cloud import translate_v2 as translate | ||
|
||
|
||
if __name__ == '__main__': | ||
# set up the translation client | ||
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'gcp_key.json' | ||
translate_client = translate.Client() | ||
# specify the directory containing the text files | ||
input_directory = './data/elections/estonian/' | ||
# specify the output directory for the translated files | ||
output_directory = './data/elections/english/' | ||
|
||
for subdir, dirs, files in os.walk(input_directory): | ||
for filename in files: | ||
filepath = os.path.join(subdir, filename) | ||
# only process .txt files | ||
if filepath.endswith('.txt'): | ||
input_file_path = filepath | ||
output_file_path = os.path.join(output_directory, filename) | ||
with open(input_file_path, 'r', encoding='utf-8') as input_file: | ||
text = input_file.read() | ||
# translate the text from Estonian to English | ||
try: | ||
result = translate_client.translate(text, target_language='en', format_='text') | ||
except Exception as e: | ||
print(e) | ||
continue | ||
# write the translated text to the output file | ||
with open(output_file_path, 'w', encoding='utf-8') as output_file: | ||
output_file.write(result['translatedText']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters