西西軟件園多重安全檢測下載網站、值得信賴的軟件下載站!
西西首頁 電腦軟件 安卓軟件 電腦游戲 安卓游戲 排行榜 專題合集

糾錯幫【python文稿AI糾錯工具】

v1.1
  • 糾錯幫【python文稿AI糾錯工具】v1.1
  • 軟件大小:11.7M
  • 更新時間:2020-04-24 08:21
  • 軟件語言:中文
  • 軟件廠商:
  • 軟件類別:國產軟件 / 免費軟件 / 文件處理
  • 軟件等級:4級
  • 應用平臺:WinXP, Win7, win8
  • 官方網站:暫無
  • 應用備案:
好評:50%
壞評:50%

軟件介紹

糾錯幫【python文稿AI糾錯工具】,由論壇大神制作的一個論文糾錯工具,由Python語言編寫,可以檢查出文檔內的語句,錯字內容,選擇好文件導入后就可以一鍵進行檢測,能有效檢查出一些基礎錯誤,提高文章的準確率。本次放出大神制作的這個糾錯幫小程序工具下載。

糾錯幫【python文稿AI糾錯工具】

糾錯幫工具使用

選擇文件并加載

點擊開始檢測一鍵查詢

查詢結束后會顯示錯誤點并自動糾正

糾錯幫工具源碼

import PySimpleGUI as sg

from aip import AipNlp

from docx import Document

from docx.shared import RGBColor

import os

import pickle

import random

import time

class TextAIAnalyse(object):

    """

    :param

    """

    def __init__(self, doc_path, app_id, api_key, secret_key):

        """

        :param doc_path:文章路徑

        :param app_id: 應用id,自己去百度ai控制臺構建一個應用,就會有id了

        :param api_key:

        :param secret_key:

        """

        self.client = AipNlp(app_id, api_key, secret_key)

        self.document = Document(doc_path)

        self.doc_path = doc_path

        text_list1 = self.filter_style()

        self.text_list2 = self.filter_short_text(text_list1, 12)

    def filter_style(self):

        """

        樣式過濾

        :param

        """

        delete_style = ['Title', 'Heading 1', 'Quote']  # 去除標題,一級標題,圖表鏈接

        list1 = [x.text for x in self.document.paragraphs if x.style.name not in delete_style]

        return list1

    @staticmethod

    def filter_short_text(list1: list, length: int):

        """

        去除短文本

        :param list1: 列表格式的文本集

        :param length:最短文本長度

        """

        list2 = [x.strip() for x in list1]  # 去除兩邊空格

        list3 = [x for x in list2 if len(x) > length]

        return list3

    def split_text(self, list1: list):

        """

        對段落進行分句,粗糙分詞

        :param

        """

        list2 = []

        for x in list1:

            x_list1 = x.split('。')  # 以句號進行分詞,分號暫時不考慮

            for xx in x_list1:

                if xx[-1:] not in ['。', ';', ':']:  # 如果本局不是以句號結尾,則給它加上句號

                    xx += '。'

                    list2.append(xx)

        list3 = self.filter_short_text(list2, 10)

        return list3

    def split_text2(self, list1: list):

        """

        對段落進行分句,加上分號

        :param

        """

        list2 = []

        for x in list1:

            x_list1 = x.split('。')  # 以句號進行分詞

            for xx in x_list1:

                x_list2 = xx.split(';')  # 以中文分號進行分詞

                for xxx in x_list2:

                    if xxx[-1:] not in ['。', ';', ':']:  # 如果本局不是以句號結尾,則給它加上句號

                        xxx += '。'

                        list2.append(xxx)

        list3 = self.filter_short_text(list2, 10)

        return list3

    def ai_analyse(self, text1):

        """

        AI對句子進行糾錯

        :param

        """

        result1 = None

        try:

            result1 = self.client.ecnet(text1)

        except Exception as err:

            return False

        vet = result1['item']['vec_fragment']  # 可替換詞

        score = result1['item']['score']  # 評分

        if len(vet) == 0:

            return False  # 沒有錯誤

        # elif score > 0.5:  # 如果可信度

        else:

            return result1  # 返回分析

    def save_analyse(self, result):

        """

        :param

        """

        text = result['text']

        text_encode = text.encode('gbk')

        vet_list = result['item']['vec_fragment']

        """ 開始寫入word """

        basename = os.path.basename(self.doc_path)

        path = './分析結果/{}.docx'.format(basename[:6])

        dir_name = './分析結果'

        if not os.path.exists(dir_name):  # 如果文件夾不存在

            os.mkdir(dir_name)

        a = vet_list[0]['begin_pos']  # 獲取第一個錯誤標簽的開始位置

        b = vet_list[-1]['end_pos']  # 獲取最后一個錯誤的結束位置

        if not os.path.exists(path):  # 如果文件不存在

            doc = Document()

        else:

            doc = Document(path)

        doc.add_paragraph('錯誤寫法', style='heading 1')  # 一級標題

        p = doc.add_paragraph()  # 創(chuàng)建一個空段落

        p.add_run(text_encode[:a].decode('gbk'))  # 寫入沒有錯誤的部分

        start_list = [x['begin_pos'] for x in vet_list]  # 記錄開始的位置

        end_list = [x['end_pos'] for x in vet_list]  # 記錄結束的位置

        if len(vet_list) == 1:  # 如果只有一個錯誤

            run1 = p.add_run(text_encode[start_list[0]:end_list[0]].decode('gbk'))

            run1.font.color.rgb = RGBColor(255, 0, 0)  # 設置紅色字體

        else:  # 如果有多個錯誤

            for i in range(len(vet_list)):

                run = p.add_run(text_encode[start_list:end_list[i]].decode('gbk'))

                run.font.color.rgb = RGBColor(255, 0, 0)  # 設置紅色字體

                if i < len(vet_list) - 1 and start_list[i + 1] - end_list[i] > 1:  # 如果后面一處錯誤與前面一處錯誤存在間距

                    p.add_run(text_encode[end_list:start_list[i + 1]].decode('gbk'))  # 增加一個沒有樣式的普通字體

        if len(text_encode) - b > 1:  # 如果最后一個錯誤后面存在正確的內容

            p.add_run(text_encode[b:].decode('gbk'))  # 寫入后面無錯的內容

        doc.add_paragraph('正確寫法', style='heading 1')  # 一級標題

        correct = result['item']['correct_query']  # 正確的內容

        doc.add_paragraph(correct)  # 寫入正確的內容

        doc.save(path)

