Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.2')
- import groovyx.net.http.*
- def http = new HTTPBuilder('https://2ch.hk/dev/res/1746652.html')
- def html = http.get([:])
- def posts = html.'**'.findAll { it.name() == 'BLOCKQUOTE' && it.@class == 'post-message' }*.toString()
- def text = posts*.replaceAll(/[^-а-яА-Я,.!: ]+/, ' ').join(' ').replaceAll(/\s+/, ' ')
- def generateMarkovChain(String text) {
- def trigramms = text.toList().collate(3, 1, false)*.join('')
- return trigramms.groupBy { it[0..1] }.collectEntries { [it.key, it.value.countBy {it[2]} ] }
- }
- String randomChar(text) {
- return text[RNG.nextInt(text.size())]
- }
- String randomLetter() {
- return randomChar([*'а'..'я', 'ё'])
- }
- String generateStart() {
- return randomLetter().toUpperCase() + randomLetter()
- }
- def nextChar(String start, chain, String text) {
- if (!(start in chain)) return randomChar(text)
- def weights = chain[start]
- int total = weights.values().sum()
- int pick = RNG.nextInt(total)
- return weights.entrySet().find { (pick -= it.value) < 0 }.key
- }
- def chain = generateMarkovChain(text)
- RNG = new Random()
- def start = generateStart()
- print start
- 998.times {
- def next = nextChar(start, chain, text)
- print next
- start = start[1] + next
- }
Advertisement
Add Comment
Please, Sign In to add comment