修复glossary导出csv时的bug
This commit is contained in:
@@ -1,3 +1,3 @@
|
|||||||
# SPDX-FileCopyrightText: 2025 QinHan
|
# SPDX-FileCopyrightText: 2025 QinHan
|
||||||
# SPDX-License-Identifier: MPL-2.0
|
# SPDX-License-Identifier: MPL-2.0
|
||||||
__version__="1.3.0"
|
__version__="1.3.0b1"
|
||||||
@@ -1,32 +1,38 @@
|
|||||||
# SPDX-FileCopyrightText: 2025 QinHan
|
# SPDX-FileCopyrightText: 2025 QinHan
|
||||||
# SPDX-License-Identifier: MPL-2.0
|
# SPDX-License-Identifier: MPL-2.0
|
||||||
|
import csv
|
||||||
|
from io import StringIO
|
||||||
|
|
||||||
from docutranslate.ir.document import Document
|
from docutranslate.ir.document import Document
|
||||||
|
|
||||||
|
|
||||||
class Glossary:
|
class Glossary:
|
||||||
def __init__(self,glossary_dict:dict[str:str]=None):
|
def __init__(self, glossary_dict: dict[str:str] = None):
|
||||||
self.glossary_dict=glossary_dict
|
self.glossary_dict = glossary_dict
|
||||||
|
|
||||||
def update(self,update_dict:dict[str:str]):
|
def update(self, update_dict: dict[str:str]):
|
||||||
for src,dst in update_dict.items():
|
for src, dst in update_dict.items():
|
||||||
if src not in self.glossary_dict:
|
if src not in self.glossary_dict:
|
||||||
self.glossary_dict[src]=dst
|
self.glossary_dict[src] = dst
|
||||||
|
|
||||||
def append_system_prompt(self,text:str):
|
def append_system_prompt(self, text: str):
|
||||||
flag=False
|
flag = False
|
||||||
prompt="\n以下为参考术语表:\n"
|
prompt = "\n以下为参考术语表:\n"
|
||||||
for src,dst in self.glossary_dict.items():
|
for src, dst in self.glossary_dict.items():
|
||||||
if src in text:
|
if src in text:
|
||||||
prompt+=f"{src}=>{dst}\n"
|
prompt += f"{src}=>{dst}\n"
|
||||||
flag=True
|
flag = True
|
||||||
prompt+="术语表结束\n"
|
prompt += "术语表结束\n"
|
||||||
if flag:
|
if flag:
|
||||||
return prompt
|
return prompt
|
||||||
else:
|
else:
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def glossary_dict2csv(glossary_dict: dict[str, str], seperator=",", stem="glossary_gen") -> Document:
|
def glossary_dict2csv(glossary_dict: dict[str, str], delimiter=",", stem="glossary_gen") -> Document:
|
||||||
content = f"src{seperator}dst\n"
|
csv_rows = [[src, dst] for src, dst in glossary_dict.items()]
|
||||||
for src, dst in glossary_dict.items():
|
content = StringIO()
|
||||||
content += f"{src}{seperator}{dst}\n"
|
writer = csv.writer(content, delimiter=delimiter)
|
||||||
return Document.from_bytes(content=content.encode("utf-8"), suffix=".csv", stem=stem)
|
writer.writerow(['src', 'dst'])
|
||||||
|
writer.writerows(csv_rows)
|
||||||
|
return Document.from_bytes(content=content.getvalue().encode("utf-8"), suffix=".csv", stem=stem)
|
||||||
|
|||||||
Reference in New Issue
Block a user