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

📄 miningdataspecification.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }

    /**
     * Sets the transformations dictionary that
     * has driven the transformations.
     *
     * @param miningTransformationActivity the transformation dictionary of the transformations
     */
    public void setMiningTransformationActivity( MiningTransformationActivity miningTransformationActivity )
    {
        this.miningTransformationActivity = miningTransformationActivity;
    }

    /**
     * Returns file name.
     *
     * @return file name
     */
    public String getFileName()
    {
        return fileName;
    }

    /**
     * Sets file name.
     *
     * @param fileName new file name
     */
    public void setFileName(String fileName)
    {
        this.fileName = fileName;
    }

    /**
     * Returns reference to meta data operations object owned by this meta data.
     *
     * @return associated meta data operations object
     */
    public MetaDataOperations getMetaDataOp()
    {
      return metaDataOp;
    }

    // -----------------------------------------------------------------------
    //  Methods of PMML handling
    // -----------------------------------------------------------------------
    /**
     * Creates PMML document of mining data specification.
     * PMMLs DataDictionary is used.
     * If transformation was applied, additionally
     * PMMLs TransformationDictionary is created.
     *
     * @param writer writer for PMML model
     * @throws MiningException if can't write metadata to the pmml destination
     * @see com.prudsys.pdm.Adapters.PmmlVersion20.DataDictionary
     */
    public void writePmml( Writer writer ) throws MiningException
    {
        PMML pmml = new PMML();
        pmml.setVersion( "2.0" );
        pmml.setHeader( (Header)PmmlUtils.getHeader() );

        // Add data and transformation dictionary:
        if ( isTransformed() )
        {
            pmml.setDataDictionary( (DataDictionary) getPretransformedMetaData().createPmmlObject() );
            pmml.setTransformationDictionary( (com.prudsys.pdm.Adapters.PmmlVersion20.TransformationDictionary)
              getMiningTransformationActivity().createPmmlObject());
        }
        else
        {
            pmml.setDataDictionary( (DataDictionary)createPmmlObject() );
        }

        // Add encoding and write to document:
        PmmlUtils.setEncoding();
        PmmlUtils.marshalPmml(writer, pmml);
    }

    /**
     * Reads PMML document of mining data specification.
     * PMMLs DataDictionary is used.
     *
     * @param reader reader for PMML model
     * @throws MiningException if can't read metadata from the pmml source
     * @see com.prudsys.pdm.Adapters.PmmlVersion20.DataDictionary
     */
    public void readPmml( Reader reader ) throws MiningException
    {
//        com.borland.xml.toolkit.XmlUtil.setEncoding( "UTF-8" );
        PMML pmml = PMML.unmarshal( reader );

        // Read data dictionary:
        DataDictionary dictionary = pmml.getDataDictionary();
        parsePmmlObject( dictionary );

        // Read transformation dictionary:
        TransformationDictionary transDict = pmml.getTransformationDictionary();
        if (transDict != null) {
              transformed = true;
              pretransformedMetaData = this;
              miningTransformationActivity = new MiningTransformationActivity();
              miningTransformationActivity.parsePmmlObject(transDict);
        };
    }

    /**
     * Creates PMML element DataDictionary of current mining data specification.
     *
     * @return PMML object DataDictionary
     * @throws MiningException if can't create pmml representation for metadata
     * @see com.prudsys.pdm.Adapters.PmmlVersion20.DataDictionary
     */
    public Object createPmmlObject() throws MiningException
    {
        DataDictionary dictionary = new DataDictionary();
        int fieldsNumber          = miningAttributes.size();
        dictionary.setNumberOfFields( String.valueOf( fieldsNumber ) );
        DataField[] dataField     = new DataField[fieldsNumber];
        for( int i = 0; i < fieldsNumber; i++ )
        {
            MiningAttribute attribute = (MiningAttribute) miningAttributes.get(i);
            dataField[i] = (DataField) attribute.createPmmlObject();
        }
        dictionary.setDataField( dataField );

        return dictionary;
    }

    /**
     * Reads PMML DataDictionary to create mining data specification.
     *
     * @param pmml pmmlObject object of DataDictionary
     * @throws MiningException if can't parse metadata from the pmml
     * @see com.prudsys.pdm.Adapters.PmmlVersion20.DataDictionary
     */
    public void parsePmmlObject( Object pmml ) throws MiningException
    {
        DataDictionary dictionary = (DataDictionary) pmml;
        DataField[] dataField     = dictionary.getDataField();
        setName("Read from PMML");
        miningAttributes.clear();
        names2attributes.clear();
        attributesArray              = null;
        settings                     = null;
        pretransformedMetaData       = null;
        transformed                  = false;
        miningTransformationActivity = null;
        metaDataOp                   = new MetaDataOperations(this);
        for( int i = 0; i < dataField.length; i++ )
        {
            MiningAttribute attribute = null;
            String type = dataField[i].getOptype();
            if( type.equals( "continuous" ) )
              attribute = new NumericAttribute();
            else if( type.equals( "categorical" ) )
              attribute = new CategoricalAttribute();
            else if( type.equals( "ordinal" ) )
              attribute = new OrdinalAttribute();
            else {
              System.out.println("Invalid attribute type.");
              continue;
            };
            attribute.parsePmmlObject( dataField[i] );
            addMiningAttribute( attribute );
        };
    }

    // -----------------------------------------------------------------------
    //  Other export and import methods
    // -----------------------------------------------------------------------
    /**
     * Creates ARFF description of mining data specification. This is just
     * the header of the ARFF files.
     *
     * @return ARFF description of mining data specification
     */
    public String createArffDescription()
    {
        String description = "@relation" + " " + StringUtils.quote( getName() ) + "\n";
        int n = miningAttributes.size();
        for( int i = 0; i < n; i++ )
        {
            MiningAttribute attribute = (MiningAttribute)miningAttributes.get( i );
            description = description + attribute.createArffDescription();
            description = description + "\n";
        }
        return description;
    }

    /**
     * Creates mining data specification from file with meta data.
     * The file could be an ARFF file or a special XML file.
     *
     * @param metadataFileName file containing the meta data
     * @throws MiningException if can't extract data specification from file
     * @return mining data specification constructed from the file, null if impossible
     */
    public static MiningDataSpecification getMiningDataSpecification( String metadataFileName ) throws MiningException
    {
        MiningDataSpecification metaData = null;
        int i = metadataFileName.lastIndexOf( '.' );
        if( i != -1 )
        {
            String extension = metadataFileName.substring( i + 1 );
            if( extension.equalsIgnoreCase( "arff" ) )
            {
                MiningArffStream stream = new MiningArffStream( metadataFileName );
                metaData = stream.getMetaData();
            }
            else if( extension.equalsIgnoreCase("txt"))
            {
              TransactionStream stream = new TransactionStream( metadataFileName );
              metaData = stream.getMetaData();
            }
            else if( extension.equalsIgnoreCase("csv"))
            {
              TransactionStream stream = new TransactionStream( metadataFileName );
              metaData = stream.getMetaData();
            }
            else if( extension.equalsIgnoreCase("txt"))
            {
              TransactionStream stream = new TransactionStream( metadataFileName );
              metaData = stream.getMetaData();
            }
            else if( extension.equalsIgnoreCase("log"))
            {
              TransactionStream stream = new TransactionStream( metadataFileName );
              metaData = stream.getMetaData();
            } 
            else if( extension.equalsIgnoreCase("xls"))
            {
              TransactionStream stream = new TransactionStream( metadataFileName );
              metaData = stream.getMetaData();
            }
            else
            {
                if( extension.equalsIgnoreCase( "xml" ) )
                {
                    try
                    {
                        metaData = new MiningDataSpecification();
                        FileReader pmmlReader = new FileReader( metadataFileName );
                        metaData.readPmml( pmmlReader );
                        pmmlReader.close();
                    }
                    catch(Exception ex)
                    {
                        throw new MiningDataException( "Can't read metadata from pmml file." );
                    }
                }
                else
                {
                    throw new MiningDataException( "Unsupported file type" );
                }
            }
        }
        metaData.setFileName( metadataFileName );
        return metaData;
    }

    // -----------------------------------------------------------------------
    //  java.lang.Object methods
    // -----------------------------------------------------------------------
    /**
     * Copy of this meta data. Shallow copy.
     *
     * @return copy of this meta data
     */
    public Object clone() {

      MiningDataSpecification mds = new MiningDataSpecification();
      mds.setName( this.getName() );
      mds.setAttributesArray( this.getAttributesArray() );
      mds.setFileName( this.getFileName() );
      mds.setTransformed( this.isTransformed() );
      mds.setPretransformedMetaData( this.getPretransformedMetaData() );
      mds.setMiningTransformationActivity( this.getMiningTransformationActivity() );

      return mds;
    }

    /**
     * Returns string representation of mining data specification.
     *
     * @return string representation
     */
    public String toString()
    {
      String relName = getName();
      if (relName != null) relName = StringUtils.quote( relName );
      String description = "relation: " + relName + "\n";
      int n = miningAttributes.size();
      for( int i = 0; i < n; i++ )
      {
          MiningAttribute attribute = (MiningAttribute)miningAttributes.get( i );
          description = description + String.valueOf(i) + "." + attribute + "\n";
      }
      return description;
    }

    /**
     * Returns HTML representation of mining data specification.
     *
     * @return HTML representation as string
     */
    public String toHtmlString()
    {
        String description = "<html>";
        description = description + "Mining attributes:<br>";
        int n = miningAttributes.size();
        for( int i = 0; i < n; i++ )
        {
            MiningAttribute attribute = (MiningAttribute)miningAttributes.get( i );

            if( attribute instanceof NumericAttribute )
            {
                description = description + "<b><font color=brown>N&nbsp;</font></b>";
            }
            else
            if( attribute instanceof CategoricalAttribute )
            {
                description = description + "<b><font color=brown>C&nbsp;</font></b>";
            }
            else
            if( attribute instanceof OrdinalAttribute )
            {
                description = description + "<b><font color=brown>O&nbsp;</font></b>";
            }
            description = description + "<a href=http://this?" + i + ">" + attribute.getName() + "</a><br>";
        }
        description = description + "</html>";
        return description;
    }

    // -----------------------------------------------------------------------
    //  Test
    // -----------------------------------------------------------------------
    /**
     * Test.
     *
     * @param args test parameters
     */
    public static void main(String[] args)
    {
        try
        {
            MiningDataSpecification i = MiningDataSpecification.getMiningDataSpecification ( "data/arff/weather.xml" );
            FileWriter writer = new FileWriter( "data/arff/weather2.arff" );
            writer.write( i.createArffDescription() );
            writer.flush();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

}

⌨️ 快捷键说明

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