java - 使用字典计算文件中的正面和负面单词(Java)

标签 java parsing dictionary compare

我正在尝试确定文件中正面和负面单词的出现次数,以计算该文件是否具有正面语气或负面语气。

我目前在尝试解析文件以获取文件中包含的正面和负面单词的数量时遇到问题。目前,我正在使用 BufferedReader 来读取主文件,我试图从中确定正面和负面单词以及包含正面和负面单词字典的两个文件。然而,我遇到的问题是将每个单词与正面和负面文件中相应的单词编号进行比较。

这是我当前的代码:

import java.io.*;
import java.util.Scanner;


public class ParseTest {

    public static void main(String args[]) throws IOException
    {
    File file1 = new File("fileforparsing");
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file1)));
    File file2 = new File("positivewordsdictionary");
    BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream(file2)));

    int positive = 0;
           Scanner sc1 = new Scanner(br);
           Scanner sc2 = new Scanner(br1);
            while (sc1.hasNext() && sc2.hasNext()) {
                String str1 = sc1.next();
                String str2 = sc2.next();
                if (str1.equals(str2))
                    positive = positive +1;
            }
            while (sc2.hasNext())
                System.out.println(positive);
            sc1.close();
            sc2.close();
    }

}

我知道出了什么问题,当我希望原始文件保持在同一行直到它完成对字典的解析时,扫描仪不断移动到下一行,但我不太确定如何让它执行我想要的操作。任何帮助将不胜感激。

提前谢谢您。

最佳答案

这行不通。您每次都需要重新打开字典文件。另一件事是它会非常慢。如果字典不是太大,您应该将它们加载到内存中,然后对您要分析的文件执行只读操作。

public static void main(String args[]) throws IOException {
    Set<String> positive = loadDictionary("positivewordsdictionary");
    Set<String> negative = loadDictionary("negativewordsdictionary");

    File file = new File("fileforparsing");
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

    Scanner sc = new Scanner(br);
    String word;
    long positiveCount = 0;
    long negativeCount = 0;
    while (sc.hasNext()) {
        word = sc.next();
        if (positive.contains(word)) {
            System.out.println("Found positive "+positiveCount+":"+word);
            positiveCount++;
        }
        if (negative.contains(word)) {
            System.out.println("Found negative "+positiveCount+":"+word);
            negativeCount++;
        }
    }
    br.close();
}


public static Set<String> loadDictionary(String fileName) throws IOException {
    Set<String> words = new HashSet<String>();
    File file = new File(fileName);
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
    Scanner sc = new Scanner(br);
    while (sc.hasNext()) {
        words.add(sc.next());
    }
    br.close();
    return words;
}

更新:我已经尝试运行代码并且它正在工作。

关于java - 使用字典计算文件中的正面和负面单词(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21259722/

相关文章:

python - 去掉string中的一个char,然后用字典做key match

java - 是否有模拟 C 的 crypt 函数的 java 函数?

java - 什么是测试BNF语法的好工具?

c++ - 如何将字符串形式的日期 ("Dec 25, 2012") 转换为一组整数 (12/25/12)?

c++ - C++中读取转换字符的问题

c# - 更新字典值并获取前一个

python - 以更快的方式创建字典 - Python

Java 垃圾收集器说明

java - 在 AS400 上显示 DB2 中的 BLOB 内容

java - QueryDSL - 涉及关节的总和