本文目录导读:

我来为你设计一个统计"马赛回旋"使用频率的Java案例,这个案例将使用简单的文本分析和关键词匹配来实现。
完整Java实现案例
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.regex.*;
/**
* 马赛回旋使用频率统计器
* 用于统计足球比赛解说文本中"马赛回旋"出现的频率
*/
public class MarseilleTurnCounter {
// 存储统计结果
private Map<String, Integer> statsMap;
private int totalMatches;
private int totalOccurrences;
public MarseilleTurnCounter() {
statsMap = new HashMap<>();
totalMatches = 0;
totalOccurrences = 0;
}
/**
* 统计单个文本文件中"马赛回旋"的使用频率
* @param text 要分析的文本
* @return 包含统计信息的Map
*/
public Map<String, Object> analyzeText(String text) {
Map<String, Object> result = new HashMap<>();
// 统计关键词出现次数
int count = countOccurrences(text, "马赛回旋");
// 统计总词数
int totalWords = text.split("\\s+").length;
// 计算频率(每千词的出现次数)
double frequencyPerThousand = totalWords > 0 ?
(count * 1000.0 / totalWords) : 0;
// 添加统计结果
result.put("keyword", "马赛回旋");
result.put("count", count);
result.put("totalWords", totalWords);
result.put("frequencyPerThousand", frequencyPerThousand);
result.put("frequencyPercent",
totalWords > 0 ? (count * 100.0 / totalWords) : 0.0);
// 更新全局统计
totalOccurrences += count;
totalMatches++;
statsMap.merge("马赛回旋", count, Integer::sum);
return result;
}
/**
* 统计文件中的"马赛回旋"使用频率
* @param filePath 文件路径
* @return 统计结果
*/
public Map<String, Object> analyzeFile(String filePath) {
try {
String content = Files.readString(Paths.get(filePath));
return analyzeText(content);
} catch (IOException e) {
System.err.println("读取文件失败: " + e.getMessage());
return null;
}
}
/**
* 统计目录下所有文本文件
* @param directoryPath 目录路径
* @return 所有文件的统计结果列表
*/
public List<Map<String, Object>> analyzeDirectory(String directoryPath) {
List<Map<String, Object>> results = new ArrayList<>();
try {
Files.walk(Paths.get(directoryPath))
.filter(Files::isRegularFile)
.filter(p -> p.toString().endsWith(".txt"))
.forEach(file -> {
Map<String, Object> fileResult = analyzeFile(file.toString());
if (fileResult != null) {
fileResult.put("fileName", file.getFileName().toString());
results.add(fileResult);
}
});
} catch (IOException e) {
System.err.println("无法访问目录: " + e.getMessage());
}
return results;
}
/**
* 统计"马赛回旋"在指定文本中的出现次数
* @param text 文本内容
* @param keyword 关键词
* @return 出现次数
*/
private int countOccurrences(String text, String keyword) {
if (text == null || keyword == null) return 0;
int count = 0;
int index = 0;
// 使用indexOf进行字符串匹配
while ((index = text.indexOf(keyword, index)) != -1) {
count++;
index += keyword.length();
}
return count;
}
/**
* 获取全局统计结果
* @return 全局统计
*/
public Map<String, Object> getGlobalStats() {
Map<String, Object> globalStats = new HashMap<>();
globalStats.put("totalMatches", totalMatches);
globalStats.put("totalOccurrences", totalOccurrences);
globalStats.put("statsMap", statsMap);
if (totalMatches > 0) {
globalStats.put("averagePerMatch",
(double) totalOccurrences / totalMatches);
}
return globalStats;
}
/**
* 格式化输出统计结果
* @param result 统计结果
*/
public void printStats(Map<String, Object> result) {
if (result == null) return;
System.out.println("=== 马赛回旋使用频率统计 ===");
if (result.containsKey("fileName")) {
System.out.println("文件: " + result.get("fileName"));
}
System.out.println("关键词: " + result.get("keyword"));
System.out.println("出现次数: " + result.get("count") + " 次");
System.out.println("总词数: " + result.get("totalWords"));
System.out.printf("频率(每千词): %.2f ‰%n",
result.get("frequencyPerThousand"));
System.out.printf("频率(百分比): %.4f%%%n",
result.get("frequencyPercent"));
System.out.println("---------------------------");
}
/**
* 主函数示例
*/
public static void main(String[] args) {
MarseilleTurnCounter counter = new MarseilleTurnCounter();
// 示例文本
String sampleText = """
今天的比赛中,球员展现出了出色的技术,他在边路连续使用马赛回旋过人了多次。
每一次马赛回旋都让防守球员十分头疼,下半场,他又用马赛回旋创造了多次机会。
凭借标志性的马赛回旋,他完成了精彩的进球。
""";
System.out.println("=== 单个文本分析 ===");
Map<String, Object> textStats = counter.analyzeText(sampleText);
counter.printStats(textStats);
// 多个文本分析示例
System.out.println("\n=== 多个比赛分析 ===");
String[] matchCommentaries = {
"比赛中马赛回旋用得很多,成为比赛亮点。",
"今天看到几次精彩的马赛回旋表演。",
"解说员多次提到马赛回旋,认为这是本场比赛的关键技术。"
};
for (String commentary : matchCommentaries) {
Map<String, Object> matchStats =
counter.analyzeText(commentary);
// 补充比赛信息
matchStats.put("fileName", "比赛_" +
(counter.getGlobalStats().get("totalMatches")));
counter.printStats(matchStats);
}
// 输出全局统计
System.out.println("\n=== 全局统计结果 ===");
Map<String, Object> globalStats = counter.getGlobalStats();
System.out.println("总比赛场次: " + globalStats.get("totalMatches"));
System.out.println("总出现次数: " + globalStats.get("totalOccurrences"));
System.out.printf("平均每场使用频率: %.2f 次/场%n",
globalStats.get("averagePerMatch"));
// 文件和目录分析示例
System.out.println("\n=== 文件分析示例 ===");
try {
// 创建临时测试文件
Path tempFile = Files.createTempFile("football", ".txt");
Files.writeString(tempFile, sampleText);
Map<String, Object> fileStats = counter.analyzeFile(tempFile.toString());
if (fileStats != null) {
fileStats.put("fileName", tempFile.getFileName().toString());
counter.printStats(fileStats);
}
Files.deleteIfExists(tempFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 进阶版:支持更多关键词和正则表达式匹配
*/
class AdvancedMarseilleCounter extends MarseilleTurnCounter {
private List<String> relatedKeywords = Arrays.asList(
"马赛回旋", "马赛旋转", "360度转身",
"背后拉球", "转身过人"
);
private Pattern pattern;
private Map<String, Integer> detailedStats = new HashMap<>();
public AdvancedMarseilleCounter() {
// 构建正则表达式模式
StringBuilder regex = new StringBuilder();
for (String keyword : relatedKeywords) {
if (regex.length() > 0) regex.append("|");
regex.append(Pattern.quote(keyword));
}
pattern = Pattern.compile(regex.toString());
}
/**
* 高级统计:使用正则表达式匹配多个相关关键词
* @param text 文本
* @return 详细统计结果
*/
public Map<String, Object> advancedAnalyze(String text) {
Map<String, Object> result = new HashMap<>();
// 使用正则表达式统计
Matcher matcher = pattern.matcher(text);
List<String> matches = new ArrayList<>();
while (matcher.find()) {
String match = matcher.group();
matches.add(match);
detailedStats.merge(match, 1, Integer::sum);
}
result.put("totalMatches", matches.size());
result.put("detailStats", new HashMap<>(detailedStats));
result.put("matches", matches);
// 统计主要关键词(马赛回旋)
int mainKeywordCount = countOccurrences(text, "马赛回旋");
result.put("mainKeywordCount", mainKeywordCount);
return result;
}
/**
* 计算词语的TF-IDF权重(简单的实现)
* @param document 文档
* @param corpus 整个语料库
* @return TF-IDF值
*/
public double calculateTFIDF(String document, List<String> corpus) {
int docCount = corpus.size();
// 计算TF(词频)
int tf = countOccurrences(document, "马赛回旋");
int totalWords = document.split("\\s+").length;
double termFrequency = (double) tf / totalWords;
// 计算IDF(逆文档频率)
int docsContaining = 0;
for (String doc : corpus) {
if (doc.contains("马赛回旋")) {
docsContaining++;
}
}
double inverseDocFrequency =
Math.log((double) docCount / (docsContaining + 1));
return termFrequency * inverseDocFrequency;
}
}
/**
* 测试类和示例
*/
class MarseilleCounterTest {
public static void main(String[] args) {
// 基础测试
MarseilleTurnCounter basicCounter = new MarseilleTurnCounter();
String testText = "马赛回旋马赛回旋,这个马赛回旋太精彩了!";
System.out.println("测试基础统计:");
Map<String, Object> basicStats = basicCounter.analyzeText(testText);
basicCounter.printStats(basicStats);
// 高级测试
AdvancedMarseilleCounter advancedCounter = new AdvancedMarseilleCounter();
String advancedText = """
球员使用马赛回旋晃过对手,紧接着一个360度转身,
然后背后拉球传给队友,最后用转身过人完成突破。
""";
System.out.println("\n测试高级统计(多关键词):");
Map<String, Object> advancedStats =
advancedCounter.advancedAnalyze(advancedText);
System.out.println("总匹配次数: " + advancedStats.get("totalMatches"));
System.out.println("主要关键词出现次数: " +
advancedStats.get("mainKeywordCount"));
@SuppressWarnings("unchecked")
Map<String, Integer> detailStats =
(Map<String, Integer>) advancedStats.get("detailStats");
System.out.println("详细统计:");
detailStats.forEach((key, value) ->
System.out.println(" " + key + ": " + value + "次"));
}
}
代码特点说明
核心功能:
- 统计单个文本中"马赛回旋"的出现次数
- 计算使用频率(每千词占比)
- 支持文件和目录批量分析
- 输出详细统计报告
设计亮点:
- 多关键词支持:可统计相关技术动作
- 正则表达式匹配:更灵活的文本匹配
- TF-IDF算法:评估关键词重要性
- 统计可视化:清晰输出分析结果
实际应用场景:
- 足球比赛解说分析
- 球员技术特点评估
- 战术风格研究
- 体育媒体内容分析
扩展建议:
- 添加分词功能(如使用 HanLP 或 IKAnalyzer)
- 支持更多技术动作比较
- 引入时间序列分析
- 加入图表展示功能
这个案例可以很好地应用于足球数据分析、体育评论研究等领域,通过量化"马赛回旋"的使用频率,为战术分析提供数据支持。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播