Spelling Error Correction with Soft-Masked BERT

Spelling error correction is an important yet challenging task because a satisfactory solution of it essentially needs human-level language understanding ability. Without loss of generality we consider Chinese spelling error correction (CSC) in this paper. A state-of-the-art method for the task selects a character from a list of candidates for correction (including non-correction) at each position of the sentence on the basis of BERT, the language representation model. The accuracy of the method can be sub-optimal, however, because BERT does not have sufficient capability to detect whether there is an error at each position, apparently due to the way of pre-training it using mask language modeling. In this work, we propose a novel neural architecture to address the aforementioned issue, which consists of a network for error detection and a network for error correction based on BERT, with the former being connected to the latter with what we call soft-masking technique. Our method of using ‘Soft-Masked BERT’ is general, and it may be employed in other language detection-correction problems. Experimental results on two datasets, including one large dataset which we create and plan to release, demonstrate that the performance of our proposed method is significantly better than the baselines including the one solely based on BERT.


Introduction
Spelling error correction is an important task which aims to correct spelling errors in a text either at word-level or at character-level (Yu and Li, 2014;Zhang et al., 2015;Wang et al., 2018b;Hong et al., 2019;Wang et al., 2019). It is crucial for many natural language applications such as search (Martins and Silva, 2004;Gao et al., 2010), optical character recognition (OCR) (Afli et al., 2016;Wang et al., 2018b), and essay scoring (Burstein and Chodorow, 1999). In this pa- Wrong: 埃及有金子塔。Egypt has golden towers.
Correct: 埃及有金字塔。Egypt has pyramids. Wrong: 他的求胜欲很强，为了越狱在挖洞。 He has a strong desire to win and is digging for prison breaks Correct: 他的求生欲很强，为了越狱在挖洞。 He has a strong desire to survive and is digging for prison breaks.
per, we consider Chinese spelling error correction (CSC) at character-level.
Spelling error correction is also a very challenging task, because to completely solve the problem the system needs to have human-level language understanding ability. There are at least two challenges here, as shown in Table 1. First, world knowledge is needed for spelling error correction. Character 字 in the first sentence is mistakenly written as 子, where 金子塔 means golden tower and 金字塔 means pyramid. Humans can correct the typo by referring to world knowledge. Second, sometimes inference is also required. In the second sentence, the 4-th character 生 is mistakenly written as 胜. In fact, 胜 and the surrounding characters form a new valid word 求胜欲 (desire to win), rather than the intended word 求生欲 (desire to survive).
Many methods have been proposed for CSC or more generally spelling error correction. Previous approaches can be mainly divided into two categories. One employs traditional machine learning and the other deep learning Tseng et al., 2015;Wang et al., 2018b). Zhang et al. (2015), for example, proposed a unified framework for CSC consisting of a pipeline of error de-tection, candidate generation, and final candidate selection using traditional machine learning. Wang et al. (2019) proposed a Seq2Seq model with copy mechanism which transforms an input sentence into a new sentence with spelling errors corrected.
More recently, BERT (Devlin et al., 2018), the language representation model, is successfully applied to many language understanding tasks including CSC (cf., (Hong et al., 2019)). In the state-ofthe-art method using BERT, a character-level BERT is first pre-trained using a large unlabelled dataset and then fine-tuned using a labeled dataset. The labeled data can be obtained via data augmentation in which examples of spelling errors are generated using a large confusion table. Finally the model is utilized to predict the most likely character from a list of candidates at each position of the given sentence. The method is powerful because BERT has certain ability to acquire knowledge for language understanding. Our experimental results show that the accuracy of the method can be further improved, however. One observation is that the error detection capability of the model is not sufficiently high, and once an error is detected, the model has a better chance to make a right correction. We hypothesize that this might be due to the way of pre-training BERT with mask language modeling in which only about 15% of the characters in the text are masked, and thus it only learns the distribution of masked tokens and tends to choose not to make any correction. This phenomenon is prevalent and represents a fundamental challenge for using BERT in certain tasks like spelling error correction.
To address the above issue, we propose a novel neural architecture in this work, referred to as Soft-Masked BERT. Soft-Masked BERT contains two networks, a detection network and a correction network based on BERT. The correction network is similar to that in the method of solely using BERT. The detection network is a Bi-GRU network that predicts the probability that the character is an error at each position. The probability is then utilized to conduct soft-masking of embedding of character at the position. Soft masking is an extension of conventional 'hard masking' in the sense that the former degenerates to the latter, when the probability of error equals one. The soft-masked embedding at each position is then inputted into the correction network. The correction network conducts error correction using BERT. This approach can force the model to learn the right context for error correc-tion under the help of the detection network, during end-to-end joint training.
We conducted experiments to compare Soft-Masked BERT and several baselines including the method of using BERT alone. As datasets we utilized the benchmark dataset of SIGHAN. We also created a large and high-quality dataset for evaluation named News Title. The dataset, which contains titles of news articles, is ten times larger than the previous datasets. Experimental results show that Soft-Masked BERT significantly outperforms the baselines on the two datasets in terms of accuracy measures.
The contributions of this work include (1) proposal of the novel neural architecture Soft-Masked BERT for the CSC problem, (2) empirical verification of the effectiveness of Soft-Masked BERT.

