我想不考虑换行符来比较两个文档。如果内容相同,但换行符的位置和数量不同,我希望将一个文档中的行映射到另一个文档中的行。
给定:
文档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如果正则表达式跨越其他文档中的多行,是否有一种方法可以匹配每个文档中的每一行?
发布于 2013-02-02 03:44:04
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-02 03:23:33
我不是python程序员,但这看起来不像是可以用regex解决的问题。
相反,您首先需要比较文档以确保内容相同(事先临时删除所有换行符)。如果不是,我不知道你想要做什么,所以我不打算解决这个问题。
创建一个名为linemappings的整数集合集合
开始循环。循环将同时遍历每个文档中的每个字符。你需要四个计数器变量。charindex1将包含文档1中的当前字符索引,charindex2将包含文档2中的当前字符索引。lineindex1将包含文档1中的当前行索引,lineindex2将包含文档2中的当前行索引。
首先,将char索引变量初始化为0,行索引变量初始化为1。
启动循环:
从每个文档中获取当前字符:文档1中的char1和文档2中的char2。
如果char1和char2都是换行符或者都不是换行符,则将charindex1和charindex2都前进1。
否则,如果char1是换行符,则将charindex1前进1。
否则,如果char2是换行符,则将charindex2前进1。如果char1或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是换行符,则将lineindex1前进1。
如果char2是换行符,则将lineindex2前进1.循环,直到到达输入末尾。
(因为我不是python程序员,所以我不能真正测试它,但希望您能理解要点,并能根据您的需要对其进行修改。)
发布于 2013-02-02 02:59:43
您可以遍历doc1的每一行并执行以下操作:
searchstring = line.replace(' ', '[ |\n]')
然后使用这个搜索字符串在doc2上进行搜索。
match = re.search(searchstring, contents)
如果match为NULL,则没有匹配项。否则,match.group(0)将为您提供与文档2匹配的内容。
'I went\nto Paris\nin July 15,\nwhere I met\nsome nice people.'
然后,这是一个简单的练习,将其拆分为'\n‘,并找出它们来自doc2中的哪些行。
https://stackoverflow.com/questions/14652826
复制相似问题