Search in sources :

Example 31 with org.haiku.haikudepotserver.dataobjects.auto._User

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];
}
Also used : structures._User(structures._User)

Example 32 with org.haiku.haikudepotserver.dataobjects.auto._User

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();
    }
}
Also used : structures._Review(structures._Review) structures._Doc(structures._Doc) structures._User(structures._User)

Example 33 with org.haiku.haikudepotserver.dataobjects.auto._User

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();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) structures._Review(structures._Review) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) structures._User(structures._User) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 34 with org.haiku.haikudepotserver.dataobjects.auto._User

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();
    }
}
Also used : structures._User(structures._User) IOException(java.io.IOException) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 35 with org.haiku.haikudepotserver.dataobjects.auto._User

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();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) structures._User(structures._User) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

structures._User (structures._User)56 Classifier.supervised.modelAdaptation._AdaptStruct (Classifier.supervised.modelAdaptation._AdaptStruct)15 File (java.io.File)10 IOException (java.io.IOException)10 structures._Review (structures._Review)10 ArrayList (java.util.ArrayList)6 PrintWriter (java.io.PrintWriter)5 BufferedReader (java.io.BufferedReader)4 FileInputStream (java.io.FileInputStream)4 InputStreamReader (java.io.InputStreamReader)4 HashMap (java.util.HashMap)3 ObjectContext (org.apache.cayenne.ObjectContext)3 ObjectNotFoundException (org.haiku.haikudepotserver.api1.support.ObjectNotFoundException)3 User (org.haiku.haikudepotserver.dataobjects.User)3 org.haiku.haikudepotserver.dataobjects.auto._User (org.haiku.haikudepotserver.dataobjects.auto._User)3 AccessDeniedException (org.springframework.security.access.AccessDeniedException)3 InvalidFormatException (opennlp.tools.util.InvalidFormatException)2 structures._SparseFeature (structures._SparseFeature)2 FileNotFoundException (java.io.FileNotFoundException)1 HashSet (java.util.HashSet)1