提升定位准确度(先按段落查找)

This commit is contained in:
LittleBoy 2024-05-13 23:08:17 +08:00
parent a03a5f4348
commit 2bf992844a
11 changed files with 88 additions and 26 deletions

Binary file not shown.

View File

@ -7,7 +7,7 @@
<SupportUrlHistory />
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<StartAction>Program</StartAction>
<StartProgram>C:\Soft\Kingsoft\WPS Office\12.1.0.16412\office6\wps.exe</StartProgram>
<StartAction>Project</StartAction>
<StartProgram>C:\Soft\Kingsoft\WPS Office\12.1.0.16910\office6\wps.exe</StartProgram>
</PropertyGroup>
</Project>

View File

@ -42,7 +42,7 @@ namespace AIProofread
private static readonly Dictionary<BridgeEvent, List<BridgeEventHandler>> eventHandlers = new Dictionary<BridgeEvent, List<BridgeEventHandler>>();
private static readonly Dictionary<int, ProofreadItem> marks = new Dictionary<int, ProofreadItem>();
public static Dictionary<int, ProofreadItem> marks;
private static int selectProofreadId = -1;
@ -215,14 +215,19 @@ namespace AIProofread
// 清除所有标记
public void clearAllProofreadMark()
{
// 设置当前文档数据
Globals.ThisAddIn.ActiveCurrentDocumentMarks();
try
{
selectProofreadId = -1;
foreach (var item in marks.Values)
{
if (item.mark != null && item.content.tag == "i" && item.content.isAccept == AcceptStatus.Default)
if (item.mark != null)
{
item.mark.Text = "";
if(item.content.tag == "i" && item.content.isAccept == AcceptStatus.Default) {
item.mark.Text = "";
}
item.ResetMarkStyle();
}
}
marks.Clear();
@ -363,6 +368,9 @@ namespace AIProofread
public void SelectMarkById(int proofreadId)
{
// 设置当前文档数据
Globals.ThisAddIn.ActiveCurrentDocumentMarks();
if (proofreadId == selectProofreadId) return;
var doc = Globals.ThisAddIn.Application.ActiveDocument;
// 取消上一个标签移除
@ -422,6 +430,7 @@ namespace AIProofread
List<DocumentCorrectItem> list = JsonConvert.DeserializeObject<List<DocumentCorrectItem>>(content);
// 先清除所有数据
clearAllProofreadMark();
//var app = Globals.ThisAddIn.Application;
//var cur = app.Selection;
//
@ -439,7 +448,7 @@ namespace AIProofread
int index = 0;
foreach (var item in correct.Diffs)
{
var mark = AddBookmark(item, index, correct.Offset, correct.Insert_len);
var mark = AddBookmark(item, index, correct.Sentence_offset, correct.Insert_len, correct.Paragraph_offset);
if (item.tag != "i") index++;
if (mark != null) {
marks.Add(item.id, new ProofreadItem(item, mark));
@ -465,22 +474,50 @@ namespace AIProofread
}
public Microsoft.Office.Tools.Word.Bookmark AddBookmark(CorrectedContent item, int findIndex, int offset, int length)
public string GetParagraph(int index)
{
var document = Globals.ThisAddIn.Application.ActiveDocument;
try
{
var paragraphs = document.Paragraphs;
var total = paragraphs.Count;
// 判断索引是否超出范围
if (index > total)
{
return null;
}
var paragraph = document.Paragraphs[index];
return paragraph.Range.Text;
}
catch (Exception ex)
{
return "error:" + ex.Message;
}
}
public Microsoft.Office.Tools.Word.Bookmark AddBookmark(CorrectedContent item, int findIndex, int offset, int length,int paragraphIndex)
{
Microsoft.Office.Tools.Word.Bookmark bookmark = null;
try
{
var document = Globals.ThisAddIn.Application.ActiveDocument;
ControlCollection controls = Globals.Factory.GetVstoObject(document).Controls;
var Start = offset;
var End = offset + length - 1;
// 整体选区的内容
// 判断并获取当前段落
if (paragraphIndex > document.Paragraphs.Count) return null;
var paragraph = document.Paragraphs[paragraphIndex];
var paragraphStart = paragraph.Range.Start;
var Start = paragraphStart + offset;
var End = paragraph.Range.Start + length - 1;
// 当前句子的选区
var fullRange = document.Range(Start, End);
string fullText = fullRange.Text;
End = offset + item.start;
Start = offset + item.end;
Start = paragraphStart + offset + item.start;
End = paragraphStart + offset + item.end;
var r = document.Range(End, Start);
// 定位要操作的文字
var r = document.Range(Start, End);
var markName = Config.BuildBookmarkName(item.id);
// 判断是否已经存在

View File

@ -90,9 +90,10 @@ namespace UtilLib
mark.Shading.BackgroundPatternColor = (WdColor)ColorTranslator.ToOle(Colors.FromHex(content.color));
}
private void ResetMarkStyle()
public void ResetMarkStyle()
{
if (mark == null) return;
mark.Range.Font.Size = originSize; // 还原
//mark.Range.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineNone;
mark.Range.Shading.BackgroundPatternColor = Microsoft.Office.Interop.Word.WdColor.wdColorAutomatic;
}

View File

@ -46,6 +46,7 @@ namespace AIProofread
public Dictionary<Word.Document,bool> panelsVisible = new Dictionary<Word.Document, bool>();
public CustomTaskPane currentDocumentTaskPane;
private static readonly Dictionary<Word.Document, Dictionary<int, ProofreadItem>> allMarks = new Dictionary<Word.Document, Dictionary<int, ProofreadItem>>();
private void Application_WindowDeactivate(Word.Document doc, Window Wn)
{
@ -67,30 +68,51 @@ namespace AIProofread
}
private void Application_WindowActivate(Word.Document Doc, Window Wn)
private void Application_WindowActivate(Word.Document activeDoc, Window Wn)
{
Logger.Log("Application_WindowActivate -- " + Doc.FullName);
// 当前文档添加书签集合
if (!allMarks.ContainsKey(activeDoc))
{
allMarks[activeDoc] = new Dictionary<int, ProofreadItem>();
}
Logger.Log("Application_WindowActivate -- " + activeDoc.FullName);
//ShowPanel(Doc);
if (!taskPanels.ContainsKey(Doc))
if (!taskPanels.ContainsKey(activeDoc))
{
ShowPanel(Doc,false);
ShowPanel(activeDoc,false);
}
this.currentDocumentTaskPane = taskPanels[Doc];
this.currentDocumentTaskPane = taskPanels[activeDoc];
if (IsWPS)
{
HideOtherPanel(Doc);
HideOtherPanel(activeDoc);
}
if (panelsVisible.ContainsKey(Doc) && panelsVisible[Doc])
if (panelsVisible.ContainsKey(activeDoc) && panelsVisible[activeDoc])
{
taskPanels[Doc].Visible = true;
taskPanels[activeDoc].Visible = true;
}
}
private void Application_DocumentBeforeClose(Word.Document Doc, ref bool Cancel)
private void Application_DocumentBeforeClose(Word.Document currentDoc, ref bool Cancel)
{
Logger.Log("Application_DocumentBeforeClose -- " + Doc.FullName);
DisposePanel(Doc);
Logger.Log("Application_DocumentBeforeClose -- " + currentDoc.FullName);
if (allMarks.ContainsKey(currentDoc))
{
allMarks.Remove(currentDoc);
}
DisposePanel(currentDoc);
}
public void ActiveCurrentDocumentMarks()
{
// 判断是否存在 没有的话初始化
var currentDoc = Application.ActiveDocument;
if (!allMarks.ContainsKey(currentDoc))
{
allMarks[currentDoc] = new Dictionary<int, ProofreadItem>();
}
Bridge.marks = allMarks[currentDoc];
}
private void Application_NewDocument(Word.Document Doc)

View File

@ -9,6 +9,8 @@ namespace UtilLib
public string New_text { get; set; }
public int Insert_len { get; set; }
public int Offset { get; set; }
public int Paragraph_offset { get; set; }
public int Sentence_offset { get; set; }
public List<CorrectedContent> Diffs { get; set; }
}
}

View File

@ -9,7 +9,7 @@ namespace UtilLib
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++)
{
sb.Append(' ');
sb.Append(" ");
}
return sb.ToString();
}