閑來無事到處逛了一下,發現了這樣的一篇文章
https://blog.csdn.net/m0_37609579/article/details/103054993
于是自己照着邏輯寫了一下java版本的,挺簡單的,純屬娛樂,自己弄着玩,沒有其他用途
同時大家可以對比一下python代碼,真的是驗證了那句話,人生苦短,我用python
直接上代碼
package com.consul.consumer;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import org.apache.commons.io.FileUtils;
import org.springframework.util.ResourceUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.*;
public class TestArticle {
public static void main(String[] args) throws IOException {
//讀取json檔案
File jsonFile = ResourceUtils.getFile("classpath:json/data.json");
String s = FileUtils.readFileToString(jsonFile, "utf-8");
JSONObject jsonObject = JSONObject.parseObject(s);
String title = "";
//擷取名人名言a 代表前面墊話,b代表後面墊話
Object famous = jsonObject.get("famous");
List<String> 名人名言 = JSONArray.parseArray(famous + "").toJavaList(String.class);
//擷取前面電話
Object 前面墊話 = jsonObject.get("before");
List<String> 前面墊話S = JSONArray.parseArray(前面墊話 + "").toJavaList(String.class);
Object 後面墊話 = jsonObject.get("after");
List<String> 後面墊話S = JSONArray.parseArray(後面墊話 + "").toJavaList(String.class);
Object 廢話 = jsonObject.get("bosh");
List<String> 廢話S = JSONArray.parseArray(廢話 + "").toJavaList(String.class);
Random random = new Random(100);
// Scanner scanner = new Scanner(System.in);
// title = scanner.next();
title="小明";
String tmp ="";
int i = 0;
List<String> list = new ArrayList<>(廢話S);
while (tmp.length() < 6000) {
int fenzhi = random.nextInt(100);
if (fenzhi<5){
tmp = tmp.substring(0,tmp.length()-2);//解決标點問題
tmp += nextParagraph(tmp);
}else if (fenzhi<20){
tmp += getfamousWords(名人名言,getRandomWords(後面墊話S),getRandomWords(前面墊話S));
}
else {
List<String> next = new ArrayList<>();
if (i<list.size()){
next = next(list, i);
}else {
i=0;
next= list;
}
tmp += next.get(i);
i++;
}
tmp= tmp.replace("x",title);
}
System.out.println(tmp);
String filePath ="E:\\test.txt";
FileWriter fwriter = null;
try {
// true表示不覆寫原來的内容,而是加到檔案的後面。若要覆寫原來的内容,直接省略這個參數就好
fwriter = new FileWriter(filePath);
fwriter.write(tmp);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
fwriter.flush();
fwriter.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
//換行
public static String nextParagraph(String nextParagraph) {
nextParagraph = ". ";
nextParagraph += "\r\n";
nextParagraph += " ";
return nextParagraph;
}
//擷取随機得一句話
public static String getRandomWords(List<String> list) {
Random random = new Random();
int i = random.nextInt(list.size()-1);
return list.get(i);
}
//擷取一句名人名言
public static String getfamousWords(List<String> famous, String beforeWords, String afterWords) {
String randomWords = getRandomWords(famous);
String a = randomWords.replace("a", afterWords);
String b = a.replace("b", beforeWords);
return b;
}
private static List<String> next(List<String> list,Integer num){
boolean remove = list.remove(num);
return list;
}
}
感興趣的朋友可以自己去拿json資料
狗屁不通文章生成器:
https://github.com/menzi11/BullshitGenerator
網頁版:
https://suulnnka.github.io/BullshitGenerator/index.html