62,620
社区成员
发帖
与我相关
我的任务
分享
import java.io.*;
import java.util.*;
public class java2
{
public static void main (String[] args) throws IOException
{
String fileName = "test1.txt";
BufferedReader bufReader = new BufferedReader (new FileReader (fileName));
StreamTokenizer stToken = new StreamTokenizer (bufReader);
Map<String, Integer> mapWords = new HashMap<String, Integer> ();
Set mapSet = null;
Map.Entry[] mapEntries = null;
Integer numWords = null;
int tokenType = 0;
int i;
// 小寫(lower case)模式
stToken.lowerCaseMode (true);
stToken.ordinaryChars (0, 'A' - 1);
stToken.ordinaryChars ('Z' + 1, 'a' - 1);
stToken.ordinaryChars ('z' + 1, 255);
stToken.eolIsSignificant (true);
while (bufReader.ready ())
{
tokenType = stToken.nextToken ();
switch (tokenType)
{
case StreamTokenizer.TT_WORD:
{
numWords = mapWords.get (stToken.sval);
if (numWords == null)
{
numWords = new Integer (1);
}
else
{
numWords++;
}
mapWords.put (stToken.sval, numWords);
}
break;
default:
break;
}
}
mapSet = mapWords.entrySet ();
mapEntries = (Map.Entry[]) mapSet.toArray (new Map.Entry[mapSet.size ()]);
Arrays.sort (mapEntries, new Comparator ()
{
public int compare (Object o1, Object o2)
{
Object v1 = ((Map.Entry)o1).getValue ();
Object v2 = ((Map.Entry)o2).getValue ();
return ((Comparable)v2).compareTo (v1);
}
}
);
// test.txt
BufferedWriter bw = new BufferedWriter
(new FileWriter("test.txt"));
for (i = 0; i < mapEntries.length; i++)
{
bw.write( mapEntries[i].getKey () + " " + mapEntries[i].getValue ()+"\r\n" );
}
bw.close();
}
}
<papers>
<paper>
<title>A corporatecreditratingmodelusingmulti-classsupportvectormachines
with anordinalpairwisepartitioningapproach</title>
<authors>Kyoung-jae Kim a,HyunchulAhn</authors>
<journal>Computers & OperationsResearch</journal>
<year>2012</year>
<vol>39</vol>
<pages>1800-1811</pages>
<abstract>
Predicting corporate credit-rating using statistical and artificial intelligence (AI) techniques has received considerable research attention in the literature. In recent years, multi-class support vector machines (MSVMs) have become a very appealing machine-learning approach due to their good performance. Until now, researchers have proposed a variety of techniques for adapting support vector machines (SVMs) to multi-class classification, since SVMs were originally devised for binary classifica- tion. However, most of them have only focused on classifying samples into nominal categories; thus, the unique characteristic of credit-rating – ordinality – seldom has been considered in the proposed approaches. This study proposes a new type of MSVM classifier (named OMSVM) that is designed to extend the binary SVMs by applying an ordinal pairwise partitioning (OPP) strategy. Our model can efficiently and effectively handle multiple ordinal classes. To validate OMSVM, we applied it to a real-world case of bond rating. We compared the results of our model with those of conventional MSVM approaches and other AI techniques including MDA, MLOGIT, CBR, and ANNs. The results showed that our proposed model improves the performance of classification in comparison to other typical multi-class classification techniques and uses fewer computational resources.
</abstract>
<keywords>
Corporate credit rating Support vector machines Multi-class classification Ordinal pairwise partitioning
</keywords>
<content>
package readfile;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class WordsCount {
HashMap<String,Integer> hashMap;
BufferedReader infile;
String filename = "src/readfile/test1.txt";
String string;
String outpath = "src/readfile/test.txt";
@SuppressWarnings("unchecked")
public WordsCount() throws IOException{
infile = new BufferedReader(new FileReader(filename));
hashMap=new HashMap<String,Integer>();
while((string = infile.readLine()) !=null) {
String[] words=string.split(" ");
for(int i=0;i<words.length;i++){
if(words[i].trim().equals("")){
continue;
}
String astr=words[i].trim();
if(astr.endsWith(".")||astr.endsWith(",")){
astr=astr.substring( 0,astr.length());
}
if(hashMap.containsKey(words[i])){
Integer count=(Integer) hashMap.get(words[i]);
count++;
hashMap.remove(astr);
hashMap.put(astr, count);
}else{
hashMap.put(astr, 1);
}
}
}
infile.close();
List<String> arrayList=new ArrayList<String>();
Iterator<?> iter = hashMap.entrySet().iterator();
outer:while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String key = (String)entry.getKey();
char aChar=key.charAt(0);
for(int i=0;i<arrayList.size();i++){
if(aChar<arrayList.get(i).charAt(0)){
arrayList.add(i,key);
continue outer;
}
}
arrayList.add(key);
}
StringBuffer outContent=new StringBuffer();
for(int i=0;i<arrayList.size();i++){
String key=arrayList.get(i);
outContent.append(key+hashMap.get(key)+" 次"+"\r\n");
System.out.println(key+hashMap.get(key));
}
FileOutputStream outs=new FileOutputStream(new File(outpath));
outs.write(outContent.toString().getBytes());
outs.flush();
outs.close();
}
public static void main(String[] args){
try {
new WordsCount();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

