-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsentence_segmenter.rb
More file actions
executable file
·78 lines (63 loc) · 2.48 KB
/
Copy pathsentence_segmenter.rb
File metadata and controls
executable file
·78 lines (63 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# frozen_string_literal: true
require 'nokogiri'
require 'punkt-segmenter'
# Sentence segmenter -- splits <p> elements into something like
# <div class="real-paragraph">
# <p class="temp-sentence"> ... </p>
# <p class="temp-sentence"> ... </p>
# </div>
# The mangle and unmangle methods expect HTML fragment files, output by the
# build process using the alternate fragment template. They transform the
# content and update it in-place, destroying the previous content of the files.
module PuppetDocs
module SentenceSegmenter
def self.segment_on_sentences(text)
parsed = Nokogiri::HTML::DocumentFragment.parse(text)
tokenizer = Punkt::SentenceTokenizer.new(text)
all_paragraphs = parsed.css('p')
all_paragraphs.each do |graf|
sentences = tokenizer.sentences_from_text(graf.inner_html, output: :sentences_text)
new_div = '<div class="real-paragraph"> <p class="temp-sentence">' << sentences.join('</p> <p class="temp-sentence">') << '</p></div>'
graf.replace(new_div)
end
parsed.to_html
end
def self.mangle_file(filename)
full_path = File.expand_path(filename)
print "Mangling #{full_path}... "
mangled_html = segment_on_sentences(File.read(full_path, encoding: 'utf-8'))
File.write(full_path, mangled_html)
print " done.\n"
end
def self.unsegment_paragraphs(text)
parsed = Nokogiri::HTML::DocumentFragment.parse(text)
all_sentences = parsed.css('p.temp-sentence')
all_real_paragraphs = parsed.css('div.real-paragraph')
# break internal bubbles
all_sentences.each do |sentence|
sentence.replace(sentence.inner_html)
end
# transform into paragraph
all_real_paragraphs.each do |graf|
graf.replace('<p>' << graf.inner_html << '</p>')
end
# build yaml frontmatter by subbing in title from div.title
title_div = parsed.at_css('div.title')
if title_div
title = title_div.content
frontmatter = %(---\ntitle: "#{title}"\n---\n\n)
title_div.remove
else # at_css can return nil if there's no title div.
frontmatter = %{---\ntitle: "(no title)"\n---\n\n}
end
frontmatter + parsed.to_html
end
def self.unmangle_file(filename)
full_path = File.expand_path(filename)
print "Un-mangling #{full_path}... "
fixed_html = unsegment_paragraphs(File.read(full_path, encoding: 'utf-8'))
File.write(full_path, fixed_html)
print " done.\n"
end
end
end