コード例 #1
0
    private static string getArticleIdsByGroup()
    {
        string        output = "getArticleIdsByGroup\n";
        PrimaryGroups pg     = new PrimaryGroups();

        pg.GroupId = primaryGroupId;
        List <int> list = Article2GroupManager.getArticleIdsByGroup(pg);

        if (list == null)
        {
            output += "Error! 调用getArticleIdsByGroup失败,返回null。\n";
            errorCount++;
            return(output);
        }
        for (int i = 0; i < count; i++)
        {
            if (!list.Contains(ids[i]))
            {
                output += "Error! id为" + primaryGroupId + "的主分类中未找到id为" + ids[i] + "的文章!\n";
                errorCount++;
            }
            else
            {
                output += "Ok! id为" + primaryGroupId + "的主分类中找到了id为" + ids[i] + "的文章!\n";
            }
        }
        return(output);
    }
コード例 #2
0
    private static string getKeyWordsOfCertainPrimaryGroup()
    {
        string        output = "getKeyWordsOfCertainPrimaryGroup\n";
        PrimaryGroups pg     = new PrimaryGroups(primaryGroupId, "");
        List <string> list   = PrimaryGroupKeyWordsManager.getKeyWordsOfCertainPrimaryGroup(pg);

        if (list == null)
        {
            output += "Error! getKeyWordsOfCertainPrimaryGroup失败,返回null。\n";
            errorCount++;
            return(output);
        }
        for (int i = 0; i < count; i++)
        {
            if (!list.Contains(keys[i]))
            {
                output += "Error! 未拿到keyWord为" + keys[i] + "的测试记录!\n";
            }
            else
            {
                output += "Ok! 成功拿到keyWord为" + keys[i] + "的测试记录!\n";
            }
        }

        return(output);
    }
コード例 #3
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);
            string        name = "测试样例" + no;
            PrimaryGroups pg   = new PrimaryGroups(no, name);
            int           id   = PrimaryGroupMananger.addRecord(pg);
            ids[i] = id;
            if (id < 0)
            {
                output += "Error! 新增主分类\"" + name + "\"失败!返回值为" + id + "\n";
                errorCount++;
            }
            else
            {
                output += "Ok! 新增主分类\"" + name + "\"成功!返回值为" + id + "\n";
            }
        }
        return(output);
    }
コード例 #4
0
    // 删除传入参数中id所示的记录,不对除PrimaryGroups表之外的任何表进行操作。仅读取传入参数中的PrimaryGroupsId字段
    // 仅供测试函数调用
    public static bool deleteRecord(PrimaryGroups pg)
    {
        string     sqlStr = "DELETE FROM primaryGroups WHERE groupId=@gId";
        SqlCommand cmd    = new SqlCommand(sqlStr);

        cmd.Parameters.AddWithValue("@gId", pg.GroupId);
        return(DBHelper.ExecSQL(cmd));
    }
コード例 #5
0
    //根据传入参数更新数据库中groupId为传入参数的groupId的相应记录
    //成功返回true,失败返回false
    public static bool updateRecord(PrimaryGroups g)
    {
        string     sqlStr = "UPDATE primaryGroups SET groupName=@gName WHERE groupId=@gId";
        SqlCommand cmd    = new SqlCommand(sqlStr);

        cmd.Parameters.AddWithValue("@gName", g.GroupName);
        cmd.Parameters.AddWithValue("@gId", g.GroupId);
        return(DBHelper.ExecSQL(cmd));
    }
