Java and Weka: Creating a string attribute - java

I'm trying to create a Java program that converts a text file to an ARFF file for Weka. Somehow my name attribute is set to numerical, but it should be set to a string. I tried everything, I tried fixing it fixing
attr.add(new Attribute("name"));
to
attr.add(new Attribute("name",true));
But when I run it, it prints the names as number (which is in the 2nd column)
1,0,?,?,?
1000,1,?,?,?
1002,2,?,?,?
2,3,?,?,?
3000,4,?,?,?
What am I doing wrong?
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instance;
import java.util.*;
import weka.core.Instances;
import weka.core.converters.ArffSaver;
public class WekaCreateARFF {
private static final String FILENAME = "Some File";
public static void main(String[] args) throws IOException {
ArrayList<String> input = new ArrayList<String>();
ArrayList<Attribute> attr = new ArrayList<Attribute>();
Instances dataset;
double [] values;
BufferedReader br = null;
FileReader fr = null;
String date = null;
double id;
String n = null;
Instance inst = new DenseInstance(5);
List nominal_state = new ArrayList(5);
nominal_state.add("CA");
nominal_state.add("NC");
nominal_state.add("TX");
nominal_state.add("SC");
nominal_state.add("NY");
List nominal_party = new ArrayList(2);
nominal_party.add("republican");
nominal_party.add("democrat");
attr.add(new Attribute("id"));
attr.add(new Attribute("name",true));
attr.add(new Attribute("political party", nominal_party));
attr.add(new Attribute("state", nominal_state));
attr.add(new Attribute("birth date", date));
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String entry;
dataset = new Instances("SimpleARFF",attr,0);
values = new double[dataset.numAttributes()];
while ((entry = br.readLine()) != null) {
//System.out.println(entry);
input.add(entry);
for (int i = 0; i<5; i++ ) {
String[] parts = entry.split(",");
String part1 = parts[0];
String name = parts[1];
id = Double.parseDouble(part1);
inst.setValue(attr.get(0), id);
inst.setValue(attr.get(1), name);
}
System.out.println(inst);
dataset.add(new DenseInstance(1.0, values));
}
//System.out.println(dataset);
//ArffSaver arff = new ArffSaver();
//arff.setInstances(dataset);
//arff.setFile(new File("Simple.arff"));
//arff.writeBatch();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}

You probably want this constructor:
http://weka.sourceforge.net/doc.dev/weka/core/Attribute.html#Attribute-java.lang.String-boolean-
That is, you essentially have to add a boolean flag to tell Weka that you want a String attribute, and not a numeric attribute (the default):
new Attribute("blah", true)
should give you a String-attribute.

Related

how to use relative file path to read a file in java?

file path is not working, input1.txt is located in the same directory as library.java.
What should i do to correct it ?
How should i give path so that it read thr text file ?
package SimpleLibrarySystem;
import java.time.LocalDateTime;
import java.util.*;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
public class Library
{
ArrayList <Book> var = new ArrayList<Book>();
HashMap<Book, LocalDateTime> var1 = new HashMap<Book, LocalDateTime>();
public Library(String person, LocalDateTime time)
{
try{
File myfile = new File("input1.txt") ;
Scanner br = new Scanner(myfile);
String line = br.nextLine();
while ((line != null))
{
String a = line;
line = br.nextLine();
String b = line;
Book a1 = new Book(a,b,person);
Book a2 = new Book (a,b, "");
var.add(a2);
var1.put(a1,time);
//System.out.println(a + " "+ b);
line = br.nextLine();
}
br.close();
}
catch (Exception e)
{
System.out.println("not working");
}
}
}
with code:
public class Main{
public static void main(String[] args) {
int counter = 0;
try (FileReader fileReader = new FileReader("src/file.txt"); Scanner sc = new Scanner(fileReader)) {
while (sc.hasNextLine()) {
++counter;
sc.nextLine();
}
} catch (Exception e) {
throw new IllegalStateException(e.getMessage());
}
System.out.println(counter);
}
}
checkout: how to read file in Java

Problem with Parsing CSV and Marshalling to XML

I am trying to solve an issue with Parsing data from CSV file and Marshalling to XML. The code I wrote works almost flawless, but output is not ok, since program outputs only last line into the XML. Can please someone help me where I went wrong?
I have tried to put whole new Object into the While loop, but then it outputs last line into the XML for n-times (if CSV file has 24 lines, it will output the last line 24 times into XML)....
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.regex.Pattern;
import generated.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
public class eSpremnicaHandel {
public void eSpremnicaHandel(String csvFile) {
String line = "";
String cvsSplitBy = "|";
RegistriranaPosiljka regpos = new RegistriranaPosiljka();
Naslovnik nasl = new Naslovnik();
Posiljka pos = new Posiljka();
Storitev sto = new Storitev();
Oddaja oddaja = new Oddaja();
ArrayOfRegPosiljka regp = new ArrayOfRegPosiljka();
oddaja.setWPID("CertSubject");
oddaja.setKOMID(21553);
oddaja.setSTODD(0);
oddaja.setPOGID(2427);
oddaja.setPODID(0);
oddaja.setPOSID("7733");
oddaja.setRegistriranePosiljke(regp);
ObjectFactory fc = new ObjectFactory();
Oddaja odd = fc.createOddaja();
odd.setWPID(oddaja.getWPID());
odd.setSTODD(oddaja.getSTODD());
odd.setKOMID(oddaja.getKOMID());
odd.setPOGID(oddaja.getPOGID());
odd.setPODID(oddaja.getPODID());
odd.setPOSID(oddaja.getPOSID());
regp.getREGPOS().add(regpos);
odd.getRegistriranePosiljke();
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
// use delimiter as separator
String[] podatki = line.split(Pattern.quote(cvsSplitBy));
regpos.setZAPST(Integer.parseInt(podatki[0]));
regpos.setREG(podatki[1]);
nasl.setNAZ(podatki[2]);
nasl.setPOSTST(podatki[3]);
pos.setMAS(new BigDecimal(podatki[4]));
sto.setNAZ(podatki[7]);
regpos.setNAS(nasl);
}
} catch (IOException e) {
e.printStackTrace();
}
try{
JAXBContext jc = JAXBContext.newInstance("generated");
JAXBElement<Oddaja> element = fc.createOddaja(oddaja);
Marshaller ms = jc.createMarshaller();
ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
ms.marshal(element, System.out);
ms.marshal(element, new File("src/eSpremnicaXML/test2.xml"));
} catch(Exception e) {
e.printStackTrace();
}
}
}
Please try this version of method. Main change is creating new object of RegistriranaPosiljka is moved under loop
public static void eSpremnicaHandel(String csvFile) {
String line = "";
String cvsSplitBy = "|";
Oddaja oddaja = new Oddaja();
ArrayOfRegPosiljka regp = new ArrayOfRegPosiljka();
oddaja.setWPID("CertSubject");
oddaja.setKOMID(21553);
oddaja.setSTODD(0);
oddaja.setPOGID(2427);
oddaja.setPODID(0);
oddaja.setPOSID("7733");
oddaja.setRegistriranePosiljke(regp);
ObjectFactory fc = new ObjectFactory();
Oddaja odd = fc.createOddaja();
odd.setWPID(oddaja.getWPID());
odd.setSTODD(oddaja.getSTODD());
odd.setKOMID(oddaja.getKOMID());
odd.setPOGID(oddaja.getPOGID());
odd.setPODID(oddaja.getPODID());
odd.setPOSID(oddaja.getPOSID());
odd.setRegistriranePosiljke(oddaja.getRegistriranePosiljke());
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
RegistriranaPosiljka regpos = new RegistriranaPosiljka();
// use delimiter as separator
String[] podatki = line.split(Pattern.quote(cvsSplitBy));
regpos.setZAPST(Integer.parseInt(podatki[0]));
regpos.setREG(podatki[1]);
Naslovnik nasl = new Naslovnik();
nasl.setNAZ(podatki[2]);
nasl.setPOSTST(podatki[3]);
Posiljka pos = new Posiljka();
pos.setMAS(new BigDecimal(podatki[4]));
Storitev sto = new Storitev();
sto.setNAZ(podatki[7]);
regpos.setNAS(nasl);
regp.getREGPOS().add(regpos);
}
} catch (IOException e) {
e.printStackTrace();
}
try{
JAXBContext jc = JAXBContext.newInstance("generated");
JAXBElement<Oddaja> element = fc.createOddaja(oddaja);
Marshaller ms = jc.createMarshaller();
ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
ms.marshal(element, System.out);
ms.marshal(element, new File("src/eSpremnicaXML/test2.xml"));
} catch(Exception e) {
e.printStackTrace();
}
}

