比较包含上一个和下一个文件中重复“存根”的许多文本文件,并自动删除重复文本


天线宝宝
2025-03-23 01:32:17 (1天前)
  1. 从下一篇文章的开头(到最后)开始)。

我需要删除这些存根以准备运行频率

分析
</跨度>
因为存根构成了 学术上的内容

分析
</跨度>
心理学史上一个项目的旧期刊文章。我不是程序员,但我有10年以上的linux经验,通常可以解决问题

5 条回复
  1. 0# 部落用户 | 2019-08-31 10-32



    你有一个非常重要的问题。编写代码以便在文件1的末尾和文件2的开头找到重复的文本很容易。但是你不想删除重复的文本—-你想要

    分裂

    第二篇文章开始的地方。获得正确的分裂可能是棘手的 - 一个标记是全部大写,另一个标记是

    BY

    在下一行的开头。



    它有助于从连续文件中获取示例,但下面的脚本适用于一个测试用例。

    在尝试此代码之前,请备份所有文件。
    </强>
    代码

    覆写

    现有文件。



    实施是在

    LUA


    该算法大致是:




    1. 忽略文件1末尾的空行和文件2的开头。


    2. 查找文件1末尾和文件2开头常用的长行序列。

      1. <UL>
      2. <LI>
      3. 这可以通过尝试40行,然后是39行,依此类推
      4. </LI>
      5. </UL>
      6. </LI>
      7. <LI>
      8. 从两个文件中删除序列并调用它
      9. <code>
      10. overlap
      11. </code>
      12. </LI>
      13. <LI>
      14. 在标题处拆分重叠
      15. </LI>
      16. <LI>
      17. 将重叠的第一部分附加到file1;将第二部分添加到file2。
      18. </LI>
      19. <LI>
      20. 使用行列表覆盖文件的内容。
      21. </LI>

      </醇>


      这是代码:



      1.   #!/usr/bin/env lua
      2. local ext = arg[1] == ‘-xxx and ‘.xxx or ‘’
        if #ext > 0 then table.remove(arg, 1) end

      3. local function lines(filename)
        local l = { }
        for line in io.lines(filename) do table.insert(l, (line:gsub(‘’, ‘’))) end
        assert(#l > 0, No lines in file .. filename)
        return l
        end

      4. local function write_lines(filename, lines)
        local f = assert(io.open(filename .. ext, w’))
        for i = 1, #lines do
        f:write(lines[i], \n’)
        end
        f:close()
        end

      5. local function lines_match(line1, line2)
        io.stderr:write(string.format(“%q ==? %q\n”, line1, line2))
        return line1 == line2 could do an approximate match here
        end

      6. local function lines_overlap(l1, l2, k)
        if k > #l2 or k > #l1 then return false end
        io.stderr:write(‘* k = ‘, k, \n’)
        for i = 1, k do
        if not lines_match(l2[i], l1[#l1 - k + i]) then
        if i > 1 then
        io.stderr:write(‘After ‘, i-1, matches: FAILED <====\n’)
        end
        return false
        end
        end
        return true
        end

      7. function find_overlaps(fname1, fname2)
        local l1, l2 = lines(fname1), lines(fname2)
        strip trailing and leading blank lines
        while l1[#l1]:find ‘^[%s]$ do table.remove(l1) end
        while l2[1] :find ‘^[%s]
        $ do table.remove(l2, 1) end
        local matchsize # of lines at end of file 1 that are equal to the same
        # at the start of file 2
        for k = math.min(40, #l1, #l2), 1, -1 do
        if lines_overlap(l1, l2, k) then
        matchsize = k
        io.stderr:write(‘Found match of ‘, k, lines\n’)
        break
        end
        end

      8. if matchsize == nil then
        return false failed to find an overlap
        else
        local overlap = { }
        for j = 1, matchsize do
        table.remove(l1) remove line from first set
        table.insert(overlap, table.remove(l2, 1))
        end
        return l1, overlap, l2
        end
        end

      9. local function split_overlap(l)
        for i = 1, #l-1 do
        if l[i]:match ‘%u and not l[i]:match ‘%l then has caps but no lowers
        io.stderr:write(‘Looking for byline following ‘, l[i], \n’)
        if l[i+1]:match ‘^%s*BY%s then
        local first = {}
        for j = 1, i-1 do
        table.insert(first, table.remove(l, 1))
        end
        io.stderr:write(‘Split with first line at ‘, l[1], \n’)
        return first, l
        end
        end
        end
        end

      10. local function strip_overlaps(filename1, filename2)
        local l1, overlap, l2 = find_overlaps(filename1, filename2)
        if not l1 then
        io.stderr:write(‘No overlap in ‘, filename1, an

      11. </code>

  2. 1# 妖邪 | 2019-08-31 10-32



    是标题&amp;作者总是单行?并且该行总是包含大写的单词“BY”吗?如果是这样,你可以做一个公平的工作

    AWK
    </强>
    ,使用这些标准作为开始/结束标记。




    编辑:
    </强>
    我真的不认为使用diff会起作用,因为它是比较广泛相似文件的工具。你的文件(从diff的角度来看)实际上完全不同 - 我认为它会立即失去同步。但是,我不是一个差异大师:-)


  3. 2# v-star*위위 | 2019-08-31 10-32



    快速捅一下,假设两个文件中的存根严格相同:



    1.   #!/usr/bin/perl
    2. use strict;

    3. use List::MoreUtils qw/ indexes all pairwise /;

    4. my @files = @ARGV;

    5. my @previous_text;

    6. for my $filename ( @files ) {
      open my $in_fh, ‘<’, $filename or die;
      open my $out_fh, ‘>’, $filename.’.clean or die;

    7. my @lines = <$in_fh>;
    8. print $out_fh destub( \@previous_text, @lines );
    9. @previous_text = @lines;
    10. }

    11. sub destub {
      my @previous = @{ shift() };
      my @lines = @_;

    12. my @potential_stubs = indexes { $_ eq $lines[0] } @previous;
    13. for my $i ( @potential_stubs ) {
    14.     # check if the two documents overlap for that index
    15.     my @p = @previous[ $i.. $#previous ];
    16.     my @l = @lines[ 0..$#previous-$i ];
    17.     return @lines[ $#previous-$i + 1 .. $#lines ]
    18.             if all { $_ } pairwise { $a eq $b } @p, @l;
    19. }
    20. # no stub detected
    21. return @lines;
    22. }

    23. </code>

  4. 3# 荧惑 | 2019-08-31 10-32



    是存根

    相同

    到上一个文件的末尾?或者不同的行结尾/ OCR错误?



    有没有办法辨别一篇文章的开头?也许是一个缩进的摘要?然后你可以浏览每个文件并丢弃第一个之前和之后(包括)第二个标题的所有内容。


登录 后才能参与评论