<commit_before>require "docx/cloner/version"
require 'zip/zip'  #rubyzip gem
require 'nokogiri'

module Docx
  module Cloner
    class WordXmlFile
      def self.open(path, &block)
        self.new(path, &block)
      end

      def initialize(path, &block)
        @replace = {}
        if block_given?
          @zip = Zip::ZipFile.open(path)
          yield self
          @zip.close
        else
          @zip = Zip::ZipFile.open(path)
        end
      end

      def merge(rec)
        _xml = @zip.read("word/document.xml")
        doc = Nokogiri::XML(_xml)
        tags = doc.root.xpath("//w:t[contains(., '_Name')]")
        tags.each do |field|
          new_field = field
          if field.content == 'First_Name'
            field.inner_html = 'Adi'
            new_field.inner_html = 'My Adi'
            field.add_next_sibling(new_field.to_html)
          elsif field.content == 'Last_Name'
            field.inner_html = 'Zhou'          
          end
        end
        @replace["word/document.xml"] = doc.serialize :save_with => 0
      end

      def save(path)
        Zip::ZipFile.open(path, Zip::ZipFile::CREATE) do |out|
          @zip.each do |entry|
            out.get_output_stream(entry.name) do |o|
              if @replace[entry.name]
                o.write(@replace[entry.name])
              else
                o.write(@zip.read(entry.name))
              end
            end
          end
        end
        @zip.close
      end
    end

    class DocxReader
      def initialize(file)
        @zip = Zip::ZipFile.open(file)
        @replace = {}
        _xml = @zip.read("word/document.xml")
        doc = Nokogiri::XML(_xml)
        wp_set = doc.xpath(".//w:p")
        puts "#{wp_set.size}'s wp"
        wp_set.each do | p |
          s = ""
          p.xpath(".//w:t").each do | t |
            #s << t.content
            s << t.to_html
          end
          puts s
        end

      end
      def release
        @zip.close
      end

      def set_regx(regx)<commit_msg>将段落存储为哈希表<commit_after>require "docx/cloner/version"
require 'zip/zip'  #rubyzip gem
require 'nokogiri'

module Docx
  module Cloner
    class WordXmlFile
      def self.open(path, &block)
        self.new(path, &block)
      end

      def initialize(path, &block)
        @replace = {}
        if block_given?
          @zip = Zip::ZipFile.open(path)
          yield self
          @zip.close
        else
          @zip = Zip::ZipFile.open(path)
        end
      end

      def merge(rec)
        _xml = @zip.read("word/document.xml")
        doc = Nokogiri::XML(_xml)
        tags = doc.root.xpath("//w:t[contains(., '_Name')]")
        tags.each do |field|
          new_field = field
          if field.content == 'First_Name'
            field.inner_html = 'Adi'
            new_field.inner_html = 'My Adi'
            field.add_next_sibling(new_field.to_html)
          elsif field.content == 'Last_Name'
            field.inner_html = 'Zhou'          
          end
        end
        @replace["word/document.xml"] = doc.serialize :save_with => 0
      end

      def save(path)
        Zip::ZipFile.open(path, Zip::ZipFile::CREATE) do |out|
          @zip.each do |entry|
            out.get_output_stream(entry.name) do |o|
              if @replace[entry.name]
                o.write(@replace[entry.name])
              else
                o.write(@zip.read(entry.name))
              end
            end
          end
        end
        @zip.close
      end
    end

    class DocxReader
      def initialize(file)
        @zip = Zip::ZipFile.open(file)
        @paragraph = []
        @replace = {}
        _xml = @zip.read("word/document.xml")
        doc = Nokogiri::XML(_xml)
        wp_set = doc.xpath(".//w:p")
        puts "#{wp_set.size}'s wp"
        wp_set.each do |p|
          h = {text_content: '', text_run: []}
          p.xpath(".//w:t").each do |t|
            h[:text_content] << t.content
            h[:text_run] << t
          end
          @paragraph << h
        end
        puts @paragraph
      end

      def release
        @zip.close
      end

      def set_regx(regx)
{'fraction_non_alphanumeric': 0.12167392356071602, 'fraction_numerical': 0.0004837929366231253, 'mean_word_length': 1.486470234515935, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3348381', 'n_tokens_mistral': 1362, 'n_tokens_neox': 1326, 'n_words': 270}