📄 ageparser.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.
*/
/*
* Created on 2005/1/13
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package eti.bi.alphaminer.core.transform.filter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import com.prudsys.pdm.Core.MiningAttribute;
import com.prudsys.pdm.Core.MiningDataSpecification;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.NumericAttribute;
import com.prudsys.pdm.Input.MiningStoredData;
import com.prudsys.pdm.Input.MiningVector;
import com.prudsys.pdm.Input.Records.Arff.MiningArffStream;
import com.prudsys.pdm.Transform.MiningTransformationFactory;
import com.prudsys.pdm.Transform.MiningTransformationStep;
import com.prudsys.pdm.Transform.MultipleToMultiple.AddAttribute;
import com.prudsys.pdm.Transform.Special.CopyMiningStream;
import eti.bi.exception.SysException;
/**
* @author tyleung
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class AgeParser extends ETIFilter{
private NumericAttribute m_Attr = new NumericAttribute();
private String m_DateFormat;
private String m_SourceName;
public MiningStoredData transform (MiningStoredData inputMiningStoredData) throws SysException{
//----------------------------------------------------------
//add a new attribute to the mining stored data
AddAttribute addAttr = new AddAttribute();
MiningStoredData outputMiningStoredData;
MiningDataSpecification metaData;
try {
metaData = inputMiningStoredData.getMetaData();
MiningAttribute[] miningAttributes = metaData.getAttributesArray();
String [] newAttrsName = new String [metaData.getAttributesNumber()+1];
for(int i=0; i<newAttrsName.length-1; i++ ){
newAttrsName[i] = miningAttributes[i].getName();
}
newAttrsName[newAttrsName.length-1] = m_Attr.getName();
// addAttr.setSourceName(newAttrsName);
addAttr.setNewAttribute(m_Attr);
//create transformation step
MiningTransformationFactory mtf = new MiningTransformationFactory();
mtf.addMultipleToMultipleMapping(addAttr);
MiningTransformationStep mts = mtf.createMiningTransformationStep();
// Run transformation:
MiningStoredData msd = new MiningStoredData();
mts.transform(inputMiningStoredData, msd);
// System.out.println(msd);
//---------------------------------------------------------------------
//---------------------------------------------------------------------
//add value to the data
//prepare output mining stored data
outputMiningStoredData = new MiningStoredData();
outputMiningStoredData.updateSetMetaData(msd.getMetaData());
outputMiningStoredData.updateRemoveAllVectors();
//get the current time
Calendar currentTime = Calendar.getInstance();
//get the source attribute index
MiningAttribute source = msd.getMetaData().getMiningAttribute(m_SourceName);
int sourceAttrIndex = msd.getMetaData().getAttributeIndex(source);
ArrayList<MiningVector> miningVectors = new ArrayList<MiningVector>();
int numInstances = msd.getVectorsNumber();
MiningVector instance;
for(int i=0; i< numInstances; i++){
instance = (MiningVector)msd.get(i);
try{
if(!instance.isMissing(sourceAttrIndex)){
Object time = instance.getValueCategory(sourceAttrIndex).getValue();
if(time!=null)
instance.setValue(newAttrsName.length-1, getAge(currentTime, (String)time));
else
instance.setValue(newAttrsName.length-1, Double.NaN);
}else
instance.setValue(newAttrsName.length-1, Double.NaN);
}catch(ParseException e){
System.out.println(e.getMessage());
instance.setValue(newAttrsName.length-1, Double.NaN);
}
miningVectors.add(instance);
}
outputMiningStoredData.addAll(miningVectors);
} catch (MiningException e1) {
throw new SysException(e1);
}
return outputMiningStoredData;
}
public void setNewAttribute(NumericAttribute a_Attr){
m_Attr = a_Attr;
}
public NumericAttribute getNewAttribute(){
return m_Attr;
}
public void setNewAttributeName(String a_Name){
m_Attr.setName(a_Name);
}
public void setDateFormat(String a_DateFormat){
m_DateFormat = a_DateFormat;
}
public void setSourceName(String a_Source){
m_SourceName = a_Source;
}
private double getAge(Calendar a_CurrentTime, String a_InputDate)
throws ParseException{
double age = 0;
SimpleDateFormat formatter = new SimpleDateFormat(m_DateFormat);
Calendar inputDate = Calendar.getInstance();
inputDate.setTime(formatter.parse(a_InputDate));
if(!a_CurrentTime.after(inputDate))
return -1;
age = a_CurrentTime.get(Calendar.YEAR)- inputDate.get(Calendar.YEAR);
if(a_CurrentTime.get(Calendar.MONTH) < inputDate.get(Calendar.MONTH)){
age =age -1;
}
else if (a_CurrentTime.get(Calendar.MONTH) == inputDate.get(Calendar.MONTH)){
if(a_CurrentTime.get(Calendar.DAY_OF_MONTH) == inputDate.get(Calendar.DAY_OF_MONTH)){
age = age -1;
}
else if(a_CurrentTime.get(Calendar.DAY_OF_MONTH) == inputDate.get(Calendar.DAY_OF_MONTH)){
if(a_CurrentTime.get(Calendar.HOUR_OF_DAY) < inputDate.get(Calendar.HOUR_OF_DAY)){
age = age-1;
}
else if (a_CurrentTime.get(Calendar.HOUR_OF_DAY) == inputDate.get(Calendar.HOUR_OF_DAY)){
if(a_CurrentTime.get(Calendar.MINUTE) < inputDate.get(Calendar.MINUTE)){
age = age-1;
}
else if (a_CurrentTime.get(Calendar.MINUTE) == inputDate.get(Calendar.MINUTE)){
if(a_CurrentTime.get(Calendar.SECOND) < inputDate.get(Calendar.SECOND)){
age = age-1;
}
}
}
}
}
return age;
}
public static void main (String [] argv)throws Exception{
// Open 'iris.arff':
MiningArffStream arff = new MiningArffStream( "date.arff" );
// Display original stream:
// System.out.println( arff );
// 1. Transformation -> Copy:
MiningStoredData msd = new MiningStoredData();
(new CopyMiningStream()).transform(arff, msd);
// Display "transformed" stream:
System.out.println("1. Transformed by 'CopyMiningStream': ");
msd.reset();
System.out.println( msd );
// 2. Transformation -> FirstNSample
AgeParser sample = new AgeParser();
sample.setSourceName("date");
sample.setNewAttributeName("new-attribute");
sample.setDateFormat("yyyy/MM/dd");
msd = sample.transform(msd);
System.out.println(msd);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -