diff --git a/src/main/java/com/jmfy/utils/ChineseExtractor.java b/src/main/java/com/jmfy/utils/ChineseExtractor.java new file mode 100644 index 0000000..ecabbea --- /dev/null +++ b/src/main/java/com/jmfy/utils/ChineseExtractor.java @@ -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 fileTypes = Arrays.asList(".txt", ".html", ".java"); // 指定多个文件类型 + String outputExcelPath = "D:\\Code\\gm_tw_admin\\src\\output.xlsx"; // 输出Excel文件路径 + List files = listFiles(directoryPath, fileTypes); + List chinesePhrases = new ArrayList<>(); + + for (File file : files) { + List 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 listFiles(String directoryPath, List fileTypes) throws IOException { + List 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 extractChinesePhrases(File file) throws IOException { + String content = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8); + List 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 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; + } + } +} \ No newline at end of file diff --git a/src/main/java/com/jmfy/utils/ReplaceChineseWithVietnamese.java b/src/main/java/com/jmfy/utils/ReplaceChineseWithVietnamese.java new file mode 100644 index 0000000..5498aa8 --- /dev/null +++ b/src/main/java/com/jmfy/utils/ReplaceChineseWithVietnamese.java @@ -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> fileToPhrasesMap = readExcelFile(excelFilePath); + replaceChineseWithVietnamese(fileToPhrasesMap); + System.out.println("Replacement complete."); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static Map> readExcelFile(String excelFilePath) throws IOException { + Map> 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> fileToPhrasesMap) throws IOException { + for (Map.Entry> entry : fileToPhrasesMap.entrySet()) { + String filePath = entry.getKey(); + Map phrasesMap = entry.getValue(); + + File file = new File(filePath); + String content = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8); + + for (Map.Entry 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)); + } + } + } +} \ No newline at end of file