예제 #1
0
        private void Main_Run_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {
                button7.Enabled     = true;
                searchSubs.Enabled  = true;
                button5.Enabled     = true;
                button9.Enabled     = true;
                button2.Enabled     = false;
                folderPath.ReadOnly = false;
                button11.Enabled    = true;
                button12.Enabled    = true;
                savePath.ReadOnly   = false;
                fileType.Enabled    = true;
                fileType.Checked    = false;
                UpdateProgressTextDelegate   UpdateText     = ProgressLabel;
                UpdateStatusTextDelegate     UpdateStatus   = StatusLabel;
                UpdateStatusProgressDelegate UpdateProgress = Progress;
                if (main.IsBusy)
                {
                    Invoke(UpdateText, "Stopping..");
                    Invoke(UpdateStatus, "Stopping..");
                }
                Invoke(UpdateText, "Complete.");
                Invoke(UpdateStatus, "");

                Invoke(UpdateProgress, 0);
                string pages = servRef.GetPagesLeft(clientID, secret);
                label8.Text         = "Pages left: " + pages;
                languages.Enabled   = true;
                progressBar.Visible = false;
                folderPath.Text     = "";
                //reset away mode
                Kernel32.SetThreadExecutionState(Kernel32.EXECUTION_STATE.None |
                                                 Kernel32.EXECUTION_STATE.None |
                                                 Kernel32.EXECUTION_STATE.None);
                MessageBox.Show("Run Complete.", "Quill", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                //assume worker closed
                MessageBox.Show("Oops. Something went wrong.", "Quill", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
예제 #2
0
 public void UpdateStatusText(String Text)
 {
     if (lblInstance.InvokeRequired)
     {
         UpdateStatusTextDelegate ustd = new UpdateStatusTextDelegate(UpdateStatusText);
         if (!lblInstance.IsDisposed && !lblInstance.Disposing)
         {
             try
             {
                 lblInstance.Invoke(ustd, Text);
             }
             catch (InvalidOperationException ex)
             {
                 String s = ex.Message;
             }
         }
     }
     else
     {
         lblInstance.Text = Text;
         Application.DoEvents();
     }
 }
예제 #3
0
        void _manager_SwipeDetected(object sender, SwipeGestureRecognisedEventArgs e)
        {
            string text = "State: Swipe Detected of type" + e.DetectedGesture;
            System.Windows.Threading.Dispatcher pdDispatcher = lblState.Dispatcher;
            //create a new delegate for updating our progress text
            UpdateStatusTextDelegate update = new UpdateStatusTextDelegate(UpdateStatusText);
            //invoke the dispatcher and pass the percentage and max record count
            pdDispatcher.BeginInvoke(update, text);

            tabDispatcher = tabControl1.Dispatcher;
            UpdateTabControlDelegate updateTab = new UpdateTabControlDelegate(UpdateTabControl);
            tabDispatcher.BeginInvoke(updateTab, e.DetectedGesture);
        }
예제 #4
0
 void _manager_StillDetected(object sender, StillGestureRecognisedEventArgs e)
 {
     string text = "State: Still hand Detected";
     System.Windows.Threading.Dispatcher pdDispatcher = lblState.Dispatcher;
     //create a new delegate for updating our progress text
     UpdateStatusTextDelegate update = new UpdateStatusTextDelegate(UpdateStatusText);
     //invoke the dispatcher and pass the percentage and max record count
     pdDispatcher.BeginInvoke(update, text);
 }
예제 #5
0
        private void Main_Run_DoWork(object sender, DoWorkEventArgs e)
        {
            #region Set Up Run
            DataTable finishedRun = new DataTable();
            finishedRun.Columns.Add("Digitised Text");
            finishedRun.Columns.Add("Translated Text");
            finishedRun.Columns.Add("Fields Found");
            finishedRun.Columns.Add("Clauses Found");

            Globals.dpi     = GetConfiguration.GetConfigurationValueDPI();
            Globals.meta    = GetConfiguration.GetConfigurationValueMeta();
            Globals.ocrType = GetConfiguration.GetConfigurationValueOCR();
            strLineRemoval  = GetConfiguration.GetConfigurationValueGrayScale();
            if (Globals.ocrType.Equals("Microsoft Cloud"))
            {
                Globals.ocrType = "1";
            }
            else if (Globals.ocrType.Equals("Google Cloud"))
            {
                Globals.ocrType = "2";
            }
            else if (Globals.ocrType.Equals("AWS Textract"))
            {
                Globals.ocrType = "3";
            }
            else
            {
                Globals.ocrType = "0";
            }

            //Make sure thread does not send PC to sleep
            Kernel32.SetThreadExecutionState(Kernel32.EXECUTION_STATE.ES_CONTINUOUS |
                                             Kernel32.EXECUTION_STATE.ES_SYSTEM_REQUIRED |
                                             Kernel32.EXECUTION_STATE.ES_AWAYMODE_REQUIRED);
            string[] docList = null;
            if (fileType.Checked == false)
            {
                try
                {
                    if (searchSubDirects == true)
                    {
                        docList = Directory.GetFiles(folderPath.Text.Trim(), "*.*", SearchOption.AllDirectories);
                    }
                    else
                    {
                        docList = Directory.GetFiles(folderPath.Text.Trim(), "*.*");
                    }
                }
                catch
                {
                    MessageBox.Show("Unable to find Run directory..", "Quill", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    return;
                }
            }
            else
            {
                if (Globals.files != null)
                {
                    docList = Globals.files;
                }
                else
                {
                    MessageBox.Show("Please choose some files..", "Quill", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    return;
                }
            }
            docList = Array.FindAll(docList, isNotLockFile);
            if (!Directory.Exists(savePath.Text.Trim()))
            {
                MessageBox.Show("Unable to find Save directory..", "Quill", MessageBoxButtons.OK, MessageBoxIcon.Error);

                return;
            }
            if (docList.Length <= 0)
            {
                MessageBox.Show("Unable to find any files in the Run directory..", "Quill", MessageBoxButtons.OK, MessageBoxIcon.Error);

                return;
            }

            //Prepare Run
            string prepare = servRef.PrepareRun(clientID, secret, Globals.sqlCon);
            if (!prepare.ToUpper().Equals("SUCCESS"))
            {
                MessageBox.Show("Oops. Something went wrong.", "Quill", MessageBoxButtons.OK, MessageBoxIcon.Error);

                return;
            }
            #endregion
            string lastFile = docList[docList.Length - 1];
            foreach (string file in docList)
            {
                UpdateStatusProgressDelegate UpdateProgress = Progress;
                UpdateProgressTextDelegate   UpdateText     = ProgressLabel;
                UpdateStatusTextDelegate     UpdateStatus   = StatusLabel;
                UpdatePagesTextDelegate      UpdatePages    = PagesLabel;
                bool   convertedFile = false;
                string runFile       = file;
                string tempFile      = file;
                bool   corruptFile   = false;
                if (Path.GetExtension(file).ToUpper().Trim().Equals(".DOCX"))
                {
                    Invoke(UpdateStatus, "Resaving Word Document as PDF");
                    try
                    {
                        //check for mixed media
                        var wordApplication = new Microsoft.Office.Interop.Word.Application();
                        wordApplication.Visible = false;

                        var  document = wordApplication.Documents.Open(file, false, true);
                        Guid strGuid  = Guid.NewGuid();
                        //convert to pdf
                        string tempPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                        tempPath = Path.Combine(tempPath, "temps");
                        if (!Directory.Exists(tempPath))
                        {
                            Directory.CreateDirectory(tempPath);
                        }
                        string tempFileName = Path.GetFileNameWithoutExtension(file);
                        document.ExportAsFixedFormat(System.IO.Path.Combine(tempPath, strGuid + ".pdf"), WdExportFormat.wdExportFormatPDF);
                        runFile       = System.IO.Path.Combine(tempPath, strGuid + ".pdf");
                        convertedFile = true;

                        // Close word
                        wordApplication.Quit();
                        document        = null;
                        wordApplication = null;
                        GC.Collect();
                    }
                    catch (Exception exe)
                    {
                        if (wordWarning == false)
                        {
                            MessageBox.Show("Without Microsoft Word on this machine we will only be able to use the native text these types of document(.DOCX) document. To digitise any pictures in the document, please install Microsoft Word. Alternatively convert it to a .pdf and rerun Quill.", "Quill", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            wordWarning = true;
                        }
                    }
                }

                #region Loop Files
                if (cancelMain == true)
                {
                    break;
                }

                string fileName = Path.GetFileName(runFile);
                Invoke(UpdateProgress, 0);
                if (!string.IsNullOrEmpty(tempFile))
                {
                    Invoke(UpdateText, "Working on file: " + Path.GetFileName(tempFile));
                }
                else
                {
                    Invoke(UpdateText, "Working on file: " + fileName);
                }

                Invoke(UpdateStatus, "Transmitting File..");
                #region Send File
                //Convert to Bytes
                FileInfo     fi        = new FileInfo(runFile);
                long         numBytes  = fi.Length;
                FileStream   fs        = new FileStream(runFile, FileMode.Open, FileAccess.Read);
                BinaryReader br        = new BinaryReader(fs);
                byte[]       fileArray = br.ReadBytes((int)numBytes);
                br.Close();
                fs.Close();
                fs.Dispose();
                Invoke(UpdateStatus, "Transmitting");
                //Transmit File
                Invoke(UpdateProgress, 10);
                if (cancelMain == true)
                {
                    return;
                }
                string transmit            = servRef.SaveClientFile(fileArray, runFile, clientID, secret);
                bool   invalidTypeContinue = false;
                if (!transmit.ToUpper().Equals("SUCCESS"))
                {
                    DialogResult type = MessageBox.Show("Oops. Quill Can't convert: " + runFile + " Would you like to continue?", "Quill", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    if (DialogResult.No == type)
                    {
                        cancelMain = true;
                        break;
                    }
                    else
                    {
                        invalidTypeContinue = true;
                    }
                }
                if (cancelMain == true)
                {
                    break;
                }
                #endregion
                if (invalidTypeContinue == false)
                {
                    #region Get File ID
                    Invoke(UpdateStatus, "Get File ID");
                    string fileID = servRef.GetFileID(fileName, clientID, secret);
                    Invoke(UpdateProgress, 20);
                    #endregion

                    #region Check file type
                    Invoke(UpdateStatus, "Native Check..");
                    string native = servRef.NativeTextCheck(fileName, Globals.sqlCon, false, clientID, secret, fileID, Globals.meta, Globals.ignoreMeta);
                    if (native.Contains("QuillException: Document Limit Reached"))
                    {
                        MessageBox.Show("Document Limit Reached. You must purchase a license to continue, please visit www.QuillDigital.co.uk", "Quill", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    Invoke(UpdateProgress, 30);
                    //Digitise
                    string fullText = string.Empty;
                    #endregion
                    #region Digitise
                    if (native.ToUpper().Equals("TRUE"))
                    {
                        if (cancelMain == true)
                        {
                            break;
                        }
                        Invoke(UpdateStatus, "Getting Text..");
                        fullText = servRef.GetFullTextByID(fileID, clientID, secret);
                        if (fullText.Contains("QuillException: Document Limit Reached"))
                        {
                            MessageBox.Show("Document Limit Reached. You must purchase a license to continue, please visit www.QuillDigital.co.uk", "Quill", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                        fullText = Regex.Replace(fullText, @"(\r\n){2,}", Environment.NewLine);

                        Invoke(UpdateProgress, 40);
                    }
                    else
                    {
                        Invoke(UpdateStatus, "Digitising..");
                        try
                        {
                            if (cancelMain == true)
                            {
                                break;
                            }
                            Invoke(UpdateProgress, 40);
                            string digitise = servRef.Digitise(fileName, fileID, clientID, secret, Globals.sqlCon, Globals.ocrType, strLineRemoval, Globals.dpi, "0");
                            if (digitise.Contains("QuillException: Document limit reached"))
                            {
                                MessageBox.Show("Document Limit Reached. You must purchase a license or more pages to continue, please visit www.QuillDigital.co.uk", "Quill", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                return;
                            }
                            if (digitise.Contains("File Corrupt- unable to convert"))
                            {
                                corruptFile = true;
                            }
                        }
                        catch (Exception ex)
                        {
                            //wait here for retort
                        }
                        Invoke(UpdateProgress, 50);
                        Invoke(UpdateStatus, "Getting Text..");
                        if (cancelMain == true)
                        {
                            break;
                        }
                        if (corruptFile == false)
                        {
                            fullText = servRef.GetFullTextByID(fileID, clientID, secret);
                            if (fullText.Contains("QuillException: Document limit reached"))
                            {
                                MessageBox.Show("Document Limit Reached. You must purchase a license to continue, please visit www.QuillDigital.co.uk", "Quill", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                return;
                            }
                            fullText = Regex.Replace(fullText, @"(\r\n){2,}", Environment.NewLine);
                        }
                        else
                        {
                            fullText = "File Corrupt - unable to convert";
                        }
                    }
                    string clausesFound = string.Empty;
                    string fields       = string.Empty;
                    string translated   = string.Empty;
                    #endregion
                    if (corruptFile == false)
                    {
                        Invoke(UpdateProgress, 50);
                        #region Translate
                        //Translate
                        if (!translationlang.ToUpper().Trim().Equals("NONE"))
                        {
                            Invoke(UpdateProgress, 60);
                            Invoke(UpdateStatus, "Translating..");
                            if (cancelMain == true)
                            {
                                break;
                            }
                            translated = servRef.TranslateByFileID(clientID, secret, Globals.sqlCon, fileID, translationlang);
                            if (translated.Contains("QuillException: Document Limit Reached"))
                            {
                                MessageBox.Show("Document Limit Reached. You must purchase a license to continue, please visit www.QuillDigital.co.uk", "Quill", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                return;
                            }
                            translated = Regex.Replace(translated, @"(\r\n){2,}", Environment.NewLine);
                        }
                        #endregion

                        //extract fields
                        #region Extract Fields
                        if (extractFields.Checked == true)
                        {
                            if (cancelMain == true)
                            {
                                break;
                            }



                            Invoke(UpdateProgress, 70);
                            Invoke(UpdateStatus, "Extracting Fields..");



                            if (cancelMain == true)
                            {
                                break;
                            }
                            fields = servRef.ExtractFieldsByFileID(fileID, fileName, clientID, secret, Globals.sqlCon, "0", GetConfiguration.GetConfigurationValueFields(), "0");
                            if (fields.Contains("QuillException: Document Limit Reached"))
                            {
                                MessageBox.Show("Document Limit Reached. You must purchase a license to continue, please visit www.QuillDigital.co.uk", "Quill", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                return;
                            }
                            fields = Regex.Replace(fields, @"(\r\n){1,}", Environment.NewLine);
                        }
                        #endregion
                        if (cancelMain == true)
                        {
                            break;
                        }

                        #region Check Clauses
                        if (clauses.Checked == true)
                        {
                            Invoke(UpdateProgress, 80);
                            Invoke(UpdateStatus, "Extracting Clauses..");
                            string    clausesOUT     = GetConfiguration.GetConfigurationValueClauses();
                            DataTable dtclausesFound = servRef.CheckForClausesByFileID(clientID, secret, Globals.sqlCon, fileID, fileName, clausesOUT);
                            if (clausesFound.Contains("QuillException: Document Limit Reached"))
                            {
                                MessageBox.Show("Document Limit Reached. You must purchase a license to continue, please visit www.QuillDigital.co.uk", "Quill", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                return;
                            }

                            if (dtclausesFound.Rows.Count <= 0)
                            {
                                clausesFound = "No Clauses Found.";
                            }
                            else
                            {
                                clausesFound = string.Empty;
                                foreach (DataRow row in dtclausesFound.Rows)
                                {
                                    string tagOne = row["TagOne"].ToString();
                                    if (string.IsNullOrEmpty(tagOne))
                                    {
                                        tagOne = "Tag One not found..";
                                    }
                                    string tagTwo = row["TagTwo"].ToString();
                                    if (string.IsNullOrEmpty(tagTwo))
                                    {
                                        tagTwo = "Tag Two not found..";
                                    }
                                    string tagThree = row["TagThree"].ToString();
                                    if (string.IsNullOrEmpty(tagThree))
                                    {
                                        tagThree = "Tag Three not found..";
                                    }
                                    string tagFour = row["TagFour"].ToString();
                                    if (string.IsNullOrEmpty(tagFour))
                                    {
                                        tagFour = "Tag Four not found..";
                                    }
                                    string tagFive = row["TagFive"].ToString();
                                    if (string.IsNullOrEmpty(tagFive))
                                    {
                                        tagFive = "Tag Five not found..";
                                    }
                                    string clauseFound  = row["ClauseFound"].ToString();
                                    string probablility = row["Probability"].ToString();
                                    clausesFound = clausesFound + Environment.NewLine + tagOne + Environment.NewLine + tagTwo + Environment.NewLine + tagThree + Environment.NewLine + tagFour
                                                   + Environment.NewLine + tagFive + Environment.NewLine + "Levenstein Distance: " + probablility + Environment.NewLine + Environment.NewLine;
                                }
                            }
                            clausesFound = Regex.Replace(clausesFound, @"(\r\n){2,}", Environment.NewLine);
                            //need to extract clauses
                        }

                        #endregion
                    }
                    #region NLP
                    if (Globals.nlpAnns != null)
                    {
                        Invoke(UpdateProgress, 90);
                        Invoke(UpdateStatus, "Running NLP..");
                        foreach (string anns in Globals.nlpAnns)
                        {
                            if (!string.IsNullOrEmpty(anns.Trim()))
                            {
                                Invoke(UpdateStatus, "Running " + anns + "..");
                                string nlpOUT = servRef.ProcessQuillNLP(fileID, anns.ToLower().Trim(), clientID, secret);
                            }
                        }
                    }
                    #endregion


                    Invoke(UpdateProgress, 90);
                    Invoke(UpdateStatus, "Writing Report..");
                    #region Write Report

                    string xmlDoc    = string.Empty;
                    string xmlReport = servRef.GetReportByID(clientID, secret, fileID);

                    if (string.IsNullOrEmpty(tempFile))
                    {
                        xmlDoc = Path.GetFileNameWithoutExtension(file) + ".xml";
                        System.IO.File.WriteAllText(Path.Combine(savePath.Text, xmlDoc), xmlReport);
                    }
                    else
                    {
                        xmlDoc = Path.GetFileNameWithoutExtension(tempFile) + ".xml";
                        System.IO.File.WriteAllText(Path.Combine(savePath.Text, xmlDoc), xmlReport);
                    }
                    if (convertedFile == true)
                    {
                        File.Delete(runFile);
                    }
                    if (file.ToUpper().Trim().Equals(lastFile.ToUpper().Trim()) | tempFile.ToUpper().Trim().Equals(lastFile.ToUpper().Trim()))
                    {
                        break;
                    }
                    #endregion
                    #endregion
                }
                string pages = servRef.GetPagesLeft(clientID, secret);
                Invoke(UpdatePages, "Pages left: " + pages);
            }

            return;
        }