Problem and Motivation
Chinese spelling error correction (CSC) can be formalized as the following task. Given a sequence of n characters (or words) X = (x 1 , x 2 , · · · , x n ), the goal is to transform it into another sequence of characters Y = (y 1 , y 2 , · · · , y n ) with the same length, where the incorrect characters in X are replaced with the correct characters to obtain Y . The task can be viewed as a sequential labeling problem in which the model is a mapping function f : X → Y . The task is an easier one, however, in the sense that usually no or only a few characters need to be replaced and all or most of the characters should be copied.
The state-of-the-art method for CSC is to employ BERT to accomplish the task. Our preliminary experiments show that the performance of the approach can be improved, if the erroneous characters are designated (cf., section 3.6). In general the BERT based method tends to make no correction (or just copy the original characters). Our interpretation is that in pre-training of BERT only 15% of the characters are masked for prediction, resulting in learning of a model which does not possess enough capacity for error detection. This motives us to devise a new model.

Model
We propose a novel neural network model work based on BERT. The detection network predicts the probabilities of errors and the correction network predicts the probabilities of error corrections, while the former passes its prediction results to the latter using soft masking.
More specifically, our method first creates an embedding for each character in the input sentence, referred to as input embedding. Next, it takes the sequence of embeddings as input and outputs the probabilities of errors for the sequence of characters (embeddings) using the detection network. After that it calculates the weighted sum of the input embeddings and [MASK] embeddings weighted by the error probabilities. The calculated embeddings mask the likely errors in the sequence in a soft way. Then, our method takes the sequence of soft-masked embeddings as input and outputs the probabilities of error corrections using the correction network, which is a BERT model whose final layer consists of a softmax function for all characters. There is also a residual connection between the input embeddings and the embeddings at the final layer. Next, we describe the details of the model.

Detection Network
The detection network is a sequential binary labeling model. The input is the sequence of embeddings E = (e 1 , e 2 , · · · , e n ), where e i denotes the embedding of character x i , which is the sum of word embedding, position embedding, and segment embedding of the character, as in BERT. The output is a sequence of labels G = (g 1 , g 2 , · · · , g n ), where g i denotes the label of the i character, and 1 means the character is incorrect and 0 means it is correct. For each character there is a probability p i representing the likelihood of being 1. The higher p i is the more likely the character is incorrect.
In this work, we realize the detection network as a bidirectional GRU (Bi-GRU). For each character of the sequence, the probability of error p i is defined as where P d (g i = 1|X) denotes the conditional probability given by the detection network, σ denotes the sigmoid function, h d i denotes the hidden state of Bi-GRU, W d and b d are parameters. Furthermore, the hidden state is defined as denotes concatenation of GRU hidden states from the two directions and GRU is the GRU function.
Soft masking amounts to a weighted sum of input embeddings and mask embeddings with error probabilities as weights. The soft-masked embed-ding e i for the i-th character is defined as where e i is the input embedding and e mask is the mask embedding. If the probability of error is high, then soft-masked embedding e i is close to the mask embedding e mask ; otherwise it is close to the input embedding e i .

