zoukankan      html  css  js  c++  java
  • 词频统计单元测试--------第二版

      在上一次的单元测试的基础上,我单独写了一个测试类对 单词树进行遍历的方法sortAndOutput() 进行测试,下图是这个方法的展示,他返回的是一个Map,Map里面存的是对应的单词以及其出现的次数。

    /*
         * 对文章用generateCharTree()函数生成单词树,在对生成的单词树进行排序并且输出
         */
        public Map sortAndOutput(String filePath) throws IOException {
            reader = new FileReader(filePath);
            buffer = new BufferedReader(reader);
            char c[] = new char[128]; // 防止读入一行太长而出现溢出的情况
            int len;
            String temp = ""; // 存储buffer读出的字符
            String lastWord = ""; // 若读出的最后一个字符是字母,lastWord存储那个字符所在的单词,lastWord就被加到下一轮的temp中
            while ((len = buffer.read(c)) > 0) {
                temp = ""; // 清空上一轮temp的内容
                temp += lastWord;
                for (int i = 0; i < len; i++) {
                    temp += c[i];
                }
                lastWord = ""; // 清空上一轮lastword的内容
                if (Character.isLetter(c[len - 1])) { // 当128个字符的最后一个字符为字母时,就得分情况讨论
                    int j, t;
                    for (j = len - 1, t = 0; Character.isLetter(c[j]); j--, t++)
                        ; // t代表最后一个字符(c[len-1])为字符时,前面有几个连续的字符
                    temp = temp.substring(0, temp.length() - t); // 当检测到最后一个字符为字母时,直接将最后一个字符所在的单词剔除本轮的temp,而转接到下一轮的temp中
                    for (int k = j + 1; k < len; k++) {
                        lastWord += c[k];
                    }
                }
                root = generateCharTree(temp);
            }
            List<Word> list = new ArrayList<Word>();
            searchTree(list, root);
            // 对生成的单词树按照单词的次数逆向排序排序
            Collections.sort(list, new Comparator<Object>() {
                @Override
                public int compare(Object o1, Object o2) {
                    Word word1 = (Word) o1;
                    Word word2 = (Word) o2;
                    return word2.getNum() - word1.getNum();
                }
            });
            buffer.close();
            reader.close();
            root=new CharTreeNode();  //但对于输入的是目录时,对每篇文章建立的树统计完后,应当初始化这棵树,便于下篇文章建树
        //    System.out.println("单词	数量");
            Map<String,Integer> map=new HashMap<String,Integer>();
            for (int i = 0; i < list.size(); i++) {
                //System.out.println(((Word) list.get(i)).getWord() + "	" + ((Word) list.get(i)).getNum());
                map.put(((Word) list.get(i)).getWord(), ((Word) list.get(i)).getNum());
            }
            return map;
        }

    下面是我写的测试类,我将期待的结果存到Map中,然后将通过传入的参数的结果和Map进行比对。

    @RunWith(Parameterized.class)
    public class CountWordFreqTest {
        private CountWordFreq ctf = new CountWordFreq();
        private String filePath;
        private static Map map = new HashMap<String, Integer>();
    
        public CountWordFreqTest(String filePath, Map map) {
            this.filePath = filePath;
            this.map = map;
        }
    
        @Parameters
        public static Collection data() { // 下面是一些测试样例
            map.put("very", 2);
            map.put("english", 1);
            map.put("exteremely", 1);
            map.put("is", 1);
            map.put("my", 1);
            map.put("poor", 1);
            return Arrays.asList(new Object[][] {  // 这儿的Map是填充数据后的Map
                { "f:/document/cs.txt", map }, 
                { "f:/document/hello.txt", map }, 
                { null, map }, 
            });
        }
    
        @Test
        public void testInputFilePathtest() throws IOException {
            Map rsmap = ctf.sortAndOutput(filePath);
            Assert.assertEquals("map should match rsmap", map, rsmap);
           //Assert.assertNull("空指针异常", rsmap);
        }
    }

    结果的截图如下:

    说明第二个文件的输出与期望值不符,第三个出现空指针异常。

    使用单元测试的感受:测试类中的构造方法的参数数量和类型必须和  public static Collection data()方法中Arrays.asList(new Object[][] { 的元素数量对应。不然会报参数错误异常。

    PSP:

    项目类型:个人项目

    项目完成情况:已完成

    项目改进:已变更

    项目日期:2016.9.24----2.16.9.26

    类别

    内容

    开始时间

    结束时间

    间断时间

    净时间

    学习

    Junit的使用

    8:03

    11:26

    12

    191

    测试编码

    对词频统计中的输入方式测试

    5:00

    8:48

    8

    220

    更正

    单独写一个测试类,对(遍历排序)方法进行测试

     

    9:30

    3:33

     

    12:03

    5:32

     

    43

    229

    总结

    总结Junit中的各种注解的用法。

    5:40

    5:43

    0

    3

    https://git.coding.net/muziliquan/classwork03.git

    ssh:git@git.coding.net:muziliquan/classwork03.git

    git://git.coding.net/muziliquan/classwork03.git

  • 相关阅读:
    CBR(基于案例的推理)的产生和Roger Schank=Schank教授在他的著作中提出了以“记忆组织包"
    php 设计模式
    php 常用资源
    自然语言处理著作或期刊名称2
    北京师范大学语言学及应用语言学研究生培养方案
    !!! Analysis & Design 很好的汇总+zcl的 UML 体会
    睡眠的方法
    !!!【php100教程】
    机器翻译和自然语言信息处理专业硕士研究生培养方案
    愧薪
  • 原文地址:https://www.cnblogs.com/liquan/p/5909862.html
Copyright © 2011-2022 走看看