gpt4 book ai didi

java - 检查和计算文件中的标点符号

转载 作者:行者123 更新时间:2023-12-01 09:14:08 25 4
gpt4 key购买 nike

我目前正在做一项作业,要求程序计算文本文件中的单词和标点符号。字数统计程序已经完成并正在运行,但我的教授提供了一种额外的方法来与它结合起来计算标点符号,但我似乎无法开始工作。这是工作程序:

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

public class SnippetWeek11 {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
System.out.print("Enter a filename of a text file to process: ");
String filename = input.nextLine();
File file = new File(filename);
if (file.exists()) {
processFile(file);
}
else {
System.out.println("File " + filename + " does not exist");
}
}

private static void processFile(File theFile) throws Exception {
int wordIndex;
// Create a TreeMap to hold words as key and count as value
Map<String, Integer> map = new TreeMap<>();
Scanner input = new Scanner(theFile);
String line, keyText;
String[] words;
while (input.hasNextLine()) {
line = input.nextLine();
words = line.split("[\\s+\\p{P}]");
for (wordIndex = 0; wordIndex < words.length; wordIndex++) {
keyText = words[wordIndex].toLowerCase();
updateMap(map, keyText);
}
}

// Display key and value for each entry
map.forEach((key, value) -> System.out.println(key + "\t" + value));
}

private static void updateMap(Map<String, Integer> theMap,
String theText) {
int value;
String key = theText.toLowerCase();

if (key.length() > 0) {
if (!theMap.containsKey(key)) {
// The key does not exist in the Map object (theMap), so add key and
// the value (which is a count in this case) to a new theMap element.
theMap.put(key, 1);
}
else {
// The key already exists, so obtain the value (count in this case)
// from theMap element that contains the key and update the element
// with an increased count.
value = theMap.get(key);
value++;
theMap.put(key, value);
}
}
}

这里是必须与字数统计程序结合使用的方法。如果您能提供任何帮助,我将不胜感激。谢谢。

    public static int countPunctuation(File theFile) throws Exception {
String[] punctuationString = {"[","]",".",";",",",":","!","?","(",")","{","}","'"};

Set<String> punctuationSet =
new HashSet<>(Arrays.asList(punctuationString));
int count = 0;

Scanner input = new Scanner(theFile);

while (input.hasNext()) {
String character = input.next();
if (punctuationSet.contains(character))
count++;
}
return count;
}
}

最佳答案

如果您可以使用Pattern类,您就可以做到这一点。

import java.util.regex.*;
import java.util.*;
import java.util.stream.*;

class PunctuationMatch
{
public static void main(String[] args) {
final Pattern p = Pattern.compile("^[,|.|?|!|:|;]");
System.out.println(p.splitAsStream("Hello, World! How are you?").count());
}
}

compile 方法中传递字符串时,传递您想要识别的所有标点符号。

将整个数据字符串或文件的一行一行传递到 splitAsStream 方法中,并将所有内容相加。

这是Java Docs Ref

关于java - 检查和计算文件中的标点符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40699646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com