forked from hanmin0822/MisakaTranslator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
添加了优化翻译类库(仅支持人名地名替换)、全局热键处理、修改了SQL处理类的异常机制
- Loading branch information
1 parent
6e3934c
commit b8ff978
Showing
10 changed files
with
382 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ namespace KeyboardMouseHookLibrary | |
{ | ||
|
||
//来源:https://www.cnblogs.com/CJSTONE/p/4961865.html | ||
//未测试 | ||
|
||
/* | ||
注意: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.InteropServices; | ||
using System.Windows.Forms; | ||
|
||
namespace KeyboardMouseHookLibrary | ||
{ | ||
|
||
//代码来源:https://www.cnblogs.com/margin-gu/p/5887853.html | ||
//未测试 | ||
|
||
public class GlobalHotKey | ||
{ | ||
//引入系统API | ||
[DllImport("user32.dll")] | ||
static extern bool RegisterHotKey(IntPtr hWnd, int id, int modifiers, Keys vk); | ||
[DllImport("user32.dll")] | ||
static extern bool UnregisterHotKey(IntPtr hWnd, int id); | ||
|
||
|
||
int keyid = 10; //区分不同的快捷键 | ||
Dictionary<int, HotKeyCallBackHanlder> keymap = new Dictionary<int, HotKeyCallBackHanlder>(); //每一个key对于一个处理函数 | ||
public delegate void HotKeyCallBackHanlder(); | ||
|
||
//组合控制键 | ||
public enum HotkeyModifiers | ||
{ | ||
Alt = 1, | ||
Control = 2, | ||
Shift = 4, | ||
Win = 8 | ||
} | ||
|
||
//注册快捷键 | ||
public void RegistGlobalHotKey(IntPtr hWnd, int modifiers, Keys vk, HotKeyCallBackHanlder callBack) | ||
{ | ||
int id = keyid++; | ||
if (!RegisterHotKey(hWnd, id, modifiers, vk)) | ||
throw new Exception("注册失败!"); | ||
keymap[id] = callBack; | ||
} | ||
|
||
// 注销快捷键 | ||
public void UnRegistGlobalHotKey(IntPtr hWnd, HotKeyCallBackHanlder callBack) | ||
{ | ||
foreach (KeyValuePair<int, HotKeyCallBackHanlder> var in keymap) | ||
{ | ||
if (var.Value == callBack) | ||
{ | ||
UnregisterHotKey(hWnd, var.Key); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
// 快捷键消息处理 | ||
public void ProcessHotKey(Message m) | ||
{ | ||
if (m.Msg == 0x312) | ||
{ | ||
int id = m.WParam.ToInt32(); | ||
HotKeyCallBackHanlder callback; | ||
if (keymap.TryGetValue(id, out callback)) | ||
callback(); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace TransOptimizationLibrary | ||
{ | ||
public class AfterTransHandle | ||
{ | ||
NounTransOptimization nto; | ||
BeforeTransHandle bth; | ||
|
||
public AfterTransHandle(BeforeTransHandle b) | ||
{ | ||
bth = b; | ||
} | ||
|
||
/// <summary> | ||
/// 外部调用的共用方法,自动进行翻译后处理 | ||
/// </summary> | ||
/// <param name="text">通过翻译接口翻译后的句子</param> | ||
/// <returns>处理后句子</returns> | ||
public string AutoHandle(string text) | ||
{ | ||
if (text == null || text == "") | ||
{ | ||
return ""; | ||
} | ||
|
||
//目前只支持名词的预处理,处理后检查是否有对话人名,如果有则加上 | ||
if (bth.GetPeopleChatName() != "") { | ||
text = bth.GetPeopleChatName() + ": " + text; | ||
} | ||
|
||
return text; | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace TransOptimizationLibrary | ||
{ | ||
public class BeforeTransHandle | ||
{ | ||
|
||
NounTransOptimization nto; | ||
|
||
|
||
|
||
public BeforeTransHandle(string gameName,string srcLang,string dstLang) { | ||
if (!Directory.Exists(Environment.CurrentDirectory + "\\TransOptimization")) | ||
Directory.CreateDirectory(Environment.CurrentDirectory + "\\TransOptimization"); | ||
|
||
nto = new NounTransOptimization(gameName,srcLang,dstLang); | ||
} | ||
|
||
/// <summary> | ||
/// 外部调用的共用方法,自动进行翻译前处理 | ||
/// </summary> | ||
/// <param name="text">Hook或OCR得到的原句</param> | ||
/// <returns>处理后句子</returns> | ||
public string AutoHandle(string text) { | ||
if (text == null || text == "") { | ||
return ""; | ||
} | ||
|
||
//暂支持人名地名预处理 | ||
return nto.ReplacePeoPleLocNameInSentence(text); | ||
|
||
} | ||
|
||
/// <summary> | ||
/// 显示在结果中的对话人名 以 人名:对话 的形式展示 | ||
/// </summary> | ||
/// <returns></returns> | ||
public string GetPeopleChatName() { | ||
return nto.PeopleChatName; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using SQLHelperLibrary; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
|
||
namespace TransOptimizationLibrary | ||
{ | ||
public class NounTransOptimization | ||
{ | ||
public SQLHelper sqlite; | ||
private string srcLangCode; | ||
private string dstLangCode; | ||
public string PeopleChatName;//显示在结果中的对话人名 以 人名:对话 的形式展示 | ||
|
||
public NounTransOptimization(string gameName,string srcL,string dstL) { | ||
if (File.Exists(Environment.CurrentDirectory + "\\TransOptimization\\Misaka_" + gameName + ".sqlite") == false) | ||
{ | ||
CreateNewNounTransDB(gameName); | ||
} | ||
else { | ||
sqlite = new SQLHelper(Environment.CurrentDirectory + "\\TransOptimization\\Misaka_" + gameName + ".sqlite"); | ||
} | ||
srcLangCode = srcL; | ||
dstLangCode = dstL; | ||
} | ||
|
||
|
||
|
||
/// <summary> | ||
/// 处理句子中间出现的人名地名,进行直接替换 | ||
/// </summary> | ||
/// <param name="text"></param> | ||
/// <returns></returns> | ||
public string ReplacePeoPleLocNameInSentence(string text) { | ||
//先从库中查找已经定义的人名地名列表 | ||
List<List<string>> lst = sqlite.ExecuteReader("SELECT source,userTrans,type FROM NounTransOpt WHERE type = 1 OR type = 2;", 3); | ||
|
||
PeopleChatName = ""; | ||
|
||
//直接替换 | ||
for (int i = 0;i < lst.Count;i++) { | ||
List<string> l = lst[i]; | ||
|
||
if (l[2] == "1") { | ||
//如果是人名 | ||
|
||
if (text.IndexOf(l[0]) == 0) { | ||
//出现在首部 | ||
text.Remove(0,l[0].Length); | ||
PeopleChatName = l[1]; | ||
} | ||
else if (text.LastIndexOf(l[0]) == text.Length - l[0].Length) | ||
{ | ||
//出现在尾部 | ||
text.Remove(text.LastIndexOf(l[0]), l[0].Length); | ||
PeopleChatName = l[1]; | ||
} | ||
} | ||
|
||
text = text.Replace(l[0],l[1]); | ||
} | ||
return text; | ||
} | ||
|
||
/// <summary> | ||
/// 添加一条用户自定义名词翻译 | ||
/// </summary> | ||
/// <param name="source">源单词</param> | ||
/// <param name="type">类型:1=人名 2=地名 3=专有名词(暂不支持)</param> | ||
/// <param name="userTrans">用户定义的翻译结果</param> | ||
/// <param name="machineTrans">机器翻译的结果(可空)</param> | ||
/// <returns>添加结果,如果已存在则失败</returns> | ||
public bool AddNounTrans(string source,int type,string userTrans,string machineTrans = "") { | ||
string sql = string.Format("INSERT INTO NounTransOpt VALUES('{0}','{1}',{2},'{3}','{4}','{5}');",source,srcLangCode,type,userTrans,dstLangCode,machineTrans); | ||
if (sqlite.ExecuteSql(sql) > 0) | ||
{ | ||
return true; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// (未实现)替换通过Mecab分词得到的名词 | ||
/// </summary> | ||
/// <param name="text"></param> | ||
/// <returns></returns> | ||
public string ReplaceNounInSentence(string text) { | ||
return text; | ||
|
||
} | ||
|
||
/// <summary> | ||
/// 新建一个名词翻译数据库(一个游戏一个库) | ||
/// </summary> | ||
/// <param name="gameName"></param> | ||
private void CreateNewNounTransDB(string gameName) { | ||
SQLHelper.CreateNewDatabase(Environment.CurrentDirectory + "\\TransOptimization\\Misaka_" + gameName + ".sqlite"); | ||
sqlite = new SQLHelper(Environment.CurrentDirectory + "\\TransOptimization\\Misaka_" + gameName + ".sqlite"); | ||
sqlite.ExecuteSql("CREATE TABLE NounTransOpt(source TEXT PRIMARY KEY,src_lang TEXT,type INT,userTrans TEXT,dst_lang TEXT,machineTrans TEXT);"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// 有关程序集的一般信息由以下 | ||
// 控制。更改这些特性值可修改 | ||
// 与程序集关联的信息。 | ||
[assembly: AssemblyTitle("TransOptimizationLibrary")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("TransOptimizationLibrary")] | ||
[assembly: AssemblyCopyright("Copyright © 2020")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// 将 ComVisible 设置为 false 会使此程序集中的类型 | ||
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 | ||
//请将此类型的 ComVisible 特性设置为 true。 | ||
[assembly: ComVisible(false)] | ||
|
||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID | ||
[assembly: Guid("a17b385f-8e6e-4b7d-b3ef-ec08c7a8d115")] | ||
|
||
// 程序集的版本信息由下列四个值组成: | ||
// | ||
// 主版本 | ||
// 次版本 | ||
// 生成号 | ||
// 修订号 | ||
// | ||
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 | ||
//通过使用 "*",如下所示: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
Oops, something went wrong.