Esempio n. 1
0
    private static string addRecord()
    {
        string output = "addRecord\n";

        Random r = new Random();

        for (int i = 0; i < count; i++)
        {
            int no = r.Next(1000, 9000);
            titles[i] = "测试样例" + no;
            Article a = new Article(no, titles[i], titles[i], "", DateTime.Now, 0, 0);
            ids[i] = ArticleManager.addRecord(a);
            if (ids[i] < 0)
            {
                output += "Error! 新增文章\"" + titles[i] + "\"失败!返回值为" + ids[i] + "\n";
                errorCount++;
                continue;
            }

            output += "Ok! 新增文章\"" + titles[i] + "\"成功!返回值为" + ids[i] + "\n";
            Article2Group a2g = new Article2Group(ids[i], primaryGroupId);
            if (!Article2GroupManager.addRecord(a2g))
            {
                output += "Error! 将id为" + ids[i] + "的文章关联到id为" + primaryGroupId + "的主分类时失败!\n";
                errorCount++;
            }
            else
            {
                output += "Ok! 将id为" + ids[i] + "的文章关联到id为" + primaryGroupId + "的主分类时成功!\n";
            }
        }
        return(output);
    }
Esempio n. 2
0
    private static bool prepare()
    {
        for (int i = 0; i < 2; i++)
        {
            primaryGroup[i]         = new PrimaryGroups(0, "test1");
            primaryGroup[i].GroupId = PrimaryGroupMananger.addRecord(primaryGroup[i]);
            if (primaryGroup[i].GroupId < 0)
            {
                output += "Error! 新增测试辅助主分类\"test\"失败!返回值为" + primaryGroup[i].GroupId + "。测试无法继续进行。请先解决PrimaryGroupsManager中的错误。\n";
                errorCount++;
                return(false);
            }
        }

        Random r = new Random();

        for (int i = 0; i < count; i++)
        {
            int no = r.Next(1000, 9000);
            titles[i] = "测试样例" + no;
            Article a = new Article(no, titles[i], titles[i], "", DateTime.Now, 0, 0);
            ids[i] = ArticleManager.addRecord(a);
            if (ids[i] < 0)
            {
                output += "Error! 新增文章\"" + titles[i] + "\"失败!返回值为" + ids[i] + "。测试无法继续进行。请先解决Article2GroupManager中的错误。\n";
                errorCount++;
                return(false);
            }

            output += "Ok! 新增文章\"" + titles[i] + "\"成功!返回值为" + ids[i] + "\n";
            Article2Group a2g = new Article2Group(ids[i], primaryGroup[0].GroupId);
            if (!Article2GroupManager.addRecord(a2g))
            {
                output += "Error! 将id为" + ids[i] + "的文章关联到id为" + primaryGroup[0].GroupId + "的主分类时失败!测试无法继续进行。请先解决Article2GroupManager中的错误。\n";
                errorCount++;
                return(false);
            }
        }

        return(true);
    }
Esempio n. 3
0
    private static bool prepare()
    {
        Random r = new Random();

        for (int i = 0; i < count; i++)
        {
            int no = r.Next(1000, 9000);
            titles[i] = "测试样例" + no;
            Article a = new Article(no, titles[i], titles[i], "", DateTime.Now, 0, 0);
            ids[i] = ArticleManager.addRecord(a);
            if (ids[i] < 0)
            {
                output += "Error! 新增测试文章\"" + titles[i] + "\"失败!返回值为" + ids[i] + "。测试无法继续进行。请先解决ArticleManager中的错误。\n";
                errorCount++;
                return(false);
            }
            if (i < count / 2)
            {
                Article2Group a2g = new Article2Group(ids[i], primaryGroupId);
                if (!Article2GroupManager.addRecord(a2g))
                {
                    output += "Error! 将id为" + ids[i] + "的文章关联到id为" + primaryGroupId + "的主分类时失败!测试无法继续进行。请先解决Article2GroupManager中的错误。\n";
                    errorCount++;
                    return(false);
                }
            }
            else
            {
                Article2Group a2g = new Article2Group(ids[i], OtherGroupId);
                if (!Article2GroupManager.addRecord(a2g))
                {
                    output += "Error! 将id为" + ids[i] + "的文章关联到id为" + OtherGroupId + "的主分类时失败!测试无法继续进行。请先解决Article2GroupManager中的错误。\n";
                    errorCount++;
                    return(false);
                }
            }
        }
        return(true);
    }
Esempio n. 4
0
    /*
     * 输入:一个文章的Article实例,以及一个文章所属的groupId的列表
     * 输出:成功返回true,失败返回false
     * 功能:对指定文章进行分类,将其分到groupIds所包含的所有组别中去(即一篇文章可以被分到多个类别中)。
     * 用途说明:管理员新增一篇文章到数据库中去以后,系统会自动将文章分类,并将结果显示在页面上,管理员若对自动
     *           分类的结果不满意,可以手动调整,调整完毕后调用本函数修改文章的分类结果
     */
    public static bool changeGroupRelation(Article a, List <int> groupIds)
    {
        bool result = true;

        // 先删除旧的 groupIds
        List <int> old_gids = Article2GroupManager.getGroupIdsByArticle(a);  // 这里我认为这个传入的 Article 的 id 是已经赋值了的

        foreach (int old_gid in old_gids)
        {
            Article2GroupManager.deleteRecord(new Article2Group(a.ArticleId, old_gid));
        }
        // 再插入新的 groupIds
        foreach (int gid in groupIds)
        {
            if (Article2GroupManager.addRecord(new Article2Group(a.ArticleId, gid)) == false)
            {
                result = false; // 只要有一条记录插入不成功,则返回 false
            }
        }

        return(result);
    }
