7

我想比较两个文档而不考虑换行符。如果内容相同但换行符的位置和数量不同,我想将一个文档中的行映射到另一个文档中的行。

鉴于:

文件 1

I went to Paris in July 15, where I met some nice people.
And I came back
to NY in Aug 15.
I am planning
to go there soon
after I finish what I do.

文件 2

I went
to Paris
in July 15,
where I met
some nice people.
And I came back to NY in Aug 15.
I am planning to go
there soon after I finish what I do.

我想要一种算法,能够确定文档 1 中的第 1 行包含与文档 2 中的第 1 到第 5 行相同的文本,文档 1 中的第 2 行和第 3 行包含与文档 2 中的第 6 行相同的文本,等等。

1 = 1,2,3,4,5
2,3 = 6
4,5,6 = 7,8

如果正则表达式跨越其他文档中的多行,是否有办法匹配每个文档中的每一行?

4

3 回答 3

3
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.commons.io.FileUtils;

public class Compare {
    public static void main(String[] args) throws IOException {
        String doc1 = FileUtils.readFileToString(new File("Doc1.txt"));
        String doc2 = FileUtils.readFileToString(new File("Doc2.txt"));
        String[] array1 = doc1.split("\n");
        String[] array2 = doc2.split("\n");
        int[] count1 = new int[array1.length];
        int[] count2 = new int[array2.length];
        int sum1 = 0;
        int sum2 = 0;
        for (int i=0;i<count1.length;i++) {
            count1[i] = sum1 + array1[i].split(" ").length;
            sum1 = count1[i];
        }
        for (int i=0;i<count2.length;i++) {
            count2[i] = sum2 + array2[i].split(" ").length;
            sum2 = count2[i];
        }
        ArrayList<Integer> result1 = new ArrayList<Integer>();
        ArrayList<Integer> result2 = new ArrayList<Integer>();
        for (int j=0; j<count1.length; ) {
            for (int k=0; k<count2.length; ) {
                if (count1[j]==count2[k]) {
                    result1.add(j+1);
                    result2.add(k+1);
                    System.out.println(result1.toString()+" = "+result2.toString());
                    result1 = new ArrayList<Integer>();
                    result2 = new ArrayList<Integer>();
                    j++;k++;
                } else if (count1[j]>count2[k]) {
                    result2.add(k+1);
                    k++;
                } else {
                    result1.add(j+1);
                    j++;
                }
            }
        }
    }
}

样本输出:

[1] = [1, 2, 3, 4, 5]
[2, 3] = [6]
[4, 5, 6] = [7, 8]

完整且有效的 Java 代码。它不是正则表达式解决方案,因此可能不适合您的需要。

这个想法是我们为每个文档创建一个数组。数组的大小等于每个文档中的行数。数组的第 n 个元素存储在文档第 n 行之前看到的单词数。然后我们在两个数组中识别那些相等的元素,它们的索引定义了输出的范围。

于 2013-02-01T19:44:03.813 回答
2

我不是 python 程序员,但这看起来不像是可以用正则表达式解决的问题。

相反,您首先要比较文档以确保内容相同(事先暂时删除所有换行符)。如果不是,我不知道你想要做什么,所以我不打算解决这个问题。

创建一个整数集合的集合,称为linemappings

开始一个循环。循环将同时遍历每个文档中的每个字符。您将需要四个计数器变量。charindex1将包含文档 1 中的当前字符索引,charindex2并将包含文档 2中的当前字符索引。lineindex1将包含文档 1 中的当前行索引,lineindex2并将包含文档 2 中的当前行索引。

从 char 索引变量开始为 0,行索引变量初始化为 1。

开始循环:

从每个文档中获取当前字符:char1来自文档 1 和char2来自文档 2。

如果char1ANDchar2是 BOTH 换行符或 NEITHER 都不是换行符,则两者都前进charindex11。charindex2否则
如果char1是换行符,则前进charindex11。
否则如果char2是换行符,则前进charindex21。

如果 EITHERchar1或是char2换行符,则在集合中插入一条新记录linemappings(最后的结果将类似于[[1,1],[1,2],[1,3],[1,4],[1,5],[2,6],[3,6],[4,7],[5,7],[6,7],[6,8]

如果char1是换行符,前进lineindex11。
如果char2是换行符,前进lineindex21。

循环直到输入结束。

(因为我不是 python 程序员,所以我无法真正测试这个,但希望你能理解要点并修改它以满足你的需要。)

于 2013-02-01T19:23:32.560 回答
0

您可以遍历 doc1 的每一行并执行以下操作:

searchstring = line.replace(' ', '[ |\n]')

然后使用此搜索字符串在 doc2 上进行搜索。

match = re.search(searchstring, contents)

如果matchNULL,则不匹配。否则,match.group(0)将为您提供 doc 2 的匹配内容。

'I went\nto Paris\nin July 15,\nwhere I met\nsome nice people.'

然后这是一个简单的练习,通过 '\n' 将其拆分并找出它们来自 doc2 中的哪些行。

于 2013-02-01T18:59:43.117 回答