提交 a964be16 authored 作者: 梁业锦's avatar 梁业锦 💬

Merge branch 'master' of D:\diaoyun\zion with conflicts.

上级 704fa351
差异被折叠。
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -19,6 +19,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<skipTests>true</skipTests>
</properties>
......@@ -151,6 +152,7 @@
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<!-- <version>2.10.3</version>-->
<!-- <version>2.10.3</version>-->
</dependency>
<!--swagger2-->
<dependency>
......@@ -342,6 +344,7 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.egzosn</groupId>
<artifactId>pay-java-paypal</artifactId>
......@@ -411,3 +414,4 @@
</project>
......@@ -17,6 +17,4 @@ public class ZionApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(ZionApplication.class, args);
}
}
......@@ -14,6 +14,10 @@ public class ItemInfo {
* 商品标题
*/
private String title;
/**
* 商品的品名
*/
private String variety;
/**
* 商品主图
*/
......@@ -27,6 +31,14 @@ public class ItemInfo {
*/
private String shopUrl;
public String getVariety() {
return variety;
}
public void setVariety(String variety) {
this.variety = variety;
}
public String getItemId() {
return itemId;
}
......
......@@ -8,53 +8,51 @@ import java.util.concurrent.*;
public class TaskLimitSemaphore {
//the number of threads to keep in the pool, even
// if they are idle, unless {@code allowCoreThreadTimeOut} is set
private int corePoolSize=5;
private int corePoolSize = 5;
//the maximum number of threads to allow in the pool
private int maximumPoolSize=20;
private int maximumPoolSize = 20;
//when the number of threads is greater than
//the core, this is the maximum time that excess idle threads
//will wait for new tasks before terminating.
private long keepAliveTime=60L;
private long keepAliveTime = 60L;
//线程池
private ExecutorService executor;
//控制线程数
private Semaphore semaphore;
public TaskLimitSemaphore(int limit) {
corePoolSize=limit;
this.maximumPoolSize = limit+20;
corePoolSize = limit;
this.maximumPoolSize = limit + 20;
this.semaphore = new Semaphore(limit);
this.executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
keepAliveTime, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
}
/**
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param limit 最大线程数 数值小于等于 maximumPoolSize
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param limit 最大线程数 数值小于等于 maximumPoolSize
*/
public TaskLimitSemaphore(int corePoolSize, int limit) {
corePoolSize=limit;
this.maximumPoolSize = limit+20;
corePoolSize = limit;
this.maximumPoolSize = limit + 20;
this.semaphore = new Semaphore(limit);
this.executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
keepAliveTime, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
}
/**
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the pool
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
*will wait for new tasks before terminating.
* @param limit 最大线程数 数值小于等于 maximumPoolSize
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param limit 最大线程数 数值小于等于 maximumPoolSize
*/
public TaskLimitSemaphore(int corePoolSize, int maximumPoolSize, long keepAliveTime, int limit) {
corePoolSize=limit;
this.maximumPoolSize = limit+20;
corePoolSize = limit;
this.maximumPoolSize = limit + 20;
this.keepAliveTime = keepAliveTime;
this.semaphore = new Semaphore(limit);
this.executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
......@@ -63,7 +61,7 @@ public class TaskLimitSemaphore {
public <T> Future<T> submit(final Callable<T> task) throws InterruptedException {
semaphore.acquire();
return executor.submit(()-> {
return executor.submit(() -> {
try {
return task.call();
} finally {
......@@ -72,6 +70,9 @@ public class TaskLimitSemaphore {
});
}
/**
* 关闭线程池
*/
public void shutdown() {
executor.shutdown();
}
......
......@@ -9,20 +9,22 @@ import org.apache.http.Consts;
import java.util.Map;
/** 腾讯翻译类
/**
* 腾讯翻译类
* https://api.ai.qq.com/fcgi-bin/nlp/nlp_texttranslate
* */
*/
public class TranslateCallable extends AbstractTencentCallable<TencentTranslateParam> {
//private static Logger logger = LoggerFactory.getLogger(TranslateCallable.class);
private static DomainProperties domainProperties=(DomainProperties) SpringContextUtil.getBean("domainProperties");
private static DomainProperties domainProperties = (DomainProperties) SpringContextUtil.getBean("domainProperties");
public TranslateCallable(TencentTranslateParam param) {
this.app_id = Integer.valueOf(domainProperties.getProperty("tencent.translate.app_id"));
this.app_url = domainProperties.getProperty("tencent.translate.app_url");
this.param = param;
this.charset=Consts.UTF_8.name();
this.charset = Consts.UTF_8.name();
}
// TODO 泛型
@Override
public Map<String, Object> call() throws Exception {
......@@ -34,6 +36,7 @@ public class TranslateCallable extends AbstractTencentCallable<TencentTranslateP
/**
* 填充翻译时参数
*
* @param param
*/
@Override
......@@ -58,5 +61,4 @@ public class TranslateCallable extends AbstractTencentCallable<TencentTranslateP
}
}
......@@ -24,68 +24,72 @@ public class TranslateHelper {
private static Logger logger = LoggerFactory.getLogger(TranslateHelper.class);
//默认20条线程跑翻译
private static final TaskLimitSemaphore taskLimitSemaphore=new TaskLimitSemaphore(20);
private static final TaskLimitSemaphore taskLimitSemaphore = new TaskLimitSemaphore(20);
/**
* 翻译文本
*
* @param futureList
* @param valeMap
* @param text
*/
public static void translateText(List<Map<String, Object>> futureList, Map<String,Object> valeMap, String text){
TencentTranslateParam tencentTranslateParam =new TencentTranslateParam(text);
Future<Map<String,Object>> future = null;
public static void translateText(List<Map<String, Object>> futureList, Map<String, Object> valeMap, String text) {
TencentTranslateParam tencentTranslateParam = new TencentTranslateParam(text);
Future<Map<String, Object>> future = null;
try {
future = taskLimitSemaphore.submit(new TranslateCallable(tencentTranslateParam));
Map<String,Object> map=new HashMap<>();
map.put("future",future);
map.put("value",valeMap);
Map<String, Object> map = new HashMap<>();
map.put("future", future);
map.put("value", valeMap);
futureList.add(map);
} catch (Exception e) {
logger.error(e.getMessage(),e);
logger.error(e.getMessage(), e);
}
}
/**
* 等待翻译结果
*
* @param futureList
*/
public static void waitForResult(List<Map<String, Object>> futureList) throws ExecutionException, InterruptedException, TimeoutException {
for(Map<String,Object> futureMap:futureList) {
Future<Map<String,Object>> future= (Future<Map<String, Object>>) futureMap.get("future");
Map<String,Object> valeMap = (Map<String,Object> ) futureMap.get("value");
while(!future.isDone());//Future返回如果没有完成,则一直循环等待,直到Future返回完成
Map<String,Object> resultMap=future.get(10000, TimeUnit.MILLISECONDS);
String targetText="unknow";
if(resultMap!=null&&(int)resultMap.get("ret")==0) {
Map<String,Object> dataMap=(Map<String,Object>)resultMap.get("data");
targetText= (String) dataMap.get("target_text");
for (Map<String, Object> futureMap : futureList) {
Future<Map<String, Object>> future = (Future<Map<String, Object>>) futureMap.get("future");
Map<String, Object> valeMap = (Map<String, Object>) futureMap.get("value");
while (!future.isDone()) ;//Future返回如果没有完成,则一直循环等待,直到Future返回完成
Map<String, Object> resultMap = future.get(10000, TimeUnit.MILLISECONDS);
String targetText = "unknow";
if (resultMap != null && (int) resultMap.get("ret") == 0) {
Map<String, Object> dataMap = (Map<String, Object>) resultMap.get("data");
targetText = (String) dataMap.get("target_text");
} else {
logger.error("翻译出错");
}
valeMap.put("translate",targetText);
valeMap.put("translate", targetText);
}
}
/**
* 翻译规格属性 productPropSetMap 为 ProductResponse 的productPropSetMap 属性
* @param futureList 线程回调
* 翻译规格属性 productPropSetMap 为 ProductResponse 的productPropSetMap 属性
*
* @param futureList 线程回调
* @param productPropSetMap 属性集合Map 为 ProductResponse 的productPropSetMap 属性
*/
public static void translateProp(List<Map<String, Object>> futureList, Map<String,JSONArray> productPropSetMap) {
JSONArray translateArray=new JSONArray();
public static void translateProp(List<Map<String, Object>> futureList, Map<String, JSONArray> productPropSetMap) {
JSONArray translateArray = new JSONArray();
/*腾讯翻译*/
for(Map.Entry<String, JSONArray>entry : productPropSetMap.entrySet()) {
String key=entry.getKey();
Map <String,Object> keyTranslateMap=new HashMap<>();
keyTranslateMap.put(key,key);
for (Map.Entry<String, JSONArray> entry : productPropSetMap.entrySet()) {
String key = entry.getKey();
Map<String, Object> keyTranslateMap = new HashMap<>();
keyTranslateMap.put(key, key);
JSONArray productPropSet = entry.getValue();
for(int i=0;i<productPropSet.size();i++) {
JSONObject productPropMap=productPropSet.getJSONObject(i);
for (int i = 0; i < productPropSet.size(); i++) {
JSONObject productPropMap = productPropSet.getJSONObject(i);
//翻译属性值
if(ValidateUtils.isContainChinese((String) productPropMap.get("propName"))) {
TranslateHelper.translateText(futureList,productPropMap, (String) productPropMap.get("propName"));
if (ValidateUtils.isContainChinese((String) productPropMap.get("propName"))) {
TranslateHelper.translateText(futureList, productPropMap, (String) productPropMap.get("propName"));
} else {
productPropMap.put("translate",(String) productPropMap.get("propName"));
productPropMap.put("translate", (String) productPropMap.get("propName"));
}
}
translateArray.add(keyTranslateMap);
......@@ -95,6 +99,7 @@ public class TranslateHelper {
/**
* 翻译爬取回来的数据
*
* @param resultObj ProductResponse格式的JSONObject
* @throws InterruptedException
* @throws ExecutionException
......@@ -102,11 +107,11 @@ public class TranslateHelper {
*/
public static void translateProductResponse(JSONObject resultObj) throws InterruptedException, ExecutionException, TimeoutException {
//////////////////////翻译//////////////////////
List<Map<String, Object>> futureList= new ArrayList<>();
List<Map<String, Object>> futureList = new ArrayList<>();
Map<String, JSONArray> productPropSet = resultObj.getJSONObject("productPropSet");
if(!productPropSet.isEmpty()) {
if (!productPropSet.isEmpty()) {
//调用腾讯ai,翻译属性值
TranslateHelper.translateProp(futureList,productPropSet);
TranslateHelper.translateProp(futureList, productPropSet);
//翻译属性名
//翻译属性名 // TODO 返回格式有待和前端确认
/*JSONArray translateArray =productPropSet.get("translateArray");
......@@ -129,7 +134,7 @@ public class TranslateHelper {
}
//等待翻译结果
TranslateHelper.waitForResult(futureList);
resultObj.put("productPropSet",productPropSet);
resultObj.put("productPropSet", productPropSet);
//////////////////////翻译 END//////////////////////
}
}
package com.diaoyun.zion;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 正则表达式的匹配 Test
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class RegularTest {
@Test
public void test() {
String pattern = "https://h.uniqlo.cn/#/product?pid=u0000000012717";
boolean b = Pattern.matches("^.*uniqlo.*\\/\\#\\/product\\?pid=u\\d+", pattern);
System.err.println(b);
}
@Test
public void test01() {
String s = "https://www.adidas.com.cn/item/EH2458/D56452";
Pattern pattern = Pattern.compile("\\w+\\d+");
Matcher matcher = pattern.matcher(s);
matcher.find();
System.err.println(matcher.group());
}
}
......@@ -493,4 +493,6 @@ public class ZionApplicationTests {
public void testPhone() throws IOException {
//SMSUtil.yzCode("13751400455");
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论