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 GenericDataModel implements DataModelSerializable {
 
   private static final Logger log = LoggerFactory.getLogger(GenericDataModel.class);
 
   private final long[] userIDs;
   private final long[] itemIDs;
   private final FastByIDMap<PreferenceArraypreferenceForItems;

  

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 in this GenericDataModel (see also toDataMap(org.apache.mahout.cf.taste.impl.common.FastByIDMap,boolean))
 
   @SuppressWarnings("unchecked")
   public GenericDataModel(FastByIDMap<PreferenceArrayuserData) {
     if (userData == null) {
       throw new IllegalArgumentException("userData is null");
     }
 
     this. = userData;
     FastByIDMap<Collection<Preference>> prefsForItems = new FastByIDMap<Collection<Preference>>();
     FastIDSet itemIDSet = new FastIDSet();
     int currentCount = 0;
     for (Map.Entry<LongPreferenceArrayentry : .entrySet()) {
       PreferenceArray prefs = entry.getValue();
       prefs.sortByItem();
       for (Preference preference : prefs) {
         long itemID = preference.getItemID();
         itemIDSet.add(itemID);
         List<PreferenceprefsForItem = (List<Preference>) prefsForItems.get(itemID);
         if (prefsForItem == null) {
           prefsForItem = new ArrayList<Preference>(2);
           prefsForItems.put(itemIDprefsForItem);
         }
         prefsForItem.add(preference);
       }
       if (++currentCount % 10000 == 0) {
         .info("Processed {} users"currentCount);
       }
     }
     .info("Processed {} users"currentCount);
 
     this. = itemIDSet.toArray();
     itemIDSet = null// Might help GC -- this is big
     Arrays.sort();
 
     this. = toDataMap(prefsForItemsfalse);
 
     for (Map.Entry<LongPreferenceArrayentry : .entrySet()) {
       entry.getValue().sortByUser();
     }
    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 GenericDataModel(DataModel dataModelthrows TasteException {
    this(toDataMap(dataModel));
  }

  
Swaps, in-place, java.util.Lists for arrays in java.util.Map values .

Returns:
input value
  public static FastByIDMap<PreferenceArraytoDataMap(FastByIDMap<Collection<Preference>> databoolean byUser) {
    for (Map.Entry<LongObjectentry : ((FastByIDMap<Object>) (FastByIDMap<?>) data).entrySet()) {
      List<PreferenceprefList = (List<Preference>) entry.getValue();
      entry.setValue(byUser ? new GenericUserPreferenceArray(prefList) : new GenericItemPreferenceArray(prefList));
    }
    return (FastByIDMap<PreferenceArray>) (FastByIDMap<?>) data;
  }
  private static FastByIDMap<PreferenceArraytoDataMap(DataModel dataModelthrows TasteException {
    LongPrimitiveIterator it = dataModel.getUserIDs();
    while (it.hasNext()) {
      long userID = it.nextLong();
      data.put(userIDdataModel.getPreferencesFromUser(userID));
    }
    return data;
  }
    return new LongPrimitiveArrayIterator();
  }

  
  public PreferenceArray getPreferencesFromUser(long userIDthrows NoSuchUserException {
    PreferenceArray prefs = .get(userID);
    if (prefs == null) {
      throw new NoSuchUserException();
    }
    return prefs;
  }
  public FastIDSet getItemIDsFromUser(long userIDthrows TasteException {
    PreferenceArray prefs = getPreferencesFromUser(userID);
    int size = prefs.length();
    FastIDSet result = new FastIDSet(size);
    for (int i = 0; i < sizei++) {
      result.add(prefs.getItemID(i));
    }
    return result;
  }
    return new LongPrimitiveArrayIterator();
  }
  public PreferenceArray getPreferencesForItem(long itemIDthrows NoSuchItemException {
    PreferenceArray prefs = .get(itemID);
    if (prefs == null) {
      throw new NoSuchItemException();
    }
    return prefs;
  }
  public Float getPreferenceValue(long userIDlong itemIDthrows TasteException {
    PreferenceArray prefs = getPreferencesFromUser(userID);
    int size = prefs.length();
    for (int i = 0; i < sizei++) {
      if (prefs.getItemID(i) == itemID) {
        return prefs.getValue(i);
      }
    }
    return null;
  }
  public int getNumItems() {
    return .;
  }
  public int getNumUsers() {
    return .;
  }
  public int getNumUsersWithPreferenceFor(long... itemIDs) {
    if (itemIDs == null) {
      throw new IllegalArgumentException("itemIDs is null");
    }
    int length = itemIDs.length;
    if (length == 0 || length > 2) {
      throw new IllegalArgumentException("Illegal number of item IDs: " + length);
    }
    if (length == 1) {
      PreferenceArray prefs = .get(itemIDs[0]);
      return prefs == null ? 0 : prefs.length();
    } else {
      PreferenceArray prefs1 = .get(itemIDs[0]);
      PreferenceArray prefs2 = .get(itemIDs[1]);
      if (prefs1 == null || prefs2 == null) {
        return 0;
      }
      FastIDSet users1 = new FastIDSet(prefs1.length());
      int size1 = prefs1.length();
      for (int i = 0; i < size1i++) {
        users1.add(prefs1.getUserID(i));
      }
      FastIDSet users2 = new FastIDSet(prefs2.length());
      int size2 = prefs2.length();
      for (int i = 0; i < size2i++) {
        users2.add(prefs2.getUserID(i));
      }
      users1.retainAll(users2);
      return users1.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("GenericDataModel[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