|
|
package com.yoho.search.service.recall;
|
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import com.yoho.search.core.personalized.PersonalizedSearch;
|
|
|
|
|
|
@Component
|
|
|
public class ProductFeatureFactorHepler {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ProductFeatureFactorHepler.class);
|
|
|
|
|
|
private static double baseConstant = 1;
|
|
|
private static double factorConstant = 1;
|
|
|
private static double baseConstant = 10;
|
|
|
private static double factorConstant = 10;
|
|
|
|
|
|
public double calProductFeatureFactor(String[] userFeatureFactorArr,String vectorFeatureVersion,String productFeatureFactor){
|
|
|
if(userFeatureFactorArr==null || userFeatureFactorArr.length==0){
|
|
|
return baseConstant;
|
|
|
public double calProductFeatureFactor(UserFeatureFactor userFeatureFactor,String productFeatureFactor){
|
|
|
try {
|
|
|
//用户向量不存在,则直接随机
|
|
|
if(userFeatureFactor==null || StringUtils.isBlank(userFeatureFactor.vectorFeatureVersion)){
|
|
|
return baseConstant * Math.random();//随机数
|
|
|
}
|
|
|
if(StringUtils.isBlank(productFeatureFactor)){
|
|
|
return baseConstant;
|
|
|
}
|
|
|
String versionPrefix = userFeatureFactor.vectorFeatureVersion + "|";
|
|
|
if (!productFeatureFactor.trim().startsWith(versionPrefix)) {
|
|
|
return baseConstant;
|
|
|
}
|
|
|
String[] productFeatureFactorArr = productFeatureFactor.trim().substring(versionPrefix.length()).split(",");
|
|
|
if (productFeatureFactorArr == null || productFeatureFactorArr.length != userFeatureFactor.dimensionOfFactors) {
|
|
|
return baseConstant;//随机数
|
|
|
}
|
|
|
double prodFeatureVectorNorm = 0.0D;
|
|
|
double productiveSum = 0.0D;
|
|
|
double tempProdFactor;
|
|
|
for (int i = 0; i < userFeatureFactor.dimensionOfFactors; i++) {
|
|
|
tempProdFactor = Double.parseDouble(productFeatureFactorArr[i].trim());
|
|
|
productiveSum += tempProdFactor * userFeatureFactor.userFeatureFactors[i];
|
|
|
prodFeatureVectorNorm += tempProdFactor * tempProdFactor;
|
|
|
}
|
|
|
if (prodFeatureVectorNorm == 0) {
|
|
|
return baseConstant;
|
|
|
}
|
|
|
double cosScore = productiveSum / (Math.sqrt(prodFeatureVectorNorm) * Math.sqrt(userFeatureFactor.userFeatureVectorNorm));
|
|
|
double finalScore = baseConstant + factorConstant * cosScore;
|
|
|
return finalScore;
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(),e);
|
|
|
return factorConstant * Math.random();//随机数
|
|
|
}
|
|
|
int dimensionOfFactors = userFeatureFactorArr.length;
|
|
|
double[] userFeatureFactors = new double[dimensionOfFactors];
|
|
|
double temp;
|
|
|
double userFeatureVectorNorm = 0.0D;
|
|
|
for (int index = 0; index < dimensionOfFactors; index++) {
|
|
|
temp = Double.parseDouble(userFeatureFactorArr[index].trim());
|
|
|
userFeatureFactors[index] = temp;
|
|
|
userFeatureVectorNorm += temp * temp;
|
|
|
}
|
|
|
String versionPrefix = vectorFeatureVersion + "|";
|
|
|
if (!productFeatureFactor.trim().startsWith(versionPrefix)) {
|
|
|
return baseConstant;
|
|
|
}
|
|
|
String[] productFeatureFactorArr = productFeatureFactor.trim().substring(versionPrefix.length()).split(",");
|
|
|
if (productFeatureFactorArr == null || productFeatureFactorArr.length != userFeatureFactors.length) {
|
|
|
return baseConstant;
|
|
|
}
|
|
|
double prodFeatureVectorNorm = 0.0D;
|
|
|
double productiveSum = 0.0D;
|
|
|
double tempProdFactor;
|
|
|
for (int i = 0; i < dimensionOfFactors; i++) {
|
|
|
tempProdFactor = Double.parseDouble(productFeatureFactorArr[i].trim());
|
|
|
productiveSum += tempProdFactor * userFeatureFactors[i];
|
|
|
prodFeatureVectorNorm += tempProdFactor * tempProdFactor;
|
|
|
}
|
|
|
if (prodFeatureVectorNorm == 0) {
|
|
|
return baseConstant;
|
|
|
}
|
|
|
double cosScore = productiveSum / (Math.sqrt(prodFeatureVectorNorm) * Math.sqrt(userFeatureVectorNorm));
|
|
|
double finalScore = baseConstant + factorConstant * cosScore;
|
|
|
return finalScore;
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
PersonalizedSearch personalizedSearch = new PersonalizedSearch("0","20171018","-0.43475,0.286214,0.378061,0.836893,0.023244,0.568391,0.557073,-0.693003,0.03146,-0.4763,-0.261971,-0.727929,0.208787,0.637666,0.122361,-0.170466,-0.100151,-0.116521,-0.64368,1.365271,0.033476,-0.214706,0.146333,0.286707,-0.342803,0.347786,-0.36866,0.221598,0.671105,-0.100859,0.089867,-0.380323,-0.071117,-0.766423,-0.239785,0.311385,-0.632959,0.112586,0.535046,0.081164,0.493848,0.165094,0.0257,-0.031391,-0.929653,0.064834,-0.834791,0.046314,-0.478439,0.097075,-0.640102,0.091841,2.75E-4,-0.132861,0.052851,0.021564,0.170549,0.984287,-0.02477,-0.593686,0.180803,-0.122516,0.120295,-0.387301,0.511272,-0.398194,0.734716,-0.193353,-0.252243,-0.867587,-0.505197,0.092014,0.906497,-0.372965,0.039183,0.292307,0.314153,-0.051735,-0.194966,0.667724,-0.176847,0.472125,-0.812535,0.712065,0.229543,-0.35451,0.580328,0.051884,0.841284,0.19402,1.140726,-0.217064,-1.401154,0.240292,0.134513,0.490654,0.423197,-0.536384,-0.335227,0.19993");
|
|
|
UserFeatureFactor userFeatureFactor = new UserFeatureFactor(personalizedSearch);
|
|
|
System.out.println(new ProductFeatureFactorHepler().calProductFeatureFactor(userFeatureFactor, "20171018|-0.43175,0.286214,0.378061,0.836893,0.023244,0.568391,0.557073,-0.693003,0.03146,-0.4763,-0.261971,-0.727929,0.208787,0.637666,0.122361,-0.170466,-0.100151,-0.116521,-0.64368,1.365271,0.033476,-0.214706,0.146333,0.286707,-0.342803,0.347786,-0.36866,0.221598,0.671105,-0.100859,0.089867,-0.380323,-0.071117,-0.766423,-0.239785,0.311385,-0.632959,0.112586,0.535046,0.081164,0.493848,0.165094,0.0257,-0.031391,-0.929653,0.064834,-0.834791,0.046314,-0.478439,0.097075,-0.640102,0.091841,2.75E-4,-0.132861,0.052851,0.021564,0.170549,0.984287,-0.02477,-0.593686,0.180803,-0.122516,0.120295,-0.387301,0.511272,-0.398194,0.734716,-0.193353,-0.252243,-0.867587,-0.505197,0.092014,0.906497,-0.372965,0.039183,0.292307,0.314153,-0.051735,-0.194966,0.667724,-0.176847,0.472125,-0.812535,0.712065,0.229543,-0.35451,0.580328,0.051884,0.841284,0.19402,1.140726,-0.217064,-1.401154,0.240292,0.134513,0.490654,0.423197,-0.536384,-0.335227,0.19993"));
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|