因此,我在 BERT 中输入了用于标记预测的句子数据帧,并与预测一起作为输出接收到,句子分成单词。现在我想将拆分/标记化的句子和预测的数据框恢复为原始句子。(当然我有原始句子,但我需要执行此过程,以使预测与句子标记一致)
original sentence
You couldn't have done any better because if you could have, you would have.
Post processing
['[CLS]', 'You', 'couldn', "'", 't', 'have', 'done', 'any', 'better', 'because', 'if', 'you', 'could', 'have', ',', 'you', 'would', 'have', '.', '[SEP]']
我确定了三个必要的过程。1. 删除引号 2. 删除 CLS ,SEP 及其多余的引号和逗号, 3. 删除分隔单词的逗号并将它们合并。
def fix_df(row):
sentences = row['t_words']
return remove_edges(sentences)
def remove_edges(sentences):
x = sentences[9:-9]
return remove_qmarks(x)
def remove_qmarks(x):
y = x.replace("'", "")
return join(y)
def join(y):
z = ' '.join(y)
return z
a_df['sents'] = a_df.apply(fix_df, axis=1)
前两个功能在很大程度上可以正常工作,但最后一个没有。相反,我得到了一个看起来像这样的结果。
Y o u , c o u l d n , " " , t , h a v e, d o n e ,...
逗号并没有消失,而是文字被扭曲了。我肯定错过了一些东西。那会是什么?
皈依舞
慕田峪4524236
随时随地看视频慕课网APP
相关分类