利用文本挖掘技术来找出网络中的“小鲜词”
发布时间:2021-01-17 20:43:35 所属栏目:大数据 来源:网络整理
导读:副标题#e# 开始之前,先看一下从人人网中发现的90后用户爱用的词 是不是很好玩,哈哈。写这篇文章就是让你简单的自动的从文本中找出新的词,这样就知道现在的年轻人喜欢什么了(对于博主这种上了年纪的人来说,真的是很有用,呜呜) 项目结构 当然,text.da
|
用来做文本处理,如判断是否为空、匹配字符等 package grid.common;
public class TextUtils {
public static boolean isCnLetter(char c) {
return c >= 0x4E00 && c <= 0x9FCB;
}
public static boolean isNumeric(char c) {
return c >= '0' && c <= '9';
}
public static boolean isEnLetter(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
public static boolean match(String src,int off,String dest) {
int len = dest.length();
int srcLen = src.length();
for (int i = 0; i < len; i++) {
if (srcLen <= off + i) {
return false;
}
if (dest.charAt(i) != src.charAt(off + i)) {
return false;
}
}
return true;
}
public static boolean isBlank(String str) {
return null == str || str.isEmpty() || str.trim().isEmpty();
}
}
Tree.java语法树 package grid.common;
public class Tree<T> extends Node<T> {
public Tree(T value) {
super(value);
}
}
dic里边包含CnDictionary类
CnDictionary.java词典处理 package grid.text.dic;
import grid.common.CountMap;
import grid.common.TextDatReader;
import grid.common.TextUtils;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
public class CnDictionary {
private final String COMMON_WORD_DIC_PATH = "common.dic";
/** * This text data is for character statistic. Change to your own if you * like. */
private final String COMMON_LETTER_RESOURCE_PATH = "text.dat";
private Set<String> dictionary = new HashSet<String>();
private CountMap<Character> letterCountMap = new CountMap<Character>();
private int totalLetterCount;
private static CnDictionary instance;
//单例模式
public static CnDictionary Instance() {
if (null == instance) {
try {
instance = new CnDictionary();
} catch (IOException e) {
e.printStackTrace();
}
}
return instance;
}
private CnDictionary() throws IOException {
initWordDic();
initLetterCountMap();
}
private void initLetterCountMap() throws IOException {
String letterResource = TextDatReader.read(COMMON_LETTER_RESOURCE_PATH);//读取语料数据 text.dat
final int len = letterResource.length();
char c;
for (int i = 0; i < len; i++) {
c = letterResource.charAt(i);
if (TextUtils.isCnLetter(c)) {
letterCountMap.increase(c);
}
}
totalLetterCount = letterCountMap.count();
}
private void initWordDic() throws IOException {
String bytes = TextDatReader.read(COMMON_WORD_DIC_PATH);//读取词典commondic
final int len = bytes.length();
String s = "";
char c;
for (int i = 0; i < len; i++) {
c = bytes.charAt(i);
if ('n' == c || 'r' == c || 0 == c) {
if (!TextUtils.isBlank(s)) {
dictionary.add(s.trim());
}
s = "";
} else {
s += c;
}
if (0 == c) {
break;
}
}
}
public boolean contains(String word) {
return dictionary.contains(word);
}
public double rate(char c) {
return (double) letterCountMap.get(c) / totalLetterCount;
}
public int size() {
return dictionary.size();
}
}
evolution(编辑:PHP编程网 - 金华站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐



