Skip to content

Commit

Permalink
add offline learning
Browse files Browse the repository at this point in the history
  • Loading branch information
chenww05 committed Sep 2, 2015
1 parent 657a54d commit 3cc2a16
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 44 deletions.
3 changes: 2 additions & 1 deletion server/src/api/GetRestaurantsNearby.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ protected void doGet(HttpServletRequest request,
}
JSONObject obj = new JSONObject();
try {
obj.append("username", username);
obj.put("username", username);
obj.put("name", "panda");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Expand Down
47 changes: 47 additions & 0 deletions server/src/db/DBConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
public class DBConnection {
private Connection conn = null;
private static final int MAX_RECOMMENDED_RESTAURANTS = 10;
private static final int MIN_RECOMMENDED_RESTAURANTS = 3;

public DBConnection() {
try {
Expand Down Expand Up @@ -135,6 +136,34 @@ private JSONObject getRestaurantsById(String businessId) {
}
return null;
}

private Set<String> getMoreCategories(String category, int maxCount) {
Set<String> allCategories = new HashSet<>();
try {
if (conn == null) {
return null;
}
Statement stmt = conn.createStatement();
String sql = "SELECT second_id from USER_CATEGORY_HISTORY WHERE first_id=\""
+ category + "\" ORDER BY count DESC LIMIT " + maxCount;
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String visited_restaurant = rs.getString("second_id");
allCategories.add(visited_restaurant);
}
} catch (Exception e) { /* report an error */
System.out.println(e.getMessage());
}
return allCategories;
}

private Set<String> getMoreCategories(Set<String> existingCategories) {
Set<String> allCategories = new HashSet<>();
for (String category : existingCategories) {
allCategories.addAll(getMoreCategories(category, 5));
}
return allCategories;
}

public JSONArray RecommendRestaurants(String userId) {
try {
Expand Down Expand Up @@ -163,6 +192,24 @@ public JSONArray RecommendRestaurants(String userId) {
}
}
}

if (count < MIN_RECOMMENDED_RESTAURANTS) {
allCategories.addAll(getMoreCategories(allCategories));
for (String category : allCategories) {
Set<String> set = getBusinessId(category);
allRestaurants.addAll(set);
}
for (String business_id : allRestaurants) {
if (!visitedRestaurants.contains(business_id)) {
diff.add(getRestaurantsById(business_id));
count++;
if (count >= MAX_RECOMMENDED_RESTAURANTS) {
break;
}
}
}
}

return new JSONArray(diff);
} catch (Exception e) { /* report an error */
System.out.println(e.getMessage());
Expand Down
84 changes: 41 additions & 43 deletions server/src/db/DBImport.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static void main(String[] args) {
String line = null;

try {
// default password is "password"
conn = DriverManager
.getConnection("jdbc:mysql://localhost:3306/mysql?"
+ "user=root&password=root");
Expand All @@ -60,6 +61,7 @@ public static void main(String[] args) {
if (conn == null) {
return;
}
//Step 1 Drop tables.
Statement stmt = conn.createStatement();
String sql = "DROP TABLE IF EXISTS USER_VISIT_HISTORY";
stmt.executeUpdate(sql);
Expand All @@ -73,12 +75,11 @@ public static void main(String[] args) {
sql = "DROP TABLE IF EXISTS USER_REVIEW_HISTORY";
stmt.executeUpdate(sql);

//Optional
/*
sql = "DROP TABLE IF EXISTS USER_CATEGORY_HISTORY";
stmt.executeUpdate(sql);
*/


//Step 2: create tables
sql = "CREATE TABLE RESTAURANTS "
+ "(business_id VARCHAR(255) NOT NULL, "
+ " name VARCHAR(255), " + "categories VARCHAR(255), "
Expand All @@ -88,42 +89,12 @@ public static void main(String[] args) {
+ "image_url VARCHAR(255), "
+ " PRIMARY KEY ( business_id ))";
stmt.executeUpdate(sql);

BufferedReader reader = new BufferedReader(new FileReader(
"../dataset/yelp_academic_dataset_business.json"));
while ((line = reader.readLine()) != null) {
JSONObject restaurant = new JSONObject(line);
String business_id = restaurant.getString("business_id");
String name = parseString(restaurant.getString("name"));
String categories = parseString(jsonArrayToString(restaurant
.getJSONArray("categories")));
String city = parseString(restaurant.getString("city"));
String state = restaurant.getString("state");
String fullAddress = parseString(restaurant
.getString("full_address"));
double stars = restaurant.getDouble("stars");
double latitude = restaurant.getDouble("latitude");
double longitude = restaurant.getDouble("longitude");
String imageUrl = "http://www.example.com/img.JPG";
sql = "INSERT INTO RESTAURANTS " + "VALUES ('" + business_id
+ "', \"" + name + "\", \"" + categories + "\", '"
+ city + "', '" + state + "', " + stars + ", \""
+ fullAddress + "\", " + latitude + "," + longitude
+ ", \"" +imageUrl + "\""
+ ")";
System.out.println(sql);
stmt.executeUpdate(sql);
}

System.out.println("Done creating restaurant table.");


sql = "CREATE TABLE USERS "
+ "(user_id VARCHAR(255) NOT NULL, "
+ " first_name VARCHAR(255), last_name VARCHAR(255), "
+ " PRIMARY KEY ( user_id ))";
stmt.executeUpdate(sql);
sql = "INSERT INTO USERS " + "VALUES (\"1111\", \"John\", \"Smith\")";
stmt.executeUpdate(sql);

sql = "CREATE TABLE USER_VISIT_HISTORY "
+ "(visit_history_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, "
Expand All @@ -134,11 +105,6 @@ public static void main(String[] args) {
+ "FOREIGN KEY (business_id) REFERENCES RESTAURANTS(business_id),"
+ "FOREIGN KEY (user_id) REFERENCES users(user_id))";
stmt.executeUpdate(sql);

reader.close();

reader = new BufferedReader(new FileReader(
"../dataset/yelp_academic_dataset_review.json"));

sql = "CREATE TABLE USER_REVIEW_HISTORY "
+ "(visit_review_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, "
Expand All @@ -147,8 +113,6 @@ public static void main(String[] args) {
+ " PRIMARY KEY (visit_review_id))";
stmt.executeUpdate(sql);

//Optional to create category history (takes much time)
/*
sql = "CREATE TABLE USER_CATEGORY_HISTORY "
+ "(category_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, "
+ " first_id VARCHAR(255) NOT NULL , "
Expand All @@ -157,6 +121,42 @@ public static void main(String[] args) {
+ " PRIMARY KEY (category_id))";
stmt.executeUpdate(sql);

//Step 3: insert data
BufferedReader reader = new BufferedReader(new FileReader(
"../dataset/yelp_academic_dataset_business.json"));
while ((line = reader.readLine()) != null) {
JSONObject restaurant = new JSONObject(line);
String business_id = restaurant.getString("business_id");
String name = parseString(restaurant.getString("name"));
String categories = parseString(jsonArrayToString(restaurant
.getJSONArray("categories")));
String city = parseString(restaurant.getString("city"));
String state = restaurant.getString("state");
String fullAddress = parseString(restaurant
.getString("full_address"));
double stars = restaurant.getDouble("stars");
double latitude = restaurant.getDouble("latitude");
double longitude = restaurant.getDouble("longitude");
String imageUrl = "http://www.example.com/img.JPG";
sql = "INSERT INTO RESTAURANTS " + "VALUES ('" + business_id
+ "', \"" + name + "\", \"" + categories + "\", '"
+ city + "', '" + state + "', " + stars + ", \""
+ fullAddress + "\", " + latitude + "," + longitude
+ ", \"" +imageUrl + "\""
+ ")";
System.out.println(sql);
stmt.executeUpdate(sql);
}
reader.close();

sql = "INSERT INTO USERS " + "VALUES (\"1111\", \"John\", \"Smith\")";
stmt.executeUpdate(sql);

//Optional to create category history (takes much time)
/*
reader = new BufferedReader(new FileReader(
"../dataset/yelp_academic_dataset_review.json"));
while ((line = reader.readLine()) != null) {
JSONObject review = new JSONObject(line);
String business_id = review.getString("business_id");
Expand All @@ -168,8 +168,6 @@ public static void main(String[] args) {
}
*/



System.out.println("Done Importing");
} catch (Exception e) { /* report an error */
System.out.println(e.getMessage());
Expand Down
11 changes: 11 additions & 0 deletions server/src/db/DBYelpImport.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ public static void main(String[] args) {
+ "FOREIGN KEY (business_id) REFERENCES RESTAURANTS(business_id),"
+ "FOREIGN KEY (user_id) REFERENCES users(user_id))";
stmt.executeUpdate(sql);

sql = "DROP TABLE IF EXISTS USER_CATEGORY_HISTORY";
stmt.executeUpdate(sql);

sql = "CREATE TABLE USER_CATEGORY_HISTORY "
+ "(category_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, "
+ " first_id VARCHAR(255) NOT NULL , "
+ " second_id VARCHAR(255) NOT NULL, "
+ " count bigint(20) NOT NULL, "
+ " PRIMARY KEY (category_id))";
stmt.executeUpdate(sql);

System.out.println("Done Importing");

Expand Down

0 comments on commit 3cc2a16

Please sign in to comment.