简体   繁体   English

按计数编号排序,不区分大小写

[英]Sorting by count number and case-insensitive

I'm having troubles with the code not ignoring case. 我在不忽略大小写的情况下遇到了麻烦。 Here's what I need to do 这是我需要做的

problem 问题

processInput() should open and parse the input file. processInput()应该打开并解析输入文件。 It should throw any potential exceptions and allow the using class to handle them. 它应该抛出任何潜在的异常,并允许using类处理它们。

generateStatsFile() should produce the XML file described below. generateStatsFile()应生成下面描述的XML文件。 It should also throw any potential exceptions and allow the using class to handle them. 它还应该抛出任何潜在的异常,并允许using类处理它们。 You can use PrintWriter to create your XML File, but you should research better options. 您可以使用PrintWriter创建XML文件,但是应该研究更好的选择。 See the code sample attached to this lab. 请参阅此实验室附带的代码示例。

TEXT FILE ANALYZER 文本文件分析仪

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class TextFileAnalyzer {

private File inputFile;
private File statsXMLFile;
private ArrayList<Word> words;

public static void main(String[] args) {

    HashMap<String, Integer> map = new HashMap<String, Integer>();

    TextFileAnalyzer oneTime = new TextFileAnalyzer();
    oneTime.processInput(map);
}

public void processInput(HashMap<String, Integer> map) {

    System.out.print("Enter file name: ");
    Scanner keyboard = new Scanner(System.in);
    String fileName = keyboard.next();

    try {
        keyboard = new Scanner(new File(fileName)).useDelimiter("[^a-zA-Z]+");

    }

    catch (FileNotFoundException fnf) {

        System.out.println("The file: '" + fileName + "' does not exist!");
        System.exit(0);
    }

    // has it print line by line
    while (keyboard.hasNext()) {`enter code here`

        String word = keyboard.next();
        if (map.containsKey(word)) {
            map.put(word, map.get(word) + 1);
        } else {
            map.put(word, 1);
        }
    }

    for (Map.Entry<String, Integer> entry : map.entrySet()) {

        System.out.println(entry);
    }

    keyboard.close();

}

public void generateStatsFile(HashMap<String, Integer> map) {
         //SOMECODE TO EXPORT TO XML
}

}

============== ==============

WORD

import java.io.*;
public class Word implements Comparable {

private String word;
private int usageCnt;

public Word() {
    this("??", 0);
}

public Word(String word, int usageCnt) {

}

public void reset(String word, int usageCnt) {
    this.setWord(word);
    this.usageCnt = usageCnt;
}

// ACCESSORS
public String getWord() {
    return word;
}

public void setWord(String word) {
    this.word = word;
}

public int getUsageCnt() {
    return usageCnt;
}

public void setUsageCnt(int usageCnt) {
    this.usageCnt = usageCnt;
}

// COMPARABLE
@Override
public int compareTo(Object o) {
    return usageCnt;

}

} }

ORIGINAL TEXT # 原始文本

Green Eggs and Ham
By Dr. Seuss

I am Sam

Sam I am

That Sam-I-am!
That Sam-I-am!
I do not like
that Sam-I-am!

Do you like green eggs and ham?

I do not like them, Sam-I-am.
I do not like green eggs and ham.

Would you like them here or there?

I would not like them here or there.
I would not like them anywhere.
I do not like green eggs and ham.
I do not like them, Sam-I-am.

Would you like them in a house?
Would you like them with a mouse?

I do not like them in a house.
I do not like them with a mouse.
I do not like them here or there.
I do not like them anywhere.
I do not like green eggs and ham.
I do not like them, Sam-I-am.

Would you eat them in a box?
Would you eat them with a fox?

Not in a box.
Not with a fox.
Not in a house.
Not with a mouse.
I would not eat them here or there.
I would not eat them anywhere.
I would not eat green eggs and ham.
I do not like them, Sam-I-am.

Would you?  Could you?
In a car?  
Eat them!  Eat them!
Here they are.

I would not, could not, in a car.

You may like them.
You will see.
You may like them in a tree!

I would not, could not in a tree.
Not in a car!  You let me be.
I do not like them in a box.
I do not like them with a fox.
I do not like them in a house.
I do not like them with a mouse.
I do not like them here or there.
I do not like them anywhere.
I do not like green eggs and ham.
I do not like them, Sam-I-am.

A train!  A train!
A train!  A train!
Could you, would you, on a train?

Not on a train!  Not in a tree!
Not in a car!  Sam!  Let me be!
I would not, could not, in a box.
I could not, would not, with a fox.
I will not eat them with a mouse.
I will not eat them in a house.
I will not eat them here or there.
I will not eat them anywhere.
I do not eat green eggs and ham.
I do not like them, Sam-I-am.

