您将如何评估以下java解决方案或如何解决它?
您如何评估以下任务的解决方案的结构、正确性、简单性和可测试性(任务时间约1小时): 创建一个命令行Java程序,该程序计算来自 文本文件,并列出前10个引用 将连字符和撇号视为单词的一部分,输出应如下所示: 及(514) (513) i(446) 至(324) a(310) 共(295) 我的(288) 你(211) (188) 这(185) 解决方案: java(主类)您将如何评估以下java解决方案或如何解决它?,java,software-quality,Java,Software Quality,您如何评估以下任务的解决方案的结构、正确性、简单性和可测试性(任务时间约1小时): 创建一个命令行Java程序,该程序计算来自 文本文件,并列出前10个引用 将连字符和撇号视为单词的一部分,输出应如下所示: 及(514) (513) i(446) 至(324) a(310) 共(295) 我的(288) 你(211) (188) 这(185) 解决方案: java(主类) 公共类字计算器{ /** *统计文本文件中的唯一单词,并列出前10个出现的单词。 * *@param指定命令行参数。第一个参
公共类字计算器{
/**
*统计文本文件中的唯一单词,并列出前10个出现的单词。
*
*@param指定命令行参数。第一个参数是文件路径。
*如果省略,将提示用户指定路径。
*
*@如果文件因其他原因丢失,则会引发java.io.FileNotFoundException
*无法打开以进行读取。
*
*@在发生I/O错误时引发java.io.IOException
*/
公共静态void main(字符串[]args)抛出FileNotFoundException、IOException{
文件;
List listOfWords=new ArrayList();
//如果指定了命令参数,请将其用作文件路径。
//否则,提示用户输入路径。
如果(args.length>0){
文件=新文件(参数[0]);
}否则{
扫描仪=新的扫描仪(System.in);
System.out.print(“输入文件路径:”);
file=新文件(scanner.nextLine());
}
//读取文件并将输入拆分为单词列表
try(BufferedReader br=new BufferedReader(new FileReader(file))){
弦线;
而((line=br.readLine())!=null){
addAll(WordUtil.getWordsFromString(行));
}
}捕获(FileNotFoundException ex){
Logger.getLogger(WordCalculator.class.getName()).log(Level.SEVERE,
format(“从文件“%s”读取的访问被拒绝”,file.getAbsolutePath()),ex);
掷骰子;
}捕获(IOEX异常){
Logger.getLogger(WordCalculator.class.getName()).log(Level.SEVERE,
“读取输入文件时发生I/O错误。”,例如);
掷骰子;
}
//检索前十个常用词及其频率。
Map freqMap=FrequencyUtil.getItemFrequencies(ListoWords);
列出您的解决方案是正确的,但是如果您正在寻找一个功能较少的编程解决方案和更多的OOP。您应该避免将Utils类与静态方法一起使用。相反,您可以使用WordCalculator添加实例方法和属性作为计算单词的映射。此外,正则表达式模式对性能操作有很大影响,您正在执行循环(以功能性方式)将拆分的单词添加到映射中。另一个选项是每字节读取文件字节,当您发现非字母字符时(如果文本文件很简单,就足以检查空白)将StringBuilder中的单词转储到地图中,并在计数器中添加1。这样,如果文件是一个巨大的单行文本,也可以避免可能出现的问题
更新1-新增阅读单词示例:
您的解决方案是正确的,但如果您正在寻找功能较少的编程解决方案和更多的OOP,则应避免将UTIL类与静态方法一起使用。相反,您可以使用WordCalculator添加实例方法和属性作为计数字的映射。此外,正则表达式模式对性能操作影响很大,并且您正在执行循环s(以功能性方式)将此拆分的单词添加到映射中。另一个选项是每字节读取文件字节,当您发现非字母字符时(如果文本文件很简单,则足以检查空白)将StringBuilder中的单词转储到地图中,并在计数器中添加1。这样,如果文件是一个巨大的单行文本,也可以避免可能出现的问题
更新1-新增阅读单词示例:
你问质量等问题…从OOP的角度来看,通常“实用”类是不好的,通常是把设计问题隐藏在地毯下。你实际上不需要大的单词列表
。你可以直接将小列表传递到频率图。谢谢!我完全同意!你问质量等问题…通常是“实用”从OOP的角度来看,类是不好的,通常是将设计问题隐藏在下面。实际上,您不需要大的列表词
。您可以直接将小的列表传递到频率图。谢谢!我完全同意!我看到,从OOP的角度来看,util类是不好的做法…我没有在主类中添加任何实例的原因我这是因为我希望主类只专注于做一件事,“将其全部连接起来”。我同意Stringbuffer将是一种更快更好的方法。我使用Pattern希望为英语单词找到一个内置正则表达式,但没有找到任何东西(我不关心性能,我追求简单性)。关于从文件中逐字节读取的选项效率不高(我猜每次调用read()都会触发操作系统输入请求),应该使用缓冲区。但是,对于缓冲区,我需要处理在两次读取之间解析单词的情况,我担心这可能比仅使用读取行更复杂(没有想到缓冲读取器…。。就内存效率而言,JVM对每个对象实例至少使用16个字节(包括布尔值)所以我不建议在java中有大量的单词列表…如果你不使用缓冲区就从文件中逐字节读取,当然效率很低,但是使用中间缓冲区,它会完全改变。当我说逐字节读取时,我使用的是始终缓冲区,并从缓冲区读取,所以当你到达缓冲区的e端和i
public class WordCalculator {
/**
* Counts unique words from a text file and lists the top 10 occurrences.
*
* @param args the command line arguments. First argument is the file path.
* If omitted, user will be prompted to specify path.
*
* @throws java.io.FileNotFoundException if the file for some other reason
* cannot be opened for reading.
*
* @throws java.io.IOException If an I/O error occurs
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
File file;
List<String> listOfWords = new ArrayList<>();
// If a command argument is specified, use it as the file path.
// Otherwise prompt user for the path.
if (args.length > 0) {
file = new File(args[0]);
} else {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter path to file: ");
file = new File(scanner.nextLine());
}
// Reads the file and splits the input into a list of words
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
listOfWords.addAll(WordUtil.getWordsFromString(line));
}
} catch (FileNotFoundException ex) {
Logger.getLogger(WordCalculator.class.getName()).log(Level.SEVERE,
String.format("Access denied reading from file '%s'.", file.getAbsolutePath()), ex);
throw ex;
} catch (IOException ex) {
Logger.getLogger(WordCalculator.class.getName()).log(Level.SEVERE,
"I/O error while reading input file.", ex);
throw ex;
}
// Retrieves the top ten frequent words and their frequencies.
Map<Object, Long> freqMap = FrequencyUtil.getItemFrequencies(listOfWords);
List<Map.Entry<?, Long>> topTenWords = FrequencyUtil.limitFrequency(freqMap, 10);
// Prints the top ten words and their frequencies.
topTenWords.forEach((word) -> {
System.out.printf("%s (%d)\r\n", word.getKey(), word.getValue());
});
}
}
public class FrequencyUtil {
/**
* Transforms a list into a map with elements and their frequencies.
*
* @param list, the list to parse
* @return the item-frequency map.
*/
public static Map<Object, Long> getItemFrequencies(List<?> list) {
return list.stream()
.collect(Collectors.groupingBy(obj -> obj,Collectors.counting()));
}
/**
* Sorts a frequency map in descending order and limits the list.
*
* @param objFreq the map elements and their frequencies.
* @param limit the limit of the returning list
* @return a list with the top frequent words
*/
public static List<Map.Entry<?, Long>> limitFrequency(Map<?, Long> objFreq, int limit) {
return objFreq.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.limit(limit)
.collect(Collectors.toList());
}
}
public class WordUtil {
public static final Pattern ENGLISH_WORD_PATTERN = Pattern.compile("[A-Za-z'\\-]+");
/**
*
* @param s the string to parse into a list of words. Words not matching the
* english pattern(a-z A-z ' -) will be omitted.
*
* @return a list of the words
*
*/
public static List<String> getWordsFromString(String s) {
ArrayList<String> list = new ArrayList<>();
Matcher matcher = ENGLISH_WORD_PATTERN.matcher(s);
while (matcher.find()) {
list.add(matcher.group().toLowerCase());
}
return list;
}
}
private void readWords(File file) {
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
StringBuilder build = new StringBuilder();
int value;
while ((value = bufferedReader.read()) != -1) {
if(Character.isLetterOrDigit(value)){
build.append((char)Character.toLowerCase(value));
} else {
if(build.length()>0) {
addtoWordMap(build.toString());
build = new StringBuilder();
}
}
}
if(build.length()>0) {
addtoWordMap(build.toString());
}
} catch(FileNotFoundException e) {
//todo manage exception
e.printStackTrace();
} catch (IOException e) {
//todo manage exception
e.printStackTrace();
}
}