真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java代碼詞頻 java語言詞匯

java計算一篇英文文檔詞頻 并按出現(xiàn)次數(shù)從高到低輸出(以下基礎(chǔ)上補(bǔ)充)謝謝!

String result = sb.toString();

成都創(chuàng)新互聯(lián)成立于2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站設(shè)計、網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元新區(qū)做網(wǎng)站,已為上家服務(wù),為新區(qū)各地企業(yè)和個人服務(wù),聯(lián)系電話:18982081108

String[] Str = result.split("[^A-Za-z0-9]"); //quanbu

for(String string:Str){

singleSet.add(string);

if("".equals(string)){ //這里是我加的,去除空格次數(shù)的處理

singleSet.remove("");

}

}

MapString, Integer map=new HashMapString, Integer();

for (String childString : singleSet){

int count=0;

for(String fatherString : Str){

if(fatherString.equals(childString)){

count++;

}

}

map.put(childString, count); //存儲在hashmap中

}

ArrayListEntryString,Integer l = new ArrayListEntryString,Integer(map.entrySet());

Collections.sort(l, new ComparatorObject(){

public int compare(Object e1, Object e2){

int v1 = Integer.parseInt(((EntryString,Integer)e1).getValue().toString());

int v2 = Integer.parseInt(((Entry)e2).getValue().toString());

return v2-v1; //改為v1-v2就是從小到大了

}

});

for (EntryString, Integer e: l){

System.out.println(e.getKey()+" "+e.getValue());

}

代碼僅供參考!希望對你有用

用JAVA語言設(shè)計一個類,統(tǒng)計一篇英文文章的詞頻,并按照詞頻由高到低輸出。修改下面代碼就行了。

這題目如果能增加一個類的話會高效很多。。。如果非要在這個框框里面,代碼麻煩 效率低下呢。

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 {

//保存文章的內(nèi)容

String content;

//保存分割后的單詞集合

String[] rawWords;

//保存統(tǒng)計后的單詞集合

String[] words;

//保存單詞對應(yīng)的詞頻

int[] wordFreqs;

//構(gòu)造函數(shù),輸入文章內(nèi)容

//提高部分:從文件中讀取

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 . ";

}

//對文章根據(jù)分隔符進(jìn)行分詞,將結(jié)果保存到rawWords數(shù)組中

public void splitWord(){

//分詞的時候,因為標(biāo)點符號不參與,所以所有的符號全部替換為空格

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個 //單詞

}

//統(tǒng)計詞,遍歷數(shù)組

public void countWordFreq() {

//將所有出現(xiàn)的字符串放入唯一的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;//統(tǒng)計相同字符串的個數(shù)

for(String str: rawWords){

if(str.equals(word)){

count++;

}

}

wordsList.add(word);

freqList.add(count++);

}

//存入數(shù)組當(dāng)中

words = wordsList.toArray(new String[0]);

wordFreqs = new int[freqList.size()];

for(int i = 0; i freqList.size(); i++){

wordFreqs[i] = freqList.get(i);

}

}

//根據(jù)詞頻,將詞數(shù)組和詞頻數(shù)組進(jìn)行降序排序

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;

}

}

//將排序結(jié)果輸出

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程序:統(tǒng)計單詞詞頻,

不多說,先看代碼:

import java.util.*;

import java.io.*;

public class wordsRate {

public static void main(String[] args) throws Exception {

BufferedReader infile = new BufferedReader(new FileReader("article.txt"));

String string;

String file = null;

while ((string = infile.readLine()) != null) {

file += string;

}

file = file.toLowerCase();

file = file.replaceAll("[^A-Za-z]", " ");

file = file.replaceAll("\\s+", " ");

String words[];

words = file.split("\\s+");

MapString, Integer hashMap = new HashMapString, Integer();

for (int i = 0; i words.length; i++) {

String key = words[i];

if (hashMap.get(key) != null) {

int value = ((Integer) hashMap.get(key)).intValue();

value++;

hashMap.put(key, new Integer(value));

} else {

hashMap.put(key, new Integer(1));

}

}

MapString, Object treeMap = new TreeMapString, Object(hashMap);

MapString, Object treeMap1 = new TreeMapString, Object(hashMap);

BufferedWriter bw = new BufferedWriter(new FileWriter("result.txt"));

//下面是我改動的你的代碼:

Iterator iter = treeMap.entrySet().iterator();

//定義兩個新的數(shù)組ss1和ss2,數(shù)組長度就是hashMap的長度,里面放分別是hashMap的value和key

String ss1[]=new String[treeMap.size()];;

int ss2[]=new int[treeMap.size()];

int i=0;

while (iter.hasNext()) {

Map.Entry entry = (Map.Entry) iter.next();

int val = (Integer)entry.getValue();

String key =(String) entry.getKey();

ss1[i]=key;

ss2[i]=val;

i++;

}

//下面將ss1數(shù)組進(jìn)行排序,并將其與ss2數(shù)組的內(nèi)容相對應(yīng)起來

int sValue=0;

String sKey="";

for(int j=0;jss2.length;j++){

for(int k=0;ki;k++){

if(ss2[j]ss2[k]){

sValue=ss2[j];

sKey=ss1[j];

ss2[j]=ss2[k];

ss1[j]=ss1[k];

ss2[k]=sValue;

ss1[k]=sKey;

}

}

}

for(int j=0;jss2.length;j++){

System.out.println(ss1[j]+"="+ss2[j]);

bw.write(ss1[j]+"="+ss2[j]);

bw.newLine();

bw.flush();

}

}

}

代碼是本人自己寫的,也經(jīng)過了自己的驗證,肯定沒問題,希望采納。

功能實現(xiàn)了,我是將其key和value值放在了數(shù)組之中,然后進(jìn)行排序,將其輸出到了txt文件里

排序方式不一樣,實現(xiàn)的方式也不一樣,所謂仁者見仁智者見智。


標(biāo)題名稱:java代碼詞頻 java語言詞匯
轉(zhuǎn)載注明:http://weahome.cn/article/dddseio.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部