之前的jar包有问题,现已修改.

需要的jar包,已修改

自己去Maven中央仓库下载jar包.

excel数据:

直接上代码.

程序再度优化了一遍.之后如果想再度精准,可能需要建模,最近没空继续做了.

实体类:

package org.analysisitem20181016.pojo;

public class Item {

    private int index;
private int match_text_length;
private String item_name;
private String activity_id;
private String type;
private String user_id;
private String selled_count;
private int similarity;
private String matchText; public String getItem_name() {
return item_name;
}
public void setItem_name(String item_name) {
this.item_name = item_name;
}
public String getActivity_id() {
return activity_id;
}
public void setActivity_id(String activity_id) {
this.activity_id = activity_id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getSelled_count() {
return selled_count;
}
public void setSelled_count(String selled_count) {
this.selled_count = selled_count;
}
public int getSimilarity() {
return similarity;
}
public void setSimilarity(int similarity) {
this.similarity = similarity;
}
public String getMatchText() {
return matchText;
}
public void setMatchText(String matchText) {
this.matchText = matchText;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public int getMatch_text_length() {
return match_text_length;
}
public void setMatch_text_length(int match_text_length) {
this.match_text_length = match_text_length;
} }

线程处理类(改良后使用了calculate2方法来匹配):

package org.analysisitem20181016.main;

import org.analysisitem20181016.pojo.Item;

public class ThreadMain implements Runnable{

    private int index;
private Item item; public ThreadMain(int index, Item item){
this.index = index;
this.item = item;
} @Override
public void run() {
System.out.println("任务" + index + "开始执行!");
for(int i = 0; i < CompareMain.itemList.size(); i++){
if(i == index){
continue;
}
String text = item.getItem_name();
String text2 = CompareMain.itemList.get(i).getItem_name();
String initText = null;
String initText2 = null;
if(text.length() <= text2.length()){
initText = text;
initText2 = text2;
}else{
initText = text2;
initText2 = text;
}
// String calculatedText = calculate(initText, initText, initText2, 0, 2);
String calculatedText = calculate2(initText, initText, initText2, 0, 2);
/*if(initText.equals("蒜瓣肉")){
System.out.println(item.getSimilarity());
if(item.getSimilarity() > 9){
System.out.println("initText:" + initText);
System.out.println("text:" + text);
System.out.println("text2:" + text2);
}
}*/
if(calculatedText != null && calculatedText.equals("")){
calculatedText = "无匹配数据";
}
if(calculatedText != null && !calculatedText.equals("无匹配数据")){
// System.out.println("匹配字符串:" + calculatedText);
item.setMatchText(calculatedText);
item.setSimilarity(item.getSimilarity() + 1);
}
}
/*if(item.getItem_name().equals("蒜瓣肉") && item.getSimilarity() > 9){
System.out.println("相似数量:" + item.getSimilarity());
}*/
CompareMain.calculatedItemList.add(item);
} public static String calculate2(String initText, String text, String initText2, int beginIndex, int len){
String subText = null;
if(initText2.contains(text)){
if(initText.equals("芹菜文") && initText2.equals("芹菜文")){
System.out.println(4);
System.out.println("4最后结果:" + text);
System.out.println("4结束!");
}
return text;
}else{
while(initText.length() < len){
len--;
}
if(len >= CompareMain.minTextLen){
if(initText.equals("芹菜文")){
System.out.println(1);
}
if(beginIndex + len < initText.length()){
subText = initText.substring(beginIndex, beginIndex + len);
beginIndex++;
return calculate2(initText, subText, initText2, beginIndex, len);
}else if(beginIndex + len >= initText.length()){
subText = initText.substring(beginIndex);
beginIndex = 0;
len--;
return calculate2(initText, subText, initText2, beginIndex, len);
}
}
}
return null;
} public static String calculate(String initText, String text, String text2, int beginIndex, int len){
if(text2.contains(text)){
return text;
}else{
String subText = null;
if(len < initText.length()){
if(beginIndex + len < initText.length()){
subText = initText.substring(beginIndex, beginIndex + len);
}else{
subText = initText.substring(beginIndex);
}
// System.out.println("subText:" + subText);
if(subText.length() == len){
// System.out.println("subText.length():" + subText.length());
beginIndex++;
return calculate(initText, subText, text2, beginIndex, len);
}
}
}
return null;
}
}

修复了一个bug.

分析主类(改变了一点代码,逻辑没变):

package org.analysisitem20181016.main;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import org.analysisitem20181016.pojo.Item;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook; public class CompareMain{ public static ArrayList<Item> itemList = new ArrayList<Item>();
private static String replaceReg = "[^\u4e00-\u9fa5]+";
public static int maxTextLen = 4;
public static int minTextLen = 2;
public static ArrayList<Item> calculatedItemList = new ArrayList<Item>(); public static void main(String[] args){
try{
CompareMain compareMain = new CompareMain();
compareMain.readExcel();
// compareMain.compare();
compareMain.subsectionCalculate();
compareMain.show();
compareMain.writeExcel();
}catch(Exception e) {
e.printStackTrace();
}
} public void writeExcel() throws Exception{
File file = new File("G:/Database/Item20181016_YangBing/notitle2.xls");
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("item_name");
cell = row.createCell(1);
cell.setCellValue("activity_id");
cell = row.createCell(2);
cell.setCellValue("type");
cell = row.createCell(3);
cell.setCellValue("user_id");
cell = row.createCell(4);
cell.setCellValue("selled_count");
cell = row.createCell(5);
cell.setCellValue("相似数量");
cell = row.createCell(6);
cell.setCellValue("匹配字符串");
for (int i = 0; i < calculatedItemList.size(); i++) {
Item item = calculatedItemList.get(i);
if(item != null){
row = sheet.createRow(i + 1);
cell = row.createCell(0);
cell.setCellValue(item.getItem_name());
cell = row.createCell(1);
cell.setCellValue(item.getActivity_id());
cell = row.createCell(2);
cell.setCellValue(item.getType());
cell = row.createCell(3);
cell.setCellValue(item.getUser_id());
cell = row.createCell(4);
cell.setCellValue(item.getSelled_count());
cell = row.createCell(5);
/*if(item.getItem_name().equals("蒜瓣肉")){
System.out.println("相似数量:" + item.getSimilarity());
}*/
cell.setCellValue(item.getSimilarity());
cell = row.createCell(6);
cell.setCellValue(item.getMatchText());
}
}
FileOutputStream fos = new FileOutputStream(file);
wb.write(fos);
fos.flush();
fos.close();
wb.close();
System.out.println("写入Excel文件完成!");
} public void show(){
// System.out.println(calculatedItemList.size());
for(Item item : calculatedItemList){
if(item != null){
// System.out.println("item_name:" + item.getItem_name() + ",匹配字符串:" + item.getMatchText() + ",count:" + item.getSimilarity());
}
}
} public void subsectionCalculate() throws Exception{
LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>();
int size = itemList.size();
ThreadPoolExecutor executor = new ThreadPoolExecutor(size, size, 7200, TimeUnit.SECONDS, workQueue);
for(int i = 0; i < itemList.size(); i++){
Item outerItem = itemList.get(i);
ThreadMain threadMain = new ThreadMain(i, outerItem);
executor.execute(threadMain);
}
while(true){
if(executor.getCompletedTaskCount() >= size){
executor.shutdown();
executor.shutdownNow();
break;
}
Thread.sleep(1000);
}
} /*public void compare(){
System.out.println("正在比较中...");
for(int i = 0; i < itemList.size(); i++){
Item outerItem = itemList.get(i);
for(int j = i + 1; j < itemList.size(); j++){
Item innerItem = itemList.get(j);
String outerItemName = outerItem.getItem_name();
String innerItemName = innerItem.getItem_name();
if(!filtered){
outerItemName = outerItemName.replaceAll(replaceReg, "");
innerItemName = innerItemName.replaceAll(replaceReg, "");
}
// int count = calculate(outerItemName, innerItemName, initialLen);
outerItem.setSimilarity(outerItem.getSimilarity() + count);
}
// calculatedItemList.add(outerItem);
}
System.out.println("计算完毕!");
}*/ public void readExcel() throws Exception{
File file = new File("G:/Database/Item20181016_YangBing/notitle.xls");
POIFSFileSystem fs = new POIFSFileSystem(file);
Workbook wb = new HSSFWorkbook(fs);
// int sheet_size = wb.getNumberOfSheets();
Sheet sheet = wb.getSheetAt(0);
for(int i = 1; i < sheet.getPhysicalNumberOfRows(); i++){
Row row = sheet.getRow(i);
Item item = new Item();
for(int j = 0; j < row.getLastCellNum(); j++){
Cell cell = row.getCell(j);
if(j == 0){
String item_name = cell.getStringCellValue();
item_name = item_name.replaceAll(replaceReg, "");
item.setItem_name(item_name);
}else if(j == 1){
double activity_id = cell.getNumericCellValue();
item.setActivity_id((long)activity_id + "");
}else if(j == 2){
String type = cell.getStringCellValue();
item.setType(type);
}else if(j == 3){
double user_id = cell.getNumericCellValue();
item.setUser_id((long)user_id + "");
}else if(j == 4){
double selled_count = cell.getNumericCellValue();
item.setSelled_count((long)selled_count + "");
}
}
itemList.add(item);
}
wb.close();
fs.close();
} }

现在可以匹配多个字符了,会有一点bug,暂时没空解决.

好了,有兴趣的自己看代码吧!

解析结果:

非常有问题,但是暂时没空也没心思解决.

从Excel读取数据,然后分析相似的数据,多线程处理(多线程比较相似的字符串,统计出相似的数量及字符串)的更多相关文章

  1. 大数据离线分析平台 JavaSDK数据收集引擎编写

    JavaSDK设计规则 JavaSDK提供两个事件触发方法,分别为onChargeSuccess和onChargeRefund.我们在java sdk中通过一个单独的线程来发送线程数据,这样可以减少对 ...

  2. 大数据离线分析平台 用户数据Etl

    Etl目标  解析我们收集的日志数据,将解析后的数据保存到hbase中.这里选择hbase来存储数据的主要原因就是: hbase的宽表结构设计适合我们的这样多种数据格式的数据存储(不同event有不同 ...

  3. 大数据离线分析平台 JSSDK数据收集引擎编写

    JsSDK设计规则在js sdk中我们需要收集launch.pageview.chargeRequest和eventDuration四种数据,所以我们需要在js中写入四个方法来分别收集这些数据,另外我 ...

  4. snmp数据包分析

    今天看了一下snmp数据包的报文格式,用wireshark抓了两个数据包来分析. 先说说snmp get-request的书报包格式吧,get-next-request,get-response,se ...

  5. 第三课 创建函数 - 从EXCEL读取 - 导出到EXCEL - 异常值 - Lambda函数 - 切片和骰子数据

    第 3 课   获取数据 - 我们的数据集将包含一个Excel文件,其中包含每天的客户数量.我们将学习如何对 excel 文件进​​行处理.准备数据 - 数据是有重复日期的不规则时间序列.我们将挑战数 ...

  6. 信用卡欺诈数据的分析-excel篇

    本篇文章为大家提供了数据集分析的思路和步骤,同时也分享了自己的经验. 一.背景 反欺诈是一项识别服务,是对交易诈骗.网络诈骗.电话诈骗.盗卡盗号等行为的一项风险识别.其核心是通过大数据的收集.分析和处 ...

  7. Java读取Excel并与SqlServer库中的数据比较

    先说说需求.在SQL server数据库中的表里存在一些数据,现在整理的Excel文档中也存在一些数据,现在需要通过根据比较某个字段值(唯一)来判断出,在库中有但excel中没有的数据. 大概的思路就 ...

  8. 无法读取Excel中的数据单元格。有数据,但是读出来全是空值

    C#读取Excel,取值为空的解决办法! C#读取Excel遇到无法读取的解决方法是什么呢?这样在C#读取Excel的过程中有很多问题,那么本文就向你介绍如何解决C#读取Excel遇到无法读取的解决方 ...

  9. 你别告诉我你还在用Excel做数据透视分析吧,太low了!

    来到大数据分析的时代,大量的大数据分析软件涌现,尽管如此,如果今天有人问起最常用的数据透视分析工具是什么的时候,我猜想Excel应该是大家的不二之选. 但是其实我想说,用现在的手机来打比方,Excel ...

随机推荐

  1. http_load常见问题及解决方案

    1.错误信息:byte count wrong http_load在处理时会去关注每次访问同一个URL返回结果(即字节数)是否一致,若不一致就会抛出byte count wrong 所以动态页面可以忽 ...

  2. bzoj 1127 KUP —— 最大子矩形+答案构造

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1127 首先,把权值 > 2*k 的点作为“坏点”,然后在图中用悬线法找权值最大的子矩形 ...

  3. robotframework 随机选中下拉框中的值

    示例脚本: click element id=provinceName #点击地区 省 wait until element is enabled xpath=.//*[@id='provinceNa ...

  4. org.springframework.web.struts.ContextLoaderPlugIn 和 org.springframework.web.context.ContextLoaderListener

    org.springframework.web.struts.ContextLoaderPlugIn 和 org.springframework.web.context.ContextLoaderLi ...

  5. Linux系统挂载NTFS文件系统(转载)

    转自:http://hermesbox.blogbus.com/logs/47386987.html 今天尝试并成功的将一块500G的移动硬盘挂载到了RHEL5的系统上,甚感欣慰.想到也许以后自己或其 ...

  6. bzoj 3675: [Apio2014]序列分割【斜率优化dp】

    首先看这个得分方式,容易发现就相当于分k段,每段的值和两两乘起来. 这样就很容易列出dp方程:设f[i][j]为到j分成分成i段,转移是 \[ f[i][j]=max { f[k][j]+s[k]*( ...

  7. bzoj 1007: [HNOI2008]水平可见直线【半平面交】

    其实并不算标准半平面交?但是思路差不多 先按照斜率排序,然后用栈维护凸壳,每遇到重斜率或a[i],s[top-1]交点的x轴在s[top],s[top-1]交点左侧,则说明s[top]被a[i],s[ ...

  8. HTML中a标签自动识别电话、邮箱

    HTML中a标签自动识别电话.邮箱 联系电话:<a href="tel:010-88888888">010-88888888</a><br> 联 ...

  9. [POI2008]POD Subdivision of Kingdom

    Description 给出一个具有N个结点的无向图,将其分成两个集合S1,S2. 这两个集合的点的个数一样多,但连接它们的边最少. Input 第一行给出数字N,M,代表有N个点,M条边. 下面M行 ...

  10. android:fillViewport="true"让ScrollView内的view强行match_parent

    当你想让一个高度值不足scrollview的子控件fillparent的时候,单独的定义android:layout_height="fill_parent"是不起作用的,必须加上 ...