java: Generate Chinese names (differentiating men and women)

demand

Some order data needs to be generated for some special reasons. So you need to generate random names.

Realization idea

Save the surname, common characters of boys' names, and common characters of girls' names in the dictionary. The key is a continuous integer starting from 1, and the value is the surname or single character. Cache the data in the dictionary into a HashMap, generate random integers to fetch the data from it, and combine them.

Module organization

Dictionaries: SurnameDictionaries.txt, BoynameDictionaries.txt, GirlnameDictionaries.txt, used to store last names and first names.

File module: FileUtils, used to read the dictionary and generate HashMap.

Main module: NameUtils, used to randomly read the last name, boy's name, and girl's name, and combine the read names.

Achieve results

Here are some examples after implementation:

Girl's name

Boy's name

It feels like a second name. . .

Code

FileUtils

package com.zixuan.utils;

import java.io.File;
import java.io.FileInputStream;
import java.net.URI;
import java.util.HashMap;
import java.util.Scanner;

public class FileUtils {

    //读取字典
    public HashMap<Integer,String> getDict(String dictName){
        HashMap<Integer, String> map = new HashMap<Integer, String>();
        FileInputStream fis = null;
        try {
            URI uri = this.getClass().getClassLoader().getResource(dictName).toURI();
            File file = new File(uri);
            fis = new FileInputStream(file);
            Scanner scanner = new Scanner(fis);
            while (scanner.hasNextLine()) {
                String[] strings = scanner.nextLine().split("=");
                map.put(Integer.valueOf(strings[0]),strings[1]);
            }
            scanner.close();
            fis.close();
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

NameUtils

package com.zixuan.baijiaxing;

import com.zixuan.utils.DictName;
import com.zixuan.utils.FileUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;

public class NameUtils {

    FileUtils fileUtils = new FileUtils();
    HashMap<Integer, String> surnameDict = fileUtils.getDict(DictName.SurnameDict);
    HashMap<Integer, String> boynameDict = fileUtils.getDict(DictName.BoynameDict);
    HashMap<Integer, String> grilnameDict = fileUtils.getDict(DictName.GirlnameDict);
    Random rand = new Random();
    //获取随机姓氏
    public String getSurname(){
        return surnameDict.get(Integer.valueOf(rand.nextInt(surnameDict.size())+1));
    }

    //获取随机名字
    public String getName(String sex){
        //生成名字长度1-2
        int nameLength = rand.nextInt(2) + 1;
        String name = "";
        
        //根据性别从不同的HashMap中随机读取名字
        if (sex.toLowerCase()=="girl"){
            for (int i = 0; i < nameLength; i++) {
                name = name + grilnameDict.get(Integer.valueOf(rand.nextInt(grilnameDict.size())+1));
            }
        }else if (sex.toLowerCase()=="boy"){
            for (int i = 0; i < nameLength; i++) {
                name = name + boynameDict.get(Integer.valueOf(rand.nextInt(boynameDict.size())+1));
            }
        }
        return name;
    }
}

Encapsulate the dictionary file name into a class to avoid writing the wrong file name:

package com.zixuan.utils;

public class DictName {
    static public final String SurnameDict = "SurnameDictionaries.txt";
    static public final String BoynameDict = "BoynameDictionaries.txt";
    static public final String GirlnameDict = "GirlnameDictionaries.txt";
}

Test code

package com.zixuan.kafka.controler;

import com.zixuan.baijiaxing.NameUtils;

import java.util.Random;

public class Test {
    public static void main(String[] args) {
        NameUtils surnameUtils = new NameUtils();
        Random random = new Random();
        String name = null;
        for (int i = 0; i < 200; i++) {
            name = surnameUtils.getSurname()+surnameUtils.getName("gril");
            System.out.println(name);
        }
    }
}

dictionary

SurnameDictionaries.txt

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=郎
49=鲁
50=韦
51=昌
52=马
53=苗
54=凤
55=花
56=方
57=俞
58=任
59=袁
60=柳
61=酆
62=鲍
63=史
64=唐
65=费
66=廉
67=岑
68=薛
69=雷
70=贺
71=倪
72=汤
73=滕
74=殷
75=罗
76=毕
77=郝
78=邬
79=安
80=常
81=乐
82=于
83=时
84=傅
85=皮
86=卡
87=齐
88=康
89=伍
90=余
91=元
92=卜
93=顾
94=孟
95=平
96=黄
97=和
98=穆
99=萧
100=尹

BoynameDictionaries.txt

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=瀚
49=融
50=增
51=纲
52=毅
53=澔
54=意
55=铖
56=迅
57=章
58=彰
59=谦
60=乔
61=振
62=华
63=禅
64=恬
65=澎
66=部
67=瑾
68=钦
69=展
70=采
71=景
72=励
73=彦
74=鹏
75=隆
76=福
77=艺
78=力
79=喆
80=为
81=宜
82=频
83=菁
84=基
85=亿
86=佳
87=茵
88=苓
89=韦
90=澉
91=涛
92=驹
93=震
94=凤
95=万
96=大
97=颔
98=辅
99=睿
100=谱
101=议
102=尧
103=畅
104=襦
105=敬
106=善
107=进
108=梁
109=以
110=哲
111=候
112=真
113=超
114=晋
115=浦
116=来
117=利
118=钢
119=英
120=珑
121=阔
122=娟
123=刚
124=高
125=聚
126=理
127=斌
128=颜
129=弛
130=庆
131=军
132=垒
133=采
134=亦
135=若
136=朗
137=锐
138=森
139=奕
140=艺
141=照
142=淇
143=帝
144=砂
145=兴
146=基
147=歌
148=焘
149=卿
150=亨
151=珺
152=礼
153=吉
154=雄
155=光
156=圣
157=政
158=盈
159=代
160=鑫
161=镇
162=竣
163=迎
164=贵
165=弓
166=骅
167=亮
168=逸
169=澉
170=钧
171=聪
172=筠
173=谊
174=丞
175=颔
176=飒
177=涓
178=波
179=溢
180=榕
181=富
182=炎
183=易
184=韩
185=昭
186=益
187=沧
188=星
189=茹
190=绮
191=劭
192=博
193=臻
194=罡
195=钊
196=义
197=琛
198=滢
199=社
200=征
201=凯
202=寒
203=晟

GirlnameDictionaries.txt

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=环
49=琼
50=玉
51=娈
52=岚
53=晴
54=宛
55=玲
56=瑗
57=婉
58=瑷
59=香
60=语
61=晓
62=珂
63=瑛
64=晓
65=悠
66=沛
67=菱
68=菡
69=珊
70=瑜
71=倩
72=源
73=菡
74=霜
75=巧
76=珍
77=瑶
78=瑞
79=赫
80=珊
81=紫
82=蕙
83=珠
84=瑾
85=静
86=晗
87=曼
88=莲
89=卉
90=婉
91=璇
92=颖
93=贻
94=菱
95=翠
96=靖
97=婕
98=璐
99=韶
100=楚
101=寒
102=烟
103=枫
104=清
105=璟

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/x950913/article/details/107365857