Say!
In the dark?  Here in the dark!
Would you, could you, in the dark?

I would not, could not, in the dark.

Would you, could you, in the rain?

I would not, could not, in the rain.
Not in the dark.  Not on a train.
Not in a car.  Not in a tree.
I do not like them, Sam, you see.
Not in a house.  Not in a box.
Not with a mouse.  Not with a fox.
I will not eat them here or there.
I do not like them anywhere!

You do not like green eggs and ham?

I do not like them, Sam-I-am.

Could you, would you, with a goat?

I would not, could not, with a goat!

Would you, could you, on a boat?

I could not, would not, on a boat.
I will not, will not, with a goat.
I will not eat them in the rain.
I will not eat them on a train.
Not in the dark!  Not in a tree!
Not in a car!  You let me be!
I do not like them in a box.
I do not like them with a fox.
I will not eat them in a house.
I do not like them with a mouse.
I do not like them here or there.
I do not like them ANYWHERE!
I do not like green eggs and ham!
I do not like them, Sam-I-am.

You do not like them.
So you say.
Try them!  Try them!
And you may.
Try them and you may, I say.

Sam!
If you will let me be, I will try them.
You will see.

<at which point in our story, the heretofore tormented hero cautiously 
 takes a bite of the "green eggs and ham">

Say!
I like green eggs and ham!
I do!  I like them, Sam-I-am!
And I would eat them in a boat.
And I would eat them with a goat...
And I will eat them in the rain.
And in the dark.  And on a train.
And in a car.  And in a tree.
They are so good, so good, you see!
So I will eat them in a box.
And I will eat them with a fox.
And I will eat them in a house.
And I will eat them with a mouse.
And I will eat them here and there.
Say!  I will eat them ANYWHERE!

I do so like
green eggs and ham!
Thank you!
Thank you,
Sam-I-am! 

CORRECT OUTPUT 正确的输出

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<TEXT_ANALYZER>
  <WORD>
    <WORD_TEXT>not</WORD_TEXT>
    <USAGE_CNT>84</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>i</WORD_TEXT>
    <USAGE_CNT>71</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>them</WORD_TEXT>
    <USAGE_CNT>61</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>a</WORD_TEXT>
    <USAGE_CNT>60</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>like</WORD_TEXT>
    <USAGE_CNT>44</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>in</WORD_TEXT>
    <USAGE_CNT>41</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>do</WORD_TEXT>
    <USAGE_CNT>37</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>you</WORD_TEXT>
    <USAGE_CNT>34</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>and</WORD_TEXT>
    <USAGE_CNT>27</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>would</WORD_TEXT>
    <USAGE_CNT>26</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>eat</WORD_TEXT>
    <USAGE_CNT>25</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>will</WORD_TEXT>
    <USAGE_CNT>21</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>with</WORD_TEXT>
    <USAGE_CNT>19</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>could</WORD_TEXT>
    <USAGE_CNT>14</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>the</WORD_TEXT>
    <USAGE_CNT>13</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>sam-i-am</WORD_TEXT>
    <USAGE_CNT>13</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>ham</WORD_TEXT>
    <USAGE_CNT>13</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>eggs</WORD_TEXT>
    <USAGE_CNT>13</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>green</WORD_TEXT>
    <USAGE_CNT>12</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>here</WORD_TEXT>
    <USAGE_CNT>11</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>train</WORD_TEXT>
    <USAGE_CNT>9</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>there</WORD_TEXT>
    <USAGE_CNT>9</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>or</WORD_TEXT>
    <USAGE_CNT>8</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>mouse</WORD_TEXT>
    <USAGE_CNT>8</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>house</WORD_TEXT>
    <USAGE_CNT>8</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>anywhere</WORD_TEXT>
    <USAGE_CNT>8</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>on</WORD_TEXT>
    <USAGE_CNT>7</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>fox</WORD_TEXT>
    <USAGE_CNT>7</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>dark</WORD_TEXT>
    <USAGE_CNT>7</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>car</WORD_TEXT>
    <USAGE_CNT>7</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>box</WORD_TEXT>
    <USAGE_CNT>7</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>tree</WORD_TEXT>
    <USAGE_CNT>6</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>so</WORD_TEXT>
    <USAGE_CNT>5</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>say</WORD_TEXT>
    <USAGE_CNT>5</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>sam</WORD_TEXT>
    <USAGE_CNT>5</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>try</WORD_TEXT>
    <USAGE_CNT>4</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>see</WORD_TEXT>
    <USAGE_CNT>4</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>rain</WORD_TEXT>
    <USAGE_CNT>4</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>me</WORD_TEXT>
    <USAGE_CNT>4</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>may</WORD_TEXT>
    <USAGE_CNT>4</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>let</WORD_TEXT>
    <USAGE_CNT>4</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>goat</WORD_TEXT>
    <USAGE_CNT>4</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>be</WORD_TEXT>
    <USAGE_CNT>4</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>that</WORD_TEXT>
    <USAGE_CNT>3</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>boat</WORD_TEXT>
    <USAGE_CNT>3</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>they</WORD_TEXT>
    <USAGE_CNT>2</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>thank</WORD_TEXT>
    <USAGE_CNT>2</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>good</WORD_TEXT>
    <USAGE_CNT>2</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>are</WORD_TEXT>
    <USAGE_CNT>2</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>am</WORD_TEXT>
    <USAGE_CNT>2</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>which</WORD_TEXT>
    <USAGE_CNT>1</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>tormented</WORD_TEXT>
    <USAGE_CNT>1</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>takes</WORD_TEXT>
    <USAGE_CNT>1</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>story</WORD_TEXT>
    <USAGE_CNT>1</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>seuss</WORD_TEXT>
    <USAGE_CNT>1</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>point</WORD_TEXT>
    <USAGE_CNT>1</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>our</WORD_TEXT>
    <USAGE_CNT>1</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>of</WORD_TEXT>
    <USAGE_CNT>1</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>if</WORD_TEXT>
    <USAGE_CNT>1</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>hero</WORD_TEXT>
    <USAGE_CNT>1</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>heretofore</WORD_TEXT>
    <USAGE_CNT>1</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>dr</WORD_TEXT>
    <USAGE_CNT>1</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>cautiously</WORD_TEXT>
    <USAGE_CNT>1</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>by</WORD_TEXT>
    <USAGE_CNT>1</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>bite</WORD_TEXT>
    <USAGE_CNT>1</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>&lt;at</WORD_TEXT>
    <USAGE_CNT>1</USAGE_CNT>
  </WORD>
  <WORD>
    <WORD_TEXT>"green</WORD_TEXT>
    <USAGE_CNT>1</USAGE_CNT>
  </WORD>
