Ejemplo n.º 1
0
  @Override
  public void doWork() throws OperatorException {

    ExampleSet exampleSet = exampleSetInput.getData();

    IEntityMapping user_mapping = new EntityMapping();
    IEntityMapping item_mapping = new EntityMapping();
    IRatings training_data = new Ratings();

    if (exampleSet.getAttributes().getSpecial("user identification") == null) {
      throw new UserError(this, 105);
    }

    if (exampleSet.getAttributes().getSpecial("item identification") == null) {
      throw new UserError(this, 105);
    }

    if (exampleSet.getAttributes().getLabel() == null) {
      throw new UserError(this, 105);
    }

    Attributes Att = exampleSet.getAttributes();
    AttributeRole ur = Att.getRole("user identification");
    Attribute u = ur.getAttribute();
    AttributeRole ir = Att.getRole("item identification");
    Attribute i = ir.getAttribute();
    Attribute ui = Att.getLabel();

    for (Example example : exampleSet) {

      double j = example.getValue(u);
      int uid = user_mapping.ToInternalID((int) j);

      j = example.getValue(i);
      int iid = item_mapping.ToInternalID((int) j);

      double r = example.getValue(ui);
      training_data.Add(uid, iid, r);
    }

    _slopeOne recommendAlg = new _slopeOne();

    recommendAlg.user_mapping = user_mapping;
    recommendAlg.item_mapping = item_mapping;
    recommendAlg.SetMinRating(getParameterAsInt("Min Rating"));
    recommendAlg.SetMaxRating(recommendAlg.GetMinRating() + getParameterAsInt("Range"));

    recommendAlg.SetRatings(training_data);

    recommendAlg.Train();

    exampleSetOutput.deliver(exampleSet);

    exampleSetOutput1.deliver(recommendAlg);
  }
