字幕拆分策略提示
생성일: 2024년 10월 24일
답변에 사용된 모델 O1 Preview by Chat01
생성일: 2024년 10월 24일
답변에 사용된 모델 O1 Preview by Chat01
import os,sys,json
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(file))))
from core.step2_whisper import get_whisper_language
from core.config_utils import load_key
def get_split_prompt(sentence, num_parts = 2, word_limit = 20):
# ! only support num_parts = 2
language = get_whisper_language()
split_prompt = f"""
You are a professional and experienced Netflix subtitle splitter in {language}.
Your task is to split the given subtitle text into {num_parts} parts, each should be less than {word_limit} words.
Please provide your answer in the following JSON format, <<>> represents placeholders:
{{
"analysis": "Brief analysis of the text structure and split strategy",
"split_1": "<<The first split method, output complete sentences, insert [br] as a delimiter at the split position. e.g. this is the first part [br] this is the second part.>>",
"split_2": "<<The second split method>>",
"eval": "<<Unified brief evaluation of the 2 split methods, written in one sentence, no line breaks>>",
"best": "<<The best split method number, 1 or 2>>"
}}
<split_this_sentence>\n{sentence}\n</split_this_sentence>
""".strip()
return split_prompt
def get_summary_prompt(source_content):
src_language = get_whisper_language()
TARGET_LANGUAGE = load_key("target_language")
summary_prompt = f"""
You are a professional video translation expert and terminology consultant. Your expertise lies not only in accurately understanding the original {src_language} text but also in extracting key professional terms and optimizing the translation to better suit the expression habits and cultural background of {TARGET_LANGUAGE}.
For the provided original {src_language} video text, you need to:
Please think in two steps, processing the text line by line:
Please output your analysis results in the following JSON format, where <> represents placeholders:
{{
"theme": "<Briefly summarize the theme of this video in 1 sentence>",
"terms": [
{{
"original": "<Term or name 1 in the {src_language}>",
"translation": "<{TARGET_LANGUAGE} translation or keep original>",
"explanation": "<Brief explanation of the term or name>"
}},
{{
"original": "<Term or name 2 in the {src_language}>",
"translation": "<{TARGET_LANGUAGE} translation or keep original>",
"explanation": "<Brief explanation of the term or name>"
}},
...
]
}}
{{
"theme": "Ce vidéo résume le musée du Louvre à Paris.",
"terms": [
{{
"original": "Mona Lisa",
"translation": "La Joconde",
"explanation": "Le tableau le plus célèbre du Louvre, un portrait de Léonard de Vinci"
}},
{{
"original": "pyramid",
"translation": "la pyramide",
"explanation": "Une grande structure en verre et métal en forme de pyramide située à l'entrée principale du Louvre"
}},
{{
"original": "I.M. Pei",
"translation": "I.M. Pei",
"explanation": "L'architecte américain d'origine chinoise qui a conçu la pyramide du Louvre"
}},
...
]
}}
<video_text_to_summarize>
{source_content}
</video_text_to_summarize>
""".strip()
return summary_prompt
def generate_shared_prompt(previous_content_prompt, after_content_prompt, summary_prompt, things_to_note_prompt):
return f'''### Context Information
<previous_content>
{previous_content_prompt}
</previous_content>
<subsequent_content>
{after_content_prompt}
</subsequent_content>
{summary_prompt}
{things_to_note_prompt}'''
def get_prompt_faithfulness(lines, shared_prompt):
TARGET_LANGUAGE = load_key("target_language")
# Split lines by \n
line_splits = lines.split('\n')
text# Create JSON return format example json_format = {} for i, line in enumerate(line_splits, 1): json_format[i] = { "origin": line, "direct": f"<<direct {TARGET_LANGUAGE} translation>>" } src_language = get_whisper_language() prompt_faithfulness = f'''
You are a professional Netflix subtitle translator, fluent in both {src_language} and {TARGET_LANGUAGE}, as well as their respective cultures. Your expertise lies in accurately understanding the semantics and structure of the original {src_language} text and faithfully translating it into {TARGET_LANGUAGE} while preserving the original meaning.
We have a segment of original {src_language} subtitles that need to be directly translated into {TARGET_LANGUAGE}. These subtitles come from a specific context and may contain specific themes and terminology.
Based on the provided original {src_language} subtitles, you need to:
{shared_prompt}
Please complete the following JSON data, where << >> represents placeholders that should not appear in your answer, and return your translation results in JSON format:
{json.dumps(json_format, ensure_ascii=False, indent=4)}
'''
return prompt_faithfulness.strip()
def get_prompt_expressiveness(faithfulness_result, lines, shared_prompt):
TARGET_LANGUAGE = load_key("target_language")
json_format = {}
for key, value in faithfulness_result.items():
json_format[key] = {
"origin": value['origin'],
"direct": value['direct'],
"reflection": "<<reflection on the direct translation version>>",
"free": f"<<retranslated result, aiming for fluency and naturalness, conforming to {TARGET_LANGUAGE} expression habits, DO NOT leave empty line here!>>"
}
textsrc_language = get_whisper_language() prompt_expressiveness = f'''
You are a professional Netflix subtitle translator and language consultant. Your expertise lies not only in accurately understanding the original {src_language} but also in optimizing the {TARGET_LANGUAGE} translation to better suit the target language's expression habits and cultural background.
We already have a direct translation version of the original {src_language} subtitles. Now we need you to reflect on and improve these direct translations to create more natural and fluent {TARGET_LANGUAGE} subtitles.
Based on the provided original {src_language} text and {TARGET_LANGUAGE} direct translation, you need to:
{shared_prompt}
Please use a two-step thinking process to handle the text line by line:
Direct Translation Reflection:
{TARGET_LANGUAGE} Free Translation:
Make sure to generate the correct Json format, don't output " in the value.
Please complete the following JSON data, where << >> represents placeholders that should not appear in your answer, and return your translation results in JSON format:
{json.dumps(json_format, ensure_ascii=False, indent=4)}
'''
return prompt_expressiveness.strip()
def get_align_prompt(src_sub, tr_sub, src_part):
TARGET_LANGUAGE = load_key("target_language")
src_language = get_whisper_language()
src_splits = src_part.split('\n')
num_parts = len(src_splits)
src_part = src_part.replace('\n', ' [br] ')
align_prompt = '''
You are a Netflix subtitle alignment expert fluent in both {src_language} and {target_language}. Your expertise lies in accurately understanding the semantics and structure of both languages, enabling you to flexibly split sentences while preserving the original meaning.
We have {src_language} and {target_language} original subtitles for a Netflix program, as well as a pre-processed split version of {src_language} subtitles. Your task is to create the best splitting scheme for the {target_language} subtitles based on this information.
Based on the provided original {src_language} and {target_language} original subtitles, as well as the pre-processed split version, you need to:
Please follow these steps and provide the results for each step in the JSON output:
Please complete the following JSON data, where << >> represents placeholders, and return your results in JSON format:
{{
"analysis": "<<Detailed analysis of word order, structure, and semantic correspondence between {src_language} and {target_language} subtitles>>",
"align_1": [
{align_parts_json}
],
"align_2": [
{align_parts_json}
],
"comparison": "<<Brief evaluation and comparison of the 2 alignment schemes>>",
"best": "<<Number of the best alignment scheme, 1 or 2>>"
}}
'''
textalign_parts_json = ','.join( f''' {{ "src_part_{i+1}": "<<{src_splits[i]}>>", "target_part_{i+1}": "<<Corresponding aligned {TARGET_LANGUAGE} subtitle part>>" }}''' for i in range(num_parts) ) return align_prompt.format( src_language=src_language, target_language=TARGET_LANGUAGE, src_sub=src_sub, tr_sub=tr_sub, src_part=src_part, align_parts_json=align_parts_json, )
def get_subtitle_trim_prompt(trans_text, duration):
textrule = '''Consider a. Reducing filler words without modifying meaningful content. b. Omitting unnecessary modifiers or pronouns, for example: - "Please explain your thought process" can be shortened to "Please explain thought process" - "We need to carefully analyze this complex problem" can be shortened to "We need to analyze this problem" - "Let's discuss the various different perspectives on this topic" can be shortened to "Let's discuss different perspectives on this topic" - "Can you describe in detail your experience from yesterday" can be shortened to "Can you describe yesterday's experience" ''' trim_prompt = '''
You are a professional subtitle editor, editing and optimizing lengthy subtitles that exceed voiceover time before handing them to voice actors. Your expertise lies in cleverly shortening subtitles slightly while ensuring the original meaning and structure remain unchanged.
{rule}
Please follow these steps and provide the results in the JSON output:
Please complete the following JSON data, where << >> represents content you need to fill in:
{{
"analysis": "<<Brief analysis of the subtitle, including structure, key information, and potential processing locations>>",
"trans_text_processed": "<<Optimized and shortened subtitle in the original subtitle language>>"
}}
'''
return trim_prompt.format(
trans_text=trans_text,
duration=duration,
rule=rule
)
解释一下这个代码
这段代码是一系列用于处理字幕的函数,主要用于以下任务:
以下是对代码中每个部分的详细解释:
pythonimport os, sys, json sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from core.step2_whisper import get_whisper_language from core.config_utils import load_key
os
, sys
, json
:导入Python的标准库模块,用于文件路径操作和JSON处理。sys.path.append(...)
:将上级目录添加到系统路径,方便导入其他模块。from core.step2_whisper import get_whisper_language
:从core.step2_whisper
模块中导入get_whisper_language
函数,用于获取当前处理的语言。from core.config_utils import load_key
:从core.config_utils
模块中导入load_key
函数,用于加载配置中的键值。pythondef get_split_prompt(sentence, num_parts=2, word_limit=20): # 仅支持将句子拆分成两个部分 language = get_whisper_language() split_prompt = f""" ### 角色 你是一名专业且经验丰富的{language}Netflix字幕拆分师。 ### 任务 你的任务是将给定的字幕文本拆分成 **{num_parts}** 部分,每部分应少于 {word_limit} 个单词。 ### 要求 1. 尽量保持句子意义的连贯性,根据Netflix字幕标准进行拆分,确保两个部分相对独立。 2. 每部分的长度应大致相等,且不少于3个单词,但句子的完整性更为重要。 3. 优先在标点符号处拆分,如句号、逗号和连词(例如:"and", "but", "because", "when", "then", "if", "so", "that")。 ### 步骤 1. 分析给定文本的语法和结构。 2. 提供两种不同的拆分方式,每种方式的拆分点不同,输出完整的句子(不要更改任何字母或标点),在拆分位置插入 [br] 标签。 3. 简要比较和评估上述两种拆分方法,考虑可读性、语法结构和上下文连贯性,选择最佳的拆分方法。 4. 给出最佳拆分方法的编号,1 或 2。 ### 输出格式 请以以下JSON格式提供你的答案,<<>> 表示需要填写的内容: {{ "analysis": "对文本结构和拆分策略的简要分析", "split_1": "<<第一种拆分方法,输出完整句子,在拆分位置插入 [br] 作为分隔符>>", "split_2": "<<第二种拆分方法>>", "eval": "<<对两种拆分方法的统一简要评估,用一句话,不要换行>>", "best": "<<最佳拆分方法的编号,1 或 2>>" }} ### 给定文本 <split_this_sentence> {sentence} </split_this_sentence> """.strip() return split_prompt
解释:
sentence
:需要拆分的原始句子。num_parts
:希望将句子拆分成的部分数量,默认为2。word_limit
:每部分的最大单词数限制,默认为20。split_prompt
,包含详细的任务描述、要求、步骤和输出格式。split_prompt
。pythondef get_summary_prompt(source_content): src_language = get_whisper_language() TARGET_LANGUAGE = load_key("target_language") summary_prompt = f""" ### 角色 你是一名专业的视频翻译专家和术语顾问。你不仅擅长准确理解原始的{src_language}文本,还能提取关键的专业术语,并优化翻译以更好地适应{TARGET_LANGUAGE}的表达习惯和文化背景。 ### 任务描述 对于提供的原始{src_language}视频文本,你需要: 1. 用一句话总结视频的主要主题。 2. 提取视频中出现的专业术语和名称,提供{TARGET_LANGUAGE}翻译或建议保留原文。避免提取简单、常见的词汇。 3. 对每个翻译的术语或名称,提供简要的解释。 ### 分析和总结步骤 请分两步思考,逐行处理文本: 1. 主题总结: - 快速浏览全文,理解大意。 - 用一句简洁的话总结主题。 2. 术语和名称提取: - 仔细阅读全文,标记专业术语和名称。 - 对于每个术语或名称,提供{TARGET_LANGUAGE}翻译或建议保留原文,只需要词语本身,不需要发音。 - 为每个术语或名称添加简要的解释,帮助翻译人员理解。 - 如果词语是固定缩写或专有名称,请保留原文。 ### 输出格式 请以以下JSON格式输出你的分析结果,其中 <> 表示需要填写的内容: {{ "theme": "<用一句话简要总结视频的主题>", "terms": [ {{ "original": "<{src_language}中的术语或名称1>", "translation": "<{TARGET_LANGUAGE}翻译或保留原文>", "explanation": "<对术语或名称的简要解释>" }}, ... ] }} ### 视频文本数据 <video_text_to_summarize> {source_content} </video_text_to_summarize> """.strip() return summary_prompt
解释:
source_content
:原始视频文本内容。summary_prompt
。pythondef generate_shared_prompt(previous_content_prompt, after_content_prompt, summary_prompt, things_to_note_prompt): return f'''### 上下文信息 <previous_content> {previous_content_prompt} </previous_content> <subsequent_content> {after_content_prompt} </subsequent_content> ### 内容摘要 {summary_prompt} ### 注意事项 {things_to_note_prompt}'''
解释:
previous_content_prompt
:前文内容提示。after_content_prompt
:后文内容提示。summary_prompt
:内容摘要提示。things_to_note_prompt
:需要注意的事项提示。pythondef get_prompt_faithfulness(lines, shared_prompt): TARGET_LANGUAGE = load_key("target_language") # 按行分割字幕 line_splits = lines.split('\n') # 创建JSON返回格式示例 json_format = {} for i, line in enumerate(line_splits, 1): json_format[i] = { "origin": line, "direct": f"<<直接翻译成{TARGET_LANGUAGE}>>" } src_language = get_whisper_language() prompt_faithfulness = f''' ### 角色定义 你是一名专业的Netflix字幕翻译,精通{src_language}和{TARGET_LANGUAGE},以及它们各自的文化。你的专长在于准确理解原始{src_language}文本的语义和结构,并忠实地将其翻译成{TARGET_LANGUAGE},同时保留原始含义。 ### 任务背景 我们有一段原始的{src_language}字幕,需要直接翻译成{TARGET_LANGUAGE}。这些字幕来自特定的上下文,可能包含特定的主题和术语。 ### 任务描述 基于提供的原始{src_language}字幕,你需要: 1. 将原始{src_language}字幕逐行翻译成{TARGET_LANGUAGE}。 2. 确保翻译忠实于原文,准确传达原始含义。 3. 考虑上下文和专业术语。 {shared_prompt} ### 翻译原则 1. 忠实于原文:准确传达原文的内容和意义,不得随意更改、添加或遗漏内容。 2. 术语准确:正确使用专业术语,并保持术语的一致性。 3. 理解上下文:充分理解并反映文本的背景和上下文关系。 ### 字幕数据 <subtitles> {lines} </subtitles> ### 输出格式 请完成以下JSON数据,其中 << >> 表示占位符,不应出现在你的答案中,并以JSON格式返回翻译结果: {json.dumps(json_format, ensure_ascii=False, indent=4)} ''' return prompt_faithfulness.strip()
解释:
lines
:原始字幕文本,按行分割。shared_prompt
:共享的上下文信息和注意事项提示。prompt_faithfulness
。pythondef get_prompt_expressiveness(faithfulness_result, lines, shared_prompt): TARGET_LANGUAGE = load_key("target_language") json_format = {} for key, value in faithfulness_result.items(): json_format[key] = { "origin": value['origin'], "direct": value['direct'], "reflection": "<<对直接翻译版本的反思>>", "free": f"<<重新翻译的结果,旨在流畅自然,符合{TARGET_LANGUAGE}的表达习惯,不要留空行!>>" } src_language = get_whisper_language() prompt_expressiveness = f''' ### 角色定义 你是一名专业的Netflix字幕翻译和语言顾问。你的专长不仅在于准确理解原始的{src_language},还在于优化{TARGET_LANGUAGE}翻译,使其更适合目标语言的表达习惯和文化背景。 ### 任务背景 我们已经有了原始{src_language}字幕的直接翻译版本。现在,我们需要你反思和改进这些直接翻译,创造出更自然流畅的{TARGET_LANGUAGE}字幕。 ### 任务描述 基于提供的原始{src_language}文本和{TARGET_LANGUAGE}直接翻译,你需要: 1. 逐行分析直接翻译结果,指出存在的问题。 2. 提供详细的修改建议。 3. 根据你的分析进行自由翻译。 {shared_prompt} ### 翻译分析步骤 请使用两步思考过程逐行处理文本: 1. 直接翻译反思: - 评估语言流畅度。 - 检查语言风格是否与原文一致。 - 检查字幕的简洁性,指出翻译过于冗长的地方,翻译应与原文长度接近。 2. {TARGET_LANGUAGE}自由翻译: - 基于第1步的反思,进行自由翻译。 - 旨在上下文的顺畅和自然,符合{TARGET_LANGUAGE}的表达习惯。 - 确保{TARGET_LANGUAGE}观众容易理解和接受。 - 保持字幕简洁,语言风格朴实自然,保持自由翻译与{src_language}原文在结构上的一致性。 ### 字幕数据 <subtitles> {lines} </subtitles> ### 输出格式 确保生成正确的JSON格式,不要在值中输出引号。 请完成以下JSON数据,其中 << >> 表示占位符,不应出现在你的答案中,并以JSON格式返回翻译结果: {json.dumps(json_format, ensure_ascii=False, indent=4)} ''' return prompt_expressiveness.strip()
解释:
faithfulness_result
:之前忠实翻译步骤的结果,包括原文和直接翻译。lines
:原始字幕文本。shared_prompt
:共享的上下文信息和注意事项提示。prompt_expressiveness
。pythondef get_align_prompt(src_sub, tr_sub, src_part): TARGET_LANGUAGE = load_key("target_language") src_language = get_whisper_language() src_splits = src_part.split('\n') num_parts = len(src_splits) src_part = src_part.replace('\n', ' [br] ') align_prompt = ''' ### 角色定义 你是一名精通{src_language}和{target_language}的Netflix字幕对齐专家。你的专长在于准确理解两种语言的语义和结构,使你能够灵活地拆分句子,同时保留原始含义。 ### 任务背景 我们有{src_language}和{target_language}的原始字幕,以及预处理后的{src_language}字幕拆分版本。你的任务是基于此信息,为{target_language}字幕创建最佳的拆分方案。 ### 任务描述 基于提供的{src_language}和{target_language}原始字幕,以及预处理后的拆分版本,你需要: 1. 分析{src_language}和{target_language}字幕的词序和结构对应关系。 2. 提供{target_language}字幕的两种不同拆分方案。 3. 评估这些方案并选择最佳方案。 4. 切勿留下空行。如果根据意义难以拆分,你可以适当重写需要对齐的句子。 ### 字幕数据 <subtitles> {src_language}原文: "{src_sub}" {target_language}原文: "{tr_sub}" 预处理的{src_language}字幕([br]表示拆分点): {src_part} </subtitles> ### 处理步骤 请按照以下步骤操作,并在JSON输出中提供每个步骤的结果: 1. 分析和比较:简要分析{src_language}和{target_language}字幕的词序、句子结构和语义对应关系。指出关键的词语对应、句型异同,以及可能影响拆分的语言特征。 2. 开始对齐:根据你的分析,按照格式为{target_language}字幕提供两种不同的对齐方法。{src_language}的拆分位置必须与预处理的{src_language}拆分版本一致,不能随意更改。 3. 评估和选择:检查并简要评估两种方案,考虑句子完整性、语义连贯性和拆分点的适当性等因素。 4. 最佳方案:选择最佳的对齐方案,仅输出一个数字,1或2。 ### 输出格式 请完成以下JSON数据,其中 << >> 表示需要填写的内容,并以JSON格式返回结果: {{ "analysis": "<<对{src_language}和{target_language}字幕的词序、结构和语义对应关系的详细分析>>", "align_1": [ {align_parts_json} ], "align_2": [ {align_parts_json} ], "comparison": "<<对两种对齐方案的简要评估和比较>>", "best": "<<最佳对齐方案的编号,1或2>>" }} ''' align_parts_json = ','.join( f''' {{ "src_part_{i+1}": "<<{src_splits[i]}>>", "target_part_{i+1}": "<<对应对齐的{TARGET_LANGUAGE}字幕部分>>" }}''' for i in range(num_parts) ) return align_prompt.format( src_language=src_language, target_language=TARGET_LANGUAGE, src_sub=src_sub, tr_sub=tr_sub, src_part=src_part, align_parts_json=align_parts_json, )
解释:
src_sub
:源语言的原始字幕。tr_sub
:目标语言的原始字幕。src_part
:预处理后的源语言字幕拆分版本。align_prompt
。pythondef get_subtitle_trim_prompt(trans_text, duration): rule = '''考虑: a. 在不修改有意义内容的情况下减少填充词。 b. 省略不必要的修饰词或代词,例如: - "Please explain your thought process" 可以简化为 "Please explain thought process" - "We need to carefully analyze this complex problem" 可以简化为 "We need to analyze this problem" - "Let's discuss the various different perspectives on this topic" 可以简化为 "Let's discuss different perspectives on this topic" - "Can you describe in detail your experience from yesterday" 可以简化为 "Can you describe yesterday's experience" ''' trim_prompt = ''' ### 角色 你是一名专业的字幕编辑,负责在交给配音演员之前编辑和优化超过配音时间的冗长字幕。你的专长在于巧妙地略微缩短字幕,同时确保原意和结构保持不变。 ### 字幕数据 <subtitles> 字幕: "{trans_text}" 持续时间: {duration} 秒 </subtitles> ### 处理规则 {rule} ### 处理步骤 请按照以下步骤操作,并在JSON输出中提供结果: 1. 分析:简要分析字幕的结构、关键信息和可省略的填充词。 2. 精简:根据规则和分析,优化字幕,使其更加简洁。 ### 输出格式 请完成以下JSON数据,其中 << >> 表示需要填写的内容: {{ "analysis": "<<对字幕的简要分析,包括结构、关键信息和可能的处理位置>>", "trans_text_processed": "<<优化和缩短后的原字幕语言的字幕>>" }} ''' return trim_prompt.format( trans_text=trans_text, duration=duration, rule=rule )
解释:
trans_text
:需要精简的字幕文本。duration
:配音的持续时间,单位为秒。trim_prompt
。这段代码提供了一系列用于字幕处理的函数,每个函数都生成了一个详细的提示,指导语言模型完成特定的任务。这些提示包括角色定义、任务描述、处理步骤和输出格式,确保模型能够按照预期生成符合要求的结果。
通过使用这些函数,开发者可以构建一个完整的字幕处理流水线,从拆分、摘要、翻译、对齐到精简,每个步骤都有明确的指导和格式规范,提高了处理效率和结果的一致性。
非常好,如你所见,上面是一个英文的关于口腔正畸学机制的视频内容提取字幕,上面是我给上述文字打上时间轴之后的字幕文本,请结合其口腔正畸学力学机制专业术语的内容和整体意思进行中文翻译,并保留时间轴不变,并以代码框的形式输出翻译内容,
注意弓丝、托槽、槽沟、单力偶、尖牙、前磨牙、磨牙、牵引扣,方丝、圆丝、V型带、刚性弓丝、柔性弓丝、压低力、牵出力,第一序列弯曲、第二序列弯曲、第三序列弯曲、阶梯弯、前牙、后牙、2×4矫治技术、冠状面、矢状面、水平面、well-aligned排列良好等翻译
我想加上上述的提示词,加在哪个位置比较合适
要确保在翻译过程中这些专业术语被正确翻译,您需要将这些术语添加到提示中,以便模型在翻译时特别注意它们。
根据您提供的代码,最合适的位置是将这些术语添加到things_to_note_prompt
中,然后传递给generate_shared_prompt
函数。这样,这些术语将包含在共享的提示信息中,并在翻译过程中提供给模型参考。
以下是您可以修改的地方:
定义things_to_note_prompt
并添加术语列表:
在调用generate_shared_prompt
之前,您可以创建一个things_to_note_prompt
,其中包含您列出的专业术语。例如:
pythonthings_to_note_prompt = """ ### 专业术语 请注意以下口腔正畸学力学机制的专业术语,并确保在翻译过程中准确翻译: - archwire(弓丝) - bracket(托槽) - slot(槽沟) - single force couple(单力偶) - canine(尖牙) - premolar(前磨牙) - molar(磨牙) - tie wing(牵引扣) - rectangular wire(方丝) - round wire(圆丝) - V-bend(V型弯) - stiff wire(刚性弓丝) - flexible wire(柔性弓丝) - intrusive force(压低力) - extrusive force(牵出力) - first-order bend(第一序列弯曲) - second-order bend(第二序列弯曲) - third-order bend(第三序列弯曲) - step bend(阶梯弯) - anterior teeth(前牙) - posterior teeth(后牙) - 2×4 appliance(2×4矫治技术) - coronal plane(冠状面) - sagittal plane(矢状面) - horizontal plane(水平面) - well-aligned(排列良好) """
生成共享提示:
然后,将things_to_note_prompt
传递给generate_shared_prompt
函数:
pythonshared_prompt = generate_shared_prompt(previous_content_prompt, after_content_prompt, summary_prompt, things_to_note_prompt)
在翻译提示中包含共享提示:
在get_prompt_faithfulness
和get_prompt_expressiveness
函数中,shared_prompt
已经包含在提示中,因此模型在翻译时会参考这些专业术语。
完整示例:
假设您已经有了previous_content_prompt
、after_content_prompt
和summary_prompt
,以下是如何添加专业术语并进行翻译的示例:
python# 定义专业术语提示 things_to_note_prompt = """ ### 专业术语 请注意以下口腔正畸学力学机制的专业术语,并确保在翻译过程中准确翻译: - archwire(弓丝) - bracket(托槽) - slot(槽沟) - single force couple(单力偶) - canine(尖牙) - premolar(前磨牙) - molar(磨牙) - tie wing(牵引扣) - rectangular wire(方丝) - round wire(圆丝) - V-bend(V型弯) - stiff wire(刚性弓丝) - flexible wire(柔性弓丝) - intrusive force(压低力) - extrusive force(牵出力) - first-order bend(第一序列弯曲) - second-order bend(第二序列弯曲) - third-order bend(第三序列弯曲) - step bend(阶梯弯) - anterior teeth(前牙) - posterior teeth(后牙) - 2×4 appliance(2×4矫治技术) - coronal plane(冠状面) - sagittal plane(矢状面) - horizontal plane(水平面) - well-aligned(排列良好) """ # 生成共享提示 shared_prompt = generate_shared_prompt(previous_content_prompt, after_content_prompt, summary_prompt, things_to_note_prompt) # 获取需要翻译的字幕文本 lines = """ 00:00:01,000 --> 00:00:04,000 We will now discuss the biomechanics of orthodontic tooth movement. 00:00:05,000 --> 00:00:08,000 The archwire is engaged in the bracket slot to apply force. ... (更多字幕行) """ # 获取忠实翻译提示 faithfulness_prompt = get_prompt_faithfulness(lines, shared_prompt) # 调用模型进行忠实翻译(示例,不实际调用模型) faithfulness_result = model_generate(faithfulness_prompt) # 获取流畅翻译提示 expressiveness_prompt = get_prompt_expressiveness(faithfulness_result, lines, shared_prompt) # 调用模型进行流畅翻译(示例,不实际调用模型) expressiveness_result = model_generate(expressiveness_prompt)
通过将专业术语添加到things_to_note_prompt
中,您确保了这些术语在翻译过程中受到特别关注。
注意:在实际调用模型时,请确保模型能够访问到shared_prompt
,并按照提示中的要求进行翻译。此外,如果您的字幕已经打上了时间轴,请确保在处理lines
时保留时间轴信息,模型在翻译时应保持时间轴不变。
最终输出:
模型生成的翻译结果应以代码块的形式输出,并保留时间轴不变。例如:
plaintext00:00:01,000 --> 00:00:04,000 现在我们将讨论正畸牙齿移动的生物力学。 00:00:05,000 --> 00:00:08,000 弓丝被置入托槽的槽沟中以施加力量。 ... (更多翻译后的字幕行)
希望这能帮助您在翻译过程中正确添加专业术语,并获得准确的中文翻译。
我直接在代码的最开头定义这个函数可以吗?后面是不是就不需要修改什么了
是的,您可以在代码的最开头定义things_to_note_prompt
,然后将其传递给generate_shared_prompt
函数。这种方式可以确保您的专业术语列表被包含在共享提示中,模型在翻译过程中会参考这些术语。
以下是您可以在代码开头添加的内容:
python# 定义专业术语提示 things_to_note_prompt = """ ### 专业术语 请注意以下口腔正畸学力学机制的专业术语,并确保在翻译过程中准确翻译: - archwire(弓丝) - bracket(托槽) - slot(槽沟) - single force couple(单力偶) - canine(尖牙) - premolar(前磨牙) - molar(磨牙) - tie wing(牵引扣) - rectangular wire(方丝) - round wire(圆丝) - V-bend(V型弯) - stiff wire(刚性弓丝) - flexible wire(柔性弓丝) - intrusive force(压低力) - extrusive force(牵出力) - first-order bend(第一序列弯曲) - second-order bend(第二序列弯曲) - third-order bend(第三序列弯曲) - step bend(阶梯弯) - anterior teeth(前牙) - posterior teeth(后牙) - 2×4 appliance(2×4矫治技术) - coronal plane(冠状面) - sagittal plane(矢状面) - horizontal plane(水平面) - well-aligned(排列良好) """
然后,在生成共享提示时,将things_to_note_prompt
传递给generate_shared_prompt
函数:
pythonshared_prompt = generate_shared_prompt( previous_content_prompt, after_content_prompt, summary_prompt, things_to_note_prompt )
注意事项:
确保共享提示被正确传递:在调用get_prompt_faithfulness
和get_prompt_expressiveness
函数时,确保它们使用了包含things_to_note_prompt
的shared_prompt
。
保持时间轴不变:在处理字幕文本时,确保保留时间轴信息。模型在翻译时应保持时间轴不变。
模型调用:在实际调用模型进行翻译时,使用生成的提示。例如:
python# 获取忠实翻译提示 faithfulness_prompt = get_prompt_faithfulness(lines, shared_prompt) # 调用模型进行忠实翻译(示例) faithfulness_result = model_generate(faithfulness_prompt)
继续后续处理:在得到忠实翻译结果后,您可以继续使用get_prompt_expressiveness
函数进行流畅度优化,传递相同的shared_prompt
。
完整示例:
python# 导入必要的模块和函数 import os, sys, json sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from core.step2_whisper import get_whisper_language from core.config_utils import load_key # 定义专业术语提示 things_to_note_prompt = """ ### 专业术语 请注意以下口腔正畸学力学机制的专业术语,并确保在翻译过程中准确翻译: - archwire(弓丝) - bracket(托槽) - slot(槽沟) - single force couple(单力偶) - canine(尖牙) - premolar(前磨牙) - molar(磨牙) - tie wing(牵引扣) - rectangular wire(方丝) - round wire(圆丝) - V-bend(V型弯) - stiff wire(刚性弓丝) - flexible wire(柔性弓丝) - intrusive force(压低力) - extrusive force(牵出力) - first-order bend(第一序列弯曲) - second-order bend(第二序列弯曲) - third-order bend(第三序列弯曲) - step bend(阶梯弯) - anterior teeth(前牙) - posterior teeth(后牙) - 2×4 appliance(2×4矫治技术) - coronal plane(冠状面) - sagittal plane(矢状面) - horizontal plane(水平面) - well-aligned(排列良好) """ # 生成共享提示 shared_prompt = generate_shared_prompt( previous_content_prompt, after_content_prompt, summary_prompt, things_to_note_prompt ) # 获取需要翻译的字幕文本(示例) lines = """ 00:00:01,000 --> 00:00:04,000 We will now discuss the biomechanics of orthodontic tooth movement. 00:00:05,000 --> 00:00:08,000 The archwire is engaged in the bracket slot to apply force. """ # 获取忠实翻译提示 faithfulness_prompt = get_prompt_faithfulness(lines, shared_prompt) # 调用模型进行忠实翻译(实际调用时需要替换为您的模型调用代码) # faithfulness_result = model_generate(faithfulness_prompt) # 假设得到的忠实翻译结果如下(示例): faithfulness_result = { 1: { "origin": "We will now discuss the biomechanics of orthodontic tooth movement.", "direct": "我们现在将讨论正畸牙齿移动的生物力学。" }, 2: { "origin": "The archwire is engaged in the bracket slot to apply force.", "direct": "弓丝被置入托槽的槽沟中以施加力量。" } } # 获取流畅翻译提示 expressiveness_prompt = get_prompt_expressiveness(faithfulness_result, lines, shared_prompt) # 调用模型进行流畅翻译(实际调用时需要替换为您的模型调用代码) # expressiveness_result = model_generate(expressiveness_prompt) # 最终,您可以将翻译结果与时间轴组合,并以代码框的形式输出 translated_lines = """ 00:00:01,000 --> 00:00:04,000 现在我们将讨论正畸牙齿移动的生物力学。 00:00:05,000 --> 00:00:08,000 弓丝被放入托槽的槽沟中以施加力量。 """ print("```plaintext\n" + translated_lines + "\n```")
总结:
things_to_note_prompt
,这样有助于在整个处理过程中保持一致性。shared_prompt
。希望这能帮助您顺利完成字幕的专业翻译。如有其他问题,请随时提问!
我想强调这个视频是个口腔正畸学视频,强调这个视频的性质,应该写哪些提示词添加到哪个位置
要强调视频是关于口腔正畸学的,并让模型在翻译过程中充分理解这一点,您可以在多个提示中加入有关视频性质的信息。这将指导模型在翻译时考虑到视频的专业背景,确保翻译的准确性和一致性。
以下是您可以添加提示词的位置和方法:
1. 在翻译提示的任务描述中添加视频性质
在get_prompt_faithfulness
和get_prompt_expressiveness
函数中,您可以修改“任务背景”或“任务描述”部分,明确指出字幕来自一个关于口腔正畸学的视频。
示例修改:
pythondef get_prompt_faithfulness(lines, shared_prompt): TARGET_LANGUAGE = load_key("target_language") src_language = get_whisper_language() # ... 其他代码 ... prompt_faithfulness = f''' ### 角色定义 你是一名专业的Netflix字幕翻译,精通{src_language}和{TARGET_LANGUAGE},并熟悉它们各自的文化。 ### 任务背景 我们有一段原始的{src_language}字幕,来自一个关于**口腔正畸学力学机制**的视频。这些字幕包含与口腔正畸学相关的专业术语。 ### 任务描述 基于提供的原始{src_language}字幕,你需要: 1. 将原始{src_language}字幕逐行翻译成{TARGET_LANGUAGE}。 2. 确保翻译忠实于原文,准确传达原始含义。 3. 特别注意口腔正畸学力学机制的专业术语,确保准确翻译。 {shared_prompt} ### 翻译原则 1. 忠实于原文:准确传达原文的内容和意义,不得随意更改、添加或遗漏内容。 2. 术语准确:正确使用专业术语,并保持术语的一致性。 3. 理解上下文:充分理解并反映文本的背景和上下文关系。 ''' return prompt_faithfulness.strip()
在get_prompt_expressiveness
函数中,您也可以类似地添加视频性质的信息。
2. 在things_to_note_prompt
中添加视频性质和专业术语
将视频的性质和专业术语列表添加到things_to_note_prompt
中,这样在生成shared_prompt
时,模型会同时获取到视频背景和需要注意的术语。
示例:
python# 定义专业术语提示和视频性质 things_to_note_prompt = """ ### 视频性质 本视频是关于**口腔正畸学力学机制**的教学视频,涵盖了正畸治疗中的力学原理和技术。 ### 专业术语 请注意以下口腔正畸学力学机制的专业术语,并确保在翻译过程中准确翻译: - archwire(弓丝) - bracket(托槽) - slot(槽沟) - single force couple(单力偶) - canine(尖牙) - premolar(前磨牙) - molar(磨牙) - tie wing(牵引扣) - rectangular wire(方丝) - round wire(圆丝) - V-bend(V型弯) - stiff wire(刚性弓丝) - flexible wire(柔性弓丝) - intrusive force(压低力) - extrusive force(牵出力) - first-order bend(第一序列弯曲) - second-order bend(第二序列弯曲) - third-order bend(第三序列弯曲) - step bend(阶梯弯) - anterior teeth(前牙) - posterior teeth(后牙) - 2×4 appliance(2×4矫治技术) - coronal plane(冠状面) - sagittal plane(矢状面) - horizontal plane(水平面) - well-aligned(排列良好) """
3. 更新shared_prompt
使用包含视频性质和专业术语的things_to_note_prompt
生成shared_prompt
,这样所有引用shared_prompt
的函数都会获得这些信息。
pythonshared_prompt = generate_shared_prompt( previous_content_prompt, after_content_prompt, summary_prompt, things_to_note_prompt )
4. 在摘要提示中强调视频性质
在get_summary_prompt
函数中,确保生成的摘要明确提到视频是关于口腔正畸学的。
示例修改:
pythondef get_summary_prompt(source_content): src_language = get_whisper_language() TARGET_LANGUAGE = load_key("target_language") summary_prompt = f""" ### 角色 你是一名专业的视频翻译专家和术语顾问,专注于口腔正畸学领域。你不仅擅长准确理解原始的{src_language}文本,还能提取关键的专业术语,并优化翻译以更好地适应{TARGET_LANGUAGE}的表达习惯和文化背景。 ### 任务描述 对于提供的原始{src_language}视频文本,你需要: 1. 用一句话总结视频的主要主题,强调其与口腔正畸学力学机制的关联。 2. 提取视频中出现的专业术语和名称,提供{TARGET_LANGUAGE}翻译或建议保留原文。避免提取简单、常见的词汇。 3. 对每个翻译的术语或名称,提供简要的解释。 # ... 其余代码保持不变 ... """ return summary_prompt
5. 在所有相关提示中保持一致
确保在所有涉及到任务背景、任务描述或角色定义的提示中,都明确提到视频是关于口腔正畸学的。这将使模型在整个处理过程中始终关注这一主题。
6. 最终输出示例
在完成所有修改后,模型应能够准确翻译字幕,保留时间轴,并正确翻译专业术语。
示例输出:
plaintext00:00:01,000 --> 00:00:04,000 现在我们将讨论正畸牙齿移动的生物力学。 00:00:05,000 --> 00:00:08,000 弓丝被放入托槽的槽沟中以施加力量。 ... (更多翻译后的字幕行)
总结
添加视频性质到提示中:在任务描述、任务背景和角色定义中明确指出视频是关于口腔正畸学的。这有助于模型理解整体上下文。
包含专业术语:在things_to_note_prompt
中列出专业术语,确保模型在翻译时准确处理这些词汇。
更新共享提示:将包含视频性质和专业术语的things_to_note_prompt
传递给generate_shared_prompt
,使其在翻译过程中被引用。
保持一致性:在所有相关的提示和函数中,确保关于视频性质的描述一致,以便模型始终关注正确的主题。
通过以上方法,您可以强调视频的性质,使模型在翻译过程中充分理解内容,并提供准确、专业的翻译结果。
如果您有任何其他问题或需要进一步的帮助,请随时告诉我!
def get_summary_prompt(source_content):
src_language = get_whisper_language()
TARGET_LANGUAGE = load_key("target_language")
summary_prompt = f"""
你是一名专业的视频翻译专家和术语顾问,专注于口腔正畸学领域。你不仅擅长准确理解原始的{src_language}文本,还能提取关键的专业术语,并优化翻译以更好地适应{TARGET_LANGUAGE}的表达习惯和文化背景。
对于提供的原始{src_language}视频文本,你需要:
"""
return summary_prompt
还是写成英文的
Certainly! Here's the updated get_summary_prompt
function with the prompt written in English and emphasizing the orthodontics field:
pythondef get_summary_prompt(source_content): src_language = get_whisper_language() TARGET_LANGUAGE = load_key("target_language") summary_prompt = f""" ### Role You are a professional video translation expert and terminology consultant specializing in orthodontics. You excel not only in accurately understanding the original {src_language} text but also in extracting key professional terms and optimizing the translation to better suit the expression habits and cultural background of {TARGET_LANGUAGE}. ### Task Description For the provided original {src_language} video text, you need to: 1. Summarize the video's main topic in one sentence, emphasizing its relation to the biomechanics of orthodontics. 2. Extract professional terms and names that appear in the video, providing {TARGET_LANGUAGE} translations or suggesting to keep the original terms. Avoid extracting simple, common words. 3. Provide a brief explanation for each translated term or name. ### Analysis and Summary Steps Please think in two steps, processing the text line by line: 1. Topic summarization: - Quickly skim through the entire text to understand the general idea. - Summarize the topic in one concise sentence, highlighting its connection to orthodontic biomechanics. 2. Term and name extraction: - Carefully read the entire text, marking professional terms and names related to orthodontics. - For each term or name, provide a {TARGET_LANGUAGE} translation or suggest keeping the original term; only the word itself is needed, not the pronunciation. - Add a brief explanation for each term or name to help the translator understand. - If the word is a fixed abbreviation or a proper name, please keep the original. ### Output Format Please output your analysis results in the following JSON format, where <> represents placeholders: {{ "theme": "<Briefly summarize the theme of this video in 1 sentence>", "terms": [ {{ "original": "<Term or name 1 in {src_language}>", "translation": "<{TARGET_LANGUAGE} translation or keep original>", "explanation": "<Brief explanation of the term or name>" }}, ... ] }} ### Video Text Data to be Processed <video_text_to_summarize> {source_content} </video_text_to_summarize> """.strip() return summary_prompt
This updated function generates a prompt in English and emphasizes that the video is about orthodontics, specifically the biomechanics of orthodontic treatments. The prompt guides the model to:
By making these adjustments, the model should now generate a summary and term extraction that are focused on the orthodontics field, and the prompt is provided in English as per your request.