「java词频统计」汉语词频统计
今天给各位分享java词频统计的知识,其中也会对汉语词频统计进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java读取解析大文件 40G左右求出name重复次top10的信息并输出求教高手,怕以后遇到类似问题
- 2、java词频统计
- 3、词频统计案例中,map和reduce输入的数据类型是什么?
- 4、用JAVA语言设计一个类,统计一篇英文文章的词频,并按照词频由高到低输出。修改下面代码就行了。
- 5、java 如何统计txt文本中的总词数 不是总字数呀 TF–IDF 公式中需要用到
java读取解析大文件 40G左右求出name重复次top10的信息并输出求教高手,怕以后遇到类似问题
1、首先大文件统计词频如果不依赖第三方组件的话实现起来很麻烦没有相关经验的话很容易出问题
2、若要使用java原生库的话建议使用多线程构建一个MapReduce模型,多线程逐行或者按块读将各自任务下的词频统计到DB
3、若使用第三方的话建议使用solr或者flink这种高度封装的组件既可以保证结果的正确性也可以保证性能
java词频统计
在Java里面一个File既可以代表一个文件也可以代表一个目录(就是你所说的文件夹). 因此你可以直接把一个文件夹的path传进去new File(path), 然后再用list()就可以获得该文件夹下的所有文件数组, 再一个个的输入File流就行了, 可以这样写:
public void directory() {
File dir = new File("E:\temp");
File[] files = dir.listFiles();
}
词频统计案例中,map和reduce输入的数据类型是什么?
map和reduce输入的数据类型是java代码。
类似,LongWritable ~ Long,Text ~ String,IntWritable ~ Integer。
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。
Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。
Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。
发展历史:
随着IT产业的发展,许多企业中的计算机应用程序也在随之转型,企业中所应用的计算机模式逐渐向客户端较小、服务器随之增大、数据库的容量也在相应增加的方向转变。
此外,加上Java语言独有的特点,根据其在各项服务器中应用程序的开发所占有的优势,企业版的J2SE为企业环境中计算机模式的应用提供了良好的平台。
用JAVA语言设计一个类,统计一篇英文文章的词频,并按照词频由高到低输出。修改下面代码就行了。
这题目如果能增加一个类的话会高效很多。。。如果非要在这个框框里面,代码麻烦 效率低下呢。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class Article {
//保存文章的内容
String content;
//保存分割后的单词集合
String[] rawWords;
//保存统计后的单词集合
String[] words;
//保存单词对应的词频
int[] wordFreqs;
//构造函数,输入文章内容
//提高部分:从文件中读取
public Article() {
content = "kolya is one of the richest films i've seen in some time . zdenek sverak plays a confirmed old bachelor ( who's likely to remain so ) , who finds his life as a czech cellist increasingly impacted by the five-year old boy that he's taking care of . though it ends rather abruptly-- and i'm whining , 'cause i wanted to spend more time with these characters-- the acting , writing , and production values are as high as , if not higher than , comparable american dramas . this father-and-son delight-- sverak also wrote the script , while his son , jan , directed-- won a golden globe for best foreign language film and , a couple days after i saw it , walked away an oscar . in czech and russian , with english subtitles . ";
}
//对文章根据分隔符进行分词,将结果保存到rawWords数组中
public void splitWord(){
//分词的时候,因为标点符号不参与,所以所有的符号全部替换为空格
final char SPACE = ' ';
content = content.replace('\'', SPACE).replace(',', SPACE).replace('.', SPACE);
content = content.replace('(', SPACE).replace(')', SPACE).replace('-', SPACE);
rawWords = content.split("\\s+");//凡是空格隔开的都算单词,上面替换了', 所以I've 被分成2个 //单词
}
//统计词,遍历数组
public void countWordFreq() {
//将所有出现的字符串放入唯一的set中,不用map,是因为map寻找效率太低了
SetString set = new TreeSetString();
for(String word: rawWords){
set.add(word);
}
Iterator ite = set.iterator();
ListString wordsList = new ArrayListString();
ListInteger freqList = new ArrayListInteger();
//多少个字符串未知,所以用list来保存先
while(ite.hasNext()){
String word = (String) ite.next();
int count = 0;//统计相同字符串的个数
for(String str: rawWords){
if(str.equals(word)){
count++;
}
}
wordsList.add(word);
freqList.add(count++);
}
//存入数组当中
words = wordsList.toArray(new String[0]);
wordFreqs = new int[freqList.size()];
for(int i = 0; i freqList.size(); i++){
wordFreqs[i] = freqList.get(i);
}
}
//根据词频,将词数组和词频数组进行降序排序
public void sort() {
class Word{
private String word;
private int freq;
public Word(String word, int freq){
this.word = word;
this.freq = freq;
}
}
//注意:此处排序,1)首先按照词频降序排列, 2)如果词频相同,按照字母降序排列,
//如 'abc' 'ab' 'aa'
class WordComparator implements Comparator{
public int compare(Object o1, Object o2) {
Word word1 = (Word) o1;
Word word2 = (Word) o2;
if(word1.freq word2.freq){
return 1;
}else if(word1.freq word2.freq){
return -1;
}else{
int len1 = word1.word.trim().length();
int len2 = word2.word.trim().length();
String min = len1 len2? word2.word: word1.word;
String max = len1 len2? word1.word: word2.word;
for(int i = 0; i min.length(); i++){
if(min.charAt(i) max.charAt(i)){
return 1;
}
}
return 1;
}
}
}
List wordList = new ArrayListWord();
for(int i = 0; i words.length; i++){
wordList.add(new Word(words[i], wordFreqs[i]));
}
Collections.sort(wordList, new WordComparator());
for(int i = 0; i wordList.size(); i++){
Word wor = (Word) wordList.get(i);
words[i] = wor.word;
wordFreqs[i] = wor.freq;
}
}
//将排序结果输出
public void printResult() {
System.out.println("Total " + words.length + " different words in the content!");
for(int i = 0; i words.length; i++){
System.out.println(wordFreqs[i] + " " + words[i]);
}
}
//测试类的功能
public static void main(String[] args) {
Article a = new Article();
a.splitWord();
a.countWordFreq();
a.sort();
a.printResult();
}
}
-----------------------
Total 99 different words in the content!
5 and
4 the
4 i
4 a
3 as
2 with
2 who
2 to
2 time
2 sverak
2 son
2 s
2 old
2 of
2 it
2 in
2 his
2 czech
1 zdenek
1 year
1 wrote
1 writing
1 won
1 whining
1 while
1 wanted
1 walked
1 ve
1 values
1 though
1 this
1 these
1 that
1 than
1 taking
1 subtitles
1 spend
1 some
1 so
1 seen
1 script
1 saw
1 russian
1 richest
1 remain
1 rather
1 production
1 plays
1 oscar
1 one
1 not
1 more
1 m
1 likely
1 life
1 language
1 kolya
1 jan
1 is
1 increasingly
1 impacted
1 if
1 higher
1 high
1 he
1 golden
1 globe
1 foreign
1 for
1 five
1 finds
1 films
1 film
1 father
1 english
1 ends
1 dramas
1 directed
1 delight
1 days
1 couple
1 confirmed
1 comparable
1 characters
1 cellist
1 cause
1 care
1 by
1 boy
1 best
1 bachelor
1 away
1 are
1 an
1 american
1 also
1 after
1 acting
1 abruptly
java 如何统计txt文本中的总词数 不是总字数呀 TF–IDF 公式中需要用到
词频(TF)=某个词在文章中出现的次数
词频(TF)=某个词在文章中出现的次数/文章的总词数
或者:
词频(TF)=某个词在文章中出现的次数/该文出现次数最多的词的出现次数
逆文档率:
TF-IDF
:
TF-IDF=词频(TF)*逆文档率(IDF)
TF-IDF与一个词在文档中的出现次数成正比,与该词在整个语言中的出现次数成反比。
java词频统计的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于汉语词频统计、java词频统计的信息别忘了在本站进行查找喔。