Skip to content

Commit

Permalink
完成无视大小写的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhxiao committed Jul 11, 2024
1 parent addbd06 commit b955448
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
15 changes: 14 additions & 1 deletion MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@
<CheckBox Name="UseGlossaryCheckBox"
Margin="2">启用术语表(导出时使用术语表替换未翻译文本内的专有名词)</CheckBox>
<CheckBox Name="IgnoreCaseCheckBox"
IsEnabled="False"
Margin="2">忽视大小写(使用术语表进行匹配时同一个字母的大小写通用)</CheckBox>
<Grid>
<Grid.ColumnDefinitions>
Expand Down Expand Up @@ -134,10 +133,24 @@
Margin="2">将未翻译文本以.xlsx格式导出(方便上传到谷歌翻译)</CheckBox>
<CheckBox Name="SplitCheckBox"
Margin="2"
Visibility="Collapsed"
IsEnabled="False">未翻译文本过多(大于50K行时)时拆分为多个文件(防止谷歌翻译失败)</CheckBox>
<CheckBox Name="UseCompactCheckBox"
Margin="2"
Visibility="Collapsed"
IsEnabled="False">使用压缩+索引的存储格式(导出文本较少,机翻结果更统一,但是逻辑性更弱)</CheckBox>

<CheckBox Name="AutoSortCheckBox"
Margin="2"
IsEnabled="False">导出时将经术语表处理后不包含ASCII字符的文本集中到一起</CheckBox>
<CheckBox Name="AutoRemoveSpaceCheckBox"
Margin="2"
IsEnabled="False">导入时自动移除中文附近的空格</CheckBox>





<StackPanel Orientation="Horizontal"
FlowDirection="RightToLeft"
Height="50">
Expand Down
13 changes: 9 additions & 4 deletions core/Glossary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class RegexMatchRule
private IOrderedEnumerable<KeyValuePair<string, string>>? orderedPhaseDict = null;

private List<RegexMatchRule> _NormalRegexList = new List<RegexMatchRule>();

//TODO: 分两类,支持正则和字符串

private bool _IgnoreCase = false;
Expand Down Expand Up @@ -56,7 +57,10 @@ public bool Load(IEnumerable<string> glossaryFileNames)
var phaseDict = dict["phases"];
foreach (var kv in phaseDict)
{
phaseKV.TryAdd(kv.Key, kv.Value);
//如果忽略大小写,就统一使用小写字母来构建正则,方便之后做替换
//否则使用原样
var key = this._IgnoreCase ? kv.Key.ToLower() : kv.Key;
phaseKV.Add(key, kv.Value);
}
}
//读取正则表
Expand All @@ -76,13 +80,12 @@ public bool Load(IEnumerable<string> glossaryFileNames)
}


//build regex
//排序,优先替换更长的短语组
orderedPhaseDict = phaseKV.OrderByDescending(kv => kv.Key.Length); //先匹配字符串
//string regex
//构建字符替换的正则,\b(p1|p2|p3....)按照单词边界匹配,排序是为了优先匹配长的
var phasePattern = @"\b(" + string.Join("|", orderedPhaseDict.Select(kv => Regex.Escape(kv.Key))) + @")\b";
this._phaseRegex = new Regex(phasePattern, _IgnoreCase ? RegexOptions.IgnoreCase : RegexOptions.None);
//normal regex
//构建普通正则短语组
try
{
foreach (var regexString in regexKV)
Expand All @@ -100,6 +103,7 @@ public bool Load(IEnumerable<string> glossaryFileNames)
Logger.Info("无法初始化正则表达式: " + e.Message);
}
Logger.Info($"共发现{phaseKV.Count} 个短语以及 {_NormalRegexList.Count}个正则表达式");
Logger.Debug($"{phaseKV.ToArray()[0].Key}");
return true;
}

Expand All @@ -110,6 +114,7 @@ private string ProcessOne(string input)
var output = this._phaseRegex?.Replace(input, match =>
{
var key = match.Value;
if (this._IgnoreCase) key = key.ToLower(); //如果是忽视大小写模式,则根据小写进行查询,这里和上面的做统一
if (orderedPhaseDict == null) return key;
var dictEntry = orderedPhaseDict.FirstOrDefault(kv => key.Equals(kv.Key));
return dictEntry.Value ?? "[ERROR KEY]";
Expand Down
2 changes: 1 addition & 1 deletion glossaries/eldenring.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"hero's": "英雄的"
},
"regex": {
"(R|r)unes?": "卢恩"
"(R|r)unes": "卢恩"
}
}

0 comments on commit b955448

Please sign in to comment.