Correction Network
The correction network is a sequential multi-class labeling model based on BERT. The input is the sequence of soft-masked embeddings E = (e 1 , e 2 , · · · , e n ) and the output is a sequence of characters Y = (y 1 , y 2 , · · · , y n ).
BERT consists of a stack of 12 identical blocks taking the entire sequence as input. Each block contains a multi-head self-attention operation followed by a feed-forward network, defined as: where Q, K, and V are the same matrices representing the input sequence or the output of the previous block, MultiHead, Attention, and FNN denote multi-head self-attention, self-attention, and feedforward network respectively, , and b 2 are parameters. We denote the sequence of hidden states at the final layer of BERT as H c = (h c 1 , h c 2 , · · · , h c n ) For each character of the sequence, the probability of error correction is defined as where P c (y i = j|X) is the conditional probability that character x i is corrected as character j in the candidate list, softmax is the softmax function, h i denotes the hidden state, W and b are parameters. Here the hidden state h i is obtained by linear combination with the residual connection, where h c i is the hidden state at the final layer and e i is the input embedding of character x i . The last layer of correction network exploits a softmax function. The character that has the largest probability is selected from the list of candidates as output for character x i .

Learning
The learning of Soft-Masked BERT is conducted end-to-end, provided that BERT is pre-trained and training data is given which consists of pairs of original sequence and corrected sequence, denoted as = {(X 1 , Y 1 ), (X 2 , Y 2 ), . . . , (X N , Y N )}. One way to create the training data is to repeatedly generate a sequence X i containing errors given a sequence Y i without an error, using a confusion table, where i = 1, 2, · · · , N .
The learning process is driven by optimizing two objectives, corresponding to error detection and error correction respectively.
where L d is the objective for training of the detection network, and L c is the objective for training of the correction network (and also the final decision). The two functions are linearly combined as the overall objective in learning.

Datasets
We made use of the SIGHAN dataset, a benchmark for CSC 1 . SIGHAN is a small dataset containing 1,100 texts and 461 types of errors (characters). The texts are collected from the essay section of Test of Chinese as Foreign Language and the topics are in a narrow scope. We adopted the standard split of training, development, and test data of SIGHAN.
We also created a much larger dataset for testing and development, referred to as News Title. We sampled the titles of news articles at Toutiao, a Chinese news app with a large variety of content in politics, entertainment, sports, education, etc. To ensure that the dataset contains a sufficient number of incorrect sentences, we conducted the sampling from lower quality texts, and thus the error rate of the dataset is higher than usual. Three people conducted five rounds of labeling to carefully correct spelling errors in the titles. The dataset contains 15,730 texts. There are 5,423 texts containing errors, in 3,441 types. We divided the data into test set and development set, each containing 7,865 texts.
In addition, we followed the common practice in CSC to automatically generate a dataset for training. We first crawled about 5 million news titles at the Chinese news app. We also created a confusion table in which each character is associated with a number of homophonous characters as potential errors. Next, we randomly replaced 15% of the characters in the texts with other characters to artificially generate errors, where 80% of them are homophonous characters in the table and 20% of them are random characters. This is because in practice about 80% of spelling errors in Chinese are homophonous characters due to the use of Pinyin-based input methods by people.

Baselines
For comparison, we adopted the following methods as baselines. We report the results of the methods from their original papers.
NTOU is a method of using an n-gram model and a rule-based classifier (Tseng et al., 2015). NCTU-NTUT is a method of utilizing word vectors and conditional random field (Tseng et al., 2015). HanSpeller++ is an unified framework employing a hidden Markov model to generate candidates and a filter to re-rank candidates (Zhang et al., 2015). Hybrid uses a BiLSTM-based model trained on a generated dataset (Wang et al., 2018b).
Confusionset is a Seq2Seq model consisting of a pointer network and copy mechanism (Wang et al., 2019). FASPell adopts a Seq2Seq model for CSC employing BERT as a denoising autoencoder and a decoder (Hong et al., 2019). BERT-Pretrain is the method of using a pre-trained BERT. BERT-Finetune is the method of using a fine-tuned BERT.

