标签匹配函数

函数是为了尽可能找出标签与之最匹配的相关问题(10个)目前对于sqlDataReader类的调用还存在一些问题,尚未全部解决。

目前代码如下(尚不能运行,待修改):

 public int RelatedTest(int[] qid, int j, int temp)
        {
            for (int k = 0; k < j; k++)
            {
                if (qid[k] == temp)
                    return qid[k];
            }
            return -1;
        }
        public List<Question> GetRelatedQuestions(List<Tag> tag)
        {
            List<Question> list = new List<Question>();
            SqlConnection conn = DbHelper.getConnection();
            int[] qid = new int[tag.Count * 10];
            int[] resemble = new int[tag.Count * 10];
            int j = 0, k = 0, l = 0;
            int[] top1 = new int[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            int[] top2 = new int[10];
            int[] tagId = new int[tag.Count];
            for(int i=0;i<tag.Count;i++)
            {
                tagId[i]=tag[i].Id;
            }
                SqlDataReader reader = DbHelper.ExecuteReader(
                String.Format("SELECT Question.* FROM [Question],[QuestionTagAssociation] WHERE Question.qid=QuestionTagAssociation.qid and tid in('"+tagId[0]+"')"
                ,conn)
               );
                while (reader.Read())
                {
                    int temp = Convert.ToInt32(reader["qid"].ToString());
                    if (j == 0)
                    {
                        qid[j++] = temp;
                        resemble[0] = 1;
                    }
                    else
                    {
                        int a = RelatedTest(qid, j, temp);
                        if (a < 0)
                        {
                            qid[j++] = temp;
                            resemble[0] = 1;
                        }
                        else
                        {
                            resemble[a]++;
                        }
                    }
                }
            if (j > 10)
                l = 10;
            else
                l = j;
            for (int i = 0; i < l; i++)
            {
                for (k = 0; k < j; k++)
                    if (i == 0)
                    {
                        if (top1[0] < resemble[k])
                            top1[0] = resemble[k];
                        top2[0] = qid[k];
                    }
                    else if (top1[i] < resemble[k] && resemble[k] < top1[i - 1])
                        top1[i] = resemble[k];
                top2[i] = qid[k];
            }
            SqlDataReader reader2 = DbHelper.ExecuteReader(
            String.Format("SELECT  Question.* FROM [Question],[QuestionTagAssociation] WHERE Question.qid in(" + top2 + ")")
            , conn
           );
            return list;
        }
      
    }
posted @ 2012-11-10 23:54  MagicCode1023  阅读(246)  评论(0编辑  收藏  举报