代码改变世界

.敏感词过滤

2012-08-15 18:05  C#与.NET探索者  阅读(994)  评论(4编辑  收藏  举报

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace _03敏感词过滤
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            string usrInput = txtContent.Text.Trim();

            //usrInput.Contains(
            //1.先读取filter.txt文件中的内容。
            //通过正则表达式判断是否包含某个敏感词
            string[] lines = File.ReadAllLines("filter.txt", Encoding.Default);
            StringBuilder sbMode = new StringBuilder();
            StringBuilder sbBanned = new StringBuilder();
            for (int i = 0; i < lines.Length; i++)
            {
                string[] info = lines[i].Split('=');
                if (info[1] == "{MOD}")
                {
                    sbMode.Append(info[0] + "|");
                }
                else if (info[1] == "{BANNED}")
                {
                    sbBanned.Append(info[0] + "|");
                }

            }
            sbMode.Remove(sbMode.Length - 1, 1);
            sbBanned.Remove(sbBanned.Length - 1, 1);




            //2.判断用户输入的文字在filter.txt文件中是否存在


            //判断用户输入的字符串中是否包含“禁止发帖的关键词”
            bool b = Regex.IsMatch(usrInput, sbBanned.ToString());
            if (b)
            {
                MessageBox.Show("禁止发帖!");
            }
            else if (Regex.IsMatch(usrInput, sbMode.ToString()))
            {
                MessageBox.Show("需要审核");
            }
            else
            {
                MessageBox.Show("发帖成功!");
            }

            //3.提示用户。


        }
    }
}