Experiment Setting
As evaluation measures, we utilized sentence-level accuracy, precision, recall, and F1 score as in most of the previous work. We evaluated the accuracy of a method in both detection and correction. Obviously correction is more difficult than detection, because the former is dependent on the latter.
The pre-trained BERT model utilized in the experiments is the one provided at https://github.com/huggingface/transformers. In fine-tuning of BERT, we kept the default hyperparameters and only fine-tuned the parameters using Adam. In order to reduce the impact of training tricks, we did not use the dynamic learning rate strategy and maintained a learning rate 2e −5 in fine-tuning. The size of hidden unit in Bi-GRU is 256 and all models use a batch size of 320.
In the experiments on SIGHAN, for all BERTbased models, we first fine-tuned the model with the 5 million training examples and then continued the fine-tuning with the training examples in SIGHAN. We removed the unchanged texts in the training data to improve the efficiency. In the experiments on News Title, the models were fine-tuned only with the 5 million training examples.
The development sets were utilized for hyperparameter tuning for both SIGHAN and News Title. The best value for hyper-parameter λ was chosen for each dataset.

Main Results
Table 2 presents the experimental results of all methods on the two test datasets. From the table, one can observe that the proposed model Soft-Masked BERT significantly outperforms the baseline methods on both datasets. Particularly, on News Title, Soft-Masked BERT performs much better than the baselines in terms of all measures. The best results for recall of correction level on the News Title dataset are greater than 54%, which means more than 54% errors will be found and correction level precision are better than 55%.
HanSpeller++ achieves the highest precision on SIGHAN, apparently because it can eliminate false detections with its large number of manuallycrafted rules and features. Although the use of rules and features is effective, the method has high cost in development and may also have difficulties in generalization and adaptation. In some sense, it is not directly comparable with the other learningbased methods including Soft-Masked BERT. The results of all methods except Confusionset are at sentence level not at character level. (The results at character level can look better.) Nonetheless, Soft-Mask BERT still performs significantly better.
The three methods of using BERT, Soft-Masked BERT, BERT-Finetune, and FASPell, perform better than the other baselines, while the method of   BERT-Pretrain performs fairly poorly. The results indicate that BERT without fine-tuning (i.e., BERT-Pretrain) would not work and BERT with fine-tuning (i.e., BERT-Finetune, etc) can boost the performances remarkably. Here we see another successful application of BERT, which can acquire certain amount of knowledge for language understanding. Furthermore, Soft-Masked BERT can beat BERT-Finetune by large margins on both datasets. The results suggest that error detection is important for the utilization of BERT in CSC and soft masking is really an effective means.

Effect of Hyper Parameter
We present the results of Soft-Masked BERT on (the test data of) News Title to illustrate the effect of parameter and data size. Table 3 shows the results of Soft-Masked BERT as well as BERT-Finetune learned with different sizes of training data. One can find that the best result is obtained for Soft-Masked BERT when the size is 5 million, indicating that the more training data is utilized the higher performance can be achieved. One can also observe that Soft-Masked BERT is consistently superior to BERT-Finetune.
A larger λ value means a higher weight on error correction. Error detection is an easier task than error correction, because essentially the former is a binary classification problem while the latter is a multi-class classification problem. Table 5 presents the results of Soft-Masked BERT in different values of hyper-parameter λ. The highest F1 score is obtained when λ is 0.8. That means that a good compromise between detection and correction is reached.

Ablation Study
We carried out ablation study on Soft-Masked BERT on both datasets. Table 4 shows the results on News Title. (We omit the results on SIGHAN due to space limitation, which have similar trends.) In Soft-Masked BERT-R, the residual connection  in the model is removed. In Hard-Masked BERT, if the error probability given by the detection network exceeds a threshold (0.95, 0.9, 07), then the embedding of the current character is set to the embedding of the [MASK] token, otherwise the embedding remains unchanged. In Rand-Masked BERT, the error probability is randomized with a value between 0 and 1. We can see that all the major components of Soft-Masked BERT are necessary for achieving high performance. We also tried 'BERT-Finetune + Force', whose performance can be viewed as an upper bound. In the method, we let BERT-Finetune to only make prediction at the position where there is an error and select a character from the rest of the candidate list. The result indicates that there is still large room for Soft-Masked BERT to make improvement.

