void adjustWords1() { string plainText = extractPlainText(tempPdfFilePath); char[] lineSeparatingChars = { '\n' }; char[] wordSeparatingChars = { ' ' }; string[] lines = plainText.Split(lineSeparatingChars, StringSplitOptions.RemoveEmptyEntries); List <string> wordCombinationList = new List <string>(); foreach (string line in lines) { string[] words = line.Split(wordSeparatingChars, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < words.Length; i++) { string wordCombination = ""; for (int j = i; j < words.Length; j++) { wordCombination += (i == j ? "" : " ") + words[j]; wordCombinationList.Add(wordCombination); } } } for (int i = 0; i < completedWordList.Count; i++) { double bestSimilarity = 0; int bestSimilarWordId = 0; string agent1 = Regex.Replace(completedWordList[i].word, @"[^0-9a-zA-Z$@]+", ""); for (int j = 0; j < wordCombinationList.Count; j++) { string agent2 = Regex.Replace(wordCombinationList[j], @"[^0-9a-zA-Z$@]+", ""); double similarity = ContractNote.getSimilarity(agent1, agent2); if (bestSimilarity < similarity) { bestSimilarity = similarity; bestSimilarWordId = j; } } if (bestSimilarity > 0.8) { completedWordList[i].word = wordCombinationList[bestSimilarWordId]; } } }
void adjustWords2() { string plainText = extractPlainText(tempPdfFilePath); char[] lineSeparatingChars = { '\n' }; char[] wordSeparatingChars = { ' ' }; string[] lines = plainText.Split(lineSeparatingChars, StringSplitOptions.RemoveEmptyEntries); List <string> wordCombinationList = new List <string>(); foreach (string line in lines) { string[] words = line.Split(wordSeparatingChars, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < words.Length; i++) { string wordCombination = ""; for (int j = i; j < words.Length; j++) { wordCombination += (i == j ? "" : " ") + words[j]; wordCombinationList.Add(wordCombination); } } } try { using (PdfReader reader = new PdfReader(tempPdfFilePath)) { var pageRectangle = reader.GetPageSize(1); float pageWidth = pageRectangle.Width; float pageHeight = pageRectangle.Height; float sizeRate = 2754f / 595; for (int i = 0; i < completedWordList.Count; i++) { double bestSimilarity = 0; string word = completedWordList[i].word; float w = completedWordList[i].w / sizeRate; float h = completedWordList[i].h / sizeRate; float x = completedWordList[i].x / sizeRate; float y = pageHeight - completedWordList[i].y / sizeRate - h; RenderFilter[] filter = { new RegionTextRenderFilter(new System.util.RectangleJ(x, y, w, h)) }; ITextExtractionStrategy strategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), filter); string text = PdfTextExtractor.GetTextFromPage(reader, 1, strategy); string[] wordSeparatingStrings = { " " }; string[] words = text.Split(wordSeparatingStrings, StringSplitOptions.RemoveEmptyEntries); if (words.Length > 0) { bestSimilarity = ContractNote.getSimilarity(word, words[0]); if (bestSimilarity > 0.8) { completedWordList[i].word = words[0]; } } int bestSimilarWordId = 0; string agent1 = Regex.Replace(word, @"[^0-9a-zA-Z$@]+", ""); for (int j = 0; j < wordCombinationList.Count; j++) { string agent2 = Regex.Replace(wordCombinationList[j], @"[^0-9a-zA-Z$@]+", ""); double similarity = ContractNote.getSimilarity(agent1, agent2); if (bestSimilarity < similarity) { bestSimilarity = similarity; bestSimilarWordId = j; } } if (bestSimilarity > 0.8) { completedWordList[i].word = wordCombinationList[bestSimilarWordId]; } } } } catch { } }