A Sensitive Text Detection Method for Social Media Platforms
By improving the MLM masking strategy and multi-granularity cross-attention encoding mechanism, and combining the loss function of prior knowledge, the problems of low accuracy and poor adaptability of Chinese sensitive text detection on social platforms are solved, and more efficient text classification quality is achieved.
Patent Information
- Application Number
- CN202310617534.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-05-29
- Publication Date
- 2025-12-02
- Estimated Expiration
- 2043-05-29
AI Technical Summary
Existing technologies for detecting sensitive Chinese text on social media platforms suffer from high computational resource consumption, low accuracy, difficulty in handling complex and ever-changing text data, and a lack of semantic relevance and generalization capabilities.
The BERT model employs an improved MLM masking strategy, performs data augmentation through similar word replacement, and combines a multi-granularity cross-attention encoding mechanism and a loss function based on prior knowledge to enhance the model's learning and generalization capabilities and strengthen the semantic connections between words.
It improves the accuracy of Chinese sensitive text detection and the model's generalization ability, and enhances its adaptability and semantic understanding of complex and variable text data.
Smart Images

Figure CN116561318B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to a sensitive text detection method for social platforms, belonging to the fields of Internet and artificial intelligence technology. Background Technology
[0002] With the rapid development of the internet, the massive and diverse data, numerous correlations, and sporadic factors have made the online social ecosystem more complex and volatile, posing a significant threat to online platforms. However, due to the concealment, diversity, and lack of obvious emotional polarity of sensitive texts, the task of detecting sensitive texts on social platforms is highly challenging and difficult.
[0003] Currently, traditional methods based on rule matching, machine learning, deep learning, and pre-trained models are commonly used for sensitive text detection on social media platforms. However, traditional rule matching, machine learning, and deep learning methods consume significant computing power, have limited accuracy, and cannot handle complex and varied text data, making it difficult to model the long-range dependencies between words in the text effectively. Furthermore, sensitive text on social media platforms covers a wide range of topics, lacks clear sentiment polarity, and contains more diverse and concealed sensitive words. Therefore, higher demands are placed on semantic relevance, the model's learning ability, and its generalization ability.
[0004] Creating a clean and healthy social network space requires enhancing the model's learning and generalization abilities for sensitive Chinese texts to cope with more diverse and concealed sensitive texts, as well as high-quality semantic association, data augmentation, and category detection. Semantic association and data augmentation are key components in the task of detecting sensitive Chinese texts. This invention studies the task of detecting sensitive Chinese texts for social platforms and proposes a sensitive text detection method based on similar word replacement masking. The method improves data quality by cleaning the input Chinese text data, including removing pause words, web links, common punctuation marks, and replacing emojis. It improves upon traditional methods by employing a masking strategy of Chinese similar word replacement to better adapt to Chinese scenarios, enhancing the model's learning and generalization abilities for sensitive texts. Furthermore, it utilizes a multi-granularity cross-attention encoding mechanism to fully capture key features of text data at different granularities, enhancing the semantic association between words. Finally, prior knowledge is injected into the loss function to correct biases in model understanding and classification. Summary of the Invention
[0005] To address the problems and shortcomings of existing technologies, this invention proposes a sensitive text detection method for social platforms. This invention constructs a BERT model with an improved MLM masking strategy. Through a masking strategy based on similar word replacement, it mines deep semantic information to achieve data augmentation and generalization. A multi-granularity cross-attention encoding mechanism strengthens the correlation between character-level and word-level information. Based on a loss function that integrates prior knowledge, it improves the quality of text classification.
[0006] To achieve the above objectives, the technical solution of the present invention is as follows: A sensitive text detection method for social media platforms, comprising the following steps:
[0007] Step 1: Preprocessing of Sensitive Chinese Text Data. The input Chinese text data undergoes data cleaning, including removing pause words, web links, common punctuation marks, and replacing emojis. The jieba word segmentation tool is then used to preprocess the Chinese text S, yielding word segmentation results w and character segmentation results c. The importance of the word segmentation results to the sensitive word database is then evaluated using the TF-IDF (Term Frequency-Inverse Document Frequency) algorithm, selecting the top k most important keywords.
[0008] Step 2: Data Augmentation Based on Improved MLM Masking Strategy. The BERT model is used to embed sensitive Chinese text data, obtaining word vectors. A masking strategy based on similar word replacement is introduced to guide the MLM subtask. Whole Word Masking (WWM) and N-gram methods are used to mask Chinese characters and words in the text, achieving complete masking of individual characters or entire words. Words with the highest vector similarity in the word list are selected based on the word2vec word vector results, and the N-gram segmented words are replaced. By guiding the MLM subtask with a masking strategy based on similar word replacement, the deep semantic information of the Chinese text is mined, achieving data augmentation.
[0009] Step 3: Semantic association based on multi-granularity cross-attention. The word segmentation and character segmentation representations w and c obtained from data preprocessing are used as input vectors, and a word-level matrix K is obtained through linear transformation. w and V w With character-level matrix K c and V c The word-level matrix K w and V w With character-level matrix K c and V c Cross-coding is performed to obtain multi-granular text features; after passing through a multi-head attention layer, the word-level semantic encoding result Z is... w And character-level semantic encoding result Z cThe data is fed into residual connections and normalization layers (Add & Normal). The residual connection layer transmits a portion of the information from the previous layer to the next layer without error. After passing through a feedforward neural network, word-level and character-level semantic encoding results H are obtained. w and H c Finally, the vectors are concatenated to form the output of the attention layer.
[0010] Step 4: Classification Correction Based on Loss Function. An extra term is added to the loss function to inject more prior knowledge into the model. This prior knowledge is used as the model's loss term to correct biases in the model's understanding and classification.
[0011] Compared with the prior art, the advantages of the present invention are as follows:
[0012] 1) This invention adopts a masking strategy based on similar word replacement. It uses similarity-guided masking language model (MLM) to deeply mine Chinese semantic information and replaces the original text with similar words. This effectively alleviates the problem of insufficient concealment and detection ability of sensitive Chinese texts. Data augmentation is used to improve the model's learning ability and generalization ability.
[0013] 2) This invention employs a multi-granularity cross-attention coding mechanism to further explore character-level and word-level semantic information in sensitive Chinese texts, enhance the semantic correlation between words, and improve the accuracy of the model in capturing key information.
[0014] 3) This invention augments the model based on a prior knowledge base, using sensitive word database information as prior knowledge and regularization term in the loss function. By measuring the correlation between text and prior knowledge, the semantic understanding and text classification quality of the model are improved. Attached Figure Description
[0015] Figure 1 This is a technical framework diagram of an embodiment of the present invention.
[0016] Figure 2 This is a diagram illustrating the encoding mechanism of an embodiment of the present invention. Detailed Implementation
[0017] To enhance understanding of the present invention, the invention will be further explained below with reference to specific embodiments.
[0018] Example 1: A sensitive text detection method for social media platforms, the overall framework of which is as follows: Figure 1 As shown, the detailed implementation steps are as follows:
[0019] Step 1: Preprocessing of Sensitive Chinese Text Data. The input Chinese text data undergoes data cleaning, including removing pause words, web links, common punctuation marks, and replacing emoticons. The jieba word segmentation tool is then used to preprocess the Chinese text S, yielding word and character segmentation results. The word segmentation result is represented as w = (w1, w2, ..., w...). n )∈X S The result of the word segmentation is represented X S Let d represent the set of sensitive texts in dataset D. e Indicates the embedding layer dimension, l s The length of the sensitive text is indicated. Then, the importance between the text segmentation results and the sensitive word library is evaluated using the TF-IDF (Term Frequency-Inverse Document Frequency) algorithm, and the top k keywords with the highest importance are selected.
[0020] Step 2: Data Augmentation Based on Improved MLM Masking Strategy. A masking strategy based on similar word replacement is adopted. During model pre-training, the similarity of Chinese words is calculated, and similar words are used to replace the original text, replacing the traditional random [MASK] masking. This mitigates the differences between the pre-training and fine-tuning stages, achieving the effect of data augmentation.
[0021] Sub-step 2-1: The BERT model is used to embed the Chinese sensitive text data to obtain word vectors. A masking strategy based on similar word replacement is introduced to guide the MLM subtask. Whole Word Masking (WWM) and N-gram methods are used to mask Chinese words in the text. N-gram is a statistical language model that uses a sliding window approach to segment the text data into several segment sequences. In this scheme, n = 1, 2, 3, 4, corresponding to segmentation ratios of 40%, 30%, 20%, and 10%, respectively, resulting in the candidate masking word sequence w. candidate .
[0022] Sub-step 2-2: Based on the word fragments obtained from the N-gram segmentation in the previous step, this scheme uses the word2vec model to measure the similarity between words. The words with the highest vector similarity in the word2vec word vector list are selected based on the word2vec vector results, and the N-gram segmented words are then replaced. When no similar words are found, the method degenerates into random word replacement.
[0023] Sub-steps 2-3: Mask 15% of the input words, where 80% of the tags are replaced with similar words, 10% of the tags are masked with random words, and 10% of the tags remain the original words, to achieve data augmentation and improve the model's learning and generalization abilities.
[0024] Step 3: Semantic association based on multi-granularity cross-attention. For example... Figure 2 As shown, the word segmentation and character segmentation representations w and c obtained from data preprocessing are used as input vectors, and a word-level matrix K is obtained through linear transformation. w and V w With character-level matrix K c and V c The word-level matrix K w and V w With character-level matrix K c and V c Cross-coding is performed to obtain multi-granular text features; after passing through a multi-head attention layer, the word-level semantic encoding result Z is... w And character-level semantic encoding result Z c The data is fed into residual connections and normalization layers (Add & Normal). The residual connection layer transmits a portion of the information from the previous layer to the next layer without error. After passing through a feedforward neural network, word-level and character-level semantic encoding results H are obtained. w and H c Finally, the vectors are concatenated to form the output of the attention layer.
[0025] Step 4: Classification correction based on loss function. Add an extra term to the loss function to inject more prior knowledge into the model. Use the prior knowledge as the loss term of the model to correct the bias in the model's understanding and classification.
[0026] Sub-step 4-1: First, convert the first k keywords selected by the TF-IDF algorithm in the data preprocessing stage into word vectors a = (a1, a2, ..., a...). k ), which serves as prior knowledge of text S, is called a key attribute.
[0027] Sub-step 4-2: Represent the prior loss by calculating the L2 norm between the key attributes and the input text. prior .
[0028]
[0029] Among them, the L2 norm is a commonly used regularization method that can effectively prevent overfitting, reduce the influence of outliers, improve the generalization ability of the model, and maintain the continuity and differentiability of model parameters, thereby avoiding the problems of gradient vanishing or gradient exploding. n represents the vector length of the input sequence, and w... i and a i Let i and j represent the i-th word and i-th key attribute of the input text S, respectively.
[0030] Sub-step 4-3: The classification model training process includes two stages: forward propagation and back propagation. The loss function plays a guiding role in these stages. Forward propagation involves processing the input data through each neural layer to obtain the prediction results and evaluating the difference between the prediction results and the true values. For text classification tasks, the prior loss is used as the regularization term of the traditional cross-entropy loss function Loss(y,p) to obtain the joint loss function Loss. joint To train the model.
[0031]
[0032] Where y represents the true value, p represents the posterior probability of the class, and a c w c p c These are the key attributes of category c, the word segmentation result, and the posterior probability.
[0033] Sub-step 4-4: In the backpropagation phase, gradient descent is used to make the loss function converge to the global minimum or a relatively optimal local minimum more quickly, improving the efficiency of model parameter updates. Backpropagation calculates the partial derivatives of the parameters of each layer based on the loss function, as shown in the formula:
[0034]
[0035] Where θ is the parameter of the loss function Loss(θ), θ 0 Indicates the current parameter value, θ 1 This represents the parameter value after one iteration, and lr represents the learning rate, which is used to control the step size for each update.
[0036] In summary, this invention first preprocesses sensitive Chinese text data to obtain word segmentation results w and character segmentation results c, as well as the top k most important keywords. Then, based on a masking strategy of similar word replacement, during model pre-training, the similarity between Chinese words is calculated, and similar words are used to replace the original text to achieve data augmentation. Next, a multi-granularity cross-attention encoding mechanism is used to fully capture the key features of text data at different granularities, enhancing the semantic correlation between words and obtaining word-level and character-level semantic encoding results H. w and H c and for H w and H c Vector concatenation is performed as the output; finally, prior knowledge is injected into the loss function, and the prior loss is calculated. prior As a regularization term of the traditional cross-entropy loss function Loss(y,p), the joint loss function Loss is obtained. joint The model is trained using gradient descent to make the loss function converge quickly, thereby improving the efficiency of updating model parameters and enhancing classification quality.
[0037] Based on the same inventive concept, the present invention provides a sensitive text detection method for social platforms, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor. When the computer program is loaded onto the processor, it implements the aforementioned sensitive text detection method for social platforms.
[0038] Those skilled in the art will recognize that the embodiments described herein are intended to help readers understand the principles of the invention. It should be understood that the embodiments are for illustrative purposes only and are not intended to limit the scope of the invention. After reading this invention, any modifications of the invention in various equivalent forms by those skilled in the art will fall within the scope defined by the claims of this application.
Claims
1. A method for detecting sensitive text on social media platforms, characterized in that, The method includes the following steps: Step 1: Preprocessing of Chinese sensitive text data. Step 2: Data augmentation based on improved MLM masking strategy, Step 3: Semantic association based on multi-granularity cross-attention. Step 4: Classification correction based on loss function; Step 2 involves data augmentation based on an improved MLM masking strategy. The BERT model is used to embed Chinese sensitive text data, resulting in word vectors. Building upon the BERT model, a masking strategy based on similar word replacement is introduced to guide the MLM subtask. Full-word masking and N-gram methods are used to mask Chinese characters and words in the text, achieving complete masking of individual characters or entire words. The words with the highest vector similarity in the word list are selected based on the word2vec word vector results, and the N-gram segmented words are replaced. Finally, this strategy masks 15% of the input words, with 80% of the tags replaced with similar words, 10% of the tags masked with random words, and 10% of the tags remaining unchanged, thus achieving data augmentation and improving the model's learning and generalization abilities. Step 3: Based on semantic association through multi-granularity cross-attention, the word segmentation and character segmentation representations w and c obtained from data preprocessing are used as input vectors, and a word-level matrix K is obtained through linear transformation. w and V w With character-level matrix K c and V c The word-level matrix K w and V w With character-level matrix K c and V c Cross-coding is performed to obtain multi-granular text features; after passing through a multi-head attention layer, the word-level semantic encoding result Z is... w And character-level semantic encoding result Z c The information is fed into residual connection and normalization layers. The residual connection layer transmits a portion of the information from the previous layer to the next layer without error. After passing through the feedforward neural network, word-level and character-level semantic encoding results H are obtained. w and H c Finally, the vector concatenation is performed on the data to serve as the output of the attention layer. The multi-granularity cross-attention coding mechanism is used to fully mine key text information at different granularities and enhance the semantic relevance of sensitive texts.
2. The method for detecting sensitive text on social media platforms according to claim 1, characterized in that, Step 1: Preprocessing of Chinese Sensitive Text Data. The input Chinese text data undergoes data cleaning, including removing pause words, web links, common punctuation marks, and replacing emoticons. The jieba word segmentation tool is then used to preprocess the Chinese text S, yielding word and character segmentation results. The word segmentation result is represented as w = (w1, w2, ..., w...). n )∈X S The result of the word segmentation is represented X S Let d represent the set of sensitive texts in dataset D. e Indicates the embedding layer dimension, l s The length of the sensitive text is indicated, and then the importance between the text segmentation results and the sensitive word library is evaluated according to the TF-IDF algorithm. The top k keywords with the highest importance are selected.
3. The method for detecting sensitive text on social media platforms according to claim 1, characterized in that, Step 4: Classification correction based on loss function. Add an extra term to the loss function to inject more prior knowledge into the model. Use the prior knowledge as the loss term of the model to correct the bias in the model's understanding and classification.
4. The method for detecting sensitive text on social media platforms according to claim 1, characterized in that, Step 4: Classification correction based on the loss function, specifically as follows: Its implementation consists of the following sub-steps: Sub-step 4-1: First, convert the first k keywords selected by the TF-IDF algorithm in the data preprocessing stage into word vectors a = (a1, a2, ..., a...). k As prior knowledge of text S, these are called key attributes. Sub-step 4-2: Represent the prior loss by calculating the L2 norm between the key attributes and the input text. prior , Among them, the L2 norm is a commonly used regularization method that can effectively prevent overfitting, reduce the influence of outliers, improve the generalization ability of the model, and maintain the continuity and differentiability of model parameters, thereby avoiding the problems of gradient vanishing or gradient exploding. n represents the vector length of the input sequence, and w... i and a i These represent the i-th word and the i-th key attribute of the input text S, respectively. Sub-step 4-3: The classification model training process includes two stages: forward propagation and back propagation. The loss function plays a guiding role in this process. Forward propagation involves processing the input data through each neural layer to obtain the prediction results and evaluating the difference between the prediction results and the true values. For text classification tasks, the prior loss is used as the regularization term of the traditional cross-entropy loss function Loss(y,p) to obtain the joint loss function Loss. joint To train the model, Where y represents the true value, p represents the posterior probability of the class, and a c w c p c These are the key attributes of category c, the word segmentation result, and the posterior probability. Sub-step 4-4: In the backpropagation phase, gradient descent is used to make the loss function converge to the global minimum or a better local minimum more quickly, improving the efficiency of updating model parameters. Backpropagation calculates the partial derivatives of the parameters of each layer, i.e., the gradients, based on the loss function, thereby updating the parameters, as shown in the formula: Where θ is the parameter of the loss function Loss(θ), θ 0 Indicates the current parameter value, θ 1 This represents the parameter value after one iteration, and lr represents the learning rate, which is used to control the step size for each update.