forked from Devskiller/jfairy
-
Notifications
You must be signed in to change notification settings - Fork 9
/
TextProducerInternal.java
115 lines (88 loc) · 3.4 KB
/
TextProducerInternal.java
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
/*
* Copyright (c) 2013. Codearte
*/
package eu.codearte.jfairy.producer.text;
import static com.google.common.collect.Lists.*;
import static java.util.Arrays.asList;
import static org.apache.commons.lang3.StringUtils.*;
import java.util.List;
import javax.inject.Inject;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import eu.codearte.jfairy.data.DataMaster;
import eu.codearte.jfairy.producer.BaseProducer;
class TextProducerInternal {
private static final String LOREM_IPSUM = "loremIpsum";
private static final String TEXT = "text";
private static final String ALPHABET = "alphabet";
private static final int WORD_COUNT_PRECISION_IN_SENTENCE = 6;
private final BaseProducer baseProducer;
private final String loremIpsum;
private final String text;
private final List<String> words;
private final String alphabet;
private final int maxAlphabetIndex;
private final List<String> latinWords;
@Inject
public TextProducerInternal(DataMaster dataMaster, BaseProducer baseProducer) {
this.baseProducer = baseProducer;
loremIpsum = dataMaster.getString(LOREM_IPSUM);
text = dataMaster.getString(TEXT);
words = asList(split(text, ' '));
latinWords = asList(split(loremIpsum, ' '));
alphabet = dataMaster.getString(ALPHABET);
maxAlphabetIndex = alphabet.length() - 1;
}
public String loremIpsum() {
return loremIpsum;
}
public String rawWords(List<String> words, int count, int precision) {
List<String> result = readRawWords(words, count, precision);
return TextUtils.joinWithSpace(result);
}
public String cleanLatinWords(int count) {
return cleanWords(latinWords, count);
}
public String cleanWords(int count) {
return cleanWords(words, count);
}
private String cleanWords(List<String> words, int count) {
List<String> result = newArrayList();
for (String part : readRawWords(words, count, 0)) {
result.add(uncapitalize(replaceChars(part, "., ", "")));
}
return TextUtils.joinWithSpace(result);
}
public String randomString(int charsCount) {
StringBuilder sb = new StringBuilder(charsCount);
for (int i = 0; i < charsCount; i++) {
sb.append(alphabet.charAt(baseProducer.randomInt(maxAlphabetIndex)));
}
return sb.toString();
}
private List<String> readRawWords(List<String> words, int count, int precision) {
return baseProducer.randomElements(words, baseProducer.randomBetween(count, count + precision));
}
public String text() {
return text;
}
public String sentence(int wordCount) {
return sentence(words, wordCount);
}
public String latinSentence(int wordCount) {
return sentence(latinWords, wordCount);
}
private String sentence(List<String> words, int wordCount) {
String randomWords = rawWords(words, wordCount, WORD_COUNT_PRECISION_IN_SENTENCE);
List<String> sentences = newArrayList();
for (String sentence : Splitter.on(". ").split(randomWords)) {
sentences.add(capitalize(sentence));
}
String sentence = capitalize(Joiner.on(". ").join(sentences));
sentence = removeEnd(sentence, ",");
if (!endsWith(sentence, ".")) {
sentence += ".";
}
return sentence;
}
}