1
|
|
|
/* |
2
|
|
|
* Copyright 2014, Armenak Grigoryan, and individual contributors as indicated |
3
|
|
|
* by the @authors tag. See the copyright.txt in the distribution for a |
4
|
|
|
* full listing of individual contributors. |
5
|
|
|
* |
6
|
|
|
* This is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation; either version 2.1 of |
9
|
|
|
* the License, or (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This software is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14
|
|
|
* Lesser General Public License for more details. |
15
|
|
|
*/ |
16
|
|
|
package com.strider.datadefender.anonymizer.functions; |
17
|
|
|
|
18
|
|
|
import com.strider.datadefender.functions.NamedParameter; |
19
|
|
|
import com.strider.datadefender.requirement.registry.RequirementFunction; |
20
|
|
|
|
21
|
|
|
import java.io.InputStreamReader; |
22
|
|
|
import java.io.BufferedReader; |
23
|
|
|
import java.io.IOException; |
24
|
|
|
import java.util.ArrayList; |
25
|
|
|
import java.util.List; |
26
|
|
|
import java.util.Random; |
27
|
|
|
|
28
|
|
|
import org.apache.commons.lang3.StringUtils; |
29
|
|
|
|
30
|
|
|
import lombok.extern.log4j.Log4j2; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Helpers for generating text using Lorem Ipsum... |
34
|
|
|
* |
35
|
|
|
* @author Armenak Grigoryan |
36
|
|
|
*/ |
37
|
|
|
@Log4j2 |
38
|
|
|
public class Lipsum extends RequirementFunction { |
39
|
|
|
|
40
|
|
|
private Random rand = new Random(); |
41
|
|
|
private static final List<String> lipsumParagraphs = new ArrayList<>(); |
42
|
|
|
|
43
|
1 |
|
static { |
44
|
1 |
|
final BufferedReader br = new BufferedReader(new InputStreamReader(Lipsum.class.getResourceAsStream("lipsum.txt"))); |
45
|
1 |
|
final StringBuilder sb = new StringBuilder(); |
46
|
1 |
|
try { |
47
|
1 |
|
for (String line; (line = br.readLine()) != null; ) { |
48
|
598 |
|
if (line.trim().length() == 0) { |
49
|
149 |
|
lipsumParagraphs.add(sb.toString()); |
50
|
149 |
|
sb.setLength(0); |
51
|
149 |
|
continue; |
52
|
|
|
} |
53
|
150 |
|
sb.append(line); |
54
|
|
|
} |
55
|
|
|
} catch (IOException ex) { |
56
|
|
|
log.fatal("Error initializing lipsum.txt", ex); |
57
|
|
|
System.exit(1); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Generates between min and max (inclusive) lorem ipsum sentences. |
63
|
|
|
* |
64
|
|
|
* The sentences are generated from the beginning of paragraphs, although |
65
|
|
|
* the first paragraph chosen is random. Paragraphs are joined together to |
66
|
|
|
* form sentences without line breaks. |
67
|
|
|
* |
68
|
|
|
* @param min Minimum number of sentences to generate |
69
|
|
|
* @param max Maximum number of sentences to generate |
70
|
|
|
* @return the generated sentences. |
71
|
|
|
* @throws IOException if an error occurs reading from the lipsum text file |
72
|
|
|
*/ |
73
|
5 |
|
public String sentences( |
74
|
|
|
@NamedParameter("pattern") int min, |
75
|
|
|
@NamedParameter("pattern") int max |
76
|
|
|
) throws IOException { |
77
|
|
|
|
78
|
5 |
|
final List<String> lp = lipsumParagraphs; |
79
|
5 |
|
final StringBuilder sb = new StringBuilder(); |
80
|
|
|
|
81
|
5 |
|
final int nSentences = max - rand.nextInt((max + 1) - min); |
82
|
5 |
|
String separator = ""; |
83
|
5 |
|
mainLoop: |
84
|
5 |
|
for (int i = 0, start = rand.nextInt(lp.size()); i < nSentences; ++start) { |
85
|
5 |
|
final String para = lp.get(start % lp.size()); |
86
|
5 |
|
final String[] sentences = para.split("\\."); |
87
|
5 |
|
for (String s : sentences) { |
88
|
11 |
|
s = s.trim().replaceAll("\\s+", " "); |
89
|
22 |
|
if (s.isEmpty()) { |
90
|
|
|
sb.append('.'); |
91
|
|
|
continue; |
92
|
|
|
} |
93
|
|
|
|
94
|
11 |
|
sb.append(separator).append(s).append('.'); |
95
|
11 |
|
separator = " "; |
96
|
11 |
|
++i; |
97
|
|
|
|
98
|
22 |
|
if (i == nSentences) { |
99
|
5 |
|
break mainLoop; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
5 |
|
return sb.toString(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Generates the specified number of paragraphs. |
109
|
|
|
* |
110
|
|
|
* The paragraphs are generated from the loaded lorem ipsum text. The start |
111
|
|
|
* position of the text is randomized, however the following paragraphs |
112
|
|
|
* appear in sequence - restarting at the beginning if all paragraphs have |
113
|
|
|
* been used. |
114
|
|
|
* |
115
|
|
|
* @param paragraphs the number of paragraphs to generate |
116
|
|
|
* @return the paragraphs |
117
|
|
|
* @throws IOException if an error occurs reading from the lipsum text file |
118
|
|
|
*/ |
119
|
1 |
|
public String paragraphs(@NamedParameter("paragraphs") int paragraphs) throws IOException { |
120
|
1 |
|
final List<String> lp = lipsumParagraphs; |
121
|
1 |
|
final StringBuilder sb = new StringBuilder(); |
122
|
5 |
|
for (int i = 0, start = rand.nextInt(lp.size()); i < paragraphs; ++i, ++start) { |
123
|
3 |
|
sb.append(lp.get(start % lp.size())).append("\r\n\r\n"); |
124
|
|
|
} |
125
|
1 |
|
return sb.toString().trim(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Generates an amount of text similar to the text passed as a parameter. |
130
|
|
|
* |
131
|
|
|
* The method counts the number of paragraphs in the text, generating the |
132
|
|
|
* same number of "lorem ipsum" paragraphs. If the text doesn't contain |
133
|
|
|
* paragraphs, the method counts the number of sentences and generates a |
134
|
|
|
* similar amount of sentences (+/- 1 sentence). |
135
|
|
|
* |
136
|
|
|
* @param text the text to use as a basis for generation |
137
|
|
|
* @return the generated lorem ipsum text |
138
|
|
|
* @throws IOException if an error occurs reading from the lipsum text file |
139
|
|
|
*/ |
140
|
5 |
|
public String similar(@NamedParameter("text") String text) throws IOException { |
141
|
5 |
|
final String sParas = text.replaceAll("\r\n", "\n"); |
142
|
5 |
|
final int nParas = StringUtils.countMatches(sParas, "\n"); |
143
|
10 |
|
if (nParas > 0) { |
144
|
1 |
|
final StringBuilder sb = new StringBuilder(); |
145
|
1 |
|
for (final String para : sParas.split("\n")) { |
146
|
10 |
|
if (para.trim().isEmpty()) { |
147
|
2 |
|
sb.append("\r\n"); |
148
|
2 |
|
continue; |
149
|
|
|
} |
150
|
3 |
|
sb.append(similar(para)).append("\r\n"); |
151
|
|
|
} |
152
|
1 |
|
return sb.toString().trim(); |
153
|
|
|
} |
154
|
4 |
|
final int nSent = StringUtils.countMatches(text.replaceAll("\\.{2,}", "."), "."); |
155
|
4 |
|
return sentences(Math.max(1, nSent - 1), Math.max(1, nSent + 1)); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|