Skip to content

Commit

Permalink
solve anjuke#6
Browse files Browse the repository at this point in the history
  • Loading branch information
kdlan committed Apr 15, 2013
1 parent d4dc2ec commit 8a286cc
Show file tree
Hide file tree
Showing 19 changed files with 472 additions and 44 deletions.
15 changes: 15 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright 2012 Anjuke Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
15 changes: 15 additions & 0 deletions add_copyright.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
license=`cat LICENSE`
license_head=" * Copyright 2012 Anjuke Inc."

for f in `find . -name "*.java" -printf "%u %g %p\n" |awk '{print $3}'`
do
file_head="`head -n 2 $f|tail -1`"
file_head=`echo "$file_head"`
ilicense_head=`echo "$license_head"`
if [ "$file_head" != "$license_head" ];then
echo "add copyright on $f"
file=`cat $f`
echo "$license" > "$f"
echo "$file" >> "$f"
fi
done
24 changes: 24 additions & 0 deletions bin/train.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh

cd $(dirname $0)/
cd ..
ROMAR_HOME=`pwd`



for f in $ROMAR_HOME/romar-core-*.jar; do
CLASSPATH=$CLASSPATH:$f;
done


for f in $ROMAR_HOME/lib/*.jar; do
CLASSPATH=${CLASSPATH}:$f;
done


java -server -Xmn512M -Xmx1G -Xms1G -XX:PermSize=64M -XX:MaxPermSize=64m \
-XX:+UseParallelGC -XX:+UseParallelOldGC \
-Dromar.home=$ROMAR_HOME \
-Dromar.config=$ROMAR_HOME/conf/romar.yaml \
-cp $CLASSPATH \
com.anjuke.romar.train.LocalFileTrainer
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,26 @@
import org.apache.mahout.common.ClassUtils;

import com.anjuke.romar.core.RomarConfig;
import com.anjuke.romar.mahout.GenericReloadDataModel;
import com.anjuke.romar.mahout.MahoutService;
import com.anjuke.romar.mahout.RecommenderWrapper;
import com.anjuke.romar.mahout.similarity.ReadableSimilarity;

public class MahoutServiceCommonRecommendFactory
extends AbstractMahoutServiceFactory implements MahoutServiceFactory {
implements MahoutServiceFactory {
@Override
public MahoutService createService() {
RomarConfig config = RomarConfig.getInstance();
Recommender recommender;
DataModel dataModel = wrapDataModel(new GenericReloadDataModel());
DataModel dataModel = PersistenceDataModelFactory.createDataModel(config);

recommender = ClassUtils.instantiateAs(
config.getCommonRecommenderClass(), Recommender.class,
new Class<?>[] {DataModel.class}, new Object[] {dataModel});
return new RecommenderWrapper(recommender);
}

@Override
public ReadableSimilarity createReadableSimilarity(DataModel dataModel) {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
*/
package com.anjuke.romar.mahout.factory;

import org.apache.mahout.cf.taste.model.DataModel;

import com.anjuke.romar.mahout.MahoutService;
import com.anjuke.romar.mahout.similarity.ReadableSimilarity;

