Skip to content

Commit

Permalink
添加了优化翻译类库(仅支持人名地名替换)、全局热键处理、修改了SQL处理类的异常机制
Browse files Browse the repository at this point in the history
  • Loading branch information
hanmin0822 committed Mar 29, 2020
1 parent 6e3934c commit b8ff978
Show file tree
Hide file tree
Showing 10 changed files with 382 additions and 3 deletions.
1 change: 1 addition & 0 deletions KeyboardMouseHookLibrary/GlobalHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace KeyboardMouseHookLibrary
{

//来源:https://www.cnblogs.com/CJSTONE/p/4961865.html
//未测试

/*
注意:
Expand Down
69 changes: 69 additions & 0 deletions KeyboardMouseHookLibrary/GlobalHotKey.cs
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();
}
}

}
}
1 change: 1 addition & 0 deletions KeyboardMouseHookLibrary/KeyboardMouseHookLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="GlobalHook.cs" />
<Compile Include="GlobalHotKey.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
10 changes: 10 additions & 0 deletions MisakaTranslator.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MecabHelperLibrary", "Mecab
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SQLHelperLibrary", "SQLHelperLibrary\SQLHelperLibrary.csproj", "{08A96F84-775C-4F7E-90B0-B54780A4DDB9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TransOptimizationLibrary", "TransOptimizationLibrary\TransOptimizationLibrary.csproj", "{A17B385F-8E6E-4B7D-B3EF-EC08C7A8D115}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -91,6 +93,14 @@ Global
{08A96F84-775C-4F7E-90B0-B54780A4DDB9}.Release|Any CPU.Build.0 = Release|Any CPU
{08A96F84-775C-4F7E-90B0-B54780A4DDB9}.Release|x64.ActiveCfg = Release|Any CPU
{08A96F84-775C-4F7E-90B0-B54780A4DDB9}.Release|x64.Build.0 = Release|Any CPU
{A17B385F-8E6E-4B7D-B3EF-EC08C7A8D115}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A17B385F-8E6E-4B7D-B3EF-EC08C7A8D115}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A17B385F-8E6E-4B7D-B3EF-EC08C7A8D115}.Debug|x64.ActiveCfg = Debug|Any CPU
{A17B385F-8E6E-4B7D-B3EF-EC08C7A8D115}.Debug|x64.Build.0 = Debug|Any CPU
{A17B385F-8E6E-4B7D-B3EF-EC08C7A8D115}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A17B385F-8E6E-4B7D-B3EF-EC08C7A8D115}.Release|Any CPU.Build.0 = Release|Any CPU
{A17B385F-8E6E-4B7D-B3EF-EC08C7A8D115}.Release|x64.ActiveCfg = Release|Any CPU
{A17B385F-8E6E-4B7D-B3EF-EC08C7A8D115}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
15 changes: 12 additions & 3 deletions SQLHelperLibrary/SQLHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class SQLHelper
{
private string m_ConnectionString;
private SQLiteConnection m_dbConnection;//数据库连接

private string errorInfo;//最后一次错误信息

/// <summary>
/// 初始化
Expand Down Expand Up @@ -47,7 +47,7 @@ public static void CreateNewDatabase(string Filepath)
}

/// <summary>
/// 执行一条非查询语句
/// 执行一条非查询语句,失败会返回-1,可通过getLastError获取失败原因
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns>返回影响的结果数</returns>
Expand All @@ -64,7 +64,8 @@ public int ExecuteSql(string sql)
catch (System.Data.SQLite.SQLiteException ex)
{
m_dbConnection.Close();
throw ex;
errorInfo = ex.Message;
return -1;
}
}

Expand Down Expand Up @@ -148,5 +149,13 @@ public List<List<string>> ExecuteReader(string sql, int columns)
throw new Exception(e.Message);
}
}

/// <summary>
/// 获取最后一次失败原因
/// </summary>
/// <returns></returns>
public string getLastError() {
return errorInfo;
}
}
}
41 changes: 41 additions & 0 deletions TransOptimizationLibrary/AfterTransHandle.cs
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;
}


}
}
47 changes: 47 additions & 0 deletions TransOptimizationLibrary/BeforeTransHandle.cs
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;
}
}
}
109 changes: 109 additions & 0 deletions TransOptimizationLibrary/NounTransOptimization.cs
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);");
}
}
}
36 changes: 36 additions & 0 deletions TransOptimizationLibrary/Properties/AssemblyInfo.cs
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")]
Loading

0 comments on commit b8ff978

Please sign in to comment.