没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|行业资讯|编辑:胡涛|2024-05-20 11:29:46.270|阅读 81 次
概述:在文档处理应用程序中比较文档的策略有很多。最常见的方法之一是逐字比较文档的文本。这是一种简单有效的文档比较方法,但它确实有一些局限性。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
TX Text Control 是一款功能类似于 MS Word 的文字处理控件,包括文档创建、编辑、打印、邮件合并、格式转换、拆分合并、导入导出、批量生成等功能。广泛应用于企业文档管理,网站内容发布,电子病历中病案模板创建、病历书写、修改历史、连续打印、病案归档等功能的实现。
在文档处理应用程序中比较文档的策略有很多。最常见的方法之一是逐字比较文档的文本。这是一种简单有效的文档比较方法,但它确实有一些局限性。
本质上,该比较算法按给定顺序比较所有段落。在段落的基础上,将按照分隔符提取所有句子。最后,将原始文档中这些句子中的单词与给定的修订文档进行比较。
结果在原始文档中标记为跟踪更改。跟踪更改在原始文档中突出显示,用户可以看到对文档所做的更改。
该示例实现了该类DocumentComparison,该类接受两个TXText控件。 其构造函数中的文本控件实例。您可以轻松地重写此类以使用非 UI TXText控件。服务器文本控件实例。
DocumentComparison dc = new DocumentComparison(textControl1, textControl2);
构造函数比较两个文档。它循环遍历原始文档中的所有段落,并将文本与修订后的文档进行比较。如果发现差异,文本将被标记为跟踪更改。
该ExtractSentences方法从当前段落中获取一个字符串,并通过在典型的分隔符处拆分它来返回句子列表。
public static List<string> ExtractSentences(string input)
{
List<string> sentences = new List<string>();
// Use regular expression to split the input string into sentences but keep white spaces
string pattern = @"([.!?])";
// split the input string into sentences with the delimiters
string[] splitSentences = Regex.Split(input, pattern);
// Trim each sentence and remove empty strings
foreach (string sentence in splitSentences)
{
sentences.Add(sentence);
}
return sentences;
}
CompareSentences 方法创建单个单词并比较每个给定句子中单词的位置。它返回一个元组列表,每个元组包含三个元素:单词 from sentence1、单词开头的字符索引以及对应的单词 from sentence2。最后,它返回两个句子之间的差异列表。
private static List<(string word, int charIndex, string replacedWord)> CompareSentences(string sentence1, string sentence2)
{
string[] words1 = sentence1.Split(' ');
string[] words2 = sentence2.Split(' ');
List<(string word, int charIndex, string replacedWord)> differences =
new List<(string word, int charIndex, string replacedWord)>();
// Track the character index
int charIndex = 0;
// Get the maximum length of the two sentences
int maxLength = Math.Max(words1.Length, words2.Length);
// Compare each word in the sentences
for (int i = 0; i < maxLength; i++)
{
// Check if the current word exists in both sentences
if (i < words1.Length && i < words2.Length)
{
// If the words are different, add the word, character index, and replaced word to the list
if (words1[i] != words2[i])
{
differences.Add((words1[i], charIndex, words2[i]));
}
}
// If one of the sentences is shorter, add the extra word to the list
else if (i < words1.Length)
{
differences.Add((words1[i], charIndex, ""));
}
else
{
differences.Add((words2[i], charIndex, ""));
}
// Update the character index for the next word
if (i < words1.Length)
charIndex += words1[i].Length + 1; // Add 1 for the space
}
return differences;
}
DocumentComparison 类的构造函数使用上述方法来查找给定 TextControl 实例之间的差异。差异被标记为原始文档中的跟踪更改。
public DocumentComparison(TXTextControl.TextControl originalDocument, TextControl revisedDocument)
{
// Initialize document references
m_originalDocument = originalDocument;
m_revisedDocument = revisedDocument;
// Enable track changes in the original document
originalDocument.IsTrackChangesEnabled = true;
// Compare paragraphs between the original and revised documents
for (int p = 1; p <= m_originalDocument.Paragraphs.Count; p++)
{
var offsetSentences = 0;
// Retrieve the original and revised paragraphs
Paragraph originalParagraph = m_originalDocument.Paragraphs[p];
if (p > m_revisedDocument.Paragraphs.Count)
break; // Break if the revised document has fewer paragraphs than the original document
Paragraph revisedParagraph = m_revisedDocument.Paragraphs[p];
// Get the start position of the original paragraph
var startParagraph = originalParagraph.Start;
var uncheckedOffset = 0;
// Check if the text of the original and revised paragraphs differ
if (originalParagraph.Text != revisedParagraph.Text)
{
// Extract sentences from the original and revised paragraphs
var originalSentences = ExtractSentences(originalParagraph.Text);
var revisedSentences = ExtractSentences(revisedParagraph.Text);
// Compare sentences and replace words in the original document
for (int i = 0; i < originalSentences.Count; i++)
{
// Trim sentences and calculate offset
var originalTrimOffset = originalSentences[i].Length - originalSentences[i].Trim().Length;
var originalSentence = originalSentences[i].Trim();
var revisedSentence = revisedSentences[i].Trim();
// Track changes offset initialization
int trackedChangeOffset = 0;
var differences = CompareSentences(originalSentence, revisedSentence);
// Check if there are any differences
if (differences.Count == 0)
uncheckedOffset = originalSentences[i].Length - 1;
// Apply differences to the original document
foreach (var difference in differences)
{
m_originalDocument.Selection.Start = trackedChangeOffset + startParagraph + offsetSentences +
difference.charIndex + originalTrimOffset + uncheckedOffset - 1;
m_originalDocument.Selection.Length = difference.word.Length;
m_originalDocument.Selection.Text = difference.replacedWord;
trackedChangeOffset += difference.replacedWord.Length;
}
// Update offset for next sentence
offsetSentences += originalSentences[i].Length + trackedChangeOffset;
}
}
}
}
逐字比较文档是文档比较的常用方法。此示例演示如何使用 TX Text Control 实现简单的逐字比较算法。该示例比较两个文档并将差异标记为跟踪原始文档中的更改。
欢迎下载|体验更多TX Text Control产品
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn




Tech Soft 3D 正式整合旗下 SDK 工具包,构建以数据、图形、仿真和建模为核心的 HOOPS 产品体系
在当今的数据驱动时代,Python 开发者经常需要将列表(一种基本的 Python 数据结构)转换为 Excel 电子表格。Excel 作为各行业通用的数据展示、报告生成与信息共享工具,无论是生成业务报告、准备分析数据,还是与非技术人员协作,掌握将 Python 列表导出为 Excel 的高效方法都至关重要。
本篇教程,我们将学习如何使用Aspose.CAD for Python via .NET将 SVG 转换为PNG 。这款 3D CAD SDK 有助于实现单个文件和多个文件格式转换的自动化。我们将编写一个完整的代码示例,以演示Aspose.CAD for Python via .NET的可用性和效率。
在制造业数字化项目中,让数据流动起来,往往比让设备动起来还难。
相关产品
TX Text Control .NET for WPF 分标准,专业,及企业三个版本,是一套专业的文字处理控件。
TX Text Control ActiveXTX Text Control ActiveX是一个强大的文字处理组件,为开发者提供一个广泛的文字处理功能。它提供了全面的文本格式,邮件合并功能和文字处理关键性功能,如表格支持,图片,页眉和页脚、页面部分等。
最新文章 MORE
半岛外围网上直营相关的文章 MORE
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@ke049m.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
半岛外围网上直营