finish 分句及bridge打通

This commit is contained in:
LittleBoy 2024-02-03 19:58:48 +08:00
parent ebce23c6c4
commit 8020ba1910
24 changed files with 173 additions and 315 deletions

4
.gitignore vendored
View File

@ -1,3 +1,5 @@
/node_modules/
bin
packages
packages
TestConsoleApp
.vs

View File

@ -134,6 +134,9 @@
<Reference Include="Microsoft.Web.WebView2.Wpf, Version=1.0.2210.55, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Web.WebView2.1.0.2210.55\lib\net45\Microsoft.Web.WebView2.Wpf.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
@ -204,6 +207,9 @@
<Compile Include="Controls\ProofreadMainControl.Designer.cs">
<DependentUpon>ProofreadMainControl.cs</DependentUpon>
</Compile>
<Compile Include="core\DocumentText.cs" />
<Compile Include="core\StringUtil.cs" />
<Compile Include="core\Tools.cs" />
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
</Compile>
@ -270,15 +276,9 @@
<None Include="Resources\clear.png" />
</ItemGroup>
<ItemGroup>
<Content Include="dist\assets\index-5bULWyUE.css" />
<Content Include="dist\assets\index-OrzmOt5X.js" />
<Content Include="dist\assets\react-libs-HP15TLKb.js" />
<Content Include="dist\index.html" />
<Content Include="dist\vite.svg" />
<None Include="Resources\setting.png" />
<None Include="Resources\lexicon.png" />
</ItemGroup>
<ItemGroup />
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

View File

@ -1,18 +1,54 @@
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace WordAddInTest2024
{
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class Bridge
{
public static Bridge bridge = new Bridge();
private static Dictionary<string, WebView2> webViewDict = new Dictionary<string, WebView2>();
private static readonly Dictionary<string, WebView2> webViewDict = new Dictionary<string, WebView2>();
public void showDialog(string message)
{
System.Windows.Forms.MessageBox.Show(message);
}
// 获取文档所有文本数据
public string getAllText()
{
return Tools.GetAllText();
}
public void noticeOtherWeb(string json, string targetWebName)
{
if (targetWebName != null)
{
if (webViewDict.ContainsKey(targetWebName))
{
SendMessageToWeb(webViewDict[targetWebName], json);
}
}
else
{
// 如果没有指定 则向所有 webview 发送消息
foreach (var item in webViewDict)
{
SendMessageToWeb(item.Value, json);
}
}
}
private void SendMessageToWeb(WebView2 webView, string json)
{
webView.CoreWebView2.PostWebMessageAsJson(json);
}
public async static void InitWebEnvAsync(string name, WebView2 webView)
{
webView.Name = name;
@ -22,14 +58,18 @@ namespace WordAddInTest2024
}
else
{
webViewDict.Add(name,webView);
webViewDict.Add(name, webView);
}
// 禁用web安全允许跨域 否则需要web编译为umd加载模式
var ops = new CoreWebView2EnvironmentOptions("--disable-web-security");
var env = await CoreWebView2Environment.CreateAsync(null, null, ops);
var env = await CoreWebView2Environment.CreateAsync(null, Config.WEB_DATA_PATH, ops);
await webView.EnsureCoreWebView2Async(env);
//webView.CoreWebView2.Settings.AreDevToolsEnabled = false;
//webView.CoreWebView2.Settings.AreDefaultScriptDialogsEnabled = false;
//webView.CoreWebView2.Settings.AreHostObjectsAllowed = true;
// 添加 js与客户端代理
webView.CoreWebView2.AddHostObjectToScript("bridge", bridge);
}
}

View File

