java - 在 ArrayList 中显示 0 的词频
问题描述
我正在寻求帮助。我制作了一个使用两个类的程序 - 我也制作了。第一类称为 CollectionOfWords,它读取文本文件并将文本文件中包含的单词存储在 HashMap 中。第二个称为 WordFrequencies,它从 CollectionOfWords 类中调用一个名为 Collection 的对象,该对象依次读入另一个文档并查看文档内容是否在 Collection 中。然后输出一个 ArrayList,其中包含文档中计算的频率。
虽然这有效并返回在集合和文档中找到的单词的频率,但我希望它能够为集合中的单词产生零值,但不是在文档中,如果这有意义的话?例如,test3 返回 [1, 1, 1],但我希望它返回 [1, 0, 0, 0, 1, 0, 1] - 其中零表示集合中的单词,但不是在test3中找到。
我使用的测试文本文件可以在这里找到: https ://drive.google.com/open?id=1B1cDpjmZZo01HizxJUSWSVIlHcQke2mU
干杯
词频
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);
}
}
词集
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());
}
}
解决方案
我没有费力找出你的整个代码,如果这个答案很愚蠢,很抱歉。
作为您的问题的解决方案,您可以使用字典映射中的每个单词将映射初始化为零。现在,您使用clear
hashmap 上的方法,这不会将所有内容设置为零,而是删除所有映射。
以下代码应该可以工作,使用它代替collection.clear()
for (Map.Entry<String, Integer> entry : collection.entrySet()) {
entry.setValue(0);
}
推荐阅读
- javascript - Django javascript-catalog 缺少 .po 文件中的数据
- spring - 无法获得 JDBC 连接;嵌套异常是 com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user
- azure - 如何在 Azure Sentinel 中获取 OfficeActivity 架构的文件哈希?
- scala - Scala中方法参数的协方差
- angular - NullInjectorError:没有 x 的提供者
- php - 权限用户角色 Wordpress 仪表板
- javascript - 在 webpack 配置中向 historyApiFallback 提供布尔值与对象之间的区别?
- android - Cookie 和自定义标头 - webview
- video-streaming - GStreamer - 更改 RSTP 流的分辨率
- javascript - 有人可以解释一下这个javascript代码是如何工作的吗?