コード例 #6
0
    public static List <Article> getArticleListOfOthersForNoLogin(int userId, GroupNode gn)
    {
        if (gn != null)
        {
            // 1、如果第二个参数不为null:
            PrimaryGroups pg = PrimaryGroupMananger.selectRecord(new PrimaryGroups(gn.PrimaryGroupId, ""));
            if (pg == null)
            {
                return(null);
            }
            List <Article> al = ArticleManager.getArticleListByPrimaryGroup(pg);
            if (al == null)
            {
                return(null);
            }
            List <Tag> tagList = TagManager.getAllTagsByCertainGroupId(pg.GroupId);
            if (tagList != null)
            {
                int tagListLength = tagList.Count;
                for (int i = 0; i < tagListLength; i++)
                {
                    Tag t = tagList[i];
                    if (t.TagName != "其他")
                    {
                        List <Article> articleListInTag = ArticleManager.getArticleListByDynamicSearch(t);
                        if (articleListInTag != null)
                        {
                            int articleListInTagLength = articleListInTag.Count;
                            for (int k = 0; k < articleListInTagLength; k++)
                            {
                                Article a        = articleListInTag[k];
                                int     alLength = al.Count;
                                for (int p = 0; p < alLength; p++)
                                {
                                    if (al[p].ArticleId == a.ArticleId)
                                    {
                                        al.RemoveAt(p);
                                        alLength -= 1;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(al);
        }
        else
        {
            // 2、如果第二个参数为null,只需返回所有文章中没有被分到任何主分类的文章的列表即可
            PrimaryGroups pg = new PrimaryGroups();
            pg.GroupId = 0; // 其它主分类的 id 为 0
            return(ArticleManager.getArticleListByPrimaryGroup(pg));
        }
    }
コード例 #7
0
    /************************************************************************************************************************************************************
    *
    * 【关于本类用法的重要说明】
    * 当前台用户点击导航树上的某一个节点时,需要调用本类中的getArticleList方法族获取文章列表。这个类的难点就在于理解获取文章列表的三个方法的差异。
    * 获取文章列表有三个不同方法,分别对应以下三种情况:
    * 1、用户点击了非“其他”的主分类之后,需要显示指定主分类下所有文章的列表,此时调用getArticleListOfCertainPrimaryGroup(GroupNode gn);
    * 2、用户点击了非“其他”的主分类下的非“其他”的子分类或兴趣标签,需要通过动态搜索显示指定子分类或兴趣标签下所有文章的列表,此时调用getArticleListByDynamicSearch(GroupNode gn);
    * 3、用户点击了“其他”主分类,需要显示所有没有被分到任何主分类的文章列表,此时调用getArticleListOfOthers( int userId , GroupNode gn ),并将第二个参数gn置为null;
    * 4、用户点击了非“其他”的主分类下的“其他”子分类,需要显示该主分类下所有未在任何一个该用户关注了的子分类和兴趣标签下的文章的列表,
    *    此时调用getArticleListOfOthers( int userId , GroupNode gn ),并将第二个参数设置为被点击的“其他”子分类对应的GroupNode实例。
    *
    * 注:关于三个函数的具体功能请参看相应函数前的注释
    *
    ************************************************************************************************************************************************************/



    /*
     * 输入:一个GroupNode的model实例(仅使用其中的id字段,将这个字段作为groupId解析)
     * 输出:Article的列表
     * 功能:读取传入参数中的id,获取数据库中该主分类的全部文章的Article的model对象列表并返回
     * 用途说明:当用户点击某个主分类查看该分类下所有文章时,此函数返回该主分类所有文章的列表
     */
    public static List <Article> getArticleListOfCertainPrimaryGroup(GroupNode gn)
    {
        PrimaryGroups pg = PrimaryGroupMananger.selectRecord(new PrimaryGroups(gn.Id, ""));

        if (pg != null)
        {
            return(ArticleManager.getArticleListByPrimaryGroup(pg));
        }
        return(null);
    }
コード例 #8
0
    private static bool prepare()
    {
        Random r = new Random();

        // add global user
        sampleUser        = new User("test", "test", "test");
        sampleUser.UserId = UserManager.addUser(sampleUser);
        if (sampleUser.UserId < 0)
        {
            output += "Error! 新增用户\"test\"失败!返回值为" + sampleUser.UserId + "。测试无法继续进行。请先解决UserManager中的错误。\n";
            errorCount++;
            return(false);
        }

        for (int i = 0; i < count; i++)
        {
            int no = r.Next(1000, 9000);

            // add primary groups
            pgs[i]         = new PrimaryGroups(100, no + "");
            pgs[i].GroupId = PrimaryGroupMananger.addRecord(pgs[i]);
            if (pgs[i].GroupId < 0)
            {
                output += "Error! 新增主分类失败!返回值为" + pgs[i].GroupId + "。测试无法继续进行。请先解决PrimaryGroupManager中的错误。\n";
                errorCount++;
                return(false);
            }

            // add public sub groups
            tags[i]       = new Tag("sub" + no, "sub" + no, pgs[i].GroupId, DateTime.Now, 0);
            tags[i].TagId = TagManager.addTag(tags[i]);
            if (tags[i].TagId < 0)
            {
                output += "Error! 新增公共子分类失败!返回值为" + tags[i].TagId + "。测试无法继续进行。请先解决TagManager中的错误。\n";
                errorCount++;
                return(false);
            }

            // link user to sub groups
            User2Tag u2t = new User2Tag(sampleUser.UserId, tags[i].TagId);
            if (!User2TagManager.addRecord(u2t))
            {
                output += "Error! 新增User2Tag记录失败!测试无法继续进行。请先解决User2TagManager中的错误。\n";
                errorCount++;
                return(false);
            }
        }

        return(true);
    }
コード例 #9
0
    //根据传入参数中的groupId查询一条相应记录(仅读取传入参数中的groupId字段)
    //返回一个Group的model
    //失败则返回null
    public static PrimaryGroups selectRecord(PrimaryGroups g)
    {
        string     sqlStr = "SELECT * FROM primaryGroups WHERE groupId=@gId";
        SqlCommand cmd    = new SqlCommand(sqlStr);

        cmd.Parameters.AddWithValue("@gId", g.GroupId);
        DataSet resultSet = DBHelper.GetDataSet(cmd);

        if (resultSet.Tables[0].Rows.Count != 0)
        {
            g.GroupName = resultSet.Tables[0].Rows[0]["groupName"].ToString();
            return(g);
        }
        return(null);
    }
コード例 #10
0
    //根据传入参数增加一条记录
    //成功返回新增记录的groupId,失败返回-1
    //groupId非自增,由程序判断,选择空的
    public static int addRecord(PrimaryGroups g)
    {
        string     sqlStr = "INSERT INTO primaryGroups (groupName) VALUES (@pName) SELECT Scope_Identity()";
        SqlCommand cmd    = new SqlCommand(sqlStr);

        cmd.Parameters.AddWithValue("@pName", g.GroupName);
        Object result = DBHelper.GetOneResult(cmd);

        if (result != null)
        {
            g.GroupId = Convert.ToInt32(result);
            return(g.GroupId);
        }
        return(-1);
    }
コード例 #11
0
    /*
     * 输入:组别ID的列表
     * 输出:对应的组别名称的列表
     * 功能:查询数据库,将传入的组别ID翻译成组别名称,并返回
     * 用途说明:管理员通过getGroupIdsByArticleWrapper函数得到了一系列组别的ID,再通过本函数将之翻译成为组别的名称。
     * 注:之所以要将getGroupIdsByArticleWrapper和getGroupNamesByGroupIds两个函数的功能分开,主要是考虑到降低代码耦合性,
     *     同时,即便将两个函数的功能合并到一起,也不会降低数据库的读操作开销。
     */
    public static List <String> getPrimaryGroupNamesByPrimaryGroupIds(List <int> groupIds)
    {
        List <string> result = new List <string>();

        Console.Write(groupIds[0]);
        foreach (int id in groupIds)
        {
            PrimaryGroups g = new PrimaryGroups();
            g.GroupId = id;
            PrimaryGroups sg = PrimaryGroupMananger.selectRecord(g);
            if (sg != null)
            {
                result.Add(g.GroupName);
            }
        }

        return(result);
    }
コード例 #12
0
    //仅读取传入参数中的GroupId,获取该分类下的所有文章的articleId的列表
    //返回articleId的列表List<int>
    public static List <int> getArticleIdsByGroup(PrimaryGroups g)
    {
        int     groupId = g.GroupId;
        string  sqlStr  = "SELECT articleId FROM article2group WHERE groupId=" + groupId;
        DataSet dataset = SqlManager.GetDataSet(sqlStr, "article2group"); //执行查询操作

        if (dataset.Tables[0].Rows.Count == 0)
        {
            return(null);
        }
        List <int> resultList = new List <int>();

        for (int i = 0; i <= dataset.Tables[0].Rows.Count - 1; i++)
        {
            //将取得的dataset中所有的articleId强制转化为int类型并加入到resultList中
            resultList.Add(Convert.ToInt32(dataset.Tables[0].Rows[i]["articleId"]));
        }
        return(resultList);
    }
コード例 #13
0
    private static string deleteRecord()
    {
        string output = "deleteRecord\n";

        for (int i = 0; i < count; i++)
        {
            PrimaryGroups pg = new PrimaryGroups(ids[i], "");
            if (!PrimaryGroupMananger.deleteRecord(pg))
            {
                output += "Error! 删除测试主分类(id为\"" + ids[i] + "\")失败!\n";
                errorCount++;
            }
            else
            {
                output += "Ok! 删除测试主分类(id为\"" + ids[i] + "\")成功!\n";
            }
        }

        return(output);
    }
コード例 #14
0
    //返回指定主分类的所有关键词的列表
    public static List <string> getKeyWordsOfCertainPrimaryGroup(PrimaryGroups g)
    {
        List <string> result = new List <string>();
        string        sqlStr = "SELECT * FROM primaryGroupKeyWords WHERE primaryGroupId=@pgId";
        SqlCommand    cmd    = new SqlCommand(sqlStr);

        cmd.Parameters.AddWithValue("@pgId", g.GroupId);
        DataSet resultSet = DBHelper.GetDataSet(cmd);

        if (resultSet.Tables[0].Rows.Count != 0)
        {
            for (int i = 0; i < resultSet.Tables[0].Rows.Count; i++)
            {
                string tempStr = resultSet.Tables[0].Rows[i]["keyWord"].ToString();
                result.Add(tempStr);
            }
            return(result);
        }
        return(null);
    }
コード例 #15
0
ファイル: TestManagerAssist.cs プロジェクト: zccshome/sepj2
    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);
    }
コード例 #16
0
    private static string selectRecord()
    {
        string output = "selectRecord\n";

        for (int i = 0; i < count; i++)
        {
            PrimaryGroups pg = new PrimaryGroups(ids[i], "");
            pg = PrimaryGroupMananger.selectRecord(pg);
            if (pg == null)
            {
                output += "Error! 获取id为" + ids[i] + "的主分类记录失败!返回值为null。\n";
                errorCount++;
            }
            else
            {
                output += "Ok! 获取id为" + ids[i] + "的主分类记录成功!\n";
            }
        }

        return(output);
    }
コード例 #17
0
ファイル: TestArticleManager.cs プロジェクト: zccshome/sepj2
    private static string getArticleListByPrimaryGroup(int primaryGroupId)
    {
        string        output = "getArticleListByPrimaryGroup(默认查询id为" + primaryGroupId + "的主分类)\n";
        PrimaryGroups pg     = new PrimaryGroups();

        pg.GroupId = primaryGroupId;
        List <Article> list = ArticleManager.getArticleListByPrimaryGroup(pg);

        if (list == null)
        {
            //output += "Error! 调用getArticleListByPrimaryGroup失败,返回null。\n";
            output += "调用getArticleListByPrimaryGroup失败,返回null。\n";
            //errorCount++;
        }
        else
        {
            output += "Ok! 调用getArticleListByPrimaryGroup成功,返回的文章数为" + list.Count + "。\n";
        }

        return(output);
    }
コード例 #18
0
    /**
     * 输入:指定用户的userId
     * 输出:该用户关注的所有公共分类(主分类+子分类)的列表
     * 功能:获取所有主分类的所有子分类的列表
     * 注意:返回的结果中包括主分类下的各个“其他”子分类,额外地,还要通过各个“其他”子分类还原出用户关注的主分类,并添加到结果列表中。
     */
    public static List <GroupNode> getFocusedPublicGroupsByUserId(int userId)
    {
        List <GroupNode> focusedPublicGroupList = new List <GroupNode>();
        User             user      = new User(userId, "", "", "");
        List <int>       tagIdList = User2TagManager.getTagListByUserId(user);
        List <Tag>       tagList   = TagManager.getTagsByIdList(tagIdList);

        if (tagList == null)
        {
            return(null);
        }
        foreach (Tag tag in tagList)
        {
            if (tag.IsPrivate == 1)
            {
                continue;
            }
            GroupNode groupNode = new GroupNode();
            groupNode.Id              = tag.TagId;
            groupNode.IsPrimaryGroup  = false;
            groupNode.IsInterestLabel = false;
            groupNode.NodeName        = tag.TagName;
            groupNode.PrimaryGroupId  = tag.GroupId;
            focusedPublicGroupList.Add(groupNode);
            if (tag.TagName.Equals("其他"))
            {
                GroupNode groupNode2 = new GroupNode();
                groupNode2.Id              = tag.GroupId;
                groupNode2.IsPrimaryGroup  = true;
                groupNode2.IsInterestLabel = false;
                PrimaryGroups primaryGroup = new PrimaryGroups();
                primaryGroup.GroupId      = tag.GroupId;
                primaryGroup              = PrimaryGroupMananger.selectRecord(primaryGroup);
                groupNode2.NodeName       = primaryGroup.GroupName;
                groupNode2.PrimaryGroupId = tag.GroupId;
                focusedPublicGroupList.Add(groupNode2);
            }
        }
        return(focusedPublicGroupList);
    }
コード例 #19
0
    // 返回详细测试结果报告
    public static string test(int testTimes)
    {
        count        = testTimes;
        errorCount   = 0;
        ids          = new int[count];
        titles       = new string[count];
        primaryGroup = new PrimaryGroups();

        output  = "";
        output += "开始测试NewsAssist(测试样本数为" + count + ")\n\n";

        // Prepare
        if (!prepare())
        {
            return(output);
        }

        // Test
        output += ">> Case 1 :\n";
        getArticleListOfCertainPrimaryGroup();
        output += "\n";
        output += ">> Case 2 :\n";
        getArticleListByDynamicSearch();
        output += "\n";
        output += ">> Case 3 :\n";
        getArticleListOfOthers();
        output += "\n";
        output += ">> Case 4 :\n";
        getArticleByIdWrapper();
        output += "\n";
        output += ">> Case 5 :\n";
        getArticleContentByArticleModel();
        output += "\n";

        // Clean
        clean();//*/

        return(output);
    }
コード例 #20
0
    private static string updateRecord()
    {
        string output = "updateRecord\n";

        for (int i = 0; i < count; i++)
        {
            PrimaryGroups pg = new PrimaryGroups(ids[i], "test");
            //output +=  pg.GroupId + "\t" + pg.GroupName + "\n";
            if (!PrimaryGroupMananger.updateRecord(pg))
            {
                output += "Error! 调用updateRecord对id为\"" + ids[i] + "\"的测试主分类记录失败!返回值为false。\n";
                errorCount++;
            }
            else
            {
                output += "Ok! 调用updateRecord对id为\"" + ids[i] + "\"的测试主分类记录成功!返回值为true。\n";
            }
            pg = PrimaryGroupMananger.selectRecord(pg);
            if (pg == null)
            {
                output += "Error! 获取id为" + ids[i] + "的测试主分类失败!返回值为空。\n";
                errorCount++;
                continue;
            }
            if (!pg.GroupName.Equals("test"))
            {
                output += "Error! 对id为" + ids[i] + "的测试主分类更新GroupName失败!GroupName被更改为" + pg.GroupName + "。\n";
                errorCount++;
            }
            else
            {
                output += "Ok! 对id为" + ids[i] + "的测试主分类更新GroupName成功!GroupName被更改为" + pg.GroupName + "。\n";
            }
        }

        return(output);
    }
コード例 #21
0
    /**
     * 输入:新主分类的名称,以及相应的关键词列表。各个关键词之间用空格分隔。
     * 输出:返回所有未被分到任何主分类的文章列表
     * 功能:1、新增一个主分类
     *       2、在该主分类下增加一个“其他”子分类
     *       2、调用NewsAssist.cs中getArticleListOfOthers( int userId , GroupNode gn )方法,通过将第二个参数置为null,拿到所有未被分到任何主分类的文章列表;
     *       3、返回这些文章的列表,让管理员查看一下其中有没有可以直接被分到新的主分类去的文章。
     */
    public static List <Article> addPrimaryGroup(string name, string keywordsList)
    {
        // 功能:1、新增一个主分类
        PrimaryGroups pg = new PrimaryGroups();

        pg.GroupName = name;
        int gid = PrimaryGroupMananger.addRecord(pg);
        PrimaryGroupKeyWords pgk = new PrimaryGroupKeyWords();

        pgk.PrimaryGroupId = gid;
        pgk.KeyWord        = keywordsList;
        bool addPGKsuccess = PrimaryGroupKeyWordsManager.addRecord(pgk);

        if (addPGKsuccess)
        {
            addSecondaryGroup(gid, "其他", "");
        }

        // 功能:2、调用Articlemanager.cs 中的方法,返回所有未分类的文章
        PrimaryGroups p = new PrimaryGroups();

        p.GroupId = 0;
        return(ArticleManager.getArticleListByPrimaryGroup(p));
    }
コード例 #22
0
    /*
     * 输入:第一个参数用户的userId,第二个参数有两种可能:
     *       1、若第二个参数不为null,则表示一个普通主分类下的“其他”子分类的GroupNode实例(仅使用其中的primaryGroupId字段);
     *       2、若第二个参数为null,则表示“其他”主分类。
     * 输出:Article的列表
     * 功能:这个方法的功能比较难理解。以下是大致实现思路:
     *       1、如果第二个参数不为null:
     *          (1)根据参数gn的primaryGroupId字段确定主分类,记为M;
     *          (2)拿到M下的所有文章列表,记为Q;
     *          (3)根据userId查找该用户关注了M下的哪些子分类和兴趣标签;
     *          (4)利用getArticleListByDynamicSearch函数拿到在M下该用户关注的所有子分类和兴趣标签对应的文章的并集,记为R;
     *          (5)返回值为 Q - R
     *       2、如果第二个参数为null,比较简单,只需返回所有文章中没有被分到任何主分类的文章的列表即可
     * 用途说明:当用户点击某个主分类下“其他”子分类或“其他”主分类查看该分类下所有文章时,此函数返回该分类所有文章的s列表
     */
    public static List <Article> getArticleListOfOthers(int userId, GroupNode gn)
    {
        if (gn != null)
        {
            // 1、如果第二个参数不为null:
            PrimaryGroups pg = PrimaryGroupMananger.selectRecord(new PrimaryGroups(gn.PrimaryGroupId, ""));
            if (pg == null)
            {
                return(null);
            }
            List <Article> al = ArticleManager.getArticleListByPrimaryGroup(pg);
            if (al == null)
            {
                return(null);
            }
            User u = new User();
            u.UserId = userId;
            List <int> tagList = User2TagManager.getTagListByUserId(u);
            if (tagList != null)
            {
                int tagListLength = tagList.Count;
                for (int i = 0; i < tagListLength; i++)
                {
                    Tag t = new Tag();
                    t.TagId = tagList[i];
                    t       = TagManager.getTag(t);
                    if (t.TagName != "其他")
                    {
                        List <Article> articleListInTag = ArticleManager.getArticleListByDynamicSearch(t);
                        if (articleListInTag != null)
                        {
                            int articleListInTagLength = articleListInTag.Count;
                            for (int k = 0; k < articleListInTagLength; k++)
                            {
                                Article a        = articleListInTag[k];
                                int     alLength = al.Count;
                                for (int p = 0; p < alLength; p++)
                                {
                                    if (al[p].ArticleId == a.ArticleId)
                                    {
                                        al.RemoveAt(p);
                                        alLength -= 1;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(al);
        }
        else
        {
            // 2、如果第二个参数为null,只需返回所有文章中没有被分到任何主分类的文章的列表即可
            List <Article> al = new List <Article>();

            List <string[]> allPG = PrimaryGroupMananger.getAllGroups();
            if (allPG != null)
            {
                for (int i = 0; i < allPG.Count; i++)
                {
                    PrimaryGroups pg = new PrimaryGroups();
                    pg.GroupId   = Convert.ToInt32(allPG[i][0]);
                    pg.GroupName = allPG[i][1];
                    List <Article> onePGAL = ArticleManager.getArticleListByPrimaryGroup(pg);
                    if (onePGAL != null)
                    {
                        for (int j = 0; j < onePGAL.Count; j++)
                        {
                            al.Add(onePGAL[j]);
                        }
                    }
                }
            }

            User u = new User();
            u.UserId = userId;
            List <int> tagList = User2TagManager.getTagListByUserId(u);
            if (tagList != null)
            {
                int tagListLength = tagList.Count;
                for (int i = 0; i < tagListLength; i++)
                {
                    Tag t = new Tag();
                    t.TagId = tagList[i];
                    t       = TagManager.getTag(t);

                    List <Article> articleListInTag = ArticleManager.getArticleListByDynamicSearch(t);
                    if (articleListInTag != null)
                    {
                        int articleListInTagLength = articleListInTag.Count;
                        for (int k = 0; k < articleListInTagLength; k++)
                        {
                            Article a        = articleListInTag[k];
                            int     alLength = al.Count;
                            for (int p = 0; p < alLength; p++)
                            {
                                if (al[p].ArticleId == a.ArticleId)
                                {
                                    al.RemoveAt(p);
                                    alLength -= 1;
                                }
                            }
                        }
                    }
                }
            }

            return(al);
        }
    }
コード例 #23
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);
    }
コード例 #24
0
ファイル: TestManagerAssist.cs プロジェクト: zccshome/sepj2
    private static void addPrimaryGroup()
    {
        output += "addPrimaryGroup\n";

        // add
        string name     = "sample123";
        string keywords = "qawsed rftgyh";

        ManagerAssist.addPrimaryGroup(name, keywords);

        // check
        List <string[]> list    = PrimaryGroupMananger.getAllGroups();
        bool            found   = false;
        int             groupId = -1;

        foreach (string[] item in list)
        {
            if (item[1].Equals("sample123"))
            {
                found   = true;
                groupId = int.Parse(item[0]);
                break;
            }
        }
        if (!found)
        {
            output += "Error! addPrimaryGroup执行失败!主分类未加入数据库中。\n";
            errorCount++;
        }
        else
        {
            output += "Ok! addPrimaryGroup执行成功!主分类已加入数据库中。\n";
        }

        PrimaryGroups pg    = new PrimaryGroups(groupId, name);
        List <string> list2 = PrimaryGroupKeyWordsManager.getKeyWordsOfCertainPrimaryGroup(pg);

        if (list2.Count != 2)
        {
            output += "Error! PrimaryGroupKeyWords数目不对!应为2\n";
        }
        else
        {
            output += "Ok! PrimaryGroupKeyWords数目正确!应为2\n";
        }

        if (list2.First().Equals("qawsed") || list2.First().Equals("rftgyh"))
        {
            output += "Ok! PrimaryGroupKeyWords中对应的第一个关键词通过验证!\n";
        }
        else
        {
            output += "Error! PrimaryGroupKeyWords中对应的第一个关键词未通过验证!\n";
            errorCount++;
        }

        List <Tag> tagL = TagManager.getAllTagsByCertainGroupId(groupId);

        if (tagL == null || tagL.Count != 1)
        {
            output += "Error! addPrimaryGroup调用后没有将“其他”子分类插入tag表!\n";
            errorCount++;
        }
        else
        {
            output += "Ok! addPrimaryGroup调用后成功将“其他”子分类插入tag表!\n";
        }

        // delete
        foreach (string item in list2)
        {
            if (!PrimaryGroupKeyWordsManager.deleteRecord(new PrimaryGroupKeyWords(groupId, item)))
            {
                output += "Error! 删除测试关键词失败!\n";
                errorCount++;
            }
        }
        Tag t = new Tag();

        t.TagId = tagL.First().TagId;
        if (!TagManager.deleteTag(t))
        {
            output += "Error! 删除测试主分类下“其他”子分类失败!\n";
            errorCount++;
        }
        PrimaryGroups temp = new PrimaryGroups();

        temp.GroupId = groupId;
        if (!PrimaryGroupMananger.deleteRecord(temp))
        {
            output += "Error! 删除测试主分类失败!\n";
            errorCount++;
        }
    }
コード例 #25
0
ファイル: ArticleManager.cs プロジェクト: zccshome/sepj2
    //获取该分类下的所有文章的article的列表(仅读取传入参数中的GroupId字段)
    //返回article的列表List<Article>
    public static List <Article> getArticleListByPrimaryGroup(PrimaryGroups g)
    {
        List <int> articlesByGroupId = Article2GroupManager.getArticleIdsByGroup(g);

        return(getArticleListByArticleIdList(articlesByGroupId));
    }