use of org.haiku.haikudepotserver.dataobjects.auto._User in project IR_Base by Linda-sunshine.
the class MultiTaskSVM method loadUsers.
@Override
public void loadUsers(ArrayList<_User> userList) {
m_userList = new ArrayList<_AdaptStruct>();
for (_User user : userList) m_userList.add(new _AdaptStruct(user));
m_pWeights = new double[m_featureSize + 1];
}
use of org.haiku.haikudepotserver.dataobjects.auto._User in project IR_Base by Linda-sunshine.
the class MultiThreadedNetworkAnalyzer method saveCVIndex.
// shuffle the document index based on each user
public void saveCVIndex(int k, String filename) {
m_corpus.setMasks();
for (_User u : m_users) {
setMasks4Reviews(u.getReviews(), k);
}
ArrayList<_Doc> docs = m_corpus.getCollection();
int[] stat = new int[5];
try {
PrintWriter writer = new PrintWriter(new File(filename));
for (int i = 0; i < docs.size(); i++) {
_Review r = (_Review) docs.get(i);
writer.write(String.format("%s,%d,%d\n", r.getUserID(), r.getID(), r.getMask4CV()));
stat[r.getMask4CV()]++;
}
writer.close();
System.out.println("[Info]Finish writing cv index! Stat as follow:");
for (int s : stat) System.out.print(s + "\t");
} catch (IOException e) {
e.printStackTrace();
}
}
use of org.haiku.haikudepotserver.dataobjects.auto._User in project IR_Base by Linda-sunshine.
the class MultiThreadedUserAnalyzer method loadUser.
// Load one file as a user here.
protected void loadUser(String filename, int core) {
try {
File file = new File(filename);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
String line;
// UserId is contained in the filename.
String userID = extractUserID(file.getName());
// Skip the first line since it is user name.
reader.readLine();
String productID, source, category = "";
ArrayList<_Review> reviews = new ArrayList<_Review>();
_Review review;
int ylabel;
long timestamp = 0;
while ((line = reader.readLine()) != null) {
productID = line;
// review content
source = reader.readLine();
// review category
category = reader.readLine();
ylabel = Integer.valueOf(reader.readLine());
timestamp = Long.valueOf(reader.readLine());
// Construct the new review.
if (ylabel != 3) {
ylabel = (ylabel >= 4) ? 1 : 0;
review = new _Review(m_corpus.getCollection().size(), source, ylabel, userID, productID, category, timestamp);
if (AnalyzeDoc(review, core)) {
// Create the sparse vector for the review.
reviews.add(review);
}
}
}
if (reviews.size() > 1) {
// at least one for adaptation and one for testing
synchronized (m_allocReviewLock) {
allocateReviews(reviews);
// create new user from the file.
m_users.add(new _User(userID, m_classNo, reviews));
}
} else if (reviews.size() == 1) {
// added by Lin, for those users with fewer than 2 reviews, ignore them.
review = reviews.get(0);
synchronized (m_rollbackLock) {
rollBack(Utils.revertSpVct(review.getSparse()), review.getYLabel());
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
use of org.haiku.haikudepotserver.dataobjects.auto._User in project IR_Base by Linda-sunshine.
the class MultiThreadedUserAnalyzer method saveUserUserPairs.
// save the user-user pairs to graphlab for model training.
public void saveUserUserPairs(String dir) {
int trainUser = 0, testUser = 0, trainPair = 0, testPair = 0;
try {
PrintWriter trainWriter = new PrintWriter(new File(dir + "train.csv"));
PrintWriter testWriter = new PrintWriter(new File(dir + "test.csv"));
trainWriter.write("user_id,item_id,rating\n");
testWriter.write("user_id,item_id,rating\n");
for (_User u : m_users) {
if (u.getFriendSize() != 0) {
trainUser++;
for (String frd : u.getFriends()) {
trainPair++;
trainWriter.write(String.format("%s,%s,%d\n", u.getUserID(), frd, 1));
trainWriter.write(String.format("%s,%s,%d\n", frd, u.getUserID(), 1));
}
}
// for test users, we also need to write out non-friends
if (u.getTestFriendSize() != 0) {
testUser++;
for (_User nei : m_users) {
String neiID = nei.getUserID();
if (u.hasFriend(neiID) || u.getUserID().equals(neiID))
continue;
else if (u.hasTestFriend(neiID)) {
testPair++;
testWriter.write(String.format("%s,%s,%d\n", u.getUserID(), neiID, 1));
testWriter.write(String.format("%s,%s,%d\n", neiID, u.getUserID(), 1));
} else if (m_trainMap.containsKey(neiID)) {
testPair++;
testWriter.write(String.format("%s,%s,%d\n", u.getUserID(), neiID, 0));
testWriter.write(String.format("%s,%s,%d\n", neiID, u.getUserID(), 0));
}
}
}
}
trainWriter.close();
testWriter.close();
System.out.format("[Info]Finish writing (%d,%d) training users/pairs, (%d,%d) testing users/pairs.\n", trainUser, trainPair, testUser, testPair);
} catch (IOException e) {
e.printStackTrace();
}
}
use of org.haiku.haikudepotserver.dataobjects.auto._User in project IR_Base by Linda-sunshine.
the class MultiThreadedUserAnalyzer method buildFriendship.
public void buildFriendship(String filename) {
try {
File file = new File(filename);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
String line;
String[] users, friends;
while ((line = reader.readLine()) != null) {
users = line.trim().split("\t");
friends = Arrays.copyOfRange(users, 1, users.length);
if (friends.length == 0) {
continue;
}
m_trainMap.put(users[0], friends);
}
reader.close();
System.out.format("%d users have friends!", m_trainMap.size());
// map friends to users.
int count = 0;
for (_User u : m_users) {
if (m_trainMap.containsKey(u.getUserID())) {
count++;
u.setFriends(m_trainMap.get(u.getUserID()));
}
}
System.out.format("%d users' friends are set!\n", count);
} catch (IOException e) {
e.printStackTrace();
}
}
Aggregations