/
TextMessage.scala
96 lines (71 loc) · 2.68 KB
/
TextMessage.scala
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
package ngrams
import java.util.concurrent.ThreadLocalRandom
import scala.collection.mutable.ListBuffer
object TextMessage {
val alphabetSet: Set[Char] = ('a' to 'z').toSet
val alphabets = alphabetSet.toList
val vowelSet: Set[Char] = Set('a', 'e', 'i', 'o', 'u')
val vowels = vowelSet.toList
val consonantSet: Set[Char] = alphabetSet -- vowelSet
val consonants = consonantSet.toList
// Subset of Punct character class """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
val puncts: String = """.,;?!"""
def random = ThreadLocalRandom.current
def randomChar: Char = alphabets(random.nextInt(0, alphabets.length))
def mostlyVowelChar: Char = {
// 4/5th chance of vowel
val isVowel: Boolean = if (random.nextInt(0, 5) > 0) true else false
if (isVowel) vowels(random.nextInt(0, vowels.length)) else consonants(random.nextInt(0, consonants.length))
}
def maybeUpperChar: Char = {
// 1/5th chance of uppercase
val isUppercase: Boolean = if (random.nextInt(0, 5) == 0) true else false
if (isUppercase) Character.toUpperCase(randomChar) else randomChar
}
// Generate a word within a range of lengths
def genRandWord(minLen: Int, maxLen: Int): String = {
var word = new ListBuffer[Char]()
val wordLen: Int = random.nextInt(minLen, maxLen + 1)
for (i <- 1 to wordLen) {
val char = if (i == 1) maybeUpperChar else (if (i % 2 == 0) mostlyVowelChar else randomChar)
word += char
}
word.mkString
}
def genRandText(minWordsInText: Int, maxWordsInText: Int,
minWordLen: Int = 2, maxWordLen: Int = 8,
minWordsInClause: Int = 1, maxWordsInClause: Int = 10
): String = {
var text = new ListBuffer[String]()
val numWordsInText: Int = random.nextInt(minWordsInText, maxWordsInText + 1)
var wordCount: Int = 0
var textLen: Int = 0
while (wordCount < numWordsInText) {
val numWords = random.nextInt(minWordsInClause, maxWordsInClause + 1)
val numWordsInClause = if (numWordsInText - wordCount < numWords) numWordsInText - wordCount else
numWords
var clauseLen: Int = 0
// Generate a clause
for (i <- 1 to numWordsInClause) {
val word: String = genRandWord(minWordLen, maxWordLen)
text += word
clauseLen += word.length
wordCount += 1
if (i < numWordsInClause) {
text += " "
clauseLen += 1
}
}
// Add a punctuation
text += puncts.charAt(random.nextInt(0, puncts.length)).toString
clauseLen += 1
if (wordCount < numWordsInText) {
text += " "
clauseLen += 1
}
textLen += clauseLen
}
// println(s"textLen (in chars): is $textLen")
text.mkString
}
}