sequence.java

来自「Weka」· Java 代码 · 共 574 行 · 第 1/2 页

JAVA
574
字号
		canElements.removeElementAt(j);		//check if the (k-1)-sequence is contained in the set of kMinusOneSequences		int containedAt = kMinusOneSequences.indexOf(candidate);		if (containedAt != -1) {		  origEvents[k] = helpEvent;		  canElements.insertElementAt(origElement, j);		  break;		} else {		  isFrequent = false;		  break;		}	      } else {		//check if the (k-1)-sequence is contained in the set of kMinusOneSequences		int containedAt = kMinusOneSequences.indexOf(candidate);		if (containedAt != -1) {		  origEvents[k] = helpEvent;		  continue;		} else {		  isFrequent = false;		  break;		}	      }	    }	  }	} else {	  break;	}      }      if (isFrequent) {	prunedCandidates.addElement(candidate);      }    }    return prunedCandidates;  }  /**   * Returns a String representation of a set of Sequences where the numeric    * value of each event/item is represented by its respective nominal value.   *    * @param setOfSequences 	the set of Sequences   * @param dataSet 		the corresponding data set containing the header    * 				information   * @param filterAttributes	the attributes to filter out   * @return 			the String representation   */  public static String setOfSequencesToString(FastVector setOfSequences, Instances dataSet, FastVector filterAttributes) {    StringBuffer resString = new StringBuffer();    Enumeration SequencesEnum = setOfSequences.elements();    int i = 1;    boolean printSeq;    while(SequencesEnum.hasMoreElements()) {      Sequence seq = (Sequence) SequencesEnum.nextElement();      Integer filterAttr = (Integer) filterAttributes.elementAt(0);      printSeq = true;      if (filterAttr.intValue() != -1) {	for (int j=0; j < filterAttributes.size(); j++) {	  filterAttr = (Integer) filterAttributes.elementAt(j);	  FastVector seqElements = seq.getElements();	  if (printSeq) {	    for (int k=0; k < seqElements.size(); k++) {	      Element currentElement = (Element) seqElements.elementAt(k);	      int[] currentEvents = currentElement.getEvents();	      if (currentEvents[filterAttr.intValue()] != -1) {		continue;	      } else {		printSeq = false;		break;	      }	    }	  }	}      }      if (printSeq) {	resString.append("[" + i++ + "]" + " " + seq.toNominalString(dataSet));      }    }    return resString.toString();  }  /**   * Updates the support count of a set of Sequence candidates according to a    * given set of data sequences.   *    * @param candidates 		the set of candidates   * @param dataSequences 	the set of data sequences   */  public static void updateSupportCount(FastVector candidates, FastVector dataSequences) {    Enumeration canEnumeration = candidates.elements();    while(canEnumeration.hasMoreElements()){      Enumeration dataSeqEnumeration = dataSequences.elements();      Sequence candidate = (Sequence) canEnumeration.nextElement();      while(dataSeqEnumeration.hasMoreElements()) {	Instances dataSequence = (Instances) dataSeqEnumeration.nextElement();	if (candidate.isSubsequenceOf(dataSequence)) {	  candidate.setSupportCount(candidate.getSupportCount() + 1);	}      }    }  }  /**   * Returns a deep clone of a Sequence.   *    * @return 		the cloned Sequence   */  public Sequence clone() {    try {      Sequence clone = (Sequence) super.clone();      clone.setSupportCount(m_SupportCount);      FastVector cloneElements = new FastVector(m_Elements.size());      for (int i = 0; i < m_Elements.size(); i++) {	Element helpElement = (Element) m_Elements.elementAt(i);	cloneElements.addElement(helpElement.clone());      }      clone.setElements(cloneElements);      return clone;    } catch (CloneNotSupportedException exc) {      exc.printStackTrace();    }    return null;  }  /**   * Deletes either the first or the last event/item of a Sequence. If the    * deleted event/item is the only value in the Element, it is removed, as well.   *    * @param position 		the position of the event/item (first or last)   * @return 			the Sequence with either the first or the last    * 				event/item deleted   */  protected Sequence deleteEvent(String position) {    Sequence cloneSeq = clone();    if (position.equals("first")) {      Element element = (Element) cloneSeq.getElements().firstElement();      element.deleteEvent("first");      if (element.isEmpty()) {	cloneSeq.getElements().removeElementAt(0);      }      return cloneSeq;    }    if (position.equals("last")) {      Element element = (Element) cloneSeq.getElements().lastElement();      element.deleteEvent("last");      if (element.isEmpty()) {	cloneSeq.getElements().removeElementAt(m_Elements.size()-1);      }      return cloneSeq;    }    return null;  }  /**   * Checks if two Sequences are equal.   *    * @return 			true, if the two Sequences are equal, else false   */  public boolean equals(Object obj) {    Sequence seq2 = (Sequence) obj;    FastVector seq2Elements = seq2.getElements();    for (int i = 0; i < m_Elements.size(); i++) {      Element thisElement = (Element) m_Elements.elementAt(i);      Element seq2Element = (Element) seq2Elements.elementAt(i);      if (!thisElement.equals(seq2Element)) {	return false;      }    }    return true;  }  /**   * Returns the Elements of the Sequence.   *    * @return 			the Elements   */  protected FastVector getElements() {    return m_Elements;  }  /**   * Returns the support count of the Sequence.   *    * @return 			the support count   */  protected int getSupportCount() {    return m_SupportCount;  }  /**   * Checks if the Sequence is subsequence of a given data sequence.   *    * @param dataSequence 	the data sequence to verify against   * @return 			true, if the Sequnce is subsequence of the data    * 				sequence, else false   */  protected boolean isSubsequenceOf(Instances dataSequence) {    FastVector elements = getElements();    Enumeration elementEnum = elements.elements();    Element curElement = (Element) elementEnum.nextElement();    for (int i = 0; i < dataSequence.numInstances(); i++) {      if (curElement.isContainedBy(dataSequence.instance(i))) {	if (!elementEnum.hasMoreElements()) {	  return true;	} else {	  curElement = (Element) elementEnum.nextElement();	  continue;	}      }    }    return false;  }  /**   * Sets the Elements of the Sequence.   *    * @param elements 		the Elements to set   */  protected void setElements(FastVector elements) {    m_Elements = elements;  }  /**   * Sets the support count of the Sequence.   *    * @param supportCount 	the support count to set   */  protected void setSupportCount(int supportCount) {    m_SupportCount = supportCount;  }  /**   * Returns a String representation of a Sequences where the numeric value    * of each event/item is represented by its respective nominal value.   *    * @param dataSet 		the corresponding data set containing the header    * 				information   * @return 			the String representation   */  public String toNominalString(Instances dataSet) {    String result = "";    result += "<";    for (int i = 0; i < m_Elements.size(); i++) {      Element element = (Element) m_Elements.elementAt(i);      result += element.toNominalString(dataSet);    }    result += "> (" + getSupportCount() + ")\n";    return result;  }  /**   * Returns a String representation of a Sequence.   *    * @return 			the String representation   */  public String toString() {    String result = "";    result += "Sequence Output\n";    result += "------------------------------\n";    result += "Support Count: " + getSupportCount() + "\n";    result += "contained elements/itemsets:\n";    for (int i = 0; i < m_Elements.size(); i++) {      Element element = (Element) m_Elements.elementAt(i);      result += element.toString();    }    result += "\n\n";    return result;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?