public static bool GetReceiptTotalTopLevelOnly(BaiOcrLine ocr) { var newContent = RefineToMoney(ocr.Content); var length = newContent.Length; Regex moneyPattern = new Regex(@"[.\d,]+"); bool isMatched = (moneyPattern.Match(newContent).Value.Length == length); if (((length > 4 && GetPercentNumber(newContent) > 70) || (length > 3 && GetPercentNumber(newContent) == 100)) && isMatched) { string result = string.Empty; foreach (char c in newContent) { if (char.IsNumber(c) || char.IsDigit(c) || c == '.' || c == ',') { result += c; int len = result.Length; int index = (len - 2) - 1; if (index > 1) { if (result[index] == '.') { Global.CurrentReciept.Amount = result; return(true); } } } } } return(false); }
public static void GetReceiptTINValue(BaiOcrLine ocr) { var newContent = RefineToTIN(ocr.Content); var length = newContent.Length; if (length > 5) { Global.CurrentReciept.Tax_Identification = newContent; return; } else { int i = ocr.LineNo + 1; var nextLine = Global.OcrLines.Where(o => o.LineNo == i).FirstOrDefault(); if (nextLine == null || nextLine.LineNo == 0) { Global.CurrentReciept.Tax_Identification = RefineToTIN(Global.OcrLines.OrderByDescending(o => o.WeightedAsVendorTINTitle).First().Content); } else { GetReceiptTINValue(nextLine); } }; }
public static void GetReceiptTotalValue(BaiOcrLine ocr) { //do 1st level if (!GetReceiptTotalTopLevelOnly(ocr)) { //check next int i = ocr.LineNo + 1; var nextLine = Global.OcrLines.Where(o => o.LineNo == i).FirstOrDefault(); if (nextLine == null || nextLine.LineNo == 0) { Global.CurrentReciept.Amount = RefineToMoney(Global.OcrLines.OrderByDescending(o => o.WeightedAsTotalTitle).First().Content); return; } else { //do 2nd level if (!GetReceiptTotalTopLevelOnly(nextLine)) { GetReceiptTotalValue(nextLine); } } //if not found dig more... GetReceiptTotalValue(nextLine); } ; }
private void btnTest_Click(object sender, EventArgs e) { //TestCmd(); List <BaiOcrLine> testLines = new List <BaiOcrLine>(); BaiOcrLine ocr = new BaiOcrLine { Content = "Date:10/11/19 TIN:123456789 address: 154 bisalao st. Maligay Nov Q.C. Vendor: SEven eleven" }; testLines.Add(ocr); BaiOcrLine ocr1 = new BaiOcrLine { Content = "TOTAL:100 CHANGE:123 Cash: 500" }; testLines.Add(ocr1); List <BaiOcrLine> result = new List <BaiOcrLine>(); foreach (var thisOcr in testLines) { RocsTextService.RefineOcr(thisOcr, ref result); } int i = 0; foreach (BaiOcrLine item in result) { i += 1; item.LineNo = i; Console.WriteLine(string.Format("{0}. {1}", item.LineNo.ToString(), item.Content)); } }
public static void GetReceiptDateValue(BaiOcrLine ocr) { var newContent = RefineToDate(ocr.Content); var year = DateTime.Now.Date.Year; string strYear = year.ToString(); strYear = strYear.Substring(2, 2); var length = newContent.Length; if (length >= 8) { if (newContent.EndsWith(strYear)) { Global.CurrentReciept.Date = newContent; } } else { int i = ocr.LineNo + 1; var nextLine = Global.OcrLines.Where(o => o.LineNo == i).FirstOrDefault(); if (nextLine == null || nextLine.LineNo == 0) { Global.CurrentReciept.Date = RefineToDate(Global.OcrLines.OrderByDescending(o => o.WeightedAsDateTitle).First().Content); } else { GetReceiptDateValue(nextLine); } }; }
//remove all elected first then do this policy public static void AssertAsDetail(ref BaiOcrLine ocrLine) { var dim = ReceiptParts.Detail.ToString(); using (MyDBContext db = new MyDBContext()) { var wfactors = db.WeightFactors.ToList(); var factors = wfactors.Where(f => f.Dimension == dim); foreach (var f in factors) { if (ocrLine.Content.ToLower().Contains(f.keyWord.ToLower())) { ocrLine.WeightedAsProduct += f.Weight; } } } if (ocrLine.PercentNumber < 25) { ocrLine.WeightedAsAddress += 2; } if (ocrLine.Content.Contains(":")) { ocrLine.WeightedAsAddress -= 1; } }
public static void RefineOcr(BaiOcrLine ocr, ref List <BaiOcrLine> newLines) { //string totalContent = ocr.Content; var splitAll = ocr.Content.Split(':').ToList(); HashSet <string> hashResult = new HashSet <string>(); for (int i = 0; i < splitAll.Count; i++) { string content = string.Empty; content = splitAll[i]; Console.WriteLine(content); if (string.IsNullOrEmpty(content)) { continue; } content = content.Trim(); var head = content.Split().Last(); var first = content.Replace(head, string.Empty); if (splitAll.Count == 1) { head = splitAll[0]; // first = splitAll[1]; hashResult.Add(head); } else if (splitAll.Count == 2) { hashResult.Add(splitAll[0]); hashResult.Add(splitAll[1]); } else if (i == splitAll.Count - 1) { hashResult.Add(content); } else { if (!string.IsNullOrEmpty(first)) { hashResult.Add(first); } hashResult.Add(head); } } foreach (string l in hashResult) { BaiOcrLine ocr1 = new BaiOcrLine { Content = l }; newLines.Add(ocr1); Console.WriteLine(l); } }
public static double GetPercentLocation(BaiOcrLine ocr) { var totalCnt = Convert.ToDouble(Global.OcrLines.Count()); var i = Convert.ToDouble(ocr.LineNo); double percent = i / totalCnt; return(percent * 100); }
public static void AssertAsDateTitle(ref BaiOcrLine ocrLine) { var dim = ReceiptParts.DateTitle.ToString(); using (MyDBContext db = new MyDBContext()) { var wfactors = db.WeightFactors.ToList(); var factors = wfactors.Where(f => f.Dimension == dim); foreach (var f in factors) { if (ocrLine.Content.ToLower().Contains(f.keyWord.ToLower())) { ocrLine.WeightedAsDateTitle += f.Weight; } } //as observed of SI entry if ((!string.IsNullOrEmpty(ocrLine.Content) && ocrLine.Content.Trim() == "SI")) { ocrLine.WeightedAsDateTitle += 3; } } //do not for 11/11/11 if (ocrLine.PercentNumber == 0) { ocrLine.WeightedAsDateTitle += 1; } if (ocrLine.Content.Contains("/")) { ocrLine.WeightedAsDateTitle -= 1; } var y = DateTime.Now.Year.ToString(); if (ocrLine.Content.Contains(y)) { ocrLine.WeightedAsDateTitle -= 2; } var y2 = y.Substring(2, 2); if (ocrLine.Content.Contains(y2)) { ocrLine.WeightedAsDateTitle -= 1; } //add weight by line if (ocrLine.WeightedAsDateTitle > 2) { var maxLine = Global.OcrLines.Count; var weightLine = maxLine - ocrLine.LineNo; ocrLine.WeightedAsDateTitle += weightLine; } }
public static void AssertAsAddress(ref BaiOcrLine ocrLine) { var dim = ReceiptParts.Address.ToString(); using (MyDBContext db = new MyDBContext()) { var wfactors = db.WeightFactors.ToList(); var factors = wfactors.Where(f => f.Dimension == dim); foreach (var f in factors) { if (ocrLine.Content.ToLower().Contains(f.keyWord.ToLower())) { ocrLine.WeightedAsAddress += f.Weight; } } } //add weight by line if (ocrLine.WeightedAsAddress > 2) { var maxLine = Global.OcrLines.Count; var weightLine = maxLine - ocrLine.LineNo; ocrLine.WeightedAsAddress += weightLine; } if (ocrLine.LineNo < 3) { ocrLine.WeightedAsAddress += 3; } if (ocrLine.LineNo == 1) { ocrLine.WeightedAsAddress += -3; } if (ocrLine.PercentNumber < 25) { ocrLine.WeightedAsAddress += 3; } var split = ocrLine.Content.Split(); if (split.Length == 1) { ocrLine.WeightedAsAddress += -5; } if (split.Length > 3) { ocrLine.WeightedAsAddress += 2; } }
// Retrieve the recognized text private async Task GetTextAsync( ComputerVisionClient computerVision, string operationLocation) { try { // Retrieve the URI where the recognized text will be // stored from the Operation-Location header string operationId = operationLocation.Substring( operationLocation.Length - numberOfCharsInOperationId); Console.WriteLine("\nCalling GetReadOperationResultAsync()"); ReadOperationResult result = await computerVision.GetReadOperationResultAsync(operationId); // Wait for the operation to complete int i = 0; int maxRetries = 10; while ((result.Status == TextOperationStatusCodes.Running || result.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries) { Console.WriteLine( "Server status: {0}, waiting {1} seconds...", result.Status, i); await Task.Delay(1000); result = await computerVision.GetReadOperationResultAsync(operationId); } // Display the results Console.WriteLine(); var recResults = result.RecognitionResults; int lineNo = 0; foreach (TextRecognitionResult recResult in recResults) { foreach (Line line in recResult.Lines) { lineNo += 1; Console.WriteLine(line.Text); BaiOcrLine ocr = new BaiOcrLine { LineNo = lineNo, Content = line.Text }; RawList.Add(ocr); } } Console.WriteLine(); Global.ProcessStatus = ProcessStatus.Ready.ToString(); OnReadDone?.Invoke(this, EventArgs.Empty); } catch (Exception err) { throw err; } }
public static void AssertAsTotalTitle(ref BaiOcrLine ocrLine) { var dim = ReceiptParts.PriceTitle.ToString(); using (MyDBContext db = new MyDBContext()) { var wfactors = db.WeightFactors.ToList(); var factors = wfactors.Where(f => f.Dimension == dim); foreach (var f in factors) { if (ocrLine.Content.ToLower().Contains(f.keyWord.ToLower())) { ocrLine.WeightedAsTotalTitle += f.Weight; } } } }
public static void AssertAsChangeTitle(ref BaiOcrLine ocrLine) { var dim = ReceiptParts.ChangeTitle.ToString(); using (MyDBContext db = new MyDBContext()) { var wfactors = db.WeightFactors.ToList(); var factors = wfactors.Where(f => f.Dimension == dim); foreach (var f in factors) { if (ocrLine.Content.ToLower().Contains(f.keyWord.ToLower())) { ocrLine.WeightedAsChangeTitle += f.Weight; } } } if (ocrLine.GetPercentLocation > 40 && ocrLine.GetPercentLocation < 70) { ocrLine.WeightedAsTenderTitle += 2; } }
public static void DigValue(BaiOcrLine ocr, ref double?value) { var newContent = RefineToMoney(ocr.Content); var length = newContent.Length; Regex moneyPattern = new Regex(@"[.\d,]+"); bool isMatched = (moneyPattern.Match(newContent).Value.Length == length); if (((length > 4 && GetPercentNumber(newContent) > 70) || (length > 3 && GetPercentNumber(newContent) == 100)) && isMatched) { try { value = Convert.ToDouble(newContent); return; } catch (Exception) { //throw; //continue search } } int i = ocr.LineNo + 1; var nextLine = Global.OcrLines.Where(o => o.LineNo == i).FirstOrDefault(); if (nextLine == null || nextLine.LineNo == 0) { value = null; return; } else { DigValue(nextLine, ref value); } }
public static void AssertAsVendorName(ref BaiOcrLine ocrLine) { var dim = ReceiptParts.VendorName.ToString(); using (MyDBContext db = new MyDBContext()) { var wfactors = db.WeightFactors.ToList(); var factors = wfactors.Where(f => f.Dimension == dim); foreach (var f in factors) { if (ocrLine.Content.ToLower().Contains(f.keyWord.ToLower())) { ocrLine.WeightedAsVendorName += f.Weight; } } } if (ocrLine.LineNo < 3) { ocrLine.WeightedAsVendorName += 3; } if (ocrLine.PercentNumber < 10) { ocrLine.WeightedAsVendorName += 3; } //add weight by line if (ocrLine.WeightedAsVendorName > 2) { var maxLine = Global.OcrLines.Count; var weightLine = maxLine - ocrLine.LineNo; ocrLine.WeightedAsVendorName += weightLine; } }