if __name__ == '__main__':

    my_font = 'Deja_Vu_Sans_Mono.ttf'

    my_font_style1 = (my_font, 11, "normal")

    # 菜單欄

    menu_def = [

        ['&菜單', ['使用說明', '更新記錄']],

        ['&文件', ['載入配置', '保存配置']]

    ]

    # 布局欄

    layout1 = [

        [sg.Menu(menu_def, tearoff=True)],

        [sg.Text('APP_ID:', size=(12, None)), sg.Input(key='app_id')],

        [sg.Text('API_KEY', size=(12, None)), sg.Input(key='api_key')],

        [sg.Text('SECRET_KEY', size=(12, None)), sg.Input(key='secret_key')],

        [sg.Text('文件位置'), sg.Input(key='file_name', size=(51, None))],

        [sg.FileBrowse('選擇文件', target='file_name'), sg.Button('開始檢測'),

         sg.CBox('中文分號分句', default=False, key='split_type'), sg.Button('退出')]

    ]

    # 窗口欄

    windows1 = sg.Window('糾錯幫V1.1', layout=layout1, font=my_font_style1)

    for i in range(10):

        event1, value1 = windows1.read()

        if event1 in ('退出', None):

            break

        elif event1 == '使用說明':

            sg.popup('1.搜索百度AI開放平臺', '2.點擊控制臺,注冊并登錄', '3.選擇自然語音處理', '4.創(chuàng)建應用',

                     '5.填寫appid, api_key, secret_key', '6.選擇需要糾錯的文件', '7.點擊開始檢測', title='使用說明',

                     font=my_font_style1)

        elif event1 == '更新記錄':

            sg.popup(

                'V1.1更新記錄'

                '1.增加了分號分句功能',

                '2.增加了導出word對比功能',

                title='提示', font=my_font_style1)

        elif event1 == '保存配置':

            APP_ID = windows1['app_id'].get()

            API_KEY = windows1['api_key'].get()

            SECRET_KEY = windows1['secret_key'].get()

            file_path = windows1['file_name'].get()

            split_type = windows1['split_type'].get()

            if len(APP_ID) > 3 and len(API_KEY) > 5 and len(SECRET_KEY) > 5:

                dict1 = {

                    'app_id': APP_ID,

                    'api_key': API_KEY,

                    'secret_key': SECRET_KEY,

                    'split_type': split_type

                }

                with open('info.pkl', 'wb') as f:

                    pickle.dump(dict1, f)

                sg.popup('保存完畢', '已經生成一個info.pkl文件到本地', title='提示', auto_close=True,

                         auto_close_duration=3, font=my_font_style1)

            else:

                sg.popup('請檢查你的api相關信息是否填寫完成', title='錯誤提示', font=my_font_style1)

        elif event1 == '載入配置':

            if not os.path.exists('info.pkl'):

                sg.popup('沒有找到你的配置文件info.pkl', '請檢查你的文件是否在當前路徑', title='錯誤提示',

                         font=my_font_style1)

            else:

                with open('info.pkl', 'rb') as f:

                    dict2 = pickle.load(f)

                    windows1['app_id'].update(dict2['app_id'])

                    windows1['api_key'].update(dict2['api_key'])

                    windows1['secret_key'].update(dict2['secret_key'])

                    windows1['split_type'].update(dict2['split_type'])

                    sg.popup('配置文件載入完畢', title='提示', auto_close_duration=3, auto_close=True, font=my_font_style1)

        elif event1 == '開始檢測':

            APP_ID = windows1['app_id'].get()

            API_KEY = windows1['api_key'].get()

            SECRET_KEY = windows1['secret_key'].get()

            file_path = windows1['file_name'].get()

            split_type = windows1['split_type'].get()

            if len(APP_ID) > 3 and len(API_KEY) > 5 and len(SECRET_KEY) > 5:

                doc = TextAIAnalyse(file_path, APP_ID, API_KEY, SECRET_KEY)

                text_list2 = doc.text_list2

                if split_type:  # 如果選擇的True,也就是支持分號

                    text_list3 = doc.split_text(text_list2)

                else:

                    text_list3 = doc.split_text2(text_list2)

                sg.popup('開始檢測,共有{}句'.format(len(text_list3)),

                         '預計用時{}秒'.format(len(text_list3)*2), auto_close_duration=5, auto_close=True)

                layout2 = [

                    [sg.Text('處理進度條', font=my_font_style1),

                     sg.ProgressBar(len(text_list3), orientation='h', key='bar', size=(50, 20))],

                    [sg.Button('取消', font=my_font_style1)]

                ]

                windows2 = sg.Window(title='進度條', layout=layout2, font=my_font_style1)

                bar = windows2['bar']

                for ii in range(len(text_list3)):

                    event2, value2 = windows2.read(timeout=10)

                    if event2 in ('取消', None):

                        break

                    result2 = doc.ai_analyse(text_list3[ii])

                    print(text_list3[ii])

                    if bool(result2):

                        print(result2)

                        doc.save_analyse(result2)

                    time.sleep(0.5 + random.random() / 10)

                    bar.UpdateBar(ii + i)

                windows2.close()

                sg.popup('已經檢測完成', '并且生成了一個“分析結果”文件夾到本地', title='提示', font=my_font_style1)

            elif len(file_path) < 5:

                sg.popup('親!', '你還沒有選擇檢測的文件', title='提示', font=my_font_style1)

            else:

                sg.popup('請輸入你的api信息', '詳情請查看使用說明', title='提示',font=my_font_style1)

    windows1.close()

相關新聞

文本糾錯

文本糾錯目前可以做到糾正形近字、音近字、成語使用、量詞搭配、語法這些錯誤。

形近字糾錯

就比如把“時候”寫成了“時侯”,“領域”寫成“領城”。如果自己檢查的時候,很難發(fā)現(xiàn)形近字錯誤。

有了秘塔寫作貓,直接把word文檔上傳到里面,有什么錯誤直接就智能查找并顯示出來了。

軟件截圖

糾錯幫【python文稿AI糾錯工具】 v1.1

其他版本下載

發(fā)表評論

昵稱:
表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
查看所有(0)條評論 > 字數(shù): 0/500

TOP
軟件下載