Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package rf_test3;
- import java.io.BufferedReader;
- import java.io.DataInput;
- import java.io.DataInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Iterator;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import org.json.simple.JSONArray;
- import org.json.simple.JSONObject;
- import org.json.simple.parser.JSONParser;
- import org.json.simple.parser.ParseException;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- /**
- *
- * @author OvidiuS
- */
- public class setOfPatterns {
- ArrayList<Pattern> obj = new ArrayList<>();
- public ArrayList<Pattern> readFromFile(String numef){
- obj = new ArrayList<>();
- try{
- FileInputStream fstream = new FileInputStream(numef);
- DataInputStream data = new DataInputStream(fstream);
- BufferedReader bf = new BufferedReader(new InputStreamReader(data));
- String linie = bf.readLine();
- String []splited = linie.split(" ");
- int nrObj = Integer.parseInt(splited[0]);
- int nrEl = Integer.parseInt(splited[1]);
- for(int i = 0; i < nrObj; i++){
- linie = bf.readLine();
- splited = linie.split(" ");
- ArrayList<Double> dob = new ArrayList<>();
- for(int j = 0; j < nrEl; j++)
- dob.add(Double.valueOf(splited[j]));
- String nume = splited[nrEl];
- obj.add(new Pattern(dob, nume));
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- return obj;
- }
- public double distantaEuclidiana(Pattern a, Pattern b){
- double d = 0;
- for(int i = 0; i < a.values.size(); i++)
- d += (a.values.get(i) - b.values.get(i)) * (a.values.get(i) - b.values.get(i));
- return d;
- }
- public double Mean(String nume, int nr){
- double mean = 0;
- double count = 0;
- for(int i = 0; i < obj.size(); i++){
- if(obj.get(i).numeClasa.compareTo(nume) == 0){
- mean +=obj.get(i).values.get(nr);
- count ++;
- }
- }
- return mean / count;
- }
- public Pattern centroid(String nume){
- if(obj.size() == 0)
- return new Pattern(0, nume);
- Pattern cg = new Pattern(obj.get(0).values.size(), nume);
- double count = 0;
- for(int i = 0; i < obj.size(); i++){
- if(obj.get(i).numeClasa.compareTo(nume) == 0){
- count ++;
- for(int j = 0; j < obj.get(i).values.size(); j++)
- cg.values.set(j, obj.get(i).values.get(j) + cg.values.get(j));
- }
- }
- if (count > 0)
- for(int i = 0; i < cg.values.size(); i++)
- cg.values.set(i, cg.values.get(i) / count);
- return cg;
- }
- public String Recognise(ArrayList<Pattern> in, Pattern p){
- String clasa = "";
- double minim = Integer.MAX_VALUE;
- for(int i = 0; i < in.size(); i++)
- {
- double d = distantaEuclidiana(in.get(i), p);
- if(d < minim){
- minim = d;
- clasa = in.get(i).numeClasa;
- }
- }
- return clasa;
- }
- public ArrayList<Pattern> readFromXMLFile(String numef){
- obj = new ArrayList<>();
- try {
- File fXmlFile = new File(numef);
- DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
- Document doc = dBuilder.parse(fXmlFile);
- doc.getDocumentElement().normalize();
- NodeList nList = doc.getElementsByTagName("pattern");
- for (int temp = 0; temp < nList.getLength(); temp++) {
- Node nNode = nList.item(temp);
- if (nNode.getNodeType() == Node.ELEMENT_NODE) {
- Element eElement = (Element) nNode;
- String nume = eElement.getAttribute("class");
- String values = eElement.getAttribute("features");
- String []splited = values.split(",");
- ArrayList<Double> dob = new ArrayList<>();
- for(int i = 0; i < splited.length; i++)
- dob.add(Double.valueOf(splited[i]));
- obj.add(new Pattern(dob, nume));
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return obj;
- }
- public void readFromJSONFile(String numef){
- obj = new ArrayList<>();
- JSONParser parser = new JSONParser();
- try {
- Object object = parser.parse(new FileReader(numef));
- JSONObject jsonObject = (JSONObject) object;
- int i = 1;
- do{
- JSONObject pattern = (JSONObject) jsonObject.get("sample " + i);
- String name = (String) pattern.get("class");
- String values = (String) pattern.get("features");
- System.out.println("name: " + name);
- System.out.println("valori: " + values);
- i++;
- }while(i <=18);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ParseException e) {
- e.printStackTrace();
- }
- }
- public ArrayList<Pattern> jsonFile(String numef){
- BufferedReader br = null;
- FileReader fr = null;
- try {
- fr = new FileReader(numef);
- br = new BufferedReader(fr);
- String sCurrentLine, allLines = "";
- br = new BufferedReader(new FileReader(numef));
- while ((sCurrentLine = br.readLine()) != null) {
- sCurrentLine = sCurrentLine.replace("{", "");
- sCurrentLine = sCurrentLine.replace("}", "");
- sCurrentLine = sCurrentLine.replace("\"", "");
- sCurrentLine = sCurrentLine.replace(",", "");
- sCurrentLine = sCurrentLine.replace(":", "");
- sCurrentLine = sCurrentLine.replace("class", "");
- sCurrentLine = sCurrentLine.replace("features", "");
- sCurrentLine = sCurrentLine.trim();
- allLines +=sCurrentLine + "\n";
- }
- allLines = allLines.replaceAll("(?m)^sample.*", "");
- allLines = allLines.replaceAll("(?m)^[ \t]*\r?\n", "");
- String []splited = allLines.split("\n");
- System.out.println(allLines);
- for(int i = 0; i < splited.length; i = i + 2){
- String nume = splited[i];
- String values = splited[i+1];
- String []split = values.split(" ");
- ArrayList <Double> dob = new ArrayList<>();
- for(int j = 0; j <split.length; j++)
- dob.add(Double.valueOf(split[j]));
- obj.add(new Pattern(dob, nume));
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (br != null)
- br.close();
- if (fr != null)
- fr.close();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- return obj;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement