Class: Uniword::Docx::DocumentStatistics

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/docx/document_statistics.rb

Constant Summary collapse

CJK_REGEX =
/[一-鿿㐀-䶿豈-﫿⼀-⿟⺀-⻿]/
WHITESPACE_REGEX =
/[ \t\r\n]/

Instance Method Summary collapse

Constructor Details

#initialize(package) ⇒ DocumentStatistics

Returns a new instance of DocumentStatistics.



9
10
11
# File 'lib/uniword/docx/document_statistics.rb', line 9

def initialize(package)
  @package = package
end

Instance Method Details

#calculateHash{Symbol => Integer}

Returns:

  • (Hash{Symbol => Integer})


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/uniword/docx/document_statistics.rb', line 14

def calculate
  body = @package.document&.body
  return empty_stats unless body

  text_per_paragraph = []
  collect_text(body, text_per_paragraph)
  collect_notes(text_per_paragraph)
  collect_headers_footers(text_per_paragraph)

  non_empty = text_per_paragraph.reject { |t| t.strip.empty? }

  {
    pages: estimate_pages(non_empty.size),
    words: count_words(non_empty),
    characters: count_characters_no_spaces(non_empty),
    characters_with_spaces: count_characters_with_spaces(non_empty),
    paragraphs: non_empty.size,
    lines: estimate_lines(non_empty.size),
  }
end