Esempio n. 5
0
    /*
     * 输入:一个Article的model实例以及其content字符串
     * 输出:执行结果标示符(暂定如下:0表示成功;-1~-5表示不同类型的失败)
     * 功能:
     *      1、对传入参数标示的文章内容进行分词、词频统计、TF·IDF计算等
     *      2、将文章总词数(article表的wordCount字段)更新到Article表中
     *      3、将分词结果更新到GlobalParse数据表中,注意参数中的articleNumber字段值要设置好:如果GlobalParse表中尚无该词记录,则
     *         articleNumber设为1;若GlobalParse表中已有该词记录,则读取其articleNumber并加一再存进去
     *      4、将分词结果更新到LocalParse数据表中,注意参数中的count字段值的设置要设置好:如果LocalParse表中尚无该词记录,则
     *         count设为1;若LocalParse表中已有该词记录,则读取其count并加一再存进去
     *      5、对文章进行分类,并将分类结果更新到Article2Group数据表中。若没有任何类别适合该文章,则默认应该将其分到groupId为0的类别中,该类别即“分组失败”类别或“其他”类别(视用户需求而定)
     *      6、返回执行结果。请实现者自己定义一下不同的返回值标示何种类型的失败
     *
     * 用途说明:该函数为私有函数,只被addArticleWrapper函数在将新文章添加到数据库后自动调用,用于解析新文章。
     *
     */
    private static void parseArticle(Article a, string content)
    {
        // 在这里先行数据库中读出 各个 primary group 的 关键词列表
        List <List <string> > allGroupKeywordList = new List <List <string> >();
        List <string[]>       allGroup            = PrimaryGroupMananger.getAllGroups();

        foreach (string[] ag in allGroup)
        {
            PrimaryGroups g = new PrimaryGroups();
            g.GroupId   = Convert.ToInt32(ag[0]);
            g.GroupName = ag[1];
            allGroupKeywordList.Add(PrimaryGroupKeyWordsManager.getKeyWordsOfCertainPrimaryGroup(g));
        }


        List <String[]> parseList = stringParse(content);

        a.WordCount = parseList.Count;
        Dictionary <String, Int32> dic = new Dictionary <String, Int32>();
        int wordMount = 0;

        foreach (String[] tempString in parseList)
        {
            if (stop_list.Contains(tempString[0]))
            {
                continue;
            }
            if (!dic.ContainsKey(tempString[0]))
            {
                dic.Add(tempString[0], 1);
                wordMount++;
            }
            else
            {
                int tempInt = dic[tempString[0]];
                dic[tempString[0]] = tempInt + 1;
            }
        }
        int articleID = a.ArticleId;
        Dictionary <String, Double> tf_idf = new Dictionary <String, Double>();

        foreach (KeyValuePair <String, Int32> keyPair in dic)
        {
            GlobalParse tempGP = new GlobalParse();
            tempGP.ArticleNumber = 1; tempGP.WordContent = keyPair.Key; tempGP.Type = "q";
            if (GlobalParseManager.addRecord(tempGP) == false)
            {
                int num = GlobalParseManager.selectRecordByWordContent(tempGP).ArticleNumber + 1;
                tempGP.ArticleNumber = num;
                GlobalParseManager.updateRecord(tempGP);
            }
            tempGP = GlobalParseManager.selectRecordByWordContent(tempGP);
            LocalParse tempLP = new LocalParse();
            tempLP.ArticleId = articleID; tempLP.WordContent = tempGP.WordContent; tempLP.Count = keyPair.Value;
            tempLP.Type      = "q";
            LocalParseManager.addRecord(tempLP);

            // Counting tf_idf
            int    tf_fenzi = tempLP.Count; int tf_fenmu = wordMount;
            int    total_document_number     = ArticleManager.countArticleNum();
            int    document_number_with_word = tempGP.ArticleNumber;
            double tf_idf_value              = ((double)tf_fenzi) / tf_fenmu;// *Math.Log((total_document_number / document_number_with_word), Math.E);
            tf_idf.Add(keyPair.Key, tf_idf_value);
        }
        //tf_idf.OrderByDescending(s => s.Value);
        double[] answer = new double[allGroup.Count];
        for (int i = 0; i < answer.Length; i++)
        {
            answer[i] = 0;
        }
        //Console.Write(tf_idf.ElementAt(0).Key);
        //Console.WriteLine(other.Count);

        for (int i = 0; i < answer.Length; i++)
        {
            List <string> list = allGroupKeywordList[i];
            if (list == null)
            {
                continue;
            }
            foreach (String tempS in list)
            {
                if (tf_idf.ContainsKey(tempS))
                {
                    answer[i] += tf_idf[tempS];
                }
            }
        }
        Article2Group a2g = new Article2Group();

        a2g.ArticleId = a.ArticleId;
        a2g.GroupId   = max(answer);
        Article2GroupManager.addRecord(a2g);
    }