Advertisement
Guest User

RF

a guest
Apr 27th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.11 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package rf_test3;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.DataInput;
  10. import java.io.DataInputStream;
  11. import java.io.File;
  12. import java.io.FileInputStream;
  13. import java.io.FileNotFoundException;
  14. import java.io.FileReader;
  15. import java.io.IOException;
  16.  
  17.  
  18. import java.io.InputStreamReader;
  19. import java.util.ArrayList;
  20. import java.util.Iterator;
  21. import javax.xml.parsers.DocumentBuilder;
  22. import javax.xml.parsers.DocumentBuilderFactory;
  23. import org.json.simple.JSONArray;
  24. import org.json.simple.JSONObject;
  25. import org.json.simple.parser.JSONParser;
  26. import org.json.simple.parser.ParseException;
  27. import org.w3c.dom.Document;
  28. import org.w3c.dom.Element;
  29. import org.w3c.dom.Node;
  30. import org.w3c.dom.NodeList;
  31.  
  32. /**
  33. *
  34. * @author OvidiuS
  35. */
  36. public class setOfPatterns {
  37. ArrayList<Pattern> obj = new ArrayList<>();
  38.  
  39. public ArrayList<Pattern> readFromFile(String numef){
  40. obj = new ArrayList<>();
  41.  
  42. try{
  43.  
  44. FileInputStream fstream = new FileInputStream(numef);
  45. DataInputStream data = new DataInputStream(fstream);
  46. BufferedReader bf = new BufferedReader(new InputStreamReader(data));
  47.  
  48. String linie = bf.readLine();
  49. String []splited = linie.split(" ");
  50.  
  51. int nrObj = Integer.parseInt(splited[0]);
  52. int nrEl = Integer.parseInt(splited[1]);
  53.  
  54. for(int i = 0; i < nrObj; i++){
  55. linie = bf.readLine();
  56. splited = linie.split(" ");
  57. ArrayList<Double> dob = new ArrayList<>();
  58. for(int j = 0; j < nrEl; j++)
  59. dob.add(Double.valueOf(splited[j]));
  60. String nume = splited[nrEl];
  61. obj.add(new Pattern(dob, nume));
  62. }
  63.  
  64. }catch(Exception e){
  65. e.printStackTrace();
  66. }
  67.  
  68. return obj;
  69. }
  70.  
  71. public double distantaEuclidiana(Pattern a, Pattern b){
  72. double d = 0;
  73. for(int i = 0; i < a.values.size(); i++)
  74. d += (a.values.get(i) - b.values.get(i)) * (a.values.get(i) - b.values.get(i));
  75. return d;
  76. }
  77.  
  78. public double Mean(String nume, int nr){
  79. double mean = 0;
  80. double count = 0;
  81.  
  82. for(int i = 0; i < obj.size(); i++){
  83. if(obj.get(i).numeClasa.compareTo(nume) == 0){
  84. mean +=obj.get(i).values.get(nr);
  85. count ++;
  86. }
  87. }
  88. return mean / count;
  89. }
  90.  
  91. public Pattern centroid(String nume){
  92. if(obj.size() == 0)
  93. return new Pattern(0, nume);
  94. Pattern cg = new Pattern(obj.get(0).values.size(), nume);
  95. double count = 0;
  96. for(int i = 0; i < obj.size(); i++){
  97. if(obj.get(i).numeClasa.compareTo(nume) == 0){
  98. count ++;
  99. for(int j = 0; j < obj.get(i).values.size(); j++)
  100. cg.values.set(j, obj.get(i).values.get(j) + cg.values.get(j));
  101. }
  102. }
  103. if (count > 0)
  104. for(int i = 0; i < cg.values.size(); i++)
  105. cg.values.set(i, cg.values.get(i) / count);
  106. return cg;
  107. }
  108.  
  109. public String Recognise(ArrayList<Pattern> in, Pattern p){
  110. String clasa = "";
  111. double minim = Integer.MAX_VALUE;
  112. for(int i = 0; i < in.size(); i++)
  113. {
  114. double d = distantaEuclidiana(in.get(i), p);
  115. if(d < minim){
  116. minim = d;
  117. clasa = in.get(i).numeClasa;
  118. }
  119. }
  120. return clasa;
  121. }
  122.  
  123. public ArrayList<Pattern> readFromXMLFile(String numef){
  124. obj = new ArrayList<>();
  125.  
  126. try {
  127.  
  128. File fXmlFile = new File(numef);
  129. DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  130. DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
  131. Document doc = dBuilder.parse(fXmlFile);
  132.  
  133. doc.getDocumentElement().normalize();
  134.  
  135.  
  136.  
  137. NodeList nList = doc.getElementsByTagName("pattern");
  138.  
  139. for (int temp = 0; temp < nList.getLength(); temp++) {
  140.  
  141. Node nNode = nList.item(temp);
  142.  
  143. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  144.  
  145. Element eElement = (Element) nNode;
  146.  
  147. String nume = eElement.getAttribute("class");
  148. String values = eElement.getAttribute("features");
  149. String []splited = values.split(",");
  150.  
  151. ArrayList<Double> dob = new ArrayList<>();
  152. for(int i = 0; i < splited.length; i++)
  153. dob.add(Double.valueOf(splited[i]));
  154. obj.add(new Pattern(dob, nume));
  155. }
  156. }
  157. } catch (Exception e) {
  158. e.printStackTrace();
  159. }
  160.  
  161. return obj;
  162.  
  163. }
  164.  
  165. public void readFromJSONFile(String numef){
  166. obj = new ArrayList<>();
  167.  
  168. JSONParser parser = new JSONParser();
  169.  
  170. try {
  171. Object object = parser.parse(new FileReader(numef));
  172.  
  173. JSONObject jsonObject = (JSONObject) object;
  174.  
  175. int i = 1;
  176. do{
  177. JSONObject pattern = (JSONObject) jsonObject.get("sample " + i);
  178. String name = (String) pattern.get("class");
  179. String values = (String) pattern.get("features");
  180. System.out.println("name: " + name);
  181. System.out.println("valori: " + values);
  182.  
  183. i++;
  184. }while(i <=18);
  185. } catch (FileNotFoundException e) {
  186. e.printStackTrace();
  187. } catch (IOException e) {
  188. e.printStackTrace();
  189. } catch (ParseException e) {
  190. e.printStackTrace();
  191. }
  192. }
  193.  
  194. public ArrayList<Pattern> jsonFile(String numef){
  195. BufferedReader br = null;
  196. FileReader fr = null;
  197.  
  198. try {
  199.  
  200. fr = new FileReader(numef);
  201. br = new BufferedReader(fr);
  202.  
  203. String sCurrentLine, allLines = "";
  204.  
  205. br = new BufferedReader(new FileReader(numef));
  206.  
  207. while ((sCurrentLine = br.readLine()) != null) {
  208. sCurrentLine = sCurrentLine.replace("{", "");
  209. sCurrentLine = sCurrentLine.replace("}", "");
  210. sCurrentLine = sCurrentLine.replace("\"", "");
  211. sCurrentLine = sCurrentLine.replace(",", "");
  212. sCurrentLine = sCurrentLine.replace(":", "");
  213. sCurrentLine = sCurrentLine.replace("class", "");
  214. sCurrentLine = sCurrentLine.replace("features", "");
  215. sCurrentLine = sCurrentLine.trim();
  216. allLines +=sCurrentLine + "\n";
  217. }
  218. allLines = allLines.replaceAll("(?m)^sample.*", "");
  219. allLines = allLines.replaceAll("(?m)^[ \t]*\r?\n", "");
  220. String []splited = allLines.split("\n");
  221.  
  222. System.out.println(allLines);
  223.  
  224. for(int i = 0; i < splited.length; i = i + 2){
  225. String nume = splited[i];
  226. String values = splited[i+1];
  227. String []split = values.split(" ");
  228. ArrayList <Double> dob = new ArrayList<>();
  229. for(int j = 0; j <split.length; j++)
  230. dob.add(Double.valueOf(split[j]));
  231. obj.add(new Pattern(dob, nume));
  232. }
  233.  
  234. } catch (IOException e) {
  235.  
  236. e.printStackTrace();
  237.  
  238. } finally {
  239.  
  240. try {
  241.  
  242. if (br != null)
  243. br.close();
  244.  
  245. if (fr != null)
  246. fr.close();
  247.  
  248. } catch (IOException ex) {
  249.  
  250. ex.printStackTrace();
  251.  
  252. }
  253.  
  254. }
  255. return obj;
  256. }
  257.  
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement