Text difference comparison method and device, electronic equipment and storage medium
Patent Information
- Application Number
- CN202310316987.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-29
- Publication Date
- 2026-10-27
- Estimated Expiration
- 2043-03-29
AI Technical Summary
但是在两篇文档的字号、字体、排版不一致的情况下,同一行显示的文字的数量往往不一致,因此单纯以行为单位进行文本比较会导致无法得到准确、客观的差异结果
[0033]上述本申请提供的文本差异比较方法、装置、电子设备及存储介质,根据对第一文本和第二文本对应的第一文本采集行集合与第二文本采集行集合逐行进行相似度计算,按照相似度完全相同、完全不相同、部分相似三种情况分别对第一文本采集行集合和第二文本采集行集合中的文本行进行对齐和组合,生成第一文本比较行集合与第二文本比较行集合。再对第一文本比较行集合和第二文本比较行集合进行比较,生成第一差异描述信息,包括第一文本比较行集合和第二文本比较行集合中增加、删除和修改的字符以及字符在两个比较行集合中的位置。最后,根据比较行集合与采集行集合的行对应关系,将第一差异描述信息转换为第二差异描述信息,包括第一文本采集行集合和第二文本采集行集合中增加、删除和修改的字符以及字符在两个采集行集合中的位置,从而准确识别和标明第一文本和第二文本的差异,避免了因文本文件的字号、字体、页边距等排版不一致导致比较结果存在较大误差的问题。
Smart Images

