compfuntine-v1.0.6.py
4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import difflib
from bs4 import BeautifulSoup
def compare_texts(file1_path, file2_path, output_file_path, fromdesc='Text 1', todesc='Text 2'):
print('================================start...===============================')
# 从文件中读取文本内容
with open(file1_path, 'r', encoding='utf-8') as file1:
text1 = file1.read()
with open(file2_path, 'r', encoding='utf-8') as file2:
text2 = file2.read()
# 计算file2的总字符数
total_chars = len(text2)
# 创建HtmlDiff对象来生成HTML格式的差异报告
d = difflib.HtmlDiff()
diff_html = d.make_file(text1.splitlines(), text2.splitlines(), fromdesc=fromdesc, todesc=todesc)
# 使用BeautifulSoup解析HTML报告
soup = BeautifulSoup(diff_html, 'html.parser')
# 提取text2列中diff_add和diff_chg标记的内容
added_chars = 0
modified_chars = 0
modified_text = []
# 找到所有<tr>标签
rows = soup.find_all('tr')
for row in rows:
# 找到每个<tr>标签中的最后一个<td>标签
cells = row.find_all('td')
if cells:
last_cell = cells[-1]
# 在最后一个<td>标签中找到所有带有diff_add和diff_chg的<span>标签
spans = last_cell.find_all('span', {'class': ['diff_add', 'diff_chg']})
for span in spans:
text = span.get_text()
if 'diff_add' in span['class']:
added_chars += len(text)
elif 'diff_chg' in span['class']:
modified_chars += len(text)
modified_text.append(text)
# print("\nModified (diff_chg) Text:")
# print("".join(modified_text))
# 生成统计信息
stats_info = f"""
<h2>Comparison Report</h2>
<p><strong>file2.txt Total Characters:</strong> {total_chars}</p>
<p><strong>Added Characters (diff_add):</strong> {added_chars}; Added Characters Rate(%):</strong> {((added_chars / total_chars) * 100):.2f}%</p>
<p><strong>Modified Characters (diff_chg):</strong> {modified_chars}; Modified Characters Rate(%):</strong> {((modified_chars / total_chars) * 100):.2f}%</p>
<hr>
"""
# 打印详细的新增和修改内容的字符数
print(f"Added Characters (diff_add): {added_chars}")
print(f"Modified Characters (diff_chg): {modified_chars}")
print(f"Compare Report: {stats_info}")
# 将统计信息添加到差异报告的开头
full_report = stats_info + str(soup)
# 将结果保存为指定路径和名称的HTML文件
with open(output_file_path, 'w', encoding='utf-8') as f:
f.write(full_report)
print(f"差异报告已生成并保存为'{output_file_path}'")
# 示例:文件路径
personfile = 'person'
filename = '82874'
# file1_path = f'voicetranseform/compareresult/asresult/{filename}/{personfile}'
# file2_path = f'voicetranseform/compareresult/asresult/{filename}/fun001'
# output_file_path = f'voicetranseform/compareresult/asresult/{filename}/diff_output_v1.0.6.html'
# compare_texts(file1_path, file2_path, output_file_path)
file1_path = f'D:/software/python/project/funasr_web/audios/leven/correct.txt'
file2_path = f'D:/software/python/project/funasr_web/audios/leven/origin.txt'
file3_path = f'D:/software/python/project/funasr_web/audios/leven/output1.txt'
file4_path = f'D:/software/python/project/funasr_web/audios/leven/output2.txt'
file5_path = f'D:/software/python/project/funasr_web/audios/leven/output3.txt'
output_file_path = f'D:/software/python/project/funasr_web/audios/leven/diff_output_v1.0.6.source.html'
compare_texts(file1_path, file2_path, f'D:/software/python/project/funasr_web/audios/leven/diff/origin.html', fromdesc='标注答案', todesc='初次推理')
compare_texts(file1_path, file3_path, f'D:/software/python/project/funasr_web/audios/leven/diff/output1.html', fromdesc='标注答案', todesc='第一次训练后')
compare_texts(file1_path, file4_path, f'D:/software/python/project/funasr_web/audios/leven/diff/output2.html', fromdesc='标注答案', todesc='第二次训练后')
compare_texts(file1_path, file5_path, f'D:/software/python/project/funasr_web/audios/leven/diff/output3.html', fromdesc='标注答案', todesc='第三次训练后')