-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Iván Aguilar - Movie recommendation solution #26
Open
Ivan12273
wants to merge
3
commits into
Nearsoft:main
Choose a base branch
from
Ivan12273:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11"> | ||
<output url="file://$MODULE_DIR$/target/classes" /> | ||
<output-test url="file://$MODULE_DIR$/target/test-classes" /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> | ||
<excludeFolder url="file://$MODULE_DIR$/target" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="library" name="Maven: org.apache.mahout:mahout-core:0.9" level="project" /> | ||
<orderEntry type="library" name="Maven: org.apache.mahout:mahout-math:0.9" level="project" /> | ||
<orderEntry type="library" name="Maven: com.google.guava:guava:16.0" level="project" /> | ||
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-core-asl:1.9.12" level="project" /> | ||
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-mapper-asl:1.9.12" level="project" /> | ||
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.5" level="project" /> | ||
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.1" level="project" /> | ||
<orderEntry type="library" name="Maven: com.thoughtworks.xstream:xstream:1.4.4" level="project" /> | ||
<orderEntry type="library" name="Maven: xmlpull:xmlpull:1.1.3.1" level="project" /> | ||
<orderEntry type="library" name="Maven: xpp3:xpp3_min:1.1.4c" level="project" /> | ||
<orderEntry type="library" name="Maven: org.apache.lucene:lucene-core:4.6.1" level="project" /> | ||
<orderEntry type="library" name="Maven: org.apache.lucene:lucene-analyzers-common:4.6.1" level="project" /> | ||
<orderEntry type="library" name="Maven: org.apache.mahout.commons:commons-cli:2.0-mahout" level="project" /> | ||
<orderEntry type="library" name="Maven: org.apache.commons:commons-math3:3.2" level="project" /> | ||
<orderEntry type="library" name="Maven: org.apache.solr:solr-commons-csv:3.5.0" level="project" /> | ||
<orderEntry type="library" name="Maven: org.apache.hadoop:hadoop-core:1.2.1" level="project" /> | ||
<orderEntry type="library" name="Maven: commons-cli:commons-cli:1.2" level="project" /> | ||
<orderEntry type="library" name="Maven: com.sun.jersey:jersey-core:1.8" level="project" /> | ||
<orderEntry type="library" name="Maven: com.sun.jersey:jersey-json:1.8" level="project" /> | ||
<orderEntry type="library" name="Maven: org.codehaus.jettison:jettison:1.1" level="project" /> | ||
<orderEntry type="library" name="Maven: stax:stax-api:1.0.1" level="project" /> | ||
<orderEntry type="library" name="Maven: com.sun.xml.bind:jaxb-impl:2.2.3-1" level="project" /> | ||
<orderEntry type="library" name="Maven: javax.xml.bind:jaxb-api:2.2.2" level="project" /> | ||
<orderEntry type="library" name="Maven: javax.xml.stream:stax-api:1.0-2" level="project" /> | ||
<orderEntry type="library" name="Maven: javax.activation:activation:1.1" level="project" /> | ||
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-jaxrs:1.7.1" level="project" /> | ||
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-xc:1.7.1" level="project" /> | ||
<orderEntry type="library" name="Maven: com.sun.jersey:jersey-server:1.8" level="project" /> | ||
<orderEntry type="library" name="Maven: asm:asm:3.1" level="project" /> | ||
<orderEntry type="library" name="Maven: commons-io:commons-io:2.1" level="project" /> | ||
<orderEntry type="library" name="Maven: commons-httpclient:commons-httpclient:3.0.1" level="project" /> | ||
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.0.3" level="project" /> | ||
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.4" level="project" /> | ||
<orderEntry type="library" name="Maven: org.apache.commons:commons-math:2.1" level="project" /> | ||
<orderEntry type="library" name="Maven: commons-configuration:commons-configuration:1.6" level="project" /> | ||
<orderEntry type="library" name="Maven: commons-collections:commons-collections:3.2.1" level="project" /> | ||
<orderEntry type="library" name="Maven: commons-lang:commons-lang:2.4" level="project" /> | ||
<orderEntry type="library" name="Maven: commons-digester:commons-digester:1.8" level="project" /> | ||
<orderEntry type="library" name="Maven: commons-beanutils:commons-beanutils:1.7.0" level="project" /> | ||
<orderEntry type="library" name="Maven: commons-beanutils:commons-beanutils-core:1.8.0" level="project" /> | ||
<orderEntry type="library" name="Maven: commons-net:commons-net:1.4.1" level="project" /> | ||
<orderEntry type="library" name="Maven: commons-el:commons-el:1.0" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.7" level="project" /> | ||
<orderEntry type="library" name="Maven: org.slf4j:slf4j-simple:1.7.30" level="project" /> | ||
</component> | ||
</module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/test/java/nearsoft/academy/bigdata/recommendation/MovieRecommender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package nearsoft.academy.bigdata.recommendation; | ||
|
||
import org.apache.commons.collections.BidiMap; | ||
import org.apache.commons.collections.bidimap.TreeBidiMap; | ||
import org.apache.mahout.cf.taste.common.TasteException; | ||
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel; | ||
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood; | ||
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender; | ||
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; | ||
import org.apache.mahout.cf.taste.model.DataModel; | ||
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood; | ||
import org.apache.mahout.cf.taste.recommender.RecommendedItem; | ||
import org.apache.mahout.cf.taste.recommender.UserBasedRecommender; | ||
import org.apache.mahout.cf.taste.similarity.UserSimilarity; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.BufferedWriter; | ||
import java.io.FileReader; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class MovieRecommender { | ||
private String txtPath; | ||
private String csvPath; | ||
private BidiMap userIds; | ||
private BidiMap productsIds; | ||
private int totalReviews; | ||
private int totalProducts; | ||
private int totalUsers; | ||
|
||
public MovieRecommender(String txtPath) throws IOException { | ||
this.txtPath = txtPath; | ||
this.csvPath = "data/movies.csv"; | ||
this.userIds = new TreeBidiMap(); | ||
this.productsIds = new TreeBidiMap(); | ||
this.totalReviews = 0; | ||
this.totalProducts = 0; | ||
this.totalUsers = 0; | ||
this.convertFileToCSV(); | ||
} | ||
|
||
public int getTotalReviews() { | ||
return this.totalReviews; | ||
} | ||
|
||
public int getTotalProducts() { | ||
return this.totalProducts; | ||
} | ||
|
||
public int getTotalUsers() { | ||
return this.totalUsers; | ||
} | ||
|
||
public List<String> getRecommendationsForUser(String user) throws IOException, TasteException { | ||
UserBasedRecommender recommender = createRecommender(this.csvPath); | ||
int userKey = Integer.parseInt(userIds.get(user).toString()); | ||
|
||
List<RecommendedItem> recommendations = recommender.recommend(userKey, 10); | ||
List<String> recommendedMovies = new ArrayList<>(); | ||
String product; | ||
for (RecommendedItem recommendation : recommendations) { | ||
product = this.productsIds.getKey((int) recommendation.getItemID()).toString(); | ||
recommendedMovies.add(product); | ||
} | ||
|
||
return recommendedMovies; | ||
} | ||
|
||
private UserBasedRecommender createRecommender(String path) throws IOException, TasteException { | ||
DataModel model = new FileDataModel(new File(path)); | ||
UserSimilarity similarity = new PearsonCorrelationSimilarity(model); | ||
UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model); | ||
UserBasedRecommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity); | ||
|
||
return recommender; | ||
} | ||
|
||
public void convertFileToCSV() throws IOException { | ||
BufferedReader br = new BufferedReader(new FileReader(this.txtPath)); | ||
BufferedWriter bw = new BufferedWriter(new FileWriter(this.csvPath)); | ||
|
||
String line; | ||
String[] newLine = {"", "", ""}; | ||
while((line = br.readLine()) != null) { | ||
String[] values = line.split(": ", -1); | ||
if(values[0].equals("review/userId")) { | ||
if(this.userIds.get(values[1]) == null) { | ||
this.userIds.put(values[1], this.totalUsers); | ||
newLine[0] = this.userIds.get(values[1]).toString(); | ||
this.totalUsers++; | ||
} else { | ||
newLine[0] = this.userIds.get(values[1]).toString(); | ||
} | ||
} | ||
if(values[0].equals("product/productId")) { | ||
if(this.productsIds.get(values[1]) == null) { | ||
this.productsIds.put(values[1], this.totalProducts); | ||
newLine[1] = this.productsIds.get(values[1]).toString(); | ||
this.totalProducts++; | ||
} else { | ||
newLine[1] = this.productsIds.get(values[1]).toString(); | ||
} | ||
} | ||
if(values[0].equals("review/score")) { | ||
newLine[2] = values[1]; | ||
bw.write(newLine[0] + "," + newLine[1] + "," + newLine[2] + '\n'); | ||
this.totalReviews++; | ||
} | ||
} | ||
br.close(); | ||
bw.close(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+5.6 KB
target/test-classes/nearsoft/academy/bigdata/recommendation/MovieRecommender.class
Binary file not shown.
Binary file added
BIN
+1.49 KB
target/test-classes/nearsoft/academy/bigdata/recommendation/MovieRecommenderTest.class
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are allowed to change how you read the recommender's data, but you are not allowed to add or make any other changes in the test file. Please remove
recommender.convertFileToCSV();
and make sure to make the required changes so that your class still passes all tests.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, I removed it from the test file and modified the MovieRecommender, it still passed all the tests.