python - 如何在python中为数据添加标点符号
问题描述
我正在为 Tacotron 2 编写代码,它将从 youtube 获取成绩单并将其格式化为文件。不幸的是,它从 YT 收到的数据并没有说明句子在哪里结束。所以,我尝试在最后添加句号,但大多数句子都不是一个完整的句子。那么,我怎样才能让它只在句子的结尾添加句号。它收到的唯一其他数据是时间戳。
# Batch file for Tacotron 2
from youtube_transcript_api import YouTubeTranscriptApi
transcript_txt = YouTubeTranscriptApi.get_transcript('DY0ekRZKtm4')
def write_transcript():
with open('transcript.txt', 'a+') as transcript_object:
transcript_object.seek(0)
subtitles = transcript_object.read(100)
if len(subtitles) > 0:
transcript_object.write('\n')
for i in transcript_txt:
ii = i['text']
if ii[-1] != '.':
iii = ii + '.'
else:
iii = ii
print(iii)
transcript_object.write(iii + '\n')
transcript_object.close()
write_transcript()
这是一个例子:
What it saves:
sometimes it was possible to completely.
fall.
out of the world if the lag was bad.
enough.
What I want:
sometimes it was possible to completely
fall
out of the world if the lag was bad
enough.
解决方案
没有简单的解决方案。我能想到的最省力的方法是设置spaCy,nlp
整个成绩单并希望最好。虽然它不是在没有标点符号的数据上训练的,所以不要期望完美的结果,但它会检测一些句子边界(大部分基于语法)。
import spacy
nlp = spacy.load('en_core_web_trf')
text = """sometimes it was possible to completely
fall
out of the world if the lag was bad
enough
we solved that by
adding more test data"""
doc = nlp(text)
for s in doc.sents:
print(f"'{s}'")
输出:
'sometimes it was possible to completely
fall
out of the world if the lag was bad
enough
'
'we solved that by
adding more test data'
所以在这种情况下,它奏效了。一旦你有了它,你可以做一些额外的处理,手动添加标点符号等。
推荐阅读
- node.js - 按字段 1 聚合组并计算字段 2 的总和
- mongodb - Mongoose $lookup 未按预期响应
- tensorflow - 训练模型上的 TensorFlow 错误(在 GPU 上)
- sql - 视图的自动源到目标列映射 - SQL Server
- css - Woocommerce:产品类别小部件
- excel - 在 Excel Desktop 中创建和设置时,Office.Settings 无法在 Excel Online 中检索
- python - + 和 | 有什么区别?添加到另一个变量时?
- java - WebLogic Server 中的 Firebase FCM 问题
- swift - 嵌入式导航链接
- amazon-web-services - 如何拉取 AWS Lambda 容器镜像