Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You 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 org.apache.mahout.cf.taste.impl.model;
 
 
 import java.util.List;
 import java.util.Map;

A simple org.apache.mahout.cf.taste.model.DataModel which uses a given java.util.List of users as its data source. This implementation is mostly useful for small experiments and is not recommended for contexts where performance is important.

 
 public final class GenericBooleanPrefDataModel implements DataModelSerializable {
 
   private final long[] userIDs;
   private final long[] itemIDs;
   private final FastByIDMap<FastIDSetpreferenceForItems;

  

Creates a new GenericDataModel from the given users (and their preferences). This org.apache.mahout.cf.taste.model.DataModel retains all this information in memory and is effectively immutable.

Parameters:
userData users to include
 
   @SuppressWarnings("unchecked")
     if (userData == null) {
       throw new IllegalArgumentException("userData is null");
     }
 
     this. = userData;
     this. = new FastByIDMap<FastIDSet>();
     FastIDSet itemIDSet = new FastIDSet();
     for (Map.Entry<LongFastIDSetentry : .entrySet()) {
       long userID = entry.getKey();
       FastIDSet itemIDs = entry.getValue();
       itemIDSet.addAll(itemIDs);
       LongPrimitiveIterator it = itemIDs.iterator();
       while (it.hasNext()) {
         long itemID = it.next();
         FastIDSet userIDs = .get(itemID);
         if (userIDs == null) {
           userIDs = new FastIDSet(2);
           .put(itemIDuserIDs);
         }
         userIDs.add(userID);
       }
     }
 
     this. = itemIDSet.toArray();
     itemIDSet = null// Might help GC -- this is big
     Arrays.sort();
 
     this. = new long[userData.size()];
     int i = 0;
     LongPrimitiveIterator it = userData.keySetIterator();
     while (it.hasNext()) {
       [i++] = it.next();
     }
     Arrays.sort();
 
   }

  

Creates a new GenericDataModel containing an immutable copy of the data from another given org.apache.mahout.cf.taste.model.DataModel.

Parameters:
dataModel org.apache.mahout.cf.taste.model.DataModel to copy
Throws:
org.apache.mahout.cf.taste.common.TasteException if an error occurs while retrieving the other org.apache.mahout.cf.taste.model.DataModel's users
  public GenericBooleanPrefDataModel(DataModel dataModelthrows TasteException {
    this(toDataMap(dataModel));
  }
  private static FastByIDMap<FastIDSettoDataMap(DataModel dataModelthrows TasteException {
    FastByIDMap<FastIDSetdata = new FastByIDMap<FastIDSet>(dataModel.getNumUsers());
    LongPrimitiveIterator it = dataModel.getUserIDs();
    while (it.hasNext()) {
      long userID = it.nextLong();
      data.put(userIDdataModel.getItemIDsFromUser(userID));
    }
    return data;
  }
    for (Map.Entry<LongObjectentry : ((FastByIDMap<Object>) (FastByIDMap<?>) data).entrySet()) {
      PreferenceArray prefArray = (PreferenceArrayentry.getValue();
      int size = prefArray.length();
      FastIDSet itemIDs = new FastIDSet(size);
      for (int i = 0; i < sizei++) {
        itemIDs.add(prefArray.getItemID(i));
      }
      entry.setValue(itemIDs);
    }
    return (FastByIDMap<FastIDSet>) (FastByIDMap<?>) data;
  }
    return new LongPrimitiveArrayIterator();
  }

  
  public PreferenceArray getPreferencesFromUser(long userIDthrows NoSuchUserException {
    FastIDSet itemIDs = .get(userID);
    if (itemIDs == null) {
      throw new NoSuchUserException();
    }
    PreferenceArray prefArray = new BooleanUserPreferenceArray(itemIDs.size());
    int i = 0;
    LongPrimitiveIterator it = itemIDs.iterator();
    while (it.hasNext()) {
      prefArray.setUserID(iuserID);
      prefArray.setItemID(iit.next());
      i++;
    }
    return prefArray;
  }
  public FastIDSet getItemIDsFromUser(long userIDthrows TasteException {
    FastIDSet itemIDs = .get(userID);
    if (itemIDs == null) {
      throw new NoSuchUserException();
    }
    return itemIDs;
  }
    return new LongPrimitiveArrayIterator();
  }
  public PreferenceArray getPreferencesForItem(long itemIDthrows NoSuchItemException {
    FastIDSet userIDs = .get(itemID);
    if (userIDs == null) {
      throw new NoSuchItemException();
    }
    PreferenceArray prefArray = new BooleanItemPreferenceArray(userIDs.size());
    int i = 0;
    LongPrimitiveIterator it = userIDs.iterator();
    while (it.hasNext()) {
      prefArray.setUserID(iit.next());
      prefArray.setItemID(iitemID);
      i++;
    }
    return prefArray;
  }
  public Float getPreferenceValue(long userIDlong itemIDthrows NoSuchUserException {
    FastIDSet itemIDs = .get(userID);
    if (itemIDs == null) {
      throw new NoSuchUserException();
    }
    if (itemIDs.contains(itemID)) {
      return 1.0f;
    }
    return null;
  }
  public int getNumItems() {
    return .;
  }
  public int getNumUsers() {
    return .;
  }
  public int getNumUsersWithPreferenceFor(long... itemIDsthrows NoSuchItemException {
    if (itemIDs.length == 0) {
      return 0;
    }
    FastIDSet userIDs = .get(itemIDs[0]);
    if (userIDs == null) {
      throw new NoSuchItemException();
    }
    FastIDSet intersection = new FastIDSet(userIDs.size());
    intersection.addAll(userIDs);
    int i = 1;
    while (!intersection.isEmpty() && i < itemIDs.length) {
      userIDs = .get(itemIDs[i]);
      if (userIDs == null) {
        throw new NoSuchItemException();
      }
      intersection.retainAll(userIDs);
      i++;
    }
    return intersection.size();
  }
  public void removePreference(long userIDlong itemID) {
    throw new UnsupportedOperationException();
  }
  public void setPreference(long userIDlong itemIDfloat value) {
    throw new UnsupportedOperationException();
  }
  public void refresh(Collection<RefreshablealreadyRefreshed) {
    // Does nothing
  }
  public String toString() {
    StringBuilder result = new StringBuilder(200);
    result.append("GenericBooleanPrefDataModel[users:");
    for (int i = 0; i < Math.min(3, .); i++) {
      if (i > 0) {
        result.append(',');
      }
      result.append([i]);
    }
    if (result.length() > 3) {
      result.append("...");
    }
    result.append(']');
    return result.toString();
  }
New to GrepCode? Check out our FAQ X