最小风险贝叶斯决策在广告微博过滤的实现算法

1.对微博文本前期的处理,除去西文字符只剩下中文字符。

/** 除去西文字符 */
	public String replace(String st)
	{
		char[] chs = st.toCharArray();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < chs.length; i++)
		{
			if ((chs[i] >= 0x4e00) && (chs[i] <= 0x9fbb))
			{
				sb.append(chs[i]);
			}
		}
		return sb.toString();
	}

2.微博文本的分词,用最终将一条微博内容用向量形式表示

正向最大匹配方法

/*** 正向最大匹配法 
     * @param ck 分词词库
     * @param text 文本
     * */
    public static Vector<String> MM(Vector<String> ck, String text)
    {
        Vector<String> res = new Vector<String>();
        String tmp = text;
        while (true)
        {
            String str = null;
            if (tmp.length() > maxlen)//
            {
                str = tmp.substring(0, maxlen);//
            } else
            {
                str = tmp;
            }
            // 判断字串是否成词
            while (true)
            {
                if (str.length() == 1)
                {
                    res.add(str);
                    tmp = tmp.substring(str.length());
                    break;
                }
                if (ck.contains(str))// 依据特征词表,判断它是否成词
                {
                    res.add(str);
                    tmp = tmp.substring(str.length());
                    break;
                }
                if (tmp.length() == 0)
                {
                    return res;
                }
                str = str.substring(0, str.length() - 1);
            }

        }

    }


3.统计样本中的特征词数量及每个特征词在两种微博类别中的频率

	/**
	 * 计算 样本集的所有分词(不重复)频率
	 * 
	 * @param ybs  样本集
	 */
	public final int N(Vector<YanBen> ybs)
	{
		HashSet<String> hs = new HashSet<String>();
		for (YanBen yb : ybs)
		{
			for (String s : yb.vs)
			{
				hs.add(s);
			}
		}
		return hs.size();
	}

	/**
	 * 计算 type类样本集合y中分词t的频率
	 * 
	 * @param t
	 *            分词
	 * @param ybs
	 *            样本集合
	 * @param type
	 *            类型
	 */
	public final int N(String t, Vector<YanBen> ybs, int type)
	{
		int k1 = 0;
		for (YanBen y : ybs)
		{
			if (y.c == type)
			{
				k1 = k1 + N(t, y);
			}
		}
		return k1;
	}


4.计算每个特征词的先验概率

/**
	 * 计算分词t的type类的先验概率
	 * 
	 * @param t
	 *            分词
	 * @param type
	 *            type类类型
	 */
	public final double ptc(Vector<YanBen> yanben, Vector<String> ck, String t,
			int type)
	{

		/** 特征词的个数 */
		if (n == -1)
		{
			// n=yanben.size();
			n = N(yanben);// ck.size()
			// n=ck.size();
		}
		// t在tyle类的频数
		int nt = N(t, yanben, type);
		// 所有分词库在type类的频数和
		if (nc[type] == -1)
		{
			nc[type] = N(ck, yanben, type);
		}
		double ptc = (double) (1 + nt) / (n + nc[type]);
		// double ptc1 = (double) (nt) / (nc[type]);

		return ptc;
	}

5.计算一条微博的后验概率

	/**
	 * 计算内容是type类的概率
	 * 
	 * @param text
	 *            文本内容
	 * @param cs
	 *            训练后的词库
	 * */
	public final double p(String text, Vector<CI> cs, int type)
	{
		Vector<String> sk = new Vector<String>();
		for (CI c : cs)
		{
			sk.add(c.t);
		}
		// 分词
		Vector<String> vs = MM(sk, this.replace(text));
		double pnpc[] = new double[2];
		pnpc[0] = pc[0];//样本中正常类的概率 
		pnpc[1] = pc[1];//样本中广告类的概率 
		String lg = "";
		for (String t : vs)
		{
			for (CI c : cs)
			{
				if (t.equals(c.t))
				{
					lg = lg + "/" + t;
					pnpc[1] = pnpc[1] * c.ptc1;//特征词在广告类中先验概率 
					pnpc[0] = pnpc[0] * c.ptc0;//特征词在正常类中先验概率
				}
			}
		}
		double d = pnpc[type] / (pnpc[1] + pnpc[0]);
		// 最小风险系数
		int r = 9;
		double t = (double) r / (1 + r);
		if (d > t)
		{
			System.err.println(text);
			System.err.println(lg);
			System.err.println(pnpc[type] + "/(" + pnpc[0] + "+" + pnpc[1]
					+ ")=" + d);
		} else
		{
			System.out.println(text);
			System.out.println(lg);
			System.out.println(pnpc[type] + "/(" + pnpc[0] + "+" + pnpc[1] + ")=" + d);
		}

		return d;
	}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值