我正在寻找帮助。我已经制作了一个使用两个类的程序-我也做了。第一类称为CollectionOfWords,它读取文本文件,并将文本文件中包含的单词存储在HashMap中。第二个称为WordFrequency,它从CollectionOfWords类调用一个名为Collection的对象,该对象依次读取另一个文档,并查看文档内容是否在Collection中。然后,输出带有文档中计数频率的ArrayList。
虽然这可行并返回在集合和文档中找到的单词的频率,但我希望它能够为集合中的单词生成零值,但对于文档中的单词则不会,如果那样的话说得通?例如,test3返回[1,1,1],但我希望它返回[1,0,0,0,1,0,1]-其中零表示集合中的单词,但不是在test3中找到。
我使用的测试文本文件可以在这里找到: https://drive.google.com/open?id=1B1cDpjmZZo01HizxJUSWSVIlHcQke2mU
欢呼
WordFrequencies
public class WordFrequencies {
static HashMap<String, Integer> collection = new HashMap<>();
private static ArrayList<Integer> processDocument(String inFileName) throws IOException {
// Rests collections frequency values to zero
collection.clear();
// Reads in the new document file to an ArrayList
Scanner textFile = new Scanner(new File(inFileName));
ArrayList<String> file = new ArrayList<String>();
while(textFile.hasNext()) {
file.add(textFile.next().trim().toLowerCase());
}
/* Iterates the ArrayList of words -and- updates collection with
frequency of words in the document */
for(String word : file) {
Integer dict = collection.get(word);
if (!collection.containsKey(word)) {
collection.put(word, 1);
} else {
collection.put(word, dict + 1);
}
}
textFile.close();
// Stores the frequency values in an ArrayList
ArrayList<Integer> values = new ArrayList<>(collection.values());
return values;
}
public static void main(String[] args) {
// Stores text files for the dictionary (collection of words)
List<String> textFileList = Arrays.asList("Test.txt", "Test2.txt");
// Declares empty ArrayLists for output of processDocument function
ArrayList<Integer> test3 = new ArrayList<Integer>();
ArrayList<Integer> test4 = new ArrayList<Integer>();
// Creates a new CollectionOfWords object called dictionary
CollectionOfWords dictionary = new CollectionOfWords(collection);
// Reads in the ArrayLists text files and processes it
for (String text : textFileList) {
dictionary.scanFile(text);
}
try {
test3 = processDocument("test3.txt");
test4 = processDocument("test4.txt");
} catch(IOException e){
e.printStackTrace();
}
System.out.println(test3);
System.out.println(test4);
}
}
CollectionOfWords
public class CollectionOfWords {
// Declare set in a higher scope (making it a property within the object)
private HashMap<String, Integer> collection = new HashMap<String, Integer>();
// Assigns the value of the parameter to the field of the same name
public CollectionOfWords(HashMap<String, Integer> collection) {
this.collection = collection;
}
// Gets input text file, removes white spaces and adds to dictionary object
public void scanFile(String textFileName) {
try {
Scanner textFile = new Scanner(new File(textFileName));
while (textFile.hasNext()) {
collection.put(textFile.next().trim(), 0);
}
textFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void printDict(HashMap<String, Integer> dictionary) {
System.out.println(dictionary.keySet());
}
}
答案 0 :(得分:2)
我没有弄清楚整个代码的麻烦,因此,如果这个答案很愚蠢,那就对不起。
作为您的问题的解决方案,您可以将字典中的每个单词映射为零来初始化映射。现在,您在哈希图中使用clear
方法,这不会将所有内容都设置为零,而是会删除所有映射。
以下代码应该可以使用,而不是collection.clear()
for (Map.Entry<String, Integer> entry : collection.entrySet()) {
entry.setValue(0);
}