public interface MahoutServiceFactory {
MahoutService createService();
ReadableSimilarity createReadableSimilarity(DataModel dataModel);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,32 @@

import java.io.File;

import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.CachingItemSimilarity;
import org.apache.mahout.cf.taste.impl.similarity.GenericItemSimilarity;
import org.apache.mahout.cf.taste.impl.similarity.GenericItemSimilarity.ItemItemSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.ItemSimilarity;
import org.apache.mahout.common.ClassUtils;

import com.anjuke.romar.core.RomarConfig;
import com.anjuke.romar.mahout.GenericReloadDataModel;
import com.anjuke.romar.mahout.MahoutService;
import com.anjuke.romar.mahout.RecommenderWrapper;
import com.anjuke.romar.mahout.similarity.ReadableGenericSimilarityProxy;
import com.anjuke.romar.mahout.similarity.ReadableSimilarity;
import com.anjuke.romar.mahout.similarity.RomarFileItemSimilarity;
import com.anjuke.romar.mahout.similarity.file.RomarFileSimilarityIterator;
import com.anjuke.romar.mahout.similarity.file.RomarFileSimilarityIterator.IteratorBuiler;

public class MahoutServiceItemRecommendFactory extends AbstractMahoutServiceFactory
implements MahoutServiceFactory {
public class MahoutServiceItemRecommendFactory implements MahoutServiceFactory {

@Override
public MahoutService createService() {
RomarConfig config = RomarConfig.getInstance();
Recommender recommender;
DataModel dataModel = wrapDataModel(new GenericReloadDataModel());
DataModel dataModel = PersistenceDataModelFactory.createDataModel(config);
ItemSimilarity similarity;
if (config.isUseFileSimilarity()) {
File file = new File(config.getSimilarityFile());
Expand All @@ -62,16 +64,34 @@ public MahoutService createService() {
}
similarity = new RomarFileItemSimilarity(file, iteratorBuilder);
} else {
similarity = ClassUtils.instantiateAs(config.getItemSimilarityClass(),
ItemSimilarity.class, new Class<?>[] {DataModel.class},
new Object[] {dataModel});
similarity = createSimilarity(config, dataModel);
if (config.isUseSimilariyCache()) {
similarity = new CachingItemSimilarity(similarity,
config.getSimilarityCacheSize());
}

}
recommender = new GenericItemBasedRecommender(dataModel, similarity);
return new RecommenderWrapper(recommender);
}

private ItemSimilarity createSimilarity(RomarConfig config, DataModel dataModel) {
ItemSimilarity similarity = ClassUtils.instantiateAs(
config.getItemSimilarityClass(), ItemSimilarity.class,
new Class<?>[] {DataModel.class}, new Object[] {dataModel});
return similarity;
}

@Override
public ReadableSimilarity createReadableSimilarity(DataModel dataModel) {
RomarConfig config = RomarConfig.getInstance();
ItemSimilarity similarity = createSimilarity(config, dataModel);
try {
return ReadableGenericSimilarityProxy
.proxySimilarity(new GenericItemSimilarity(similarity, dataModel));
} catch (TasteException e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

import java.io.File;

import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.CachingUserSimilarity;
import org.apache.mahout.cf.taste.impl.similarity.GenericUserSimilarity;
import org.apache.mahout.cf.taste.impl.similarity.GenericUserSimilarity.UserUserSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
Expand All @@ -27,20 +29,20 @@
import org.apache.mahout.common.ClassUtils;

import com.anjuke.romar.core.RomarConfig;
import com.anjuke.romar.mahout.GenericReloadDataModel;
import com.anjuke.romar.mahout.MahoutService;
import com.anjuke.romar.mahout.RecommenderWrapper;
import com.anjuke.romar.mahout.similarity.ReadableGenericSimilarityProxy;
import com.anjuke.romar.mahout.similarity.ReadableSimilarity;
import com.anjuke.romar.mahout.similarity.RomarFileUserSimilarity;
import com.anjuke.romar.mahout.similarity.file.RomarFileSimilarityIterator;
import com.anjuke.romar.mahout.similarity.file.RomarFileSimilarityIterator.IteratorBuiler;

public class MahoutServiceUserRecommendFactory extends AbstractMahoutServiceFactory
implements MahoutServiceFactory {
public class MahoutServiceUserRecommendFactory implements MahoutServiceFactory {
@Override
public MahoutService createService() {
RomarConfig config = RomarConfig.getInstance();
Recommender recommender;
DataModel dataModel = wrapDataModel(new GenericReloadDataModel());
DataModel dataModel = PersistenceDataModelFactory.createDataModel(config);

UserSimilarity similarity;
if (config.isUseFileSimilarity()) {
Expand All @@ -63,9 +65,7 @@ public MahoutService createService() {
}
similarity = new RomarFileUserSimilarity(file, iteratorBuilder);
} else {
similarity = ClassUtils.instantiateAs(config.getUserSimilarityClass(),
UserSimilarity.class, new Class<?>[] {DataModel.class},
new Object[] {dataModel});
similarity = createSimilarity(config, dataModel);
if (config.isUseSimilariyCache()) {
similarity = new CachingUserSimilarity(similarity,
config.getSimilarityCacheSize());
Expand All @@ -83,4 +83,24 @@ public MahoutService createService() {

return new RecommenderWrapper(recommender);
}

@Override
public ReadableSimilarity createReadableSimilarity(DataModel dataModel) {
RomarConfig config = RomarConfig.getInstance();
UserSimilarity similarity = createSimilarity(config,
dataModel);
try {
return ReadableGenericSimilarityProxy
.proxySimilarity(new GenericUserSimilarity(similarity, dataModel));
} catch (TasteException e) {
throw new RuntimeException(e);
}
}

private UserSimilarity createSimilarity(RomarConfig config, DataModel dataModel) {
UserSimilarity similarity = ClassUtils.instantiateAs(
config.getUserSimilarityClass(), UserSimilarity.class,
new Class<?>[] {DataModel.class}, new Object[] {dataModel});
return similarity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,28 @@

import java.io.File;

import org.apache.mahout.cf.taste.model.DataModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.anjuke.romar.core.RomarConfig;
import com.anjuke.romar.core.RomarCore;
import com.anjuke.romar.mahout.GenericReloadDataModel;
import com.anjuke.romar.mahout.PreferenceDataModel;
import com.anjuke.romar.mahout.persistence.FilePreferenceSource;
import com.anjuke.romar.mahout.persistence.PersistenceDataModelProxy;
import com.anjuke.romar.mahout.persistence.PreferenceSource;

public abstract class AbstractMahoutServiceFactory implements MahoutServiceFactory {
private static final Logger log = LoggerFactory.getLogger(RomarCore.class);
public final class PersistenceDataModelFactory {
private static final Logger log = LoggerFactory
.getLogger(PersistenceDataModelFactory.class);

protected PreferenceDataModel wrapDataModel(PreferenceDataModel dataModel) {
final String persistencePath = RomarConfig.getInstance().getPersistencePath();
private PersistenceDataModelFactory(){

}

private static PreferenceDataModel wrapDataModel(RomarConfig config,
PreferenceDataModel dataModel) {
final String persistencePath = config.getPersistencePath();
if (persistencePath == null || persistencePath.isEmpty()) {
return dataModel;
}
Expand All @@ -43,10 +50,15 @@ protected PreferenceDataModel wrapDataModel(PreferenceDataModel dataModel) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
log.info("FilePreferenceSource on file "+persistencePath +" close");
log.info("FilePreferenceSource on file " + persistencePath + " close");
source.close();
}
});
return model;
}

public static DataModel createDataModel(RomarConfig config) {
DataModel dataModel = wrapDataModel(config, new GenericReloadDataModel());
return dataModel;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
/**
* Copyright 2012 Anjuke Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.anjuke.romar.mahout.similarity;

import org.apache.mahout.cf.taste.similarity.ItemSimilarity;

public interface ReadableGenericItemSimilarity extends ItemSimilarity,ReadableSimilarity{
public interface ReadableGenericItemSimilarity extends ItemSimilarity, ReadableSimilarity {
}
Loading

0 comments on commit 8a286cc

Please sign in to comment.