Ejemplo n.º 2
0
  @Override
  public void doWork() throws OperatorException {

    ExampleSet exampleSet = exampleSetInput.getData();

    IPosOnlyFeedback training_data = new PosOnlyFeedback();
    IEntityMapping user_mapping = new EntityMapping();
    IEntityMapping item_mapping = new EntityMapping();

    if (exampleSet.getAttributes().getSpecial("user identification") == null) {
      throw new UserError(this, 105);
    }

    if (exampleSet.getAttributes().getSpecial("item identification") == null) {
      throw new UserError(this, 105);
    }

    Attributes Att = exampleSet.getAttributes();
    AttributeRole ur = Att.getRole("user identification");
    Attribute u = ur.getAttribute();
    AttributeRole ir = Att.getRole("item identification");
    Attribute i = ir.getAttribute();

    for (Example example : exampleSet) {

      double j = example.getValue(u);
      int uid = (int) j;

      j = example.getValue(i);
      int iid = (int) j;

      training_data.Add(user_mapping.ToInternalID(uid), item_mapping.ToInternalID(iid));
      checkForStop();
    }

    System.out.println(training_data.GetMaxItemID() + " " + training_data.GetMaxUserID());

    Random recommendAlg = new Random();
    recommendAlg.SetFeedback(training_data);
    recommendAlg.user_mapping = user_mapping;
    recommendAlg.item_mapping = item_mapping;
    recommendAlg.Train();

    exampleSetOutput.deliver(exampleSet);
    exampleSetOutput1.deliver(recommendAlg);
  }
 private Attribute[] getLabels(ExampleSet exampleSet) {
   List<Attribute> attributes = new LinkedList<Attribute>();
   Iterator<AttributeRole> i = exampleSet.getAttributes().specialAttributes();
   while (i.hasNext()) {
     AttributeRole role = i.next();
     String name = role.getSpecialName();
     if (name.startsWith(Attributes.LABEL_NAME)) {
       attributes.add(role.getAttribute());
     }
   }
   Attribute[] result = new Attribute[attributes.size()];
   attributes.toArray(result);
   return result;
 }
 /**
  * Implements the method required by the superclass. For features whose name matches the input
  * name (regular expression). If the input name does not match the the input name (regular
  * expression) will not be switched off. If no parameter was provided, FALSE is always returned,
  * so no feature is switched off.
  *
  * @param attributeRole Feature to check.
  * @return TRUE if this feature should <b>not</b> be active in the output example set of this
  *     operator. FALSE otherwise.
  */
 @Override
 public boolean switchOffFeature(AttributeRole attributeRole) throws OperatorException {
   Attribute attribute = attributeRole.getAttribute();
   Matcher nameSkipMatcher = skipPattern.matcher(attribute.getName());
   Matcher specialNameSkipMatcher = null;
   if (attributeRole.isSpecial())
     specialNameSkipMatcher = skipPattern.matcher(attributeRole.getSpecialName());
   Matcher exceptionMatcher =
       exceptionPattern != null ? exceptionPattern.matcher(attribute.getName()) : null;
   Matcher specialExceptionMatcher = null;
   if (attributeRole.isSpecial())
     specialExceptionMatcher =
         exceptionPattern != null
             ? exceptionPattern.matcher(attributeRole.getSpecialName())
             : null;
   return (nameSkipMatcher.matches()
           || ((specialNameSkipMatcher != null) && (specialNameSkipMatcher.matches())))
       && ((exceptionMatcher == null) || (!exceptionMatcher.matches()))
       && ((specialExceptionMatcher == null) || (!specialExceptionMatcher.matches()));
 }
  @Override
  public ExampleSet apply(ExampleSet exampleSet) throws OperatorException {
    // determine new value types
    int valueType = Ontology.REAL;
    Iterator<AttributeRole> a = exampleSet.getAttributes().allAttributeRoles();
    while (a.hasNext()) {
      AttributeRole attributeRole = a.next();
      if (!attributeRole.isSpecial()
          || !attributeRole.getSpecialName().equals(Attributes.ID_NAME)) {
        if (attributeRole.getAttribute().isNominal()) {
          valueType = Ontology.NOMINAL;
          break;
        }
      }
    }

    // create new attributes
    List<Attribute> newAttributes = new ArrayList<Attribute>(exampleSet.size());
    Attribute newIdAttribute =
        AttributeFactory.createAttribute(Attributes.ID_NAME, Ontology.NOMINAL);
    newAttributes.add(newIdAttribute);

    Attribute oldIdAttribute = exampleSet.getAttributes().getId();
    if (oldIdAttribute != null) {
      for (Example e : exampleSet) {
        double idValue = e.getValue(oldIdAttribute);
        String attributeName = "att_" + idValue;
        if (oldIdAttribute.isNominal()) {
          if (Double.isNaN(idValue)) {
            newAttributes.add(AttributeFactory.createAttribute(valueType));
          } else {
            attributeName = oldIdAttribute.getMapping().mapIndex((int) idValue);
            newAttributes.add(AttributeFactory.createAttribute(attributeName, valueType));
          }
        } else {
          newAttributes.add(AttributeFactory.createAttribute(attributeName, valueType));
        }
      }
    } else {
      for (int i = 0; i < exampleSet.size(); i++) {
        newAttributes.add(AttributeFactory.createAttribute("att_" + (i + 1), valueType));
      }
    }

    // create and fill table
    MemoryExampleTable table = new MemoryExampleTable(newAttributes);
    a = exampleSet.getAttributes().allAttributeRoles();
    while (a.hasNext()) {
      AttributeRole attributeRole = a.next();
      if (!attributeRole.isSpecial()
          || !attributeRole.getSpecialName().equals(Attributes.ID_NAME)) {
        Attribute attribute = attributeRole.getAttribute();
        double[] data = new double[exampleSet.size() + 1];
        data[0] = newIdAttribute.getMapping().mapString(attribute.getName());
        int counter = 1;
        for (Example e : exampleSet) {
          double currentValue = e.getValue(attribute);
          data[counter] = currentValue;
          Attribute newAttribute = newAttributes.get(counter);
          if (newAttribute.isNominal()) {
            if (!Double.isNaN(currentValue)) {
              String currentValueString = currentValue + "";
              if (attribute.isNominal())
                currentValueString = attribute.getMapping().mapIndex((int) currentValue);
              data[counter] = newAttribute.getMapping().mapString(currentValueString);
            }
          }
          counter++;
        }
        table.addDataRow(new DoubleArrayDataRow(data));
      }
    }

    // create and deliver example set
    ExampleSet result = table.createExampleSet(null, null, newIdAttribute);
    result.getAnnotations().addAll(exampleSet.getAnnotations());
    return result;
  }
  @Override
  public ExampleSet apply(ExampleSet exampleSet) throws OperatorException {
    // init
    char decimalPointCharacter = getParameterAsString(PARAMETER_DECIMAL_POINT_CHARACTER).charAt(0);
    Character groupingCharacter = null;
    if (isParameterSet(PARAMETER_NUMBER_GROUPING_CHARACTER)) {
      groupingCharacter = getParameterAsString(PARAMETER_NUMBER_GROUPING_CHARACTER).charAt(0);
    }

    Set<Attribute> attributeSet = attributeSelector.getAttributeSubset(exampleSet, false);
    int size = attributeSet.size();

    int[] valueTypes = new int[size];

    int index = 0;
    for (Attribute attribute : attributeSet) {
      valueTypes[index++] = attribute.getValueType();
    }

    // guessing
    int[] guessedValueTypes = new int[valueTypes.length];
    int checkedCounter = 0;
    for (Example example : exampleSet) {
      index = 0;
      for (Attribute attribute : attributeSet) {
        if (!attribute.isNominal() && !attribute.isNumerical()) {
          continue;
        }

        double originalValue = example.getValue(attribute);
        if (!Double.isNaN(originalValue)) {
          if (guessedValueTypes[index] != Ontology.NOMINAL) {
            try {
              String valueString = example.getValueAsString(attribute);
              if (!Attribute.MISSING_NOMINAL_VALUE.equals(valueString)) {
                if (groupingCharacter != null) {
                  valueString = valueString.replace(groupingCharacter.toString(), "");
                }
                valueString = valueString.replace(decimalPointCharacter, '.');
                double value = Double.parseDouble(valueString);
                if (guessedValueTypes[index] != Ontology.REAL) {
                  if (Tools.isEqual(Math.round(value), value)) {
                    guessedValueTypes[index] = Ontology.INTEGER;
                  } else {
                    guessedValueTypes[index] = Ontology.REAL;
                  }
                }
              }
            } catch (NumberFormatException e) {
              guessedValueTypes[index] = Ontology.NOMINAL;
              checkedCounter++;
            }
          }
        }
        index++;
      }
      if (checkedCounter >= guessedValueTypes.length) {
        break;
      }
    }

    // the example set contains at least one example and the guessing was performed
    if (exampleSet.size() > 0) {
      valueTypes = guessedValueTypes;

      // new attributes
      List<AttributeRole> newAttributes = new LinkedList<AttributeRole>();
      index = 0;
      for (Attribute attribute : attributeSet) {
        if (!attribute.isNominal() && !attribute.isNumerical()) {
          continue;
        }

        AttributeRole role = exampleSet.getAttributes().getRole(attribute);

        Attribute newAttribute = AttributeFactory.createAttribute(valueTypes[index]);
        exampleSet.getExampleTable().addAttribute(newAttribute);
        AttributeRole newRole = new AttributeRole(newAttribute);
        newRole.setSpecial(role.getSpecialName());
        newAttributes.add(newRole);

        // copy data
        for (Example e : exampleSet) {
          double oldValue = e.getValue(attribute);
          if (Ontology.ATTRIBUTE_VALUE_TYPE.isA(valueTypes[index], Ontology.NUMERICAL)) {
            if (!Double.isNaN(oldValue)) {
              String valueString = e.getValueAsString(attribute);
              if (Attribute.MISSING_NOMINAL_VALUE.equals(valueString)) {
                e.setValue(newAttribute, Double.NaN);
              } else {
                if (groupingCharacter != null) {
                  valueString = valueString.replace(groupingCharacter.toString(), "");
                }
                valueString = valueString.replace(decimalPointCharacter, '.');
                e.setValue(newAttribute, Double.parseDouble(valueString));
              }
            } else {
              e.setValue(newAttribute, Double.NaN);
            }
          } else {
            if (!Double.isNaN(oldValue)) {
              String value = e.getValueAsString(attribute);
              e.setValue(newAttribute, newAttribute.getMapping().mapString(value));
            } else {
              e.setValue(newAttribute, Double.NaN);
            }
          }
        }

        // delete attribute and rename the new attribute (due to deletion and data scans: no
        // more memory used :-)
        exampleSet.getExampleTable().removeAttribute(attribute);
        exampleSet.getAttributes().remove(role);
        newAttribute.setName(attribute.getName());

        index++;
      }

      for (AttributeRole role : newAttributes) {
        if (role.isSpecial()) {
          exampleSet
              .getAttributes()
              .setSpecialAttribute(role.getAttribute(), role.getSpecialName());
        } else {
          exampleSet.getAttributes().addRegular(role.getAttribute());
        }
      }
    }

    return exampleSet;
  }