提取中文,和替换中文脚本

jieling
grimm 2024-08-05 17:27:15 +08:00
parent c4befb9cc8
commit 26b2b53ba1
2 changed files with 172 additions and 0 deletions

View File

@ -0,0 +1,102 @@
package com.jmfy.utils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ChineseExtractor {
public static void main(String[] args) {
try {
String directoryPath = "D:\\Code\\gm_tw_admin\\src"; // 指定目录路径
List<String> fileTypes = Arrays.asList(".txt", ".html", ".java"); // 指定多个文件类型
String outputExcelPath = "D:\\Code\\gm_tw_admin\\src\\output.xlsx"; // 输出Excel文件路径
List<File> files = listFiles(directoryPath, fileTypes);
List<ChinesePhraseEntry> chinesePhrases = new ArrayList<>();
for (File file : files) {
List<String> phrases = extractChinesePhrases(file);
for (String phrase : phrases) {
chinesePhrases.add(new ChinesePhraseEntry(file.getAbsolutePath(), phrase));
}
}
writeToExcel(chinesePhrases, outputExcelPath);
System.out.println("Extraction complete. Check the output file: " + outputExcelPath);
} catch (IOException e) {
e.printStackTrace();
}
}
public static List<File> listFiles(String directoryPath, List<String> fileTypes) throws IOException {
List<File> fileList = new ArrayList<>();
Files.walk(Paths.get(directoryPath))
.filter(Files::isRegularFile)
.filter(path -> {
for (String fileType : fileTypes) {
if (path.toString().endsWith(fileType)) {
return true;
}
}
return false;
})
.forEach(path -> fileList.add(path.toFile()));
return fileList;
}
public static List<String> extractChinesePhrases(File file) throws IOException {
String content = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
List<String> chinesePhrases = new ArrayList<>();
// 正则表达式匹配中文字符
Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]+");
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
chinesePhrases.add(matcher.group());
}
return chinesePhrases;
}
public static void writeToExcel(List<ChinesePhraseEntry> chinesePhrases, String outputExcelPath) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Chinese Phrases");
int rowNum = 0;
for (ChinesePhraseEntry entry : chinesePhrases) {
Row row = sheet.createRow(rowNum++);
Cell filePathCell = row.createCell(0);
Cell phraseCell = row.createCell(1);
filePathCell.setCellValue(entry.filePath);
phraseCell.setCellValue(entry.phrase);
}
try (FileOutputStream fileOut = new FileOutputStream(outputExcelPath)) {
workbook.write(fileOut);
}
workbook.close();
}
static class ChinesePhraseEntry {
String filePath;
String phrase;
ChinesePhraseEntry(String filePath, String phrase) {
this.filePath = filePath;
this.phrase = phrase;
}
}
}

View File

@ -0,0 +1,70 @@
package com.jmfy.utils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
public class ReplaceChineseWithVietnamese {
public static void main(String[] args) {
try {
String excelFilePath = "D:\\Code\\gm_tw_admin\\src\\output2.xlsx"; // 指定Excel文件路径
Map<String, Map<String, String>> fileToPhrasesMap = readExcelFile(excelFilePath);
replaceChineseWithVietnamese(fileToPhrasesMap);
System.out.println("Replacement complete.");
} catch (IOException e) {
e.printStackTrace();
}
}
public static Map<String, Map<String, String>> readExcelFile(String excelFilePath) throws IOException {
Map<String, Map<String, String>> fileToPhrasesMap = new HashMap<>();
try (FileInputStream fis = new FileInputStream(excelFilePath);
Workbook workbook = new XSSFWorkbook(fis)) {
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
Cell filePathCell = row.getCell(0);
Cell chinesePhraseCell = row.getCell(1);
Cell vietnamesePhraseCell = row.getCell(2);
if (filePathCell != null && chinesePhraseCell != null && vietnamesePhraseCell != null) {
String filePath = filePathCell.getStringCellValue();
String chinesePhrase = chinesePhraseCell.getStringCellValue();
String vietnamesePhrase = vietnamesePhraseCell.getStringCellValue();
fileToPhrasesMap
.computeIfAbsent(filePath, k -> new HashMap<>())
.put(chinesePhrase, vietnamesePhrase);
}
}
}
return fileToPhrasesMap;
}
public static void replaceChineseWithVietnamese(Map<String, Map<String, String>> fileToPhrasesMap) throws IOException {
for (Map.Entry<String, Map<String, String>> entry : fileToPhrasesMap.entrySet()) {
String filePath = entry.getKey();
Map<String, String> phrasesMap = entry.getValue();
File file = new File(filePath);
String content = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
for (Map.Entry<String, String> phraseEntry : phrasesMap.entrySet()) {
String chinesePhrase = phraseEntry.getKey();
String vietnamesePhrase = phraseEntry.getValue();
content = content.replace(chinesePhrase, vietnamesePhrase);
}
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(content.getBytes(StandardCharsets.UTF_8));
}
}
}
}