无第三方库依赖下修正星期拼写错误的技术方案咨询
无第三方库的拼写错误星期修正方案
核心思路
利用**编辑距离(Levenshtein Distance)**计算错误单词与标准星期的相似度,无需依赖任何第三方库。编辑距离指两个字符串之间转换所需的最少插入、删除或替换操作次数,通过将编辑距离转换为匹配度(1 - 编辑距离/最长字符串长度),筛选出匹配度≥80%的最优标准星期进行替换。
实现步骤
- 提取错误星期单词:根据固定句式,通过字符串截取或正则匹配提取句子中的错误星期词汇。
- 实现编辑距离算法:手动编写Levenshtein距离计算逻辑,用于衡量字符串相似度。
- 匹配与替换:遍历标准星期数组,计算每个标准词与错误词的匹配度,选取符合阈值的最优匹配替换原文本。
完整Java代码实现
public class WeekdayCorrector { // 标准星期数组 private static final String[] STANDARD_DAYS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; // 匹配阈值(80%) private static final double MATCH_THRESHOLD = 0.8; public static void main(String[] args) { // 待修正的文本行 String[] inputLines = { "You sold 356 items across all Mondy of the year", "You sold 1687 items across all Thrrsday of the year", "You sold 1718 items across all Tuesdly of the year" }; // 处理每一行文本 for (String line : inputLines) { String correctedLine = correctWeekday(line); System.out.println(correctedLine); } } // 修正单条文本中的星期错误 private static String correctWeekday(String line) { // 提取错误的星期单词:基于固定句式截取 int startIdx = line.indexOf("across all ") + "across all ".length(); int endIdx = line.indexOf(" of the year"); if (startIdx == -1 || endIdx == -1 || startIdx >= endIdx) { return line; // 不符合句式,直接返回原句 } String wrongDay = line.substring(startIdx, endIdx).trim(); // 寻找最优匹配的标准星期 String bestMatch = null; double highestMatchScore = 0.0; for (String standardDay : STANDARD_DAYS) { int editDistance = calculateLevenshteinDistance(wrongDay, standardDay); int maxLength = Math.max(wrongDay.length(), standardDay.length()); double matchScore = 1.0 - (double) editDistance / maxLength; // 更新最优匹配 if (matchScore >= MATCH_THRESHOLD && matchScore > highestMatchScore) { highestMatchScore = matchScore; bestMatch = standardDay; } } // 如果找到匹配项,替换原错误单词 if (bestMatch != null) { return line.replace(wrongDay, bestMatch); } return line; // 无匹配项,返回原句 } // 计算Levenshtein编辑距离 private static int calculateLevenshteinDistance(String s1, String s2) { int m = s1.length(); int n = s2.length(); // 创建DP表,dp[i][j]表示s1前i个字符和s2前j个字符的编辑距离 int[][] dp = new int[m + 1][n + 1]; // 初始化边界:空字符串到目标字符串的距离为长度 for (int i = 0; i <= m; i++) { dp[i][0] = i; } for (int j = 0; j <= n; j++) { dp[0][j] = j; } // 填充DP表 for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (s1.charAt(i - 1) == s2.charAt(j - 1)) { dp[i][j] = dp[i - 1][j - 1]; // 字符相同,无需操作 } else { // 取插入、删除、替换中的最小操作数+1 dp[i][j] = 1 + Math.min( Math.min(dp[i - 1][j], // 删除s1的第i个字符 dp[i][j - 1]), // 插入s2的第j个字符到s1 dp[i - 1][j - 1] // 替换s1的第i个字符为s2的第j个字符 ); } } } return dp[m][n]; } }
代码说明
- 编辑距离计算:通过动态规划实现Levenshtein距离,时间复杂度为O(m*n)(m、n为两个字符串长度),对于短字符串(星期单词最长9个字符)来说效率足够。
- 匹配逻辑:仅保留匹配度≥80%的结果,避免错误替换;若多个标准词符合阈值,选取匹配度最高的。
- 兼容性:针对固定句式提取错误单词,若句式有变化,可替换为正则表达式(如
Pattern.compile("across all (\\w+) of the year"))来提取目标单词。
运行结果
You sold 356 items across all Monday of the year You sold 1687 items across all Thursday of the year You sold 1718 items across all Tuesday of the year
内容的提问来源于stack exchange,提问作者Christos Code
相关产品推荐
相关产品推荐