Want to find content difference between two text files with java

I have two text files,
a.txt
b.txt
Each text files contains some file paths. b.txt contains some more file paths than a.txt. I would like to determine which paths are added and which are removed from a.txt so that it corresponds to paths in b.txt.
For example,
abc.txt contains
E:\Users\Documents\hello\a.properties
E:\Users\Documents\hello\b.properties
E:\Users\Documents\hello\c.properties
and xyz.txt contains
E:\Users\Documents\hello\a.properties
E:\Users\Documents\hello\c.properties
E:\Users\Documents\hello\g.properties
E:\Users\Documents\hello\h.properties
Now how to find that g.prop and h.prop are added and b.prop is removed?
Could anyone explain how it is done? I could only find how to check for identical contents.
The below code will serve your purpose irrespective of the content of the file.
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Test {
public Test(){
System.out.println("Test.Test()");
}
public static void main(String[] args) throws Exception {
BufferedReader br1 = null;
BufferedReader br2 = null;
String sCurrentLine;
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
br1 = new BufferedReader(new FileReader("test.txt"));
br2 = new BufferedReader(new FileReader("test2.txt"));
while ((sCurrentLine = br1.readLine()) != null) {
list1.add(sCurrentLine);
}
while ((sCurrentLine = br2.readLine()) != null) {
list2.add(sCurrentLine);
}
List<String> tmpList = new ArrayList<String>(list1);
tmpList.removeAll(list2);
System.out.println("content from test.txt which is not there in test2.txt");
for(int i=0;i<tmpList.size();i++){
System.out.println(tmpList.get(i)); //content from test.txt which is not there in test2.txt
}
System.out.println("content from test2.txt which is not there in test.txt");
tmpList = list2;
tmpList.removeAll(list1);
for(int i=0;i<tmpList.size();i++){
System.out.println(tmpList.get(i)); //content from test2.txt which is not there in test.txt
}
}
}
The memory will be a problem as you need to load both files into the program.
I am using HashSet to ignore duplicates.Try this:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashSet;
public class FileReader1 {
public static void main(String args[]) {
String filename = "abc.txt";
String filename2 = "xyz.txt";
HashSet <String> al = new HashSet<String>();
HashSet <String> al1 = new HashSet<String>();
HashSet <String> diff1 = new HashSet<String>();
HashSet <String> diff2 = new HashSet<String>();
String str = null;
String str2 = null;
try {
BufferedReader in = new BufferedReader(new FileReader(filename));
while ((str = in.readLine()) != null) {
al.add(str);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
BufferedReader in = new BufferedReader(new FileReader(filename2));
while ((str2 = in.readLine()) != null) {
al1.add(str2);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
for (String str3 : al) {
if (!al1.contains(str3)) {
diff1.add(str3);
}
}
for (String str5 : al1) {
if (!al.contains(str5)) {
diff2.add(str5);
}
}
for (String str4 : diff1) {
System.out.println("Removed Path: "+str4);
}
for (String str4 : diff2) {
System.out.println("Added Path: "+str4);
}
}
}
Output:
Removed Path: E:\Users\Documents\hello\b.properties
Added Path: E:\Users\Documents\hello\h.properties
Added Path: E:\Users\Documents\hello\g.properties
You can simple do follow
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(final String[] args) throws IOException {
final Path firstFile = Paths.get("/home/src/main/resources/a.txt");
final Path secondFile = Paths.get("/home/src/main/resources/b.txt");
final List<String> firstFileContent = Files.readAllLines(firstFile,
Charset.defaultCharset());
final List<String> secondFileContent = Files.readAllLines(secondFile,
Charset.defaultCharset());
System.out.println(diffFiles(firstFileContent, secondFileContent));
System.out.println(diffFiles(secondFileContent, firstFileContent));
}
private static List<String> diffFiles(final List<String> firstFileContent,
final List<String> secondFileContent) {
final List<String> diff = new ArrayList<String>();
for (final String line : firstFileContent) {
if (!secondFileContent.contains(line)) {
diff.add(line);
}
}
return diff;
}
}
Compare files [Scanner and ArrayList]:
protected static void compareFiles(String firstFile, String secondFile)
throws Exception {
Scanner x = new Scanner(new File(firstFile));
List<String> list1 = getScannerList(x);
x = new Scanner(new File(secondFile));
List<String> list2 = getScannerList(x);
x.close();
System.out.println("File Extras");
printLnList(listExtras(list1, new ArrayList<String>(list2)));
System.out.println("File Removals");
printLnList(listExtras(list2, list1));
}
protected static List<String> listExtras(List<String> list1,
List<String> list2) throws Exception {
list2.removeAll(list1);
return list2;
}
protected static List<String> getScannerList(Scanner sc) throws Exception {
List<String> scannerList = new ArrayList<String>();
while (sc.hasNext())
scannerList.add(sc.nextLine());
return scannerList;
}
protected static void printLnList(List<String> list) {
for (String string : list)
System.out.println(string);
}
Program output:
File Extras
E:\Users\Documents\hello\g.properties
E:\Users\Documents\hello\h.properties
File Removals
E:\Users\Documents\hello\b.properties

How can i remove a word/line and replace it with a new one in a txt file (Java)?

For example we have a .txt file:
Name smth
Year 2012
Copies 1
And I want to replace it with that:
Name smth
Year 2012
Copies 0
Using java.io.*.
Here is the code that does that. Let me know if you have any question.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.LinkedHashMap;
import java.util.Map;
public class Test2 {
Map<String, String> someDataStructure = new LinkedHashMap<String, String>();
File fileDir = new File("c:\\temp\\test.txt");
public static void main(String[] args) {
Test2 test = new Test2();
try {
test.readFileIntoADataStructure();
test.writeFileFromADataStructure();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
private void readFileIntoADataStructure() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(fileDir)));
String line;
while ((line = in.readLine()) != null) {
if (line != null && !line.trim().isEmpty()) {
String[] keyValue = line.split(" ");
// Do you own index and null checks here this is just a sample
someDataStructure.put(keyValue[0], keyValue[1]);
}
}
in.close();
}
private void writeFileFromADataStructure() throws IOException {
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(fileDir)));
for (String key : someDataStructure.keySet()) {
// Apply whatever business logic you want to apply here
myBusinessMethod(key);
out.write(key + " " + someDataStructure.get(key) + "\n");
out.append("\r\n");
out.append("\r\n");
}
out.flush();
out.close();
}
private String myBusinessMethod(String data) {
if (data.equalsIgnoreCase("Copies")) {
someDataStructure.put(data, "0");
}
return data;
}
}
Read your original text file line by line and separate them into string tokens delimited by spaces for output, then when the part you want replaced is found (as a string), replace the output to what you want it to be. Adding the false flag to the filewrite object ("filename.txt", false) will overwrite and not append to the file allowing you to replace the contents of the file.
this is the code to do that
try {
String sCurrentLine;
BufferedReader br = new BufferedReader(new FileReader("yourFolder/theinputfile.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("yourFolder/theinputfile.txt" , false));
while ((sCurrentLine = br.readLine()) != null) {
if(sCurrentLine.indexOf("Copies")>=0){
bw.write("Copies 0")
}
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close()bw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
hopefully that help

Run two classes one after the other

How to run two classes in which one gives some data in a textfile & the other should take that file and process it?
I have two Java files. File1 processes something and outputs a text file. File2 should take that text file and process it to create a final output.
My requirement is to have two independent java files that work together.
File1
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Map;
import java.util.TreeMap;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
public class FlatFileParser
{
public static void main(String[] args)
{
try
{
// The stream we're reading from
BufferedReader in;
List<String> ls = new ArrayList<String>();
BufferedWriter out1 = new BufferedWriter(new FileWriter("inValues.txt" , true ));
BufferedReader out11 = new BufferedReader(new FileReader("inValues.txt"));
// Return value of next call to next()
String nextline;
String line="";
if (args[0].equals("1"))
{
in = new BufferedReader(new FileReader(args[1]));
nextline = in.readLine();
while(nextline != null)
{
nextline = nextline.replaceAll("\\<packet","\n<packet");
System.out.println(nextline);
nextline = in.readLine();
}
in.close();
}
else
{
in = new BufferedReader(new FileReader(args[1]));
nextline = in.readLine();
HashMap<String,String> inout = new HashMap<String,String>();
while(nextline != null)
{
try
{
if (nextline.indexOf("timetracker")>0)
{
String from = "";
String indate = "";
if (nextline.indexOf("of in")>0)
{
int posfrom = nextline.indexOf("from");
int posnextAt = nextline.indexOf("#", posfrom);
int posts = nextline.indexOf("timestamp");
from = nextline.substring(posfrom+5,posnextAt);
indate = nextline.substring(posts+11, posts+23);
String dd = indate.split(" ")[1];
String key = dd+"-"+from+"-"+indate;
//String key = from+"-"+indate;
String intime = "-in-"+nextline.substring(posts+24, posts+35);
inout.put(key, intime);
}
else if (nextline.indexOf("of out")>0)
{
int posfrom = nextline.indexOf("from");
int posnextAt = nextline.indexOf("#", posfrom);
int posts = nextline.indexOf("timestamp");
from = nextline.substring(posfrom+5,posnextAt);
indate = nextline.substring(posts+11, posts+23);
String dd = indate.split(" ")[1];
String key = dd+"-"+from+"-"+indate;
String outtime = "-out-"+nextline.substring(posts+24, posts+35);
if (inout.containsKey(key))
{
String val = inout.get(key);
if (!(val.indexOf("out")>0))
inout.put(key, val+outtime);
}
else
{
inout.put(key, outtime);
}
}
}
}
catch(Exception e)
{
System.err.println(nextline);
System.err.println(e.getMessage());
}
nextline = in.readLine();
}
in.close();
for(String key: inout.keySet())
{
String val = inout.get(key);
out1.write(key+" , "+val+"\n");
}
out1.close();
}
}
catch (IOException e)
{
throw new IllegalArgumentException(e);
}
}
File2
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.io.File;
import java.io.FileReader;
public class RecordParser
{
private static BufferedReader reader;
private List<Person> resource;
private List<String> finalRecords;
public RecordParser(BufferedReader reader)
{
this.reader = reader;
this.resource = new ArrayList<Person>();
this.finalRecords = new ArrayList<String>();
}
public void execute() throws IOException
{
String line = null;
while ((line = reader.readLine()) != null)
{
String[] parts = line.split(" , ");
addPerson(new Person(parts[0]));
if ((parts[1].contains("-in-")) && (parts[1].contains("-out-")))
{
String[] inout = parts[1].split("-out-");
Person person = getPerson(parts[0]);
person.setInTime(inout[0]);
person.setOutTime("-out-" + inout[1]);
}
else if (parts[1].contains("-in-"))
{
Person person = getPerson(parts[0]);
person.setInTime(parts[1]);
}
else
{
Person person = getPerson(parts[0]);
person.setOutTime(parts[1]);
}
}
// finalRecords the resource to the String list
for (Person p : resource)
{
finalRecords.add(p.getPerson());
}
}
private void addPerson(Person person)
{
for (Person p : resource)
{
if (p.getNameDate().equals(person.getNameDate()))
{
return;
}
}
resource.add(person);
}
private Person getPerson(String nameDate)
{
for (Person p : resource)
{
if (p.getNameDate().equals(nameDate))
{
return p;
}
}
return null;
}
public List<String> getfinalRecords()
{
return finalRecords;
}
public static void main(String[] args)
{
try {
BufferedReader reader = new BufferedReader(new FileReader("sample.txt"));
RecordParser recordParser = new RecordParser(reader);
recordParser.execute();
for (String s : recordParser.getfinalRecords())
{
System.out.println(s);
}
reader.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
public class Person
{
private String nameDate;
private String inTime;
private String outTime;
public Person (String nameDate)
{
this.nameDate = nameDate;
this.inTime = "missing in";
this.outTime = "missing out";
}
public void setInTime(String inTime)
{
this.inTime = inTime;
}
public void setOutTime(String outTime)
{
this.outTime = outTime;
}
public String getNameDate()
{
return nameDate;
}
public String getPerson()
{
StringBuilder builder = new StringBuilder();
builder.append(nameDate);
builder.append(" , ");
builder.append(inTime);
builder.append(" , ");
builder.append(outTime);
return builder.toString();
}
}
}
I want to be able to import the values from inValues.txt (created in File1) and process them in File2.
Create a batch/sh file and run one java program after the other. If you want to pass the file details to the second program you can do that by providing a run time argument.
on windows:
java -classpath .;yourjars FlatFileParser
java -classpath .;yourjars RecordParser {optionalfiledetails}
on linux
java -classpath .:yourjars FlatFileParser
java -classpath .:yourjars RecordParser {optionalfiledetails}

Categories