diff --git a/pom.xml b/pom.xml index fc031e1..95203bc 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ 4.4 31.1-jre 2.13.4.1 - 2.9.0 + 2.9.1 1.2.83 2.0.3.RELEASE 2.5.1 diff --git a/src/main/java/com/apifan/common/random/RandomSource.java b/src/main/java/com/apifan/common/random/RandomSource.java index fbbbbd6..2dd160d 100644 --- a/src/main/java/com/apifan/common/random/RandomSource.java +++ b/src/main/java/com/apifan/common/random/RandomSource.java @@ -82,6 +82,15 @@ public static SportSource sportSource() { return SportSource.getInstance(); } + /** + * 获取语言文字数据源 + * + * @return 语言文字数据源 + */ + public static LanguageSource languageSource() { + return LanguageSource.getInstance(); + } + /** * 获取其它杂项数据源 * diff --git a/src/main/java/com/apifan/common/random/source/LanguageSource.java b/src/main/java/com/apifan/common/random/source/LanguageSource.java new file mode 100644 index 0000000..a36f3a8 --- /dev/null +++ b/src/main/java/com/apifan/common/random/source/LanguageSource.java @@ -0,0 +1,252 @@ +package com.apifan.common.random.source; + +import com.apifan.common.random.entity.Poem; +import com.apifan.common.random.util.JsonUtils; +import com.apifan.common.random.util.ResourceUtils; +import com.google.common.base.Joiner; +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import org.apache.commons.lang3.RandomUtils; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.UnsupportedEncodingException; +import java.util.List; + +/** + * 语言文字数据源 + * + * @author yin + */ +public class LanguageSource { + private static final Logger logger = LoggerFactory.getLogger(LanguageSource.class); + + /** + * 中文名词 + */ + private static List chineseNounsList = Lists.newArrayList(); + + /** + * 中文动词 + */ + private static List chineseVerbsList = Lists.newArrayList(); + + /** + * 中文副词 + */ + private static List chineseAdverbsList = Lists.newArrayList(); + + /** + * 中文代词 + */ + private static List chinesePronounsList = Lists.newArrayList(); + + /** + * 中文连词 + */ + private static List chineseConjunctionsList = Lists.newArrayList(); + + /** + * 中文助词 + */ + private static List chineseParticlesList = Lists.newArrayList(); + + /** + * 废话模板 + */ + private static List nonsenseList = Lists.newArrayList(); + + /** + * 震惊类前缀 + */ + private static List astonishingPrefixList = Lists.newArrayList(); + + /** + * 标题党模板 + */ + private static List sensationalTitlesList = Lists.newArrayList(); + + /** + * 唐诗 + */ + private static List tangPoemsList = Lists.newArrayList(); + + /** + * 四字成语 + */ + private static List chineseIdiomsList = Lists.newArrayList(); + + /** + * 英文常用词语 + */ + private static List englishWordsList = Lists.newArrayList(); + + private static final LanguageSource instance = new LanguageSource(); + + private LanguageSource() { + chineseNounsList = ResourceUtils.base64DecodeLines(ResourceUtils.readLines("common-chinese-nouns.txt")); + chinesePronounsList = ResourceUtils.readLines("common-chinese-pronouns.txt"); + chineseAdverbsList = ResourceUtils.readLines("common-chinese-adverbs.txt"); + chineseVerbsList = ResourceUtils.base64DecodeLines(ResourceUtils.readLines("common-chinese-verbs.txt")); + chineseConjunctionsList = ResourceUtils.readLines("common-chinese-conjunctions.txt"); + chineseParticlesList = ResourceUtils.readLines("common-chinese-particles.txt"); + nonsenseList = ResourceUtils.base64DecodeLines(ResourceUtils.readLines("nonsense.txt")); + sensationalTitlesList = ResourceUtils.base64DecodeLines(ResourceUtils.readLines("sensational-titles.txt")); + astonishingPrefixList = ResourceUtils.base64DecodeLines(ResourceUtils.readLines("astonishing-prefix.txt")); + chineseIdiomsList = ResourceUtils.base64DecodeLines(ResourceUtils.readLines("chinese-idioms.txt")); + englishWordsList = ResourceUtils.readLines("word-en.txt"); + try { + tangPoemsList = JsonUtils.parseObjectList(ResourceUtils.readString("tang-poems.json"), Poem.class); + } catch (Exception e) { + logger.error("初始化数据异常", e); + } + } + + /** + * 获取唯一实例 + * + * @return 实例 + */ + public static LanguageSource getInstance() { + return instance; + } + + /** + * 获取随机的1个汉字 + * + * @return 随机的1个汉字 + */ + public String randomChinese() { + String str = ""; + int highCode = RandomUtils.nextInt(176, 215), lowCode = RandomUtils.nextInt(161, 254); + byte[] b = new byte[2]; + b[0] = (Integer.valueOf(highCode)).byteValue(); + b[1] = (Integer.valueOf(lowCode)).byteValue(); + try { + str = new String(b, "GBK"); + } catch (UnsupportedEncodingException e) { + logger.error("发生编码解析异常", e); + } + return str; + } + + /** + * 获取随机N个汉字 + * + * @param count 数量 + * @return 随机的N个汉字 + */ + public String randomChinese(int count) { + if (count < 1) { + return null; + } + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < count; i++) { + sb.append(randomChinese()); + } + return sb.toString(); + } + + /** + * 随机中文句子 + * + * @return 随机中文句子 + */ + public String randomChineseSentence() { + StringBuilder sb = new StringBuilder(); + int r = RandomUtils.nextInt(1, 11); + if (r % 2 == 0) { + sb.append(ResourceUtils.getRandomElement(chinesePronounsList)); + } + sb.append(ResourceUtils.getRandomElement(chineseNounsList)); + sb.append(ResourceUtils.getRandomElement(chineseAdverbsList)); + sb.append(ResourceUtils.getRandomElement(chineseVerbsList)); + sb.append(ResourceUtils.getRandomElement(chineseNounsList)); + r = RandomUtils.nextInt(1, 11); + if (r % 2 == 0) { + sb.append(ResourceUtils.getRandomElement(chineseNounsList)); + } + r = RandomUtils.nextInt(1, 101); + if (r % 2 == 0) { + r = RandomUtils.nextInt(1, 11); + if (r % 2 == 0) { + sb.append(","); + } else { + sb.append(ResourceUtils.getRandomElement(chineseParticlesList)); + sb.append("?"); + } + r = RandomUtils.nextInt(1, 11); + if (r % 2 == 0) { + sb.append(ResourceUtils.getRandomElement(chineseConjunctionsList)); + } + r = RandomUtils.nextInt(1, 11); + if (r % 3 == 0) { + sb.append(ResourceUtils.getRandomElement(chinesePronounsList)); + } + sb.append(ResourceUtils.getRandomElement(chineseNounsList)); + sb.append(ResourceUtils.getRandomElement(chineseVerbsList)); + sb.append(ResourceUtils.getRandomElement(chineseNounsList)); + } + sb.append("。"); + return sb.toString(); + } + + /** + * 随机营销号文案 + * + * @param subject 主语 + * @param behavior 行为 + * @return 营销号文案(废话) + */ + public String randomNonsense(String subject, String behavior) { + Preconditions.checkArgument(StringUtils.isNotBlank(subject), "主语不能为空"); + Preconditions.checkArgument(StringUtils.isNotBlank(behavior), "行为不能为空"); + String tpl = ResourceUtils.getRandomElement(nonsenseList); + return tpl.replaceAll("A", subject).replaceAll("B", behavior); + } + + /** + * 随机营销号文案标题 + * + * @param subject 主语 + * @param behavior 行为 + * @return 营销号文案标题 + */ + public String randomNonsenseTitle(String subject, String behavior) { + Preconditions.checkArgument(StringUtils.isNotBlank(subject), "主语不能为空"); + Preconditions.checkArgument(StringUtils.isNotBlank(behavior), "行为不能为空"); + String tpl = ResourceUtils.getRandomElement(sensationalTitlesList); + return ResourceUtils.getRandomElement(astonishingPrefixList) + "!" + tpl.replaceAll("A", subject).replaceAll("B", behavior); + } + + /** + * 随机一首唐诗 + * + * @return 唐诗 + */ + public Poem randomTangPoem() { + return ResourceUtils.getRandomElement(tangPoemsList); + } + + /** + * 随机四字成语 + * + * @return 四字成语 + */ + public String randomChineseIdiom() { + return ResourceUtils.getRandomElement(chineseIdiomsList); + } + + /** + * 随机英文文本 + * + * @param words 词语数量 + * @return 随机英文文本 + * @since 1.0.15 + */ + public String randomEnglishText(int words) { + Preconditions.checkArgument(words > 1, "词语数量必须大于1"); + return StringUtils.capitalize(Joiner.on(" ").join(ResourceUtils.getRandomElement(englishWordsList, words))); + } +} diff --git a/src/main/java/com/apifan/common/random/source/OtherSource.java b/src/main/java/com/apifan/common/random/source/OtherSource.java index 1fa48cb..b6ead1c 100644 --- a/src/main/java/com/apifan/common/random/source/OtherSource.java +++ b/src/main/java/com/apifan/common/random/source/OtherSource.java @@ -4,7 +4,6 @@ import com.apifan.common.random.entity.Area; import com.apifan.common.random.entity.EconomicCategory; import com.apifan.common.random.entity.Poem; -import com.apifan.common.random.util.JsonUtils; import com.apifan.common.random.util.ResourceUtils; import com.google.common.base.Joiner; import com.google.common.base.Preconditions; @@ -78,36 +77,6 @@ public class OtherSource { */ private static List departmentList = Lists.newArrayList(); - /** - * 中文名词 - */ - private static List chineseNounsList = Lists.newArrayList(); - - /** - * 中文动词 - */ - private static List chineseVerbsList = Lists.newArrayList(); - - /** - * 中文副词 - */ - private static List chineseAdverbsList = Lists.newArrayList(); - - /** - * 中文代词 - */ - private static List chinesePronounsList = Lists.newArrayList(); - - /** - * 中文连词 - */ - private static List chineseConjunctionsList = Lists.newArrayList(); - - /** - * 中文助词 - */ - private static List chineseParticlesList = Lists.newArrayList(); - /** * 热门手机型号 */ @@ -118,41 +87,11 @@ public class OtherSource { */ private static List ethnicNamesList = Lists.newArrayList(); - /** - * 废话模板 - */ - private static List nonsenseList = Lists.newArrayList(); - - /** - * 震惊类前缀 - */ - private static List astonishingPrefixList = Lists.newArrayList(); - - /** - * 标题党模板 - */ - private static List sensationalTitlesList = Lists.newArrayList(); - /** * 国民经济行业分类列表 */ private static final List economicCategoryList = Lists.newArrayList(); - /** - * 唐诗 - */ - private static List tangPoemsList = Lists.newArrayList(); - - /** - * 四字成语 - */ - private static List chineseIdiomsList = Lists.newArrayList(); - - /** - * 英文常用词语 - */ - private static List englishWordsList = Lists.newArrayList(); - /** * 统一社会信用代码候选字符(不使用I、O、Z、S、V) */ @@ -162,12 +101,6 @@ public class OtherSource { private static final OtherSource instance = new OtherSource(); private OtherSource() { - chineseNounsList = ResourceUtils.base64DecodeLines(ResourceUtils.readLines("common-chinese-nouns.txt")); - chinesePronounsList = ResourceUtils.readLines("common-chinese-pronouns.txt"); - chineseAdverbsList = ResourceUtils.readLines("common-chinese-adverbs.txt"); - chineseVerbsList = ResourceUtils.base64DecodeLines(ResourceUtils.readLines("common-chinese-verbs.txt")); - chineseConjunctionsList = ResourceUtils.readLines("common-chinese-conjunctions.txt"); - chineseParticlesList = ResourceUtils.readLines("common-chinese-particles.txt"); departmentList = ResourceUtils.readLines("common-department.txt"); List economicCategoryLines = ResourceUtils.readLines("national-economic-category.txt"); if (CollectionUtils.isNotEmpty(economicCategoryLines)) { @@ -186,16 +119,6 @@ private OtherSource() { } mobileModelsList = ResourceUtils.base64DecodeLines(ResourceUtils.readLines("mobile-models.txt")); ethnicNamesList = ResourceUtils.base64DecodeLines(ResourceUtils.readLines("ethnic-cn.txt")); - nonsenseList = ResourceUtils.base64DecodeLines(ResourceUtils.readLines("nonsense.txt")); - sensationalTitlesList = ResourceUtils.base64DecodeLines(ResourceUtils.readLines("sensational-titles.txt")); - astonishingPrefixList = ResourceUtils.base64DecodeLines(ResourceUtils.readLines("astonishing-prefix.txt")); - chineseIdiomsList = ResourceUtils.base64DecodeLines(ResourceUtils.readLines("chinese-idioms.txt")); - englishWordsList = ResourceUtils.readLines("word-en.txt"); - try { - tangPoemsList = JsonUtils.parseObjectList(ResourceUtils.readString("tang-poems.json"), Poem.class); - } catch (Exception e) { - logger.error("初始化数据异常", e); - } for (Province province : Province.values()) { if (Province.TW.equals(province) || Province.HK.equals(province) || Province.MO.equals(province)) { continue; @@ -332,47 +255,12 @@ public String randomCompanyDepartment() { } /** - * 随机中文句子 + * 随机中文句子(兼容) * * @return 随机中文句子 */ public String randomChineseSentence() { - StringBuilder sb = new StringBuilder(); - int r = RandomUtils.nextInt(1, 11); - if (r % 2 == 0) { - sb.append(ResourceUtils.getRandomElement(chinesePronounsList)); - } - sb.append(ResourceUtils.getRandomElement(chineseNounsList)); - sb.append(ResourceUtils.getRandomElement(chineseAdverbsList)); - sb.append(ResourceUtils.getRandomElement(chineseVerbsList)); - sb.append(ResourceUtils.getRandomElement(chineseNounsList)); - r = RandomUtils.nextInt(1, 11); - if (r % 2 == 0) { - sb.append(ResourceUtils.getRandomElement(chineseNounsList)); - } - r = RandomUtils.nextInt(1, 101); - if (r % 2 == 0) { - r = RandomUtils.nextInt(1, 11); - if (r % 2 == 0) { - sb.append(","); - } else { - sb.append(ResourceUtils.getRandomElement(chineseParticlesList)); - sb.append("?"); - } - r = RandomUtils.nextInt(1, 11); - if (r % 2 == 0) { - sb.append(ResourceUtils.getRandomElement(chineseConjunctionsList)); - } - r = RandomUtils.nextInt(1, 11); - if (r % 3 == 0) { - sb.append(ResourceUtils.getRandomElement(chinesePronounsList)); - } - sb.append(ResourceUtils.getRandomElement(chineseNounsList)); - sb.append(ResourceUtils.getRandomElement(chineseVerbsList)); - sb.append(ResourceUtils.getRandomElement(chineseNounsList)); - } - sb.append("。"); - return sb.toString(); + return LanguageSource.getInstance().randomChineseSentence(); } /** @@ -394,31 +282,25 @@ public String randomEthnicName() { } /** - * 随机营销号文案 + * 随机营销号文案(兼容) * * @param subject 主语 * @param behavior 行为 * @return 营销号文案(废话) */ public String randomNonsense(String subject, String behavior) { - Preconditions.checkArgument(StringUtils.isNotBlank(subject), "主语不能为空"); - Preconditions.checkArgument(StringUtils.isNotBlank(behavior), "行为不能为空"); - String tpl = ResourceUtils.getRandomElement(nonsenseList); - return tpl.replaceAll("A", subject).replaceAll("B", behavior); + return LanguageSource.getInstance().randomNonsense(subject, behavior); } /** - * 随机营销号文案标题 + * 随机营销号文案标题(兼容) * * @param subject 主语 * @param behavior 行为 * @return 营销号文案标题 */ public String randomNonsenseTitle(String subject, String behavior) { - Preconditions.checkArgument(StringUtils.isNotBlank(subject), "主语不能为空"); - Preconditions.checkArgument(StringUtils.isNotBlank(behavior), "行为不能为空"); - String tpl = ResourceUtils.getRandomElement(sensationalTitlesList); - return ResourceUtils.getRandomElement(astonishingPrefixList) + "!" + tpl.replaceAll("A", subject).replaceAll("B", behavior); + return LanguageSource.getInstance().randomNonsenseTitle(subject, behavior); } /** @@ -509,33 +391,32 @@ public String randomMobileModel() { } /** - * 随机一首唐诗 + * 随机一首唐诗(兼容) * * @return 唐诗 */ public Poem randomTangPoem() { - return ResourceUtils.getRandomElement(tangPoemsList); + return LanguageSource.getInstance().randomTangPoem(); } /** - * 随机四字成语 + * 随机四字成语(兼容) * * @return 四字成语 */ public String randomChineseIdiom() { - return ResourceUtils.getRandomElement(chineseIdiomsList); + return LanguageSource.getInstance().randomChineseIdiom(); } /** - * 随机英文文本 + * 随机英文文本(兼容) * * @param words 词语数量 * @return 随机英文文本 * @since 1.0.15 */ public String randomEnglishText(int words) { - Preconditions.checkArgument(words > 1, "词语数量必须大于1"); - return StringUtils.capitalize(Joiner.on(" ").join(ResourceUtils.getRandomElement(englishWordsList, words))); + return LanguageSource.getInstance().randomEnglishText(words); } /**