Trình dịch ngôn ngữ python GitHub
Trình dịch ngôn ngữ hay dịch văn bản là một công cụ để dịch văn bản, từ, cụm từ từ một ngôn ngữ này sang bất kỳ ngôn ngữ nào khác. Nó giống như một từ điển nơi chúng ta có thể dịch văn bản Show
Để thực hiện dự án này, chúng tôi sẽ sử dụng các khái niệm cơ bản về thư viện Python, Tkinter và googletrans Tkinter là một thư viện GUI Python tiêu chuẩn. mô-đun ttk cấp quyền truy cập vào bộ tiện ích theo chủ đề Tk googletrans là một mô-đun để dịch văn bản. Chúng tôi nhập Trình dịch từ googletrans, được sử dụng để thực hiện các bản dịch. Chúng tôi cũng nhập NGÔN NGỮ từ googletrans liệt kê tất cả các ngôn ngữ được hỗ trợ trong từ điển Python Để cài đặt thư viện, hãy sử dụng pip install vào dấu nhắc lệnh pip cài đặt tkinter pip cài đặt googletrans Các bước xây dựng Dự án Python Trình dịch văn bảnNhập các mô-đun cần thiết Tạo cửa sổ hiển thị Tạo tiện ích văn bản đầu vào và đầu ra Xác định Combobox để chọn ngôn ngữ Xác định chức năng Tạo nút dịch API DeepL là API dịch ngôn ngữ cho phép các chương trình máy tính khác gửi văn bản và tài liệu đến máy chủ của DeepL và nhận bản dịch chất lượng cao. Điều này mở ra cả một vũ trụ cơ hội cho các nhà phát triển. bất kỳ sản phẩm dịch thuật nào bạn có thể tưởng tượng giờ đây đều có thể được xây dựng dựa trên công nghệ dịch thuật hàng đầu của DeepL Thư viện DeepL Python cung cấp một cách thuận tiện để các ứng dụng được viết bằng Python tương tác với API DeepL. Chúng tôi dự định hỗ trợ tất cả các chức năng API với thư viện, mặc dù hỗ trợ cho các tính năng mới có thể được thêm vào thư viện sau khi chúng được thêm vào API Nhận khóa xác thựcĐể sử dụng Thư viện DeepL Python, bạn cần có khóa xác thực API. Để lấy chìa khóa,. Với tài khoản DeepL API Free, bạn có thể dịch miễn phí tối đa 500.000 ký tự/tháng Cài đặtThư viện có thể được cài đặt từ PyPI bằng pip pip install --upgrade deepl Nếu bạn cần sửa đổi mã nguồn này, hãy cài đặt các phụ thuộc bằng thơ poetry install Trên Ubuntu 22. 04 một lỗi có thể xảy ra. import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"0. Sử dụng giải pháp thay thế import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"1 như được giải thích trong báo cáo lỗi này Yêu cầuThư viện được thử nghiệm với Python phiên bản 3. 6 đến 3. 11 Mô-đun import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"2 được sử dụng để thực hiện các yêu cầu HTTP; . 0 Cách sử dụngNhập gói và xây dựng một import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"3. Đối số đầu tiên là một chuỗi chứa khóa xác thực API của bạn như được tìm thấy trong Tài khoản DeepL Pro của bạn Hãy cẩn thận để không làm lộ khóa của bạn, chẳng hạn như khi chia sẻ mã nguồn import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" Ví dụ này chỉ dành cho mục đích trình diễn. Trong mã sản xuất, khóa xác thực không được mã hóa cứng mà thay vào đó được tìm nạp từ tệp cấu hình hoặc biến môi trường import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"3 chấp nhận các tùy chọn bổ sung, xem để biết thêm thông tin Dịch văn bảnĐể dịch văn bản, hãy gọi import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"5. Đối số đầu tiên là một chuỗi chứa văn bản bạn muốn dịch hoặc danh sách các chuỗi nếu bạn muốn dịch nhiều văn bản import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6 và import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"7 chỉ định mã ngôn ngữ nguồn và đích tương ứng. import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6 là tùy chọn, nếu nó không được chỉ định, ngôn ngữ nguồn sẽ được tự động phát hiện Mã ngôn ngữ là các chuỗi phân biệt chữ hoa chữ thường theo tiêu chuẩn ISO 639-1, ví dụ: import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"9, # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # Translate multiple texts into British English result = translator.translate_text( ["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB" ) print(result[0].text) # "How are you?" print(result[0].detected_source_lang) # "JA" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'0, # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # Translate multiple texts into British English result = translator.translate_text( ["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB" ) print(result[0].text) # "How are you?" print(result[0].detected_source_lang) # "JA" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'1. Một số ngôn ngữ đích cũng bao gồm biến thể khu vực theo ISO 3166-1, ví dụ: # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # Translate multiple texts into British English result = translator.translate_text( ["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB" ) print(result[0].text) # "How are you?" print(result[0].detected_source_lang) # "JA" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'2 hoặc # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # Translate multiple texts into British English result = translator.translate_text( ["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB" ) print(result[0].text) # "How are you?" print(result[0].detected_source_lang) # "JA" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'3. Danh sách đầy đủ các ngôn ngữ được hỗ trợ có trong tài liệu API Có các đối số tùy chọn bổ sung để kiểm soát bản dịch, xem bên dưới import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"5 trả về một # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # Translate multiple texts into British English result = translator.translate_text( ["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB" ) print(result[0].text) # "How are you?" print(result[0].detected_source_lang) # "JA" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'5 hoặc một danh sách các # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # Translate multiple texts into British English result = translator.translate_text( ["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB" ) print(result[0].text) # "How are you?" print(result[0].detected_source_lang) # "JA" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'5 tương ứng với (các) văn bản đầu vào của bạn. # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # Translate multiple texts into British English result = translator.translate_text( ["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB" ) print(result[0].text) # "How are you?" print(result[0].detected_source_lang) # "JA" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'5 có hai thuộc tính. # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # Translate multiple texts into British English result = translator.translate_text( ["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB" ) print(result[0].text) # "How are you?" print(result[0].detected_source_lang) # "JA" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'8 là văn bản đã dịch và # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # Translate multiple texts into British English result = translator.translate_text( ["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB" ) print(result[0].text) # "How are you?" print(result[0].detected_source_lang) # "JA" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'9 là mã ngôn ngữ nguồn được phát hiện # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # Translate multiple texts into British English result = translator.translate_text( ["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB" ) print(result[0].text) # "How are you?" print(result[0].detected_source_lang) # "JA" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?' Tùy chọn dịch văn bảnNgoài đối số (các) văn bản đầu vào, các đối số import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"5 có sẵn là
Các tùy chọn sau chỉ được sử dụng nếu # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )2 là # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )4
Để biết giải thích chi tiết về các tùy chọn xử lý XML, hãy xem tài liệu API Dịch tài liệuĐể dịch tài liệu, bạn có thể gọi # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)7 bằng cách sử dụng các đối tượng tệp IO hoặc # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)8 bằng cách sử dụng đường dẫn tệp. Đối với cả hai chức năng, đối số thứ nhất và thứ hai tương ứng với các tệp đầu vào và đầu ra tương ứng Cũng giống như đối với hàm import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"5, các đối số import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6 và import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"7 chỉ định mã ngôn ngữ nguồn và ngôn ngữ đích Có các đối số tùy chọn bổ sung để kiểm soát bản dịch, xem bên dưới # Translate a formal document from English to German input_path = "/path/to/Instruction Manual.docx" output_path = "/path/to/Bedienungsanleitung.docx" try: # Using translate_document_from_filepath() with file paths translator.translate_document_from_filepath( input_path, output_path, target_lang="DE", formality="more" ) # Alternatively you can use translate_document() with file IO objects with open(input_path, "rb") as in_file, open(output_path, "wb") as out_file: translator.translate_document( in_file, out_file, target_lang="DE", formality="more" ) except deepl.DocumentTranslationException as error: # If an error occurs during document translation after the document was # already uploaded, a DocumentTranslationException is raised. The # document_handle property contains the document handle that may be used to # later retrieve the document from the server, or contact DeepL support. doc_id = error.document_handle.id doc_key = error.document_handle.key print(f"Error after uploading ${error}, id: ${doc_id} key: ${doc_key}") except deepl.DeepLException as error: # Errors during upload raise a DeepLException print(error) # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)7 và # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)8 là các chức năng tiện lợi bao gồm nhiều lệnh gọi API. tải lên, thăm dò trạng thái cho đến khi bản dịch hoàn tất và tải xuống. Nếu ứng dụng của bạn cần thực hiện các bước này riêng lẻ, thay vào đó, bạn có thể sử dụng trực tiếp các chức năng sau
Tùy chọn dịch tài liệuNgoài các đối số tệp đầu vào, tệp đầu ra, import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6 và import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"7, các đối số # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)7 và # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)8 có sẵn là
Bảng thuật ngữBảng thuật ngữ cho phép bạn tùy chỉnh bản dịch của mình bằng các thuật ngữ do người dùng xác định. Nhiều bảng thuật ngữ có thể được lưu trữ với tài khoản của bạn, mỗi bảng có một tên do người dùng chỉ định và một ID được chỉ định duy nhất Tạo bảng thuật ngữBạn có thể tạo bảng thuật ngữ với các thuật ngữ và tên mong muốn bằng cách sử dụng text = "The artist was awarded a prize." with_glossary = translator.translate_text( text, source_lang="EN", target_lang="DE", glossary=my_glossary, ) print(with_glossary) # "Der Maler wurde mit einem Gewinn ausgezeichnet." # For comparison, the result without a glossary: without_glossary = translator.translate_text(text, target_lang="DE") print(without_glossary) # "Der Künstler wurde mit einem Preis ausgezeichnet."4. Mỗi bảng thuật ngữ áp dụng cho một cặp ngôn ngữ nguồn-đích. Ghi chú. Bảng thuật ngữ chỉ được hỗ trợ cho một số cặp ngôn ngữ, hãy xem để biết thêm thông tin. Các mục nên được chỉ định như một từ điển Nếu thành công, bảng thuật ngữ được tạo và lưu trữ bằng tài khoản DeepL của bạn và một đối tượng # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )0 được trả về bao gồm ID, tên, ngôn ngữ và số lượng mục nhập # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries Bạn cũng có thể tải lên bảng thuật ngữ được tải xuống từ trang web DeepL bằng cách sử dụng text = "The artist was awarded a prize." with_glossary = translator.translate_text( text, source_lang="EN", target_lang="DE", glossary=my_glossary, ) print(with_glossary) # "Der Maler wurde mit einem Gewinn ausgezeichnet." # For comparison, the result without a glossary: without_glossary = translator.translate_text(text, target_lang="DE") print(without_glossary) # "Der Künstler wurde mit einem Preis ausgezeichnet."6. Thay vì cung cấp các mục nhập dưới dạng từ điển, hãy chỉ định dữ liệu CSV là text = "The artist was awarded a prize." with_glossary = translator.translate_text( text, source_lang="EN", target_lang="DE", glossary=my_glossary, ) print(with_glossary) # "Der Maler wurde mit einem Gewinn ausgezeichnet." # For comparison, the result without a glossary: without_glossary = translator.translate_text(text, target_lang="DE") print(without_glossary) # "Der Künstler wurde mit einem Preis ausgezeichnet."7 dưới dạng đối tượng giống như tệp hoặc chuỗi hoặc byte chứa nội dung tệp # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, ) Tài liệu API giải thích chi tiết về định dạng CSV dự kiến Nhận, liệt kê và xóa bảng thuật ngữ đã lưu trữCác chức năng lấy, liệt kê và xóa bảng thuật ngữ đã lưu trữ cũng được cung cấp
# Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary) Liệt kê các mục trong một bảng thuật ngữ được lưu trữĐối tượng # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )0 không chứa các mục trong bảng chú giải thuật ngữ mà thay vào đó chỉ chứa số mục trong thuộc tính poetry install05 Để liệt kê các mục có trong bảng thuật ngữ được lưu trữ, hãy sử dụng poetry install06 cung cấp đối tượng # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )0 hoặc ID bảng thuật ngữ entries = translator.get_glossary_entries(my_glossary) print(entries) # "{'artist': 'Maler', 'prize': 'Gewinn'}" Sử dụng bảng thuật ngữ được lưu trữBạn có thể sử dụng bảng thuật ngữ được lưu trữ để dịch văn bản bằng cách đặt đối số # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries9 thành ID bảng thuật ngữ hoặc đối tượng # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )0. Bạn cũng phải chỉ định đối số import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6 (bắt buộc phải có khi sử dụng bảng thuật ngữ) text = "The artist was awarded a prize." with_glossary = translator.translate_text( text, source_lang="EN", target_lang="DE", glossary=my_glossary, ) print(with_glossary) # "Der Maler wurde mit einem Gewinn ausgezeichnet." # For comparison, the result without a glossary: without_glossary = translator.translate_text(text, target_lang="DE") print(without_glossary) # "Der Künstler wurde mit einem Preis ausgezeichnet." Sử dụng bảng thuật ngữ được lưu trữ để dịch tài liệu cũng giống như vậy. đặt đối số # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries9 và chỉ định đối số import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6 poetry install0 Các hàm # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)7, # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)8 và entries = translator.get_glossary_entries(my_glossary) print(entries) # "{'artist': 'Maler', 'prize': 'Gewinn'}"4 đều hỗ trợ đối số # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries9 Kiểm tra việc sử dụng tài khoảnĐể kiểm tra việc sử dụng tài khoản, hãy sử dụng chức năng poetry install17 Đối tượng poetry install18 được trả về chứa ba kiểu con sử dụng. poetry install19, poetry install20 và poetry install21. Tùy thuộc vào loại tài khoản của bạn, một số loại phụ sử dụng có thể không hợp lệ; . Đối với tài khoản API
Mỗi loại phụ sử dụng (nếu hợp lệ) có các thuộc tính poetry install28 và poetry install29 cho biết lượng sử dụng và lượng tối đa tương ứng và thuộc tính poetry install30 kiểm tra xem việc sử dụng đã đạt đến giới hạn chưa. Đối tượng cấp cao nhất poetry install18 có thuộc tính poetry install32 để kiểm tra tất cả các kiểu con sử dụng poetry install1 Liệt kê các ngôn ngữ có sẵnBạn có thể yêu cầu danh sách các ngôn ngữ được DeepL hỗ trợ cho văn bản và tài liệu bằng các hàm poetry install33 và poetry install34. Cả hai đều trả về danh sách poetry install35 đối tượng Thuộc tính poetry install36 cung cấp tên của ngôn ngữ bằng tiếng Anh và thuộc tính poetry install37 cung cấp mã ngôn ngữ. Thuộc tính poetry install38 chỉ xuất hiện cho ngôn ngữ đích và cho biết liệu ngôn ngữ đích có hỗ trợ tham số # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries4 tùy chọn hay không poetry install2 Liệt kê các ngôn ngữ chú giải thuật ngữ có sẵnBảng thuật ngữ được hỗ trợ cho một tập hợp con các cặp ngôn ngữ. Để truy xuất các ngôn ngữ đó, hãy sử dụng hàm poetry install40, hàm này trả về một mảng gồm các đối tượng poetry install41. Mỗi thuộc tính có import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6 và import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"7 cho biết rằng cặp mã ngôn ngữ đó được hỗ trợ poetry install3 Bạn cũng có thể tìm thấy danh sách các cặp ngôn ngữ bảng thuật ngữ được hỗ trợ trong tài liệu API Lưu ý rằng bảng thuật ngữ hoạt động cho tất cả các biến thể khu vực mục tiêu. bảng thuật ngữ cho ngôn ngữ đích Tiếng Anh (_______1_______44) hỗ trợ dịch sang cả tiếng Anh Mỹ ( poetry install45) và tiếng Anh Anh ( poetry install46) ngoại lệTất cả các chức năng mô-đun có thể tăng poetry install47 hoặc một trong các lớp con của nó. Nếu các đối số không hợp lệ được cung cấp, họ có thể đưa ra các ngoại lệ tiêu chuẩn poetry install48 và poetry install49 Cấu hìnhghi nhật kýCó thể bật tính năng ghi nhật ký để xem các yêu cầu HTTP đã gửi và các phản hồi mà thư viện nhận được. Ví dụ: bật và kiểm soát ghi nhật ký bằng mô-đun poetry install50 của Python poetry install4 Cấu hình URL máy chủBạn có thể ghi đè URL của API DeepL bằng cách chỉ định đối số poetry install51 khi xây dựng một poetry install52. Điều này có thể hữu ích cho mục đích thử nghiệm. Bạn không cần chỉ định URL để phân biệt tài khoản API Free và API Pro, thư viện sẽ tự động chọn đúng URL poetry install5 cấu hình proxyBạn có thể định cấu hình proxy bằng cách chỉ định đối số poetry install53 khi xây dựng một poetry install52 poetry install6 Đối số proxy được chuyển đến phiên import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"2 bên dưới, xem phần ; Giao diện dòng lệnhThư viện có thể chạy trên dòng lệnh hỗ trợ tất cả các chức năng API. Sử dụng tùy chọn poetry install56 để biết thông tin sử dụng poetry install7 CLI yêu cầu khóa xác thực DeepL của bạn được chỉ định dưới dạng biến môi trường poetry install57, thông qua mô-đun poetry install58 hoặc sử dụng tùy chọn poetry install59 chẳng hạn poetry install8 Lưu ý rằng đối số poetry install59 phải xuất hiện trước đối số lệnh. Để sử dụng mô-đun khóa, hãy đặt trường DEEPL_AUTH_KEY trong dịch vụ deepl thành khóa API của bạn. Các lệnh được công nhận làLệnh Mô tả văn bản dịch (các) văn bản tài liệudịch (các) tài liệu sử dụng bản in thông tin sử dụng cho kỳ thanh toán hiện tại ngôn ngữ in các ngôn ngữ có sẵn bảng thuật ngữtạo, liệt kê và xóa bảng thuật ngữ Ví dụ, để dịch văn bản poetry install9 Gói các đối số văn bản trong dấu ngoặc kép để ngăn trình bao tách câu thành từ Vấn đềNếu bạn gặp sự cố khi sử dụng thư viện hoặc muốn yêu cầu một tính năng mới, vui lòng mở một sự cố Phát triểnChúng tôi hoan nghênh Yêu cầu kéo, vui lòng đọc hướng dẫn đóng góp bài kiểm traThực hiện các bài kiểm tra bằng cách sử dụng poetry install61. Các thử nghiệm giao tiếp với API DeepL bằng cách sử dụng khóa xác thực được xác định bởi biến môi trường poetry install57 Xin lưu ý rằng các thử nghiệm tạo ra các yêu cầu API DeepL góp phần vào việc sử dụng API của bạn Thay vào đó, bộ thử nghiệm có thể được cấu hình để giao tiếp với máy chủ giả do deepl-mock cung cấp. Mặc dù hầu hết các trường hợp thử nghiệm đều hoạt động với cả hai, nhưng một số trường hợp thử nghiệm chỉ hoạt động với API DeepL hoặc máy chủ giả và nếu không sẽ bị bỏ qua. Các trường hợp kiểm tra yêu cầu lỗi máy chủ kích hoạt máy chủ mô phỏng và kiểm tra xử lý lỗi máy khách. Để thực hiện các bài kiểm tra bằng deepl-mock, hãy chạy nó trong một thiết bị đầu cuối khác trong khi thực hiện các bài kiểm tra. Thực hiện các bài kiểm tra bằng cách sử dụng poetry install61 với các biến môi trường poetry install64 và poetry install65 được xác định liên quan đến máy chủ giả Trình dịch ngôn ngữ nào được sử dụng trong Python *?Để dịch văn bản từ ngôn ngữ này sang ngôn ngữ khác bằng Python, chúng tôi sẽ sử dụng gói có tên googletrans đã được gọi nội bộ tới API Google Dịch.
Có API dịch miễn phí không?Cấp API miễn phí của Microsoft Translator cho phép bạn dịch tối đa 2.000.000 ký tự mỗi tháng (hai triệu).
Chúng tôi có thể sử dụng Google dịch bằng Python không?Googletrans là thư viện python miễn phí sử dụng API Google Dịch .
Có thể sử dụng NLP để dịch thuật không?Các công cụ dịch ngôn ngữ AI sử dụng NLP để dịch văn bản viết hoặc lời nói từ một ngôn ngữ nguồn sang ngôn ngữ đích khác . |