在自然语言处理中常遇坎坷,既爱又恨,是想换些花样,捣鼓捣鼓,便偶来奇想,依葫芦画瓢,码码代码,找找手感。搁置C处理,转向R文本挖掘,很好奇诗词歌赋遇到程序代码究竟会摩擦出怎样的火花?
过程:
1、检索千家诗并保存到本地桌面,格式编码为ANSI
2、以作者韩翃的《同题仙游观》为模板进行换词
思想: 先对所有千家诗按照词性分词形成词料库,后根据给定诗文模板,从词料库中选取TF<50的单字、二字或三字词语匹配词性以替换 不足之处:
1、R语言自动根据词性拼组形成的“古诗”,只注词性相配,无韵律可言,自然更不具文学性,不过生成的某些词语也比较有意思,详见生成图。下期考虑结合诗文平仄词韵继续优化代码;
2、分词后排序依据为词频TF大小,分词不尽合理,后期将试用TFIDF(词频*逆文档词频)
#代码作诗的主意并非是我原创。清华大学在2016年就推出过作诗机器人薇薇,宣称可以通过图灵测试#https://zhuanlan.zhihu.com/p/25446637#
代码如下:
fileName <- "C:\\Users\\lenovo\\Desktop\\千家诗.txt"
SC <- readChar(fileName, file.info(fileName)$size)
substr(SC, 1000, 1100)
library(jiebaR)
cc = worker()
analysis <- as.data.frame(table(cc[SC]))
analysis <- analysis[order(-analysis$freq),]
names(analysis) <- c("word","freq")
analysis$word <- as.character(analysis$word)
head(analysis)
library(wordcloud2)
wordcloud2(analysis)
wordcloud2(analysis[analysis$freq>1& analysis$freq < 50 & nchar(analysis$word) == 1,])
wordcloud2(analysis[analysis$freq>1& analysis$freq < 50 & nchar(analysis$word) == 2,])
wordcloud2(analysis[analysis$freq>1& analysis$freq < 50 & nchar(analysis$word) == 3,])
gushi <- "仙台初见五城楼,风物凄凄宿雨收。山色遥连秦树晚,砧声近报汉宫秋。疏松影落空坛静,细草春香小洞幽。何用别寻方外去,人间亦自有丹邱。"
tagger <- worker("tag")
gushi_2 <- tagger <= gushi
gushi_2
example <- subset(analysis, freq >1 & nchar(word) <4 & freq < 50)
cixing <- attributes(gushi_2)$names
example_2 <- tagger <= example$word
write_songci <- function(m){
set.seed(m)
empty <- ""
for (i in 1:length(gushi_2)){
temp_file <- example_2[attributes(example_2)$name == cixing[i]]
temp_file <- temp_file[nchar(temp_file) == nchar(gushi_2[i])]
empty <- paste0(empty, sample(temp_file,1))
}
result <- paste0(substr(empty, 1,7), ",", substr(empty,8,14),"。",
substr(empty, 15,21), ",", substr(empty, 22,28),"。",
substr(empty, 29,35), ",", substr(empty, 36,42),"。",
substr(empty, 43,49), ",", substr(empty, 50,56),"。")
}
lapply(1:6, write_gushi)
[[1]]
[1] "海气扶持日幽州,俸钱寻常朱庆余。世事送春歌古调,歌韵风急宋之问。老大投明镜垣遵,入云峰押元韵圆。对此他何所似覆,居处暗沽酒云金。"
[[2]]
[1] "风色停船日飞上,院落有感花里逢。匈奴梅柳新雨后,秋光古木望明月。繁华生烟霞樽宫,太乙近共沾巾士。何用吾花想容昏,门户甫风吹舟翠。"
[[3]]
[1] "飞上相识两魂魄,山房凄凄何所似。红叶散入押翰韵,齐韵偏惊孟浩然。平明影姓名际翁,折露葵卢梅坡蜂。不曾别归思欲扶,先帝未早知南晓。"
[[4]]
[1] "人烟安得九行宫,斜阳高适押沁韵。幽人蒸韵共沾巾,香雾花迎戴复古。有感生蝼蚁子阙,竹里馆押御韵水。自是任无一字赏,兄弟尚相与美秦。"
[[5]]
[1] "烽火送君余行人,村庄清明夏木啭。小园之四海日生,横北难复黄庭坚。平明辅俸钱身苑,新雨后欲傍衮饮。向晚各剑佩星倒,满地岂至尊丹朱。"
[[6]]
[1] "山路掌中九旧事,小姑自足竹里馆。无情齐韵歌古调,啼时云想戴复古。孤高投归路蛙王,花想容押麻韵照。照眼别江春入停,画屏绝造化鹤灵。"
2018年02月22日 记 -----------------------------------------
望路过网友不吝斧正代码\(≧▽≦)/ 万谢! -----------------------------------------