Discussions
We observed that Soft-Masked BERT is able to make more effective use of global context information than BERT-Finetune. With soft masking the likely errors are identified, and as a result the model can better leverage the power of BERT to make sensible reasoning for error correction by referring to not only local context but also global context. For example, there is a typo in the sentence '我会说 一点儿，不过一个汉子也看不懂，所以我迷路 了'(I can speak a little Chinese, but I don't understand man. So I got lost.). The word '汉子'(man) is incorrect and should be written as '汉字'(Chinese character). BERT-Finetune can not rectify the mistake, but Soft-Masked BERT can, because the error correction can only be accurately conducted with global context information.
We also found that there are two major types of errors in almost all methods including Soft-Masked BERT, which affect the performances. For statistics of errors, we sampled 100 errors from test set. We found that 67% of errors require strong reasoning ability, 11% of errors are due to lack of world knowledge, and the remaining 22% of errors have no significant type.
The first type of errors is due to lack of inference ability. Accurate correction of such typos requires stronger inference ability. For example, for the sentence '他主动拉了姑娘的手, 心里很高心, 嘴 上故作生气' (He intentionally took the girl's hand, and was very x, but was pretending to be angry.) where the incorrect word 'x' is not comprehensible, there might be two possible corrections, changing '高心' to '寒心'(chilled) and changing '高心' to '高兴'(happy), while the latter is more reasonable for humans. One can see that in order to make more reliable corrections, the models must have stronger inference ability.
The second type of errors is due to lack of world knowledge. For example, in the sentence '芜湖: 女 子落入青戈江,众人齐救援' (Wuhu: the woman fell into the Qingge River, and people tried hard to rescue her.), '青戈江' (Qingge River) is a typo of '青弋江' (Qingyu River). Humans can discover the typo because the river in Wuhu city China is called Qingyu not Qingge. It is still very challenging for the existing models in general AI systems to detect and correct such kind of errors.

Related Work
Various studies have been conducted on spelling error correction so far, which plays an important role in many applications, including search (Gao et al., 2010), optical character recognition (OCR) (Afli et al., 2016), and essay scoring (Burstein and Chodorow, 1999).
Chinese spelling error correction (CSC) is a special case, but is more challenging due to its conflation with Chinese word segmentation, which received a considerable number of investigations Yu and Li, 2014;Tseng et al., 2015;Wang et al., 2019). Early work in CSC followed the pipeline of error detection, candidate generation, and final candidate selection. Some researchers employed unsupervised methods using language models and rules (Yu and Li, 2014;Tseng et al., 2015) and the others viewed it as a sequential labeling problem and employed conditional random fields or hidden Markov models (Tseng et al., 2015;Zhang et al., 2015). Recently, deep learning was applied to spelling error correction (Guo et al., 2019;Wang et al., 2019), and for example, a Seq2Seq model with BERT as encoder was employed (Hong et al., 2019), which transforms the input sentence into a new sentence with spelling errors corrected.
BERT (Devlin et al., 2018) is a language representation model with Transformer encoder as its architecture. BERT is first pre-trained using a very large corpus in a self-supervised fashion (mask language modeling and next sentence prediction). Then, it is fine-tuned using a small amount of labeled data in a down-stream task. Since its inception BERT has demonstrated superior performances in almost all the language understanding tasks, such as those in the GLUE challenge (Wang et al., 2018a). BERT has shown strong ability to acquire and utilize knowledge for language understanding. Recently, other language representation models have also been proposed, such as XLNET (Yang et al., 2019), Roberta , and ALBERT (Lan et al., 2019). In this work, we extend BERT to Soft Masked BERT for spelling error correction and as far as we know no similar architecture was proposed before.

Conclusion
In this paper, we have proposed a novel neural network architecture for spelling error correction, more specifically Chinese spelling error correction (CSC). Our model called Soft-Masked BERT is composed of a detection network and a correction network based on BERT. The detection network identifies likely incorrect characters in the given sentence and soft-masks the characters. The correction network takes the soft-masked characters as input and makes correction on the characters. The technique of soft-masking is general and potentially useful in other detection-correction tasks. Experimental results on two datasets show that Soft-Masked BERT significantly outperforms the state-of-art method of solely utilizing BERT. As future work, we plan to extend Soft-Masked BERT to other problems like grammatical error correction and explore other possibilities of implementing the detection network.