今天在微博闲逛的时候看见了这个:恶俗古风自动生成器

Interesting! 不过他是用 Ruby 写的 (=゚ω゚)=

所以我就用 Go 写了一个功能一毛一样的

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
package main

import (
"fmt"
"math/rand"
"strings"
"time"
)

var two_chars_words []string = strings.Split("朱砂 天下 杀伐 人家 韶华 风华 繁华 血染 墨染 白衣 素衣 嫁衣 倾城 孤城 空城 旧城 旧人 伊人 心疼 春风 古琴 无情 迷离 奈何 断弦 焚尽 散乱 陌路 乱世 笑靥 浅笑 明眸 轻叹 烟火 一生 三生 浮生 桃花 梨花 落花 烟花 离殇 情殇 爱殇 剑殇 灼伤 仓皇 匆忙 陌上 清商 焚香 墨香 微凉 断肠 痴狂 凄凉 黄梁 未央 成双 无恙 虚妄 凝霜 洛阳 长安 江南 忘川 千年 纸伞 烟雨 回眸 公子 红尘 红颜 红衣 红豆 红线 青丝 青史 青冢 白发 白首 白骨 黄土 黄泉 碧落 紫陌", " ")
var four_chars_words []string = strings.Split("情深缘浅 情深不寿 莫失莫忘 阴阳相隔 如花美眷 似水流年 眉目如画 曲终人散 繁华落尽 不诉离殇 一世长安", " ")
var sentence_model []string = strings.Split("xx,xx,xx了xx。 xxxx,xxxx,不过是一场xxxx。 你说xxxx,我说xxxx,最后不过xxxx。 xx,xx,许我一场xxxx。 一x一x一xx,半x半x半xx。 你说xxxxxxxx,后来xxxxxxxx。 xxxx,xxxx,终不敌xxxx。", " ")

func rand_str(s []string) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return s[r.Intn(len(s))]
}

func replacer(str string, old string, mod []string, sp ...int) string {
for {
if len(sp) != 0 {
str = strings.Replace(str, old, string([]rune(rand_str(mod))[rand.Intn(1)]), 1)
} else {
str = strings.Replace(str, old, rand_str(mod), 1)
}
if !strings.Contains(str, old) {
break
}
}
return str
}

func get_sentence() {
str := rand_str(sentence_model)

str = replacer(str, "xxxx", four_chars_words)
str = replacer(str, "xx", two_chars_words)
str = replacer(str, "x", two_chars_words, 1)
fmt.Println(str)
}

func main() {
tick := time.Tick(time.Second)
for {
get_sentence()
<-tick
}
}

一样也是每秒生产一句古风句子
然而功力不足,代码比起 Ruby 版长好多 ╮(╯▽╰)╭

Python版:

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
from time import sleep
from random import choice
from random import randint

two_chars_words = "朱砂 天下 杀伐 人家 韶华 风华 繁华 血染 墨染 白衣 \
素衣 嫁衣 倾城 孤城 空城 旧城 旧人 伊人 心疼 春风 古琴 无情 迷离 奈何 \
断弦 焚尽 散乱 陌路 乱世 笑靥 浅笑 明眸 轻叹 烟火 一生 三生 浮生 桃花 \
梨花 落花 烟花 离殇 情殇 爱殇 剑殇 灼伤 仓皇 匆忙 陌上 清商 焚香 墨香 \
微凉 断肠 痴狂 凄凉 黄梁 未央 成双 无恙 虚妄 凝霜 洛阳 长安 江南 忘川 \
千年 纸伞 烟雨 回眸 公子 红尘 红颜 红衣 红豆 红线 青丝 青史 青冢 白发 \
白首 白骨 黄土 黄泉 碧落 紫陌".split(" ")

four_chars_words = "情深缘浅 情深不寿 莫失莫忘 阴阳相隔 如花美眷 \
似水流年 眉目如画 曲终人散 繁华落尽 不诉离殇 一世长安".split(" ")

sentence_model = "xx,xx,xx了xx。 xxxx,xxxx,不过是一场xxxx。 \
你说xxxx,我说xxxx,最后不过xxxx。 xx,xx,许我一场xxxx。 \
一x一x一xx,半x半x半xx。 你说xxxxxxxx,后来xxxxxxxx。 \
xxxx,xxxx,终不敌xxxx。".split(" ")


def get_sentence():
model = choice(sentence_model)
while "xxxx" in model:
model = model.replace("xxxx", choice(four_chars_words), 1)
while "xx" in model:
model = model.replace("xx", choice(two_chars_words), 1)
while "x" in model:
model = model.replace(
"x", choice(two_chars_words)[randint(0, 1)])
print(model)

while True:
get_sentence()
sleep(1)