-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathLipsum.java
More file actions
157 lines (142 loc) · 5.81 KB
/
Copy pathLipsum.java
File metadata and controls
157 lines (142 loc) · 5.81 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Copyright 2014, Armenak Grigoryan, and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package com.strider.datadefender.anonymizer.functions;
import com.strider.datadefender.functions.NamedParameter;
import com.strider.datadefender.requirement.registry.RequirementFunction;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.apache.commons.lang3.StringUtils;
import lombok.extern.log4j.Log4j2;
/**
* Helpers for generating text using Lorem Ipsum...
*
* @author Armenak Grigoryan
*/
@Log4j2
public class Lipsum extends RequirementFunction {
private Random rand = new Random();
private static final List<String> lipsumParagraphs = new ArrayList<>();
static {
final BufferedReader br = new BufferedReader(new InputStreamReader(Lipsum.class.getResourceAsStream("lipsum.txt")));
final StringBuilder sb = new StringBuilder();
try {
for (String line; (line = br.readLine()) != null; ) {
if (line.trim().length() == 0) {
lipsumParagraphs.add(sb.toString());
sb.setLength(0);
continue;
}
sb.append(line);
}
} catch (IOException ex) {
log.fatal("Error initializing lipsum.txt", ex);
System.exit(1);
}
}
/**
* Generates between min and max (inclusive) lorem ipsum sentences.
*
* The sentences are generated from the beginning of paragraphs, although
* the first paragraph chosen is random. Paragraphs are joined together to
* form sentences without line breaks.
*
* @param min Minimum number of sentences to generate
* @param max Maximum number of sentences to generate
* @return the generated sentences.
* @throws IOException if an error occurs reading from the lipsum text file
*/
public String sentences(
@NamedParameter("pattern") int min,
@NamedParameter("pattern") int max
) throws IOException {
final List<String> lp = lipsumParagraphs;
final StringBuilder sb = new StringBuilder();
final int nSentences = max - rand.nextInt((max + 1) - min);
String separator = "";
mainLoop:
for (int i = 0, start = rand.nextInt(lp.size()); i < nSentences; ++start) {
final String para = lp.get(start % lp.size());
final String[] sentences = para.split("\\.");
for (String s : sentences) {
s = s.trim().replaceAll("\\s+", " ");
if (s.isEmpty()) {
sb.append('.');
continue;
}
sb.append(separator).append(s).append('.');
separator = " ";
++i;
if (i == nSentences) {
break mainLoop;
}
}
}
return sb.toString();
}
/**
* Generates the specified number of paragraphs.
*
* The paragraphs are generated from the loaded lorem ipsum text. The start
* position of the text is randomized, however the following paragraphs
* appear in sequence - restarting at the beginning if all paragraphs have
* been used.
*
* @param paragraphs the number of paragraphs to generate
* @return the paragraphs
* @throws IOException if an error occurs reading from the lipsum text file
*/
public String paragraphs(@NamedParameter("paragraphs") int paragraphs) throws IOException {
final List<String> lp = lipsumParagraphs;
final StringBuilder sb = new StringBuilder();
for (int i = 0, start = rand.nextInt(lp.size()); i < paragraphs; ++i, ++start) {
sb.append(lp.get(start % lp.size())).append("\r\n\r\n");
}
return sb.toString().trim();
}
/**
* Generates an amount of text similar to the text passed as a parameter.
*
* The method counts the number of paragraphs in the text, generating the
* same number of "lorem ipsum" paragraphs. If the text doesn't contain
* paragraphs, the method counts the number of sentences and generates a
* similar amount of sentences (+/- 1 sentence).
*
* @param text the text to use as a basis for generation
* @return the generated lorem ipsum text
* @throws IOException if an error occurs reading from the lipsum text file
*/
public String similar(@NamedParameter("text") String text) throws IOException {
final String sParas = text.replaceAll("\r\n", "\n");
final int nParas = StringUtils.countMatches(sParas, "\n");
if (nParas > 0) {
final StringBuilder sb = new StringBuilder();
for (final String para : sParas.split("\n")) {
if (para.trim().isEmpty()) {
sb.append("\r\n");
continue;
}
sb.append(similar(para)).append("\r\n");
}
return sb.toString().trim();
}
final int nSent = StringUtils.countMatches(text.replaceAll("\\.{2,}", "."), ".");
return sentences(Math.max(1, nSent - 1), Math.max(1, nSent + 1));
}
}