天天看点

垃圾文章生成器 java版

闲来无事到处逛了一下,发现了这样的一篇文章

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