Method for correcting text recognition space
By building a neural network based on language model, the problem of inaccurate recognition of blank areas in optical character recognition technology is solved, automated space correction is achieved, and the accuracy and efficiency of OCR recognition is improved, and it is suitable for a variety of scenarios.
Patent Information
- Application Number
- CN202310693597.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-06-12
- Publication Date
- 2024-12-13
AI Technical Summary
When existing optical character recognition technology processes blank areas between characters, it is difficult to accurately determine whether it is a space in the middle of a word, resulting in unstable recognition results and affecting the accuracy and efficiency of OCR recognition.
By building a neural network based on language models, using the Wikipedia dataset to train the model, identify space positions that should not exist in the sentence, and perform correction processing, including replacing characters as spaces, generating training tags, building Embedding layer, LSTM layer and output layer neural network model, automatically detects and deletes wrong spaces.
It improves the accuracy and efficiency of OCR recognition, reduces manual intervention, and is suitable for any scenario where it needs to judge the rationality of a single intermediate space in English sentences, and has wide applicability.
Smart Images

Figure CN119131801A_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the field of natural language processing and image recognition processing, and in particular relates to a method for correcting text recognition spaces. Background Art
[0002] Optical Character Recognition (OCR) is a technology that converts text images into machine-readable text formats. OCR technology is widely used in different scenarios, such as street view text recognition, document text recognition and other applications. It involves image recognition, language models and text error correction.
[0003] With the advent of the digital age, the digitization of a large number of paper documents has become increasingly common. The development of optical character recognition (OCR) technology has made document digitization more convenient and efficient. OCR technology can convert scanned paper documents into editable electronic texts, which includes the recognition and segmentation of each character in the text. However, due to factors such as inaccurate character segmentation, the final recognition effect is affected, especially when there are large adjacent blank areas between characters, errors are prone to occur. Therefore, a method is needed to determine whether these blank areas should be considered as spaces between words, so as to improve the accuracy and efficiency of OCR recognition.
[0004] There are already some methods to judge the rationality of a single space in an English sentence, such as rule-based methods, statistical model-based methods, and deep learning-based methods. However, in the OCR scenario, due to character recognition errors and the diversity of blank areas, these methods often cannot effectively judge the rationality of the space between words. Therefore, a new method is needed to solve the space judgment problem in the OCR scenario.
[0005] However, the existing recognition technology directly regards the blank segmented area as a space, which is not conducive to improving the OCR recognition accuracy and brings a lot of uncertainty to OCR recognition. Figure 1 As shown in the figure, the black area is the image to be processed, and the gray area is identified as a space due to its large blank space. After the gray area is segmented, each area is identified separately and a space is added to the identified gray area. The final result changes from the original correct "I think you are right!" to "I think y ou are right!" There is an extra space in the middle of the word "you", which affects the accuracy of the result. Summary of the invention
[0006] In order to solve the above problems, the purpose of this application is to solve the problem of unstable recognition results caused by the original segmentation based on blank areas, especially involving the correction of spaces in text recognition.
[0007] Specifically, the present invention provides a method for correcting text recognition spaces, the method comprising the following steps:
[0008] S1. Prepare language model training data and training labels:
[0009] Download the Wikipedia English dataset and process it into training data and corresponding label formats. The Wikipedia dataset refers to a structured public dataset extracted from Wikipedia. The processing flow is as follows:
[0010] S1.1, select a letter in a word in an English sentence and replace it with a space; suppose a letter in a word in sentence A is replaced with a space, then it becomes sentence B, and sentence B is used as training data here;
[0011] S1.2, construct corresponding training labels according to the sentence B. Since the task is to determine whether the space in the English sentence should exist, there are only two cases: "should" and "should not"; the training labels processed according to this case are represented by 1 and 0, where 1 and 0 respectively represent "should" and "should not" in "should not exist", corresponding to each letter in sentence B, respectively, wherein the space in the middle of the selected word is also marked as 0, indicating that the space "should not" appear here, and each of the remaining 1s represents that the space "should" appear here; the marking as 0 further includes the following steps:
[0012] 1) Define the getitem() function, with parameters self and index, expressed as:
[0013] def__getitem__(self,index);
[0014] 2), select a sentence, represented by: sentence = self.sentences[index];
[0015] 3), converted to index, expressed as: index = self.chardict.sentence2indexes(sentence);
[0016] 4), get the index corresponding to the space, expressed as: index_label = index.copy(), space_index = self.chardict.english_dict["];
[0017] 5) Get <unk>The corresponding index is expressed as:
[0018] null_index=self.chardict.english_dict[' <unk>'];
[0019] 6) Get the available positions for insertion, expressed as: available_positions = []
[0020] for i in range(1,len(index)-1):
[0021] if index[i]! =space_index and index[i-1]! =space_index and index x[i+1]! =space_index:
[0022] available_positions.append(i);
[0023] 7), randomly select one or two positions and insert ' <unk>', expressed as:
[0024] n = random.randint(1,2)
[0025] if len(available_positions)>=n:
[0026] positions_to_insert=random.sample(available_positions,n);
[0027] 8), insert the UNK character, that is, 0, which is represented as:
[0028] for p in sorted(positions_to_insert,reverse=True):
[0029] index_label.insert(p,null_index);
[0030] 9), training index, expressed as: index.insert(p, space_index);
[0031] 10), return index, index_label;
[0032] S2. Build a neural network language model and start training:
[0033] A neural network language model is constructed according to the training data generated in step S1. The neural network language model is composed of an Embedding layer, an LSTM layer and an output layer. The output of the output layer has two values: 1 and 0, which correspond to the spaces "should" and "should not" appear in step S1 respectively; the neural network structure: input size (1,5) enters the Embedding layer; output size (1,5,32) enters the LSTM layer; output size (1,5,128) enters the output layer; output size (1,5,2) argmax; output size (1,5,1);
[0034] S3. Correct the spaces:
[0035] According to the output result of step S2, assuming that there is a space that "should not" appear in the input sentence, the output after the neural network is represented by [0] and [1], and the space area corresponding to the sentence is identified by the neural network as 0, and there should be no space. Then, this space in the English sentence is deleted, and the final result is obtained after deletion.
[0036] In step S1.1, assuming that the "c" of the word "nice" in sentence A: "What a nice day!" is replaced with a space, it becomes sentence B: "What a ni e day!", where sentence B is used as training data.
[0037] In step S1.2, the training labels processed according to the situation in step S1.1 are: "[0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0]", which correspond to each letter in sentence B, where the tenth number 0 represents the space in the middle of the word "ni e", indicating that there "should not" be a space here, and each of the remaining 1s represents that there "should" be a space here.
[0038] In step S3, it is assumed that the input sentence is: "I think you are right", and the output after the neural network is:
[0039] The 10th position in the English sentence [[0],[1],[0],[0],[0],[0],[0],[1],[0],[0],[0],[0],[1],[0],[0],[0],[0],[1],[0],[0],[0],[0],[0],[0]] is a space, and the neural network recognizes that this position is 0, so there should be no space. The space in the English sentence is deleted, and the English sentence after deletion is: "I think you are right", and the final result is obtained.
[0040] Therefore, the advantages of this application are:
[0041] Automation: Using language models, incorrect single-space errors can be automatically detected and corrected without human intervention, improving efficiency and accuracy.
[0042] Accuracy: Using a trained language model can more accurately detect incorrect single-space in the middle, avoiding omissions and misjudgments.
[0043] Universality: This method is applicable to any scenario where the rationality of a single space in an English sentence needs to be judged, and has wide applicability. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] The drawings described herein are used to provide a further understanding of the present invention, constitute a part of this application, and do not constitute a limitation of the present invention.
[0045] Figure 1 This is a schematic diagram showing that the existing recognition technology directly identifies the blank segmented area as a space.
[0046] Figure 2 It is a schematic diagram of the method for correcting spaces.
[0047] Figure 3 is a schematic diagram of the process of this method.
[0048] Figure 4 It is a schematic diagram of the neural network structure.
[0049] Figure 5 It is a schematic diagram of the code marked as 0 in step S1.2 of this method. DETAILED DESCRIPTION
[0050] In order to more clearly understand the technical content and advantages of the present invention, the present invention is now further described in detail in conjunction with the accompanying drawings.
[0051] This method proposes a method for correcting spaces in text recognition. The main implementation steps of the method are as follows: Figure 3 As shown:
[0052] Step S1. Prepare language model training data and training labels
[0053] Download the Wikipedia English dataset (Wikipedia dataset refers to a structured public dataset extracted from Wikipedia) and process it into training data and corresponding label formats. The specific processing flow is as follows:
[0054] S1.1, select a letter in a word in an English sentence and replace it with a space. For example, replace the "c" in the word "nice" in sentence A: "What a nice day!" with a space, and it becomes sentence B: "What a ni e day!". Here, sentence B is used as training data. Construct the corresponding training label based on sentence B. Since the task is to determine whether the space in the English sentence should exist, there are only two cases here: "should" and "should not".
[0055] S1.2, construct corresponding training labels according to the sentence B. Since the task is to determine whether the space in the English sentence should exist, there are only two cases: "should" and "should not"; the training labels processed according to this case are represented by 1 and 0, where 1 and 0 respectively represent whether "should" and "should not" exist, corresponding to each letter in sentence B, respectively, where the space in the middle of the selected word is also marked as 0, indicating that there "should not" appear a space here, and each of the remaining 1s represents that there "should" appear a space here; the code marked as 0 is as follows Figure 5 The training labels processed according to this situation are: "[0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0]", (where [1] and [0] respectively indicate whether "should" or "should not" exist), corresponding to each letter in sentence B. The 0 at the 10th position represents the space in the middle of the word "ni e", indicating that there "should not" be a space here, and each of the remaining [1]s represents that there "should" be a space here.
[0056] Step S2. Build a neural network language model and start training
[0057] A neural network language model is constructed based on the training data generated in step S1. The neural network language model consists of an Embedding layer, an LSTM layer, and an output layer. The output of the output layer has two values [1] and [0], which correspond to the spaces "should" and "should not" appearing in step S1, respectively. The neural network structure is as follows: Figure 4 As shown: input size (1,5) enters the Embedding layer; output size (1,5,32) enters the LSTM layer; output size (1,5,128) enters the output layer; output size (1,5,2) argmax; output size (1,5,1).
[0058] Step S3. Correct the spaces
[0059] Assume that there is a space that "should not" appear in the input sentence. The output after the neural network is represented by [0] and [1]. The space area corresponding to the sentence is identified by the neural network as 0, which means there should not be a space. Then, the space in the English sentence is deleted and the final result is obtained after deletion. Figure 2 As shown, assuming that the input sentence is: "I think you are right", the output after the neural network is: [[0],[1],[0],[0],[0],[0],[0],[1],[0],[0],[0],[0],[1],[0],[0],[0],[1],[0],[0],[0],[0],[1],[0],[0],[0],[0],[0]] The 10th position in the English sentence is a space, and the neural network recognizes that this position is 0, and there should be no space. This space in the English sentence is deleted, and the deleted English sentence, that is, the corrected sentence, is: "I think you are right".
[0060] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the embodiments of the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.< / unk> < / unk> < / unk>
Claims
1. A method for correcting spaces in text recognition, characterized in that: The method comprises the following steps: S1. Prepare language model training data and training labels: Download the Wikipedia English dataset and process it into training data and corresponding label formats. The Wikipedia dataset refers to a structured public dataset extracted from Wikipedia. The processing flow is as follows: S1.1, select a letter in a word in an English sentence and replace it with a space; suppose a letter in a word in sentence A is replaced with a space, then it becomes sentence B, and sentence B is used as training data here; S1.2, construct corresponding training labels according to the sentence B. Since the task is to determine whether the space in the English sentence should exist, there are only two cases: "should" and "should not"; the training labels processed according to this case are represented by 1 and 0, wherein 1 and 0 respectively represent "should" and "should not" in "should not exist", corresponding to each letter in sentence B, wherein the space in the middle of the selected word is also marked as 0, indicating that the space "should not" appear here, and each of the remaining 1s represents that the space "should" appear here; the marking as 0 further includes the following steps: 1) Define the getitem() function, with parameters self and index, expressed as: def__getitem__(self,index); 2), select a sentence, represented by: sentence = self.sentences[index]; 3), converted to index, expressed as: index = self.chardict.sentence2indexes(sentence); 4), get the index corresponding to the space, expressed as: index_label = index.copy(), space_index = self.chardict.english_dict["]; 5) Get <unk> The corresponding index is expressed as:< / unk> null_index=self.chardict.english_dict[' <unk> '];< / unk> 6) Get the available positions for insertion, expressed as: available_positions = [] for iin range(1,len(index)-1): if index[i]! =space_index and index[i-1]! =space_index and index x[i+1]! =space_index: available_positions.append(i); 7), randomly select one or two positions and insert ' <unk> ', expressed as:< / unk> n = random.randint(1,2) iflen(available_positions)>=n: positions_to_insert=random.sample(available_positions,n); 8), insert the UNK character, that is, 0, which is represented as: for p in sorted(positions_to_insert,reverse=True): index_label.insert(p,null_index); 9), training index, expressed as: index.insert(p, space_index); 10), return index, index_label; S2. Build a neural network language model and start training: A neural network language model is constructed according to the training data generated in step S1. The neural network language model is composed of an Embedding layer, an LSTM layer and an output layer, wherein the output of the output layer has two values: 1 and 0, corresponding to the spaces "should" and "should not" appear in step S1, respectively; the neural network structure: input size (1,5) enters the Embedding layer; output size (1,5,32) enters the LSTM layer; output size (1,5,128) enters the output layer; output size (1,5,2) argmax; output size (1,5,1); S3. Correct the spaces: According to the output result of step S2, assuming that there is a space that "should not" appear in the input sentence, the output after the neural network is represented by [0] and [1], and the space area corresponding to the sentence is identified by the neural network as 0, and there should be no space. Then, this space in the English sentence is deleted, and the final result is obtained after deletion.
2. A method for correcting text recognition spaces according to claim 1, characterized in that: In step S1.1, assuming that the "c" of the word "nice" in sentence A: "What a nice day!" is replaced with a space, it becomes sentence B: "What a ni e day!", where sentence B is used as training data.
3. A method for correcting text recognition spaces according to claim 2, characterized in that: In step S1.2, the training labels processed according to the situation in step S1.1 are: "[0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0]", which correspond to each letter in sentence B, where the tenth number 0 represents the space in the middle of the word "ni e", indicating that there "should not" be a space here, and each of the remaining 1s represents that there "should" be a space here.
4. A method for correcting spaces in text recognition according to claim 1, characterized in that: In step S3, it is assumed that the input sentence is: "I think you are right", and the output after the neural network is: The 10th position in the English sentence [[0],[1],[0],[0],[0],[0],[0],[1],[0],[0],[0],[0],[1],[0],[0],[0],[0],[1],[0],[0],[0],[0],[0],[0]] is a space, and the neural network recognizes that this position is 0, so there should be no space. The space in the English sentence is deleted, and the English sentence after deletion is: "I think youare right", and the final result is obtained.