Figure CN117291166B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of text processing technology, and in particular to a method, apparatus, electronic device, and storage medium for comparing text differences. Background Technology
[0002] In the financial and legal service sectors, this involves the automatic comparison of commercial documents, legal documents, and office documents to quickly identify and locate differences between two documents, saving the workload of manual word-by-word comparison and reducing the problem of failing to identify differences due to oversight.
[0003] Traditional text comparison techniques typically compare text line by line, using the differences between lines in two documents as the comparison result. However, when two documents have different font sizes, fonts, and layouts, the number of characters displayed in the same line often differs. Therefore, simply comparing text line by line can lead to inaccurate and unobjective comparison results. For example, the same text might be displayed as 10 lines in the base document and 15 lines in the comparison document. This results in each line of text being slightly different, and current techniques would assume that every line is different, leading to the conclusion that the entire document is different, which affects the accuracy of the text comparison results. Summary of the Invention
[0004] To address the aforementioned technical problems, embodiments of this application provide a text difference comparison method, apparatus, electronic device, and storage medium.
[0005] In a first aspect, embodiments of this application provide a text difference comparison method, the method comprising:
[0006] Generate a first text collection line set and a second text collection line set based on the first text and the second text, respectively;
[0007] The similarity is calculated line by line for the first set of text collection lines and the second set of text collection lines, and the first set of text comparison lines and the second set of text comparison lines are generated based on the similarity calculation results;
[0008] The first text comparison line set and the second text comparison line set are compared line by line to generate first difference description information. The first difference description information includes characters added, deleted and modified in the first text comparison line set and the second text comparison line set, as well as the position of the characters in the first text comparison line set and the second text comparison line set.
[0009] Based on the row correspondence between the comparison row set and the collection row set, the first difference description information is converted into second difference description information. The second difference description information includes characters added, deleted, and modified in the first text collection row set and the second text collection row set, as well as the positions of the characters in the first text collection row set and the second text collection row set.
[0010] In one embodiment, the similarity calculation for each line of the first set of collected text lines and the second set of collected text lines includes:
[0011] For each text line in the first set of text collection lines:
[0012] The similarity is calculated sequentially with each text line in the second text collection set to obtain the text lines in the second text collection set that have the same similarity.
[0013] Align the text lines with the same similarity in the second text collection line set in turn, calculate the similarity of the remaining text lines after alignment, and count the total number of the remaining text lines with the same or partial similarity. When the total number is the maximum value, take the alignment position at this time as the best alignment position between the text lines in the first text collection line set and the text lines in the second text collection line set.
[0014] When all text lines in the first set of text collections are in the best alignment position with all text lines in the second set of text collections, the similarity of each text line is recalculated.
[0015] In one embodiment, generating a first set of text comparison lines and a second set of text comparison lines based on the similarity calculation results includes:
[0016] Based on the similarity calculation results of the text lines after alignment of the first text collection line set and the second text collection line set, the text lines in the first text collection line set and the second text collection line set are combined according to the three cases that the similarity calculation results are completely the same, completely different, and partially similar, respectively, to generate the first text comparison line set and the second text comparison line set.
[0017] In one embodiment, the step of combining text lines in the first text collection set and the second text collection set according to the three cases of completely identical, completely different, and partially similar similarity results respectively includes:
[0018] If any number of consecutive text lines in the first text collection set are partially similar and the similarity exceeds a preset threshold, then the multiple consecutive text lines will be merged into one text line.
[0019] If any number of consecutive text lines in the second text collection set are partially similar and the similarity exceeds a preset threshold, then the multiple consecutive text lines will be merged into one text line.
[0020] In one embodiment, generating a first set of text comparison lines and a second set of text comparison lines based on the similarity calculation results includes:
[0021] When generating the first text comparison line set and the second text comparison line set, the line correspondence between the first text comparison line set and the first text collection line set, as well as the line correspondence between the second text comparison line set and the second text collection line set, are obtained respectively.
[0022] In one embodiment, the step of comparing the first set of text comparison lines and the second set of text comparison lines line by line to generate first difference description information includes:
[0023] Based on the text line alignment relationship between the first text collection line set and the second text collection line set, the text lines of the first text comparison line set and the second text comparison line set are aligned. Then, the first text comparison line set and the second text comparison line set are compared line by line to generate the characters added, deleted, and modified in the first text comparison line set and the positions of the characters in the first text comparison line set, as well as the characters added, deleted, and modified in the second text comparison line set and the positions of the characters in the second text comparison line set.
[0024] In one embodiment, converting the first difference description information into second difference description information based on the row correspondence between the comparison row set and the collection row set includes:
[0025] Based on the line correspondence between the first text comparison line set and the first text acquisition line set, and the line correspondence between the second text comparison line set and the second text acquisition line set, the characters added, deleted, and modified in the first text comparison line set in the first difference description information are split and mapped to the corresponding text lines in the first text acquisition line set, while obtaining the position of the characters in the first text acquisition line set; and the characters added, deleted, and modified in the second text comparison line set in the first difference description information are split and mapped to the corresponding text lines in the second text acquisition line set, while obtaining the position of the characters in the second text acquisition line set.
[0026] Secondly, embodiments of the present invention provide a text difference comparison device, the device comprising:
[0027] The text line acquisition module is used to generate a first text acquisition line set and a second text acquisition line set based on the first text and the second text, respectively.
[0028] The comparison line generation module is used to perform similarity calculation on the first text collection line set and the second text collection line set line by line, and generate the first text comparison line set and the second text comparison line set based on the similarity calculation results;
[0029] The text comparison module is used to compare the first text comparison line set and the second text comparison line set line by line to generate first difference description information. The first difference description information includes characters added, deleted and modified in the first text comparison line set and the second text comparison line set, as well as the position of the characters in the first text comparison line set and the second text comparison line set.
[0030] The comparison result conversion module is used to convert the first difference description information into second difference description information according to the row correspondence between the comparison row set and the collection row set. The second difference description information includes characters added, deleted, and modified in the first text collection row set and the second text collection row set, as well as the positions of the characters in the first text collection row set and the second text collection row set.
[0031] Thirdly, embodiments of this application provide an electronic device, including a memory and a processor, wherein the memory is used to store a computer program, and the computer program executes the text difference comparison method provided in the first aspect when the processor is running.
[0032] Fourthly, embodiments of this application provide a computer-readable storage medium storing a computer program that, when run on a processor, executes the text difference comparison method provided in the first aspect.
[0033] The text difference comparison method, apparatus, electronic device, and storage medium provided in this application calculate the similarity line by line between the first and second text collection line sets corresponding to the first and second texts. The text lines in the first and second text collection line sets are aligned and combined according to three cases: completely identical, completely different, and partially similar, to generate a first text comparison line set and a second text comparison line set. The first and second text comparison line sets are then compared to generate first difference description information, including characters added, deleted, and modified in the first and second text comparison line sets, as well as the positions of the characters in the two comparison line sets. Finally, based on the line correspondence between the comparison line sets and the collection line sets, the first difference description information is converted into second difference description information, including characters added, deleted, and modified in the first and second text collection line sets, as well as the positions of the characters in the two collection line sets. This accurately identifies and marks the differences between the first and second texts, avoiding significant errors in comparison results caused by inconsistencies in font size, typeface, page margins, and other formatting of the text files. Attached Figure Description
[0034] To more clearly illustrate the technical solutions of this application, the accompanying drawings used in the embodiments will be briefly described below. It should be understood that the following drawings only show some embodiments of this application and should not be considered as a limitation on the scope of protection of this application. In the various drawings, similar components are numbered similarly.
[0035] Figure 1 A flowchart illustrating a text difference comparison method provided in an embodiment of this application is shown.
[0036] Figure 2 This paper shows a flowchart of step S102 in the text difference comparison method provided in an embodiment of this application;
[0037] Figure 3 A schematic diagram of the structure of the text difference comparison device provided in an embodiment of this application is shown. Detailed Implementation
[0038] The technical solutions in the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments.
[0039] The components of the embodiments of this application described and illustrated in the accompanying drawings can be arranged and designed in a variety of different configurations. Therefore, the following detailed description of the embodiments of this application provided in the drawings is not intended to limit the scope of the claimed application, but merely to illustrate selected embodiments of the application. All other embodiments obtained by those skilled in the art based on the embodiments of this application without inventive effort are within the scope of protection of this application.
[0040] In the following, the terms “comprising,” “having,” and their cognates, which may be used in various embodiments of this application, are intended only to indicate a particular feature, number, step, operation, element, component, or combination thereof, and should not be construed as excluding, firstly, the presence of one or more other features, numbers, steps, operations, elements, components, or combinations thereof, or adding the possibility of one or more features, numbers, steps, operations, elements, components, or combinations thereof.
[0041] Furthermore, the terms "first," "second," and "third" are used only to distinguish descriptions and should not be interpreted as indicating or implying relative importance.
[0042] Unless otherwise specified, all terms used herein (including technical and scientific terms) shall have the same meaning as commonly understood by one of ordinary skill in the art to which the various embodiments of this application pertain. Terms (such as those defined in commonly used dictionaries) shall be interpreted as having the same meaning as in their contextual meaning in the relevant technical field and shall not be construed as having an idealized or overly formal meaning, unless clearly defined in the various embodiments of this application.
[0043] In existing technologies, text comparison is generally performed line by line, taking the differences between lines of two documents as the comparison result. However, when the font size, font, margins, and other settings of the two documents are different, the number of characters displayed in the same line is often inconsistent. Therefore, simply comparing text line by line will not yield accurate and objective results regarding text differences. For example, the same text may be displayed as 10 lines in the benchmark document and 15 lines in the comparison document. This results in the text in each line of the two documents not being completely the same. Existing technologies would assume that there is a difference in each line, thus concluding that the two documents are different, which would affect the accuracy of the text comparison results.
[0044] Example 1
[0045] This disclosure provides a method for comparing text differences.
[0046] See Figure 1 Text difference comparison methods include:
[0047] Step S101: Generate a first text collection line set and a second text collection line set based on the first text and the second text, respectively.
[0048] Specifically, text is extracted line by line from the first text to generate a first text collection line set, which contains all the text lines of the first text and the line number corresponding to each text line. Similarly, text is extracted line by line from the second text to generate a second text collection line set, which contains all the text lines of the second text and the line number corresponding to each text line.
[0049] Typically, both the first and second text sets contain multiple lines of text. Due to differences in font size, font type, margins, and other settings, the number of text lines contained in the first and second text set collections may differ, as may the number of characters per line.
[0050] For example, if the first text contains 7 lines of text, and each line supports a maximum of 7 characters, then the generated collection set 'a' of the first text lines would be:
[0051] 1 abc 2 xyz 3 defghij 4 kl 5 mno 6 pqrstuv 7 wx
[0052] The second text contains 8 lines of text, and each line can support a maximum of 5 characters. Therefore, the generated set of second text collection lines, b, is:
[0053]
[0054]
[0055] Step S102: Perform similarity calculation on each line of the first text collection line set and the second text collection line set, and generate the first text comparison line set and the second text comparison line set based on the similarity calculation results.
[0056] In one embodiment, similarity is calculated line by line for the first set of collected text lines and the second set of collected text lines. Please refer to [link to relevant documentation]. Figure 2 ,include:
[0057] For each text line in the first set of text collection lines:
[0058] The similarity is calculated sequentially with each text line in the second text collection set to obtain the text lines in the second text collection set that have the same similarity.
[0059] Align the text lines with the same similarity in the second text collection line set in turn, calculate the similarity of the remaining text lines after alignment, and count the total number of the remaining text lines with the same or partial similarity. When the total number is the maximum value, take the alignment position at this time as the best alignment position between the text lines in the first text collection line set and the text lines in the second text collection line set.
[0060] When all text lines in the first set of text collections are in the best alignment position with all text lines in the second set of text collections, the similarity of each text line is recalculated.
[0061] Specifically, based on the text similarity analysis algorithm, each text line in the first set of collected text lines is sequentially compared with each text line in the second set of collected text lines. That is, a text line is selected from the first set of collected text lines in turn and compared with each text line in the second set of collected text lines for similarity calculation. The text similarity analysis algorithm can be the cosine similarity algorithm or other commonly used text similarity calculation methods.
[0062] For example, firstly, select the first text line "abc" from the first text collection set a and calculate its similarity with each text line from the second text collection set b. This yields the similarity score between "abc" and each text line from the second text collection set b. Based on the similarity scores, we can see that the first text line "abc" from the first text collection set a has the exact same similarity score as the first and fifth text lines from the second text collection set b (i.e., a similarity score of 1).
[0063]
[0064] Then, the text line "abc" in the first text collection line set a is aligned with the text lines in the second text collection line set b that have the same similarity, and the similarity of the remaining text lines after alignment is calculated. Based on the results of the previous similarity calculation, the text line "abc" has two alignment methods, namely:
[0065] Alignment method 1: Align the text line "abc" in the first text collection line set a with the first text line in the second text collection line set b;
[0066] Alignment method 2: Align the text line "abc" in the first text collection line set a with the fifth text line in the second text collection line set b.
[0067] Alignment 1:
[0068] abc abc 1 xyz xyz 1 defghij defgh 0.7 kl ijkl 0.5 mno abc 0 pqrstuv mno 0 wx pqrst 0 uvw 0
[0069] Note: If there are no matching text lines in the uvw of the second text collection line set b, then the similarity is considered to be zero.
[0070] Alignment 2:
[0071] abc 0 xyz 0 defgh 0 ijkl 0 abc abc 1 xyz mno 0 defghij pqrst 0 kl uvw 0 mno 0 pqrstuv 0 wx 0
[0072] Note: If mno, pqrstuv, and wx in the first text collection set a do not have a paired text line, then the similarity is considered to be zero. If abc, xyz, defgh, and ijkl in the second text collection set b do not have a paired text line, then the similarity is considered to be zero.
[0073] Optionally, while aligning each text line in the first set of collected text lines sequentially with text lines in the second set of collected text lines that have the same similarity, and calculating the similarity of the remaining text lines after alignment, each text line in the first set of collected text lines can also be aligned with text lines in the second set of collected text lines that have a similarity exceeding a preset threshold, and the similarity of the remaining text lines after alignment can be calculated. Based on the statistical results of the similarity of the remaining text lines, the optimal alignment line can be found. For example, in addition to aligning the text line "abc" in the first set of collected text lines a with text lines in the second set of collected text lines b that have the same similarity, the text line "abc" can also be aligned with text lines in the second set of collected text lines b that have a similarity exceeding 0.9, and the similarity of the remaining text lines after alignment can be calculated.
[0074] Count the total number of text lines in alignment 1 and alignment 2 that have the same similarity (i.e., a similarity value of 1) or partial similarity (i.e., a similarity value between 0 and 1) (text lines without a pair are considered to have a similarity of zero):
[0075] Total number of alignments 1: 1 + 1 + 1 + 1 = 4
[0076] Alignment 2 Total Quantity: 1
[0077] Therefore, the total number of text lines with completely or partially similar similarity is the largest in alignment method 1, so alignment method 1 is the best alignment position. Finally, it is determined that the first text line "abc" of the first text collection line set a is aligned with the first text line of the second text collection line set b.
[0078] Similarly, the second to seventh text lines of the first text collection set a are sequentially compared with each text line of the second text collection set b to determine the optimal alignment position between the second to seventh text lines and the second text collection set b. Then, the similarity of each aligned text line is calculated.
[0079] abc abc 1 xyz xyz 1 defghij defgh 0.7 kl ijkl 0.5 abc 0 mno mno 1 pqrstuv pqrst 0.7 wx uvw 0.25
[0080] In one embodiment, generating a first set of text comparison lines and a second set of text comparison lines based on the similarity calculation results includes:
[0081] Based on the similarity calculation results of the text lines after alignment of the first text collection line set and the second text collection line set, the text lines in the first text collection line set and the second text collection line set are combined according to the three cases that the similarity calculation results are completely the same, completely different, and partially similar, respectively, to generate the first text comparison line set and the second text comparison line set.
[0082] Specifically, for text lines in the first and second text collection sets that have completely identical similarity (i.e., similarity value of 1) and completely different similarity (i.e., similarity value of 0), they are not combined but directly placed into the first and second text comparison sets, and the corresponding line numbers are adjusted. For multiple consecutive text lines in the first and second text collection sets that have partially similar similarity (i.e., similarity value between 0 and 1), it is determined whether the similarity value of these text lines exceeds a preset threshold. If it does, the multiple consecutive text lines in the first and second text collection sets are combined separately before being placed into the first and second text comparison sets.
[0083] For example, if the first text line "abc" in the first text collection set a has the same similarity to the first text line "abc" in the second text collection set b, then the first text line "abc" in the first text collection set a is directly placed into the first text comparison set A, and the first text line "abc" in the second text collection set b is directly placed into the second text comparison set B. If the fifth text line "abc" in the second text collection set b has a completely different similarity to the corresponding text line in the first text collection set a, then the fifth text line "abc" in the second text collection set b is directly placed into the second text comparison set B.
[0084] In one embodiment, the step of combining text lines in the first text collection set and the second text collection set according to the three cases of completely identical, completely different, and partially similar similarity results respectively includes:
[0085] If any number of consecutive text lines in the first text collection set are partially similar and the similarity exceeds a preset threshold, then the multiple consecutive text lines will be merged into one text line.
[0086] If any number of consecutive text lines in the second text collection set are partially similar and the similarity exceeds a preset threshold, then the multiple consecutive text lines will be merged into one text line.
[0087] Specifically, if it is found that multiple consecutive text lines in the first or second text collection line set have partially similar similarities after text line alignment, it is determined whether the similarity values of the multiple consecutive text lines exceed a preset threshold. If they exceed the preset threshold, the multiple consecutive text lines are merged into one text line and then placed into the first or second text comparison line set, and the corresponding line numbers are adjusted.
[0088] For example, based on the similarity calculation results after aligning the text lines of the first text collection line set a and the second text collection line set b, if the third text line "defghij" and the fourth text line "kl" in the first text collection line set a are partially similar to the third text line "defgh" and the fourth text line "ijkl" in the second text collection line set b, then it is determined whether the similarity value exceeds a preset threshold. The preset threshold can be set according to the specific application scenario of text difference comparison. In this embodiment, the preset threshold is 0.2. Since the similarity values of the third and fourth text lines in the first text collection line set a both exceed the preset threshold, the third and fourth text lines are merged, and the merged text line is "defghijkl", which is then placed into the first text comparison line set A. Similarly, the third and fourth text lines in the second text collection line set b are also merged, and the merged text line is "defghijkl", which is then placed into the second text comparison line set B.
[0089] After merging all text lines whose similarity values exceed a preset threshold, the first text comparison set A and the second text comparison set B are obtained.
[0090] The first text comparison line set A is:
[0091] 1 abc 2 xyz 3 defghijkl 4 mno 5 pqrstuvwx
[0092] The second text comparison line set B is:
[0093] 1 abc 2 xyz 3 defghijkl 4 abc 5 mno 6 pqrstuvw
[0094] Optionally, if it is found that multiple consecutive text lines in the first and second text collection sets have the same degree of similarity after text line alignment, the multiple consecutive text lines can be merged into one text line and then put into the first and second text comparison sets.
[0095] Alternatively, if it is found that after text line alignment, there are multiple consecutive text lines in the first and second text collection sets that contain both completely similar and partially similar text lines, and the similarity values of the partially similar text lines all exceed a preset threshold, then the multiple consecutive text lines can be merged into one text line and then placed into the first and second text comparison sets.
[0096] In one embodiment, generating a first set of text comparison lines and a second set of text comparison lines based on the similarity calculation results includes:
[0097] When generating the first text comparison line set and the second text comparison line set, the line correspondence between the first text comparison line set and the first text collection line set, as well as the line correspondence between the second text comparison line set and the second text collection line set, are obtained respectively.
[0098] Specifically, based on the line numbers of the text lines in the comparison line set and the collection line set, a line correspondence between the text lines in the comparison line set and the text lines in the collection line set is generated in the comparison line set.
[0099] For example, based on the line numbers of the text lines in the first comparison line set A and the first collection line set a, the line correspondence between the first text comparison line set A and the first text collection line set a is generated as follows:
[0100]
[0101] Step S103: Compare the first text comparison line set and the second text comparison line set line by line to generate first difference description information. The first difference description information includes characters added, deleted and modified in the first text comparison line set and the second text comparison line set, as well as the positions of the characters in the first text comparison line set and the second text comparison line set.
[0102] Specifically, based on the alignment relationship between the first and second sets of collected text lines, the text lines of the first and second sets of compared text lines are aligned. Then, according to a text comparison algorithm, the first and second sets of compared text lines are compared line by line to generate first difference description information. The text comparison algorithm can be the edit distance algorithm, the longest common substring algorithm, or other commonly used text comparison algorithms.
[0103] For example, based on the text line alignment relationship of the first text collection line set a and the second text collection line set b, the text line alignment relationship of the first text comparison line set A and the second text comparison line set B is generated and aligned:
[0104] abc abc xyz xyz defghijkl defghijkl abc mno mno pqrstuvwx pqrstuvw
[0105] In one embodiment, the first text comparison line set and the second text comparison line set are compared line by line to generate first difference description information. Step S103 includes:
[0106] Based on the text line alignment relationship between the first text collection line set and the second text collection line set, the text lines of the first text comparison line set and the second text comparison line set are aligned. Then, the first text comparison line set and the second text comparison line set are compared line by line to generate the characters added, deleted, and modified in the first text comparison line set and the positions of the characters in the first text comparison line set, as well as the characters added, deleted, and modified in the second text comparison line set and the positions of the characters in the second text comparison line set.
[0107] Specifically, after aligning the text lines of the first and second text comparison line sets, the text comparison algorithm calculates the differences between the first and second text comparison line sets. This includes any added, deleted, or modified characters in each line and their positions within the first and second text comparison line sets. The position of a character within a comparison line set includes the line number of the character and its starting order within the line.
[0108] For example, after aligning the text lines of the first text comparison line set A and the second text comparison line set B, the first text comparison line set A and the second text comparison line set B are compared line by line to generate description information of the differences between the first text comparison line set A and the second text comparison line set B, including:
[0109] delete x Line number 5, starting 9th position
[0110] Simultaneously, it generates description information of the differences between the first text comparison line set B and the first text comparison line set A, including:
[0111] Increase abc Line number 4, starting position 1
[0112] Step S104: Based on the row correspondence between the comparison row set and the collection row set, the first difference description information is converted into second difference description information. The second difference description information includes characters added, deleted, and modified in the first text collection row set and the second text collection row set, as well as the positions of the characters in the first text collection row set and the second text collection row set.
[0113] Specifically, the row correspondence between the first text comparison row set and the first text collection row set, and the row correspondence between the second text comparison row set and the second text collection row set are read from the first text comparison row set and the second text comparison row set, respectively. Then, based on the row correspondence between the comparison row set and the collection row set, the first difference description information is converted into the second difference description information.
[0114] In one embodiment, step S104, which converts the first difference description information into second difference description information based on the row correspondence between the comparison row set and the collection row set, includes:
[0115] Based on the line correspondence between the first text comparison line set and the first text acquisition line set, and the line correspondence between the second text comparison line set and the second text acquisition line set, the characters added, deleted, and modified in the first text comparison line set in the first difference description information are split and mapped to the corresponding text lines in the first text acquisition line set, while obtaining the position of the characters in the first text acquisition line set; and the characters added, deleted, and modified in the second text comparison line set in the first difference description information are split and mapped to the corresponding text lines in the second text acquisition line set, while obtaining the position of the characters in the second text acquisition line set.
[0116] Specifically, the first difference description information is read. Based on the line correspondence between the first text comparison line set and the first text acquisition line set, the characters added, deleted, and modified in the first text comparison line set in the first difference description information are split and mapped to the corresponding text lines in the first text acquisition line set according to the line correspondence. For example, since some text lines in the first text comparison line set are formed by merging multiple text lines in the original first text acquisition line set, the characters added, deleted, and modified in those text lines in the first text comparison line set will be distributed across multiple text lines in the original first text acquisition line set. Therefore, based on the line correspondence, the added, deleted, and modified characters are split and mapped to the corresponding text lines in the first text acquisition line set, and the line number of the text line in the first text acquisition line set corresponding to the split characters and its starting order in that text line are calculated. Correspondingly, the characters added, deleted, and modified in the second text comparison line set in the first difference description information are also split and mapped, and the line number of the text line in the second text acquisition line set corresponding to the split characters and its starting order in that text line are calculated.
[0117] For example, the difference description information between the first text comparison line set A and the second text comparison line set B is that the 9th character 'x' in the text line containing line number 5 is deleted. Based on the line correspondence between the first text comparison line set A and the first text collection line set a, it is determined that line number 5 in the first text comparison line set A corresponds to line numbers 6 and 7 in the first text collection line set a. Then, the position of character 'x' in the first text collection line set a is calculated to be the text line containing line number 7, and it is the second position at the beginning of that text line. This achieves the conversion of the first difference description information of the first text into the second difference description information, so as to accurately identify the difference content in the first text.
[0118] The difference description information of the first text collection line set 'a' after conversion includes:
[0119] delete x Line number 7, starting second position
[0120] The text difference comparison method provided in this embodiment calculates the similarity of each line in the first and second text collection line sets, categorizing them into three cases: completely identical, completely different, and partially similar. It then aligns and combines the text lines in the first and second text collection line sets to generate a first text comparison line set and a second text comparison line set. The first and second text comparison line sets are then compared to generate first difference description information, including added, deleted, and modified characters and their positions in the two comparison line sets. Finally, based on the line correspondence between the comparison line sets and the collection line sets, the first difference description information is converted into second difference description information, including added, deleted, and modified characters and their positions in the two collection line sets. This accurately identifies and marks the differences between the first and second texts, avoiding significant errors in comparison results caused by inconsistencies in font size, page margins, and other layout aspects of the text files.
[0121] Example 2
[0122] Furthermore, embodiments of this disclosure provide a text difference comparison device.
[0123] Specifically, such as Figure 3 As shown, the text difference comparison device 300 includes:
[0124] The text line acquisition module 301 is used to generate a first text acquisition line set and a second text acquisition line set based on the first text and the second text, respectively.
[0125] The comparison line generation module 302 is used to perform similarity calculation on the first text collection line set and the second text collection line set line by line, and generate the first text comparison line set and the second text comparison line set based on the similarity calculation results;
[0126] The text comparison module 303 is used to compare the first text comparison line set and the second text comparison line set line by line to generate first difference description information. The first difference description information includes characters added, deleted and modified in the first text comparison line set and the second text comparison line set, as well as the position of the characters in the first text comparison line set and the second text comparison line set.
[0127] The comparison result conversion module 304 is used to convert the first difference description information into second difference description information according to the row correspondence between the comparison row set and the collection row set. The second difference description information includes characters added, deleted and modified in the first text collection row set and the second text collection row set, as well as the positions of the characters in the first text collection row set and the second text collection row set.
[0128] In one embodiment, the comparison line generation module 302 is further configured to, for each text line in the first text collection line set:
[0129] The similarity is calculated sequentially with each text line in the second text collection set to obtain the text lines in the second text collection set that have the same similarity.
[0130] Align the text lines with the same similarity in the second text collection line set in turn, calculate the similarity of the remaining text lines after alignment, and count the total number of the remaining text lines with the same or partial similarity. When the total number is the maximum value, take the alignment position at this time as the best alignment position between the text lines in the first text collection line set and the text lines in the second text collection line set.
[0131] When all text lines in the first set of text collections are in the best alignment position with all text lines in the second set of text collections, the similarity of each text line is recalculated.
[0132] In one embodiment, the comparison line generation module 302 is further configured to combine the text lines in the first text collection line set and the second text collection line set according to the similarity calculation result after the text lines of the first text collection line set and the second text collection line set are aligned, and according to the three cases that the similarity calculation result is completely the same, completely different, and partially similar, respectively, to generate a first text comparison line set and a second text comparison line set.
[0133] In one embodiment, the comparison line generation module 302 is further configured to merge multiple consecutive text lines into one text line when any multiple consecutive text lines in the first text collection line set are partially similar and the similarity exceeds a preset threshold.
[0134] If any number of consecutive text lines in the second text collection set are partially similar and the similarity exceeds a preset threshold, then the multiple consecutive text lines will be merged into one text line.
[0135] In one embodiment, the comparison line generation module 302 is further configured to, when generating the first text comparison line set and the second text comparison line set, respectively obtain the line correspondence between the first text comparison line set and the first text collection line set, and the line correspondence between the second text comparison line set and the second text collection line set.
[0136] In one embodiment, the text comparison module 303 is further configured to align the text lines of the first text comparison line set and the second text comparison line set according to the text line alignment relationship between the first text collection line set and the second text collection line set, and then compare the first text comparison line set and the second text comparison line set line by line to generate characters added, deleted and modified in the first text comparison line set and the positions of the characters in the first text comparison line set, and characters added, deleted and modified in the second text comparison line set and the positions of the characters in the second text comparison line set.
[0137] In one embodiment, the comparison result conversion module 304 is further configured to, based on the line correspondence between the first text comparison line set and the first text acquisition line set, and the line correspondence between the second text comparison line set and the second text acquisition line set, split and map the characters added, deleted, and modified in the first text comparison line set in the first difference description information to the corresponding text lines in the first text acquisition line set, and simultaneously obtain the position of the characters in the first text acquisition line set; and to split and map the characters added, deleted, and modified in the second text comparison line set in the first difference description information to the corresponding text lines in the second text acquisition line set, and simultaneously obtain the position of the characters in the second text acquisition line set.
[0138] The text difference comparison device 300 provided in this embodiment can implement the text difference comparison method provided in Embodiment 1. To avoid repetition, it will not be described again here.
[0139] The text difference comparison device provided in this embodiment calculates the similarity of each line in the first and second text collection line sets corresponding to the first and second texts. It aligns and combines the text lines in the first and second text collection line sets according to three cases: completely identical, completely different, and partially similar, generating a first text comparison line set and a second text comparison line set. The first and second text comparison line sets are then compared to generate first difference description information, including characters added, deleted, and modified in the first and second text comparison line sets, as well as the positions of the characters in the two comparison line sets. Finally, based on the line correspondence between the comparison line sets and the collection line sets, the first difference description information is converted into second difference description information, including characters added, deleted, and modified in the first and second text collection line sets, as well as the positions of the characters in the two collection line sets. This accurately identifies and marks the differences between the first and second texts, avoiding significant errors in comparison results caused by inconsistencies in font size, page margins, and other layout aspects of the text files.
[0140] Example 3
[0141] Furthermore, this disclosure provides an electronic device including a memory and a processor, wherein the memory stores a computer program that, when executed on the processor, performs the text difference comparison method provided in Embodiment 1.
[0142] The electronic device provided in this embodiment can implement the text difference comparison method provided in Embodiment 1. To avoid repetition, it will not be described again here.
[0143] Example 4
[0144] This application also provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the text difference comparison method provided in Embodiment 1.
[0145] In this embodiment, the computer-readable storage medium may be a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk, etc.
[0146] The computer-readable storage medium provided in this embodiment can implement the text difference comparison method provided in Embodiment 1. To avoid repetition, it will not be described again here.
[0147] It should be noted that, in this document, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or terminal that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or terminal. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or terminal that includes that element.
[0148] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods of the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) and includes several instructions to cause a terminal (which may be a mobile phone, computer, server, air conditioner, or network device, etc.) to execute the methods described in the various embodiments of this application.
[0149] The embodiments of this application have been described above with reference to the accompanying drawings. However, this application is not limited to the specific embodiments described above. The specific embodiments described above are merely illustrative and not restrictive. Those skilled in the art can make many other forms under the guidance of this application without departing from the spirit and scope of the claims, and all of these forms are within the protection scope of this application.
Claims
1. A text difference comparison method, characterized in that, The method includes: Generate a first text collection line set and a second text collection line set based on the first text and the second text, respectively; The similarity is calculated line by line for the first set of text collection lines and the second set of text collection lines, and the first set of text comparison lines and the second set of text comparison lines are generated based on the similarity calculation results; The first text comparison line set and the second text comparison line set are compared line by line to generate first difference description information. The first difference description information includes characters added, deleted and modified in the first text comparison line set and the second text comparison line set, as well as the position of the characters in the first text comparison line set and the second text comparison line set. Based on the row correspondence between the comparison row set and the collection row set, the first difference description information is converted into second difference description information. The second difference description information includes characters added, deleted, and modified in the first text collection row set and the second text collection row set, as well as the positions of the characters in the first text collection row set and the second text collection row set. The process of calculating the similarity line by line between the first set of collected text lines and the second set of collected text lines includes: For each text line in the first set of text collection lines: The similarity is calculated sequentially with each text line in the second text collection set to obtain the text lines in the second text collection set that have the same similarity. Align the text lines with the same similarity in the second text collection line set in turn, calculate the similarity of the remaining text lines after alignment, and count the total number of the remaining text lines with the same or partial similarity. When the total number is the maximum value, take the alignment position at this time as the best alignment position between the text lines in the first text collection line set and the text lines in the second text collection line set. When all text lines in the first set of text collections are in the best alignment position with all text lines in the second set of text collections, the similarity of each text line is recalculated.
2. The method according to claim 1, characterized in that, The step of generating a first set of text comparison lines and a second set of text comparison lines based on the similarity calculation results includes: Based on the similarity calculation results of the text lines after alignment of the first text collection line set and the second text collection line set, the text lines in the first text collection line set and the second text collection line set are combined according to the three cases that the similarity calculation results are completely the same, completely different, and partially similar, respectively, to generate the first text comparison line set and the second text comparison line set.
3. The method according to claim 2, characterized in that, The process of combining text lines from the first and second text collection sets according to the similarity calculation results (completely identical, completely different, and partially similar) includes: If any multiple consecutive text lines in the first text collection set are partially similar and the similarity exceeds a preset threshold, then the multiple consecutive text lines will be merged into one text line. If any number of consecutive text lines in the second text collection set are partially similar and the similarity exceeds a preset threshold, then the multiple consecutive text lines will be merged into one text line.
4. The method according to claim 1, characterized in that, The step of generating a first set of text comparison lines and a second set of text comparison lines based on the similarity calculation results includes: When generating the first text comparison line set and the second text comparison line set, the line correspondence between the first text comparison line set and the first text collection line set, as well as the line correspondence between the second text comparison line set and the second text collection line set, are obtained respectively.
5. The method according to claim 1, characterized in that, The step of comparing the first set of text comparison lines and the second set of text comparison lines line by line to generate first difference description information includes: Based on the text line alignment relationship between the first text collection line set and the second text collection line set, the text lines of the first text comparison line set and the second text comparison line set are aligned. Then, the first text comparison line set and the second text comparison line set are compared line by line to generate the characters added, deleted, and modified in the first text comparison line set and the positions of the characters in the first text comparison line set, as well as the characters added, deleted, and modified in the second text comparison line set and the positions of the characters in the second text comparison line set.
6. The method according to claim 1, characterized in that, The step of converting the first difference description information into second difference description information based on the row correspondence between the comparison row set and the collection row set includes: Based on the line correspondence between the first text comparison line set and the first text acquisition line set, and the line correspondence between the second text comparison line set and the second text acquisition line set, the characters added, deleted, and modified in the first text comparison line set in the first difference description information are split and mapped to the corresponding text lines in the first text acquisition line set, while obtaining the position of the characters in the first text acquisition line set; and the characters added, deleted, and modified in the second text comparison line set in the first difference description information are split and mapped to the corresponding text lines in the second text acquisition line set, while obtaining the position of the characters in the second text acquisition line set.
7. A text difference comparison device, characterized in that, The device includes: The text line acquisition module is used to generate a first text acquisition line set and a second text acquisition line set based on the first text and the second text, respectively. The comparison line generation module is used to perform similarity calculation on the first text collection line set and the second text collection line set line by line, and generate the first text comparison line set and the second text comparison line set based on the similarity calculation results; The text comparison module is used to compare the first text comparison line set and the second text comparison line set line by line to generate first difference description information. The first difference description information includes characters added, deleted and modified in the first text comparison line set and the second text comparison line set, as well as the position of the characters in the first text comparison line set and the second text comparison line set. The comparison result conversion module is used to convert the first difference description information into second difference description information according to the row correspondence between the comparison row set and the collection row set. The second difference description information includes characters added, deleted and modified in the first text collection row set and the second text collection row set, as well as the position of the characters in the first text collection row set and the second text collection row set. The comparison row generation module is also used for: For each text line in the first set of text collection lines: The similarity is calculated sequentially with each text line in the second text collection set to obtain the text lines in the second text collection set that have the same similarity. Align the text lines with the same similarity in the second text collection line set in turn, calculate the similarity of the remaining text lines after alignment, and count the total number of the remaining text lines with the same or partial similarity. When the total number is the maximum value, take the alignment position at this time as the best alignment position between the text lines in the first text collection line set and the text lines in the second text collection line set. When all text lines in the first set of text collections are in the best alignment position with all text lines in the second set of text collections, the similarity of each text line is recalculated.
8. An electronic device, characterized in that, It includes a memory and a processor, the memory storing a computer program that executes the text difference comparison method according to any one of claims 1 to 6 when the processor is running.
9. A computer-readable storage medium, characterized in that, It stores a computer program that, when run on a processor, executes the text difference comparison method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Document comparison difference output method and device, storage medium and electronic equipment
CN109740124A
System and method of generating deltas between documents
US11295076B1