使用Java或C#编写程序,计算每个程序的频率 文本中的单词,并输出每个单词的计数和行号 它出现的地方。我们将一个单词定义为一个连续的序列 非空白字符。 (提示:split())注意:不同 应考虑相同字符序列的大写 同一个词,例如Python和python,我和我。输入将是几个 空行终止文本的行(使用文本文件 输入是可选的)。只有字母和白色空格 出现在输入中。输出格式如下:
strtotime(date('Y').'-04-30');
这就是我所拥有的:
- 1 python 1
- 3 is 1 2
- 1 a 1
- 1 but 1
- 1 cool 1 2
- 1 even 2
- 1 object 2
- 1 oriented 2
- 1 it 2
- 1 language 1 2
- 1 Java 1
- 1 purely 2
- 1 since 2
我无法弄清楚如何让输出包含每个单词的行号。谢谢你的帮助。我顺便使用Java。
答案 0 :(得分:1)
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
public class Countcharacters {
/**
* @param args
*/
static HashMap<String, Integer> countcharact=new HashMap<>();
static HashMap<String, String> linenumbertrack=new HashMap<>();
static int count=1;
static void countwords(String line){
//System.out.println(line);
String[] input=line.split("\\s");
int j=0;
String linenumber="";
for(int i=0;i<input.length;i++){
//System.out.println(input[i]);
if(countcharact.containsKey(input[i])==true){
j=countcharact.get(input[i]);
linenumber=linenumbertrack.get(input[i]);
countcharact.put(input[i],j+1);
linenumbertrack.put(input[i],linenumber+" "+count);
}
else{
countcharact.put(input[i], 1);
linenumbertrack.put(input[i],count+" " );
}
}
count++;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String inp="its am here in 1st line\ni am here in 2nd line";
String[] line=inp.split("\n");
for(int i=0;i<line.length;i++){
Countcharacters.countwords(line[i]);
}
Set<String> s=countcharact.keySet();
for(String c:s){
System.out.println(countcharact.get(c)+" "+c+" "+linenumbertrack.get(c));
}
}
}
想法是使用2个hashmap。一个存储单词和出现,另一个存储它出现的单词和行号。将这两个hashmap结合起来以获得所需的输出。
上述计划的输出:
1 2nd 2
凌晨2点1 2
1 1st 1
2行1 2
2这里1 2
1其1
2 in 1 2
1 i 2
答案 1 :(得分:0)
如果您将单词及其出现数据建模为对象并使用java集合框架,则 更简单。
对于一个单词,你想知道它发生的频率,发生,所以WordData类可能如下所示:
public class WordData {
public String theWord;
public List<Integer> appearsWhere = new ArrayList<>();
}
请注意,我没有列出出现次数,因为它等于它出现的地方数量,即appearsWhere.size()
。我也省略了getter / setters。
现在,为了跟踪多个单词,您需要一个地图,单词播放键,WordData是值Map<String, WordData> wordMap = new HashMap<>()
。要查找单词数据,只需使用wordMap.get(String)
,如果返回null,则该单词尚不存在,因此您创建它并将其放入地图中。否则只需将它出现的位置添加到WordData中。
所以整个程序运行如下:
for each word {
word = word.toLowerCase();
WordData wd = wordMap.get(word);
if (wd == null) {
wd = new WordData();
wd.theWord = word;
wordMap.put(word, wd);
}
wd.appearsWhere.add(currentPlace);
}
要输出所有数据,只需遍历地图值并在一个嵌套循环中遍历showsWhere:
for (WordData wd : wordMap) {
// output count and word
for (Integer where : wd.appearsWhere) {
// output where
}
}
使用Map和List,您无需担心详细管理数据存储,这些类可以解决这个问题。你可以专注于非常简单的逻辑。