</TEXT_ANALYZER>

WHAT IS DISPLAYING 正在显示什么

Enter file name: green_eggs_and_ham.txt
heretofore=1
do=36
anywhere=6
good=2
Eggs=1
that=1
mouse=8
would=17
ham=12
me=4
let=3
Try=3
Thank=2
you=26
they=1
train=9
cautiously=1
which=1
eggs=12
rain=4
Seuss=1
like=44
in=39
tree=6
them=61
Do=1
goat=4
am=15
Dr=1
Not=19
Here=2
at=1
And=12
Eat=2
try=1
If=1
Sam=18
here=9
A=4
be=4
Could=3
In=2
I=84
box=7
Say=3
hero=1
Would=9
house=8
our=1
fox=7
point=1
not=65
see=4
car=7
are=2
and=15
bite=1
of=1
dark=7
eat=23
takes=1
so=3
on=7
That=2
a=56
green=12
or=8
may=4
will=21
could=11
ANYWHERE=2
say=2
boat=3
They=1
the=13
with=19
Ham=1
By=1
there=9
Let=1
tormented=1
So=2
Green=1
You=8
story=1

I'm pretty sure that this is what you are looking for. 我很确定这就是您要寻找的。 You want to convert to lower case every word when you take it from the Scanner . 当您从Scanner获取每个单词时,都希望将其转换为小写。 You can do this with something like this: 您可以使用以下方法执行此操作:

    String word = keyboard.next().toLowerCase();
    if (map.containsKey(word)) {
        map.put(word, map.get(word) + 1);
    } else {
        map.put(word, 1);
    }

If you would like to have both a case sensitive and case insensitive map, you can do this with parallel maps. 如果您想同时具有区分大小写和不区分大小写的映射,则可以使用并行映射来实现。

Then when it comes to sorting by number of occurrences you can do something with a list of the keys that are sorted by their corresponding number of occurrences in the map: 然后,当涉及到按出现次数排序时,您可以对键列表进行某些操作,这些键按映射中对应的出现次数进行排序:

    Map<String, Integer> map = new HashMap<String, Integer>();

    // Populate map and do stuff

    List<String> sortedByOccurences = new ArrayList<String>(map.keySet());

    Collections.sort(sortedByOccurences, new Comparator<String>() {

        @Override
        public int compare(String o1, String o2) {
            return map.get(o1) - map.get(o2);
        }

    });

Hope this is close to what you are looking for. 希望这接近您要寻找的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM