Java根据词频生成批量数据

来源:互联网 发布:日系高端护肤品知乎 编辑:程序博客网 时间:2024/05/18 10:15

1. 背景:首先条件是1200列的4W行数据,先进行各个列字段的词频统计,然后根据这些列的词频给出任意GB的生成数据。


 2. 分析:将各个列的词频信息放在Column.txt中,(Reporting_IsEditorial.txt),这个地方可以有两种方案,(a)根据区间或者上界查找(二分查找);(b)根据初始化的String[]数组。

Reporting_IsEditorialTrue6177Reporting_IsEditorialFalse33823

其中,在方案a中,定义类对象,存着Value和上界upperBound;方案b中,只需要存着array[]。

(a)List<List<Column>> columnList = new ArrayList();

二维数组,第一维是Column,第二维是各个Column的所有信息(当然在最开始的时候需要读取配置信息)

只要查找上界的方案是二分查找法。

(b)List<ColumnArray> columnList = new ArrayList();

直接存取array数组(在对象ColumnArray),这里主要是初始化消耗时间。

3. 注意:

(1)在写文件的时候,定义StringBuilder来存取一行的数据,节省时间和内存。

(2)在这类中不适合用Map,会消耗时间。

(3)可以采取多线程的方式。

(4)在b方案中,16s生成200M数据,配置信息加载是5s,可以采取。

4. 解决办法:

(a)二分查找

 protected String getValueByBinarySearch(int randNum, List<Column> columnRate) {        String columnValue = null;        int lo = 0;        int hi = columnRate.size() - 1;        int mid;        int top;        Column col, col1;        while (lo <= hi) {            mid = (lo + hi) / 2;            col = columnRate.get(mid);            top = col.getUpperBound();            if (mid > 0) {                col1 = columnRate.get(mid - 1);                if (top >= randNum && randNum > col1.getUpperBound()) {                    columnValue = columnRate.get(mid).getValue();                    return columnValue;                } else if (top < randNum) {                    lo = mid + 1;                } else {                    hi = mid - 1;                }            } else {                if (top >= randNum && randNum > 0) {                    columnValue = columnRate.get(mid).getValue();                    return columnValue;                } else if (top < randNum) {                    lo = mid + 1;                } else {                    hi = mid - 1;                }            }        }        columnValue = "search_error";        System.out.println("Problem is : " + columnValue);        return columnValue;    }

(b)设置初始化数组

private void initColumnRate() {        for (String cName : columnName) {            initColumnList(cName);        }    }    private void initColumnList(String cName) {        String columnPath = filePath + "data_dict/" + cName + ".txt";        String curLine = null;        BufferedReader br = null;        String columnsLine[] = null;        try {            br = new BufferedReader(new FileReader(columnPath));            int top = 0, bottom = 0;            ColumnArray c = new ColumnArray();            while ((curLine = br.readLine()) != null) {                // 0: columnName, 1: content, 2: rate(interval)                columnsLine = curLine.split("\t");                // 0: columnName, 1: content, 2: rate(interval)                top += Integer.valueOf(columnsLine[2]);                for (int i = bottom; i < top; i++)                    c.getArray()[i] = columnsLine[1];                bottom = top;            }            columnList.add(c);        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            if (br != null)                try {                    br.close();                } catch (IOException e) {                    e.printStackTrace();                }        }    }