@ -20,6 +20,7 @@ namespace WordAddInTest2024
/// 词库地址
/// </summary>
public static readonly string LEXICON_PATH = "https://ksrm.gachafun.com/lexicon";
public static readonly string WEB_DATA_PATH = AppDomain.CurrentDomain.BaseDirectory + "userdata";
public static string WebPath(string path)
{

View File

@ -40,7 +40,7 @@
this.web.Dock = System.Windows.Forms.DockStyle.Fill;
this.web.Location = new System.Drawing.Point(0, 0);
this.web.Name = "web";
this.web.Size = new System.Drawing.Size(430, 457);
this.web.Size = new System.Drawing.Size(434, 461);
this.web.TabIndex = 0;
this.web.ZoomFactor = 1D;
//

View File

@ -1,11 +1,4 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WordAddInTest2024.Controls
@ -15,10 +8,9 @@ namespace WordAddInTest2024.Controls
public FormLogin()
{
InitializeComponent();
Bridge.InitWebEnvAsync("login",web);
Bridge.InitWebEnvAsync("login", web);
}
private void FormLogin_Load(object sender, EventArgs e)
{
this.web.Source = new Uri(Config.WebPath("#login"));

View File

@ -16,7 +16,7 @@ namespace WordAddInTest2024.Controls
public ProofreadMainControl()
{
InitializeComponent();
Bridge.InitWebEnvAsync("main",web);
Bridge.InitWebEnvAsync("main", web);
}

View File

@ -14,7 +14,7 @@ namespace WordAddInTest2024
public DocumentText() { }
public DocumentText(byte[] hash, string text)
{
this.Hash = BitConverter.ToString(hash);
this.Hash = BitConverter.ToString(hash).ToLower().Replace("-", "");
this.Text = text;
}
}

View File

@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace WordAddInTest2024
{
public class StringUtil
{
/// <summary>
/// 分句
/// </summary>
/// <param name="para"></param>
/// <returns></returns>
static string[] CutParagraphSentences(string para)
{
para = Regex.Replace(para, @"([。!?\?])([^”’])", "$1\x01$2", RegexOptions.Multiline);
para = Regex.Replace(para, @"(\.{6})([^”’])", "$1\x01$2", RegexOptions.Multiline);
para = Regex.Replace(para, @"(\…{2})([^”’])", "$1\x01$2", RegexOptions.Multiline);
para = Regex.Replace(para, @"([。!?\?][”’])([^,。!?\?])", "$1\x01$2", RegexOptions.Multiline);
//para = para.TrimEnd('\n');
return para.Split(new char[] { '\x01' }, StringSplitOptions.None);
}
static List<string> PreProcessList(string[] paragraphs)
{
List<string> list = new List<string>();
foreach (var text in paragraphs)
{
if (text.Length == 0)
{
list[list.Count - 1] += "\n";
continue;
}
list.Add(text + "\n");
}
return list;
}
static List<string> AfterProcessList(string[] paragraphs)
{
List<string> list = new List<string>();
foreach (var text in paragraphs)
{
if (Regex.Match(text, "^\n$").Success)
{
list[list.Count - 1] += "\n";
continue;
}
list.Add(text);
}
return list;
}
/// <summary>
/// 文本进行分句
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static List<string> CutTextToSentences(string text)
{
List<string> result = new List<string>();
var paragSplitor = new string[] { "\r", "\n", "\r\n" };
// 先进行分段 方便后续将换行符放入到当前段落的最后一句
var paragraphs = PreProcessList(text.Split(paragSplitor, StringSplitOptions.None));
foreach (var paragraph in paragraphs)
{
// 分句
var list = CutParagraphSentences(paragraph);
// 将换行符放入到当前段落的最后一句
result.AddRange(AfterProcessList(list));
}
return result;
}
}
}

View File

@ -1,29 +1,12 @@
using Microsoft.Office.Interop.Word;
using Newtonsoft.Json;
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace WordAddInTest2024
{
public class Tools
{
public static List<string> CutSentences(string para)
{
para = Regex.Replace(para, @"([。!?\?])([^”’])", "$1\n$2", RegexOptions.Multiline);
para = Regex.Replace(para, @"(\.{6})([^”’])", "$1\n$2", RegexOptions.Multiline);
para = Regex.Replace(para, @"(\…{2})([^”’])", "$1\n$2", RegexOptions.Multiline);
para = Regex.Replace(para, @"([。!?\?][”’])([^,。!?\?])", "$1\n$2", RegexOptions.Multiline);
para = para.TrimEnd('\n');
return para.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
public static string GetAllText()
{
// 获取当前文档所有文本
@ -32,31 +15,21 @@ namespace WordAddInTest2024
if (allText != null && allText.Length > 0)
{
string[] splitor = { "\r\n", "\r", "\n" };
// 开始分割
string[] lines = allText.Split(splitor, StringSplitOptions.RemoveEmptyEntries);
MD5 md5 = new MD5CryptoServiceProvider();
foreach (string line in lines)
List<string> lines = StringUtil.CutTextToSentences(allText);
foreach (string text in lines)
{
//var sentenceArr = Regex.Split(line, "(?<=[。|.])");
var i = 0;
//foreach (var sentence in sentenceArr)
//{
// string text = sentence + (i + 1 == sentenceArr.Length ? "\r\n" : "");
// byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(text));
// list.Add(new DocumentText(hash, text));
//}
var matches = Regex.Matches(line, "。");
foreach (Match match in matches)
{
string text = match.Value + (i + 1 == matches.Count ? "\n" : "");
byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(text));
list.Add(new DocumentText(hash, text));
}
byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(text));
list.Add(new DocumentText(hash, text));
}
}
return GetJSONString(list);
var map = new Dictionary<string, object>
{
{ "list", list },
{ "text", allText }
};
return GetJSONString(map);
}
public static string GetJSONString(object data)

View File

@ -1 +0,0 @@
.login-form{padding:10px}.login-form-button{margin-top:20px}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,15 +0,0 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="./vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
<script type="module" crossorigin src="./assets/index-OrzmOt5X.js"></script>
<link rel="modulepreload" crossorigin href="./assets/react-libs-HP15TLKb.js">
<link rel="stylesheet" crossorigin href="./assets/index-5bULWyUE.css">
</head>
<body>
<div id="root"></div>
</body>
</html>

View File

@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>

Before

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -1 +1 @@
f8fc04f6cedea53f8bf5057ddd2cd790d7e32d69
807f1761530d4d967d185da482c785b09fff46bd8d94791c4c6c8c487bc9b51f

View File

@ -22,3 +22,28 @@ C:\Users\home-dev\source\repos\WordAddInTest2024\WordAddInTest2024\bin\Debug\Mic
C:\Users\home-dev\source\repos\WordAddInTest2024\WordAddInTest2024\obj\Debug\AIProofread.csproj.CopyComplete
C:\Users\home-dev\source\repos\WordAddInTest2024\WordAddInTest2024\obj\Debug\AIProofread.dll
C:\Users\home-dev\source\repos\WordAddInTest2024\WordAddInTest2024\obj\Debug\AIProofread.pdb
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\bin\Debug\runtimes\win-x86\native\WebView2Loader.dll
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\bin\Debug\runtimes\win-x64\native\WebView2Loader.dll
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\bin\Debug\runtimes\win-arm64\native\WebView2Loader.dll
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\bin\Debug\AIProofread.dll
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\bin\Debug\AIProofread.pdb
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\bin\Debug\AIProofread.dll.manifest
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\bin\Debug\AIProofread.vsto
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\bin\Debug\Microsoft.Office.Tools.Common.v4.0.Utilities.dll
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\bin\Debug\Microsoft.Web.WebView2.Core.dll
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\bin\Debug\Microsoft.Web.WebView2.WinForms.dll
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\bin\Debug\Microsoft.Web.WebView2.Wpf.dll
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\bin\Debug\Newtonsoft.Json.dll
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\bin\Debug\Microsoft.Web.WebView2.Core.xml
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\bin\Debug\Microsoft.Web.WebView2.WinForms.xml
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\bin\Debug\Microsoft.Web.WebView2.Wpf.xml
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\obj\Debug\AIProofread.csproj.AssemblyReference.cache
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\obj\Debug\WordAddInTest2024.Controls.FormLogin.resources
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\obj\Debug\WordAddInTest2024.Controls.ProofreadMainControl.resources
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\obj\Debug\WordAddInTest2024.Properties.Resources.resources
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\obj\Debug\WordAddInTest2024.Ribbon1.resources
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\obj\Debug\AIProofread.csproj.GenerateResource.cache
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\obj\Debug\AIProofread.csproj.CoreCompileInputs.cache
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\obj\Debug\AIProofread.csproj.CopyComplete
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\obj\Debug\AIProofread.dll
C:\Users\yaclt\source\repos\repos\AIProofread\AIProofread\obj\Debug\AIProofread.pdb

Binary file not shown.

Binary file not shown.