⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transactionstream.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
字号:
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

 /**
  * Title: XELOPES Data Mining Library
  * Description: The XELOPES library is an open platform-independent and data-source-independent library for Embedded Data Mining.
  * Copyright: Copyright (c) 2002 Prudential Systems Software GmbH
  * Company: ZSoft (www.zsoft.ru), Prudsys (www.prudsys.com)
  * @author Valentine Stepanenko (valentine.stepanenko@zsoft.ru)
  * @version 1.0
  */

package com.prudsys.pdm.Input.Records.Transactions;

import java.io.*;
import java.util.*;

import com.prudsys.pdm.Core.*;
import com.prudsys.pdm.Input.*;

/**
 * Stream for reading transactions.
 */
public class TransactionStream extends MiningFileStream {

  private CategoricalAttribute transactId;
  private CategoricalAttribute itemId;
  private NumericAttribute itemIndex;
  private String currentLine;
  private Vector<String> tokens = new Vector<String>();
  private int currentToken;
  private BufferedReader buffered;
  private int tid = -1;
  private double tidKey;
  private int position;

  /**
   * Constructs transaction stream for a given file.
   *
   * @param fileName file to be processed
   * @throws MiningException cannot open transaction stream
   */
  public TransactionStream(String fileName) throws MiningException {

    MiningDataSpecification mds = new MiningDataSpecification();
    transactId = new CategoricalAttribute("transactId");
    mds.addMiningAttribute(transactId);
    itemId=  new CategoricalAttribute("itemId");
    mds.addMiningAttribute(itemId);
    itemIndex = new NumericAttribute("itemIndex");
    mds.addMiningAttribute(itemIndex);
    metaData = mds;
    this.fileName = fileName;
    reset();
  }

  /**
   * Returns supported strteam methods.
   *
   * @return supported stream methods
   */
  public Enumeration<String> getSupportedStreamMethods() {

    Vector<String> suppmeth = new Vector<String>();
    suppmeth.addElement("reset");

    return suppmeth.elements();
  }


  /**
   * Sets cursor before first position.
   *
   * @throws MiningException cannot place cursor
   */
  public void reset() throws MiningException
  {
    super.reset();
    buffered = new BufferedReader(reader);
  }

  /**
   * Advance cursor by one position.
   *
   * @return true if next vector exists, else false
   * @exception MiningException cannot advance cursor position
   */
  public boolean next() throws MiningException
  {
    if(currentLine == null || currentToken == tokens.size())
    {
      try
      {
        int l = 0;
        while(l == 0 && buffered.ready())
        {
          currentLine = buffered.readLine();
          System.out.println("Line: "+currentLine);
          StringTokenizer tokenizer = new StringTokenizer(currentLine);
          tokens.clear();
          while(tokenizer.hasMoreTokens())
            tokens.add(tokenizer.nextToken());
          l = tokens.size();
        }
        if(l == 0) return false;
      }
      catch (IOException ex)
      {
        throw new MiningException("failed to read next line: "+ex.getMessage());
      }
      currentToken = 0;
      String tidValue = "transact_"+Integer.toString(++tid);
      tidKey = transactId.addCategory(new Category(tidValue,tidValue,new CategoryProperty(CategoryProperty.VALID)));
      position = 0;
    }
    return true;
  }

  /**
   * Reads current data vector.
   *
   * @return data vector at current cursor position
   * @exception MiningException thrown if something went wrong
   */
  public MiningVector read() throws MiningException
  {
    if(currentLine == null || currentToken == tokens.size()) return null;
    double[] values = new double[3];
    MiningVector vec = new MiningVector(values);
    values[0] = tidKey;
    String item = tokens.get(currentToken++);
    Category category = new Category(item,item,new CategoryProperty(CategoryProperty.VALID));
    double key = itemId.getKey(category);
    if(Category.isMissingValue(key))
    {
      key = itemId.addCategory(category);
    }
    values[1] = key;
    values[2] = (double)(position++);
    vec.setMetaData(metaData);
    return vec;
  }

  /**
   * Test of transaction stream.
   *
   * @param args arguments (ignored)
   */
  public static void main(String[] args)
  {
    try
    {
      TransactionStream sts = new TransactionStream("data/t.txt");
      System.out.println("Meta data:\n"+sts.getMetaData().toString());
      int n = 0;
      while(sts.next())
      {
        MiningVector vec = sts.read();
        System.out.println("Vec: "+vec.toString());
        n++;
      }
      System.out.println(Integer.toString(n)+" vectors readed");
    }
    catch (MiningException ex)
    {
      System.out.println("Exception: "+ex.getMessage());
    }
    return;
  }
}

⌨️ 快捷键说明

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