Advertisement
lukibeni

lastMI

Dec 8th, 2015
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.83 KB | None | 0 0
  1. import org.jsoup.Jsoup;
  2. import org.jsoup.nodes.Document;
  3. import sun.org.mozilla.javascript.internal.Function;
  4.  
  5. import java.io.*;
  6. import java.nio.charset.StandardCharsets;
  7. import java.nio.file.Files;
  8. import java.nio.file.Paths;
  9. import java.util.*;
  10.  
  11. /**
  12. * Created by lukibeni on 2015.12.08..
  13. */
  14. public class Main {
  15.  
  16. static Map<String, Set<String>> uberData = new TreeMap<>();
  17.  
  18. public static void main(String[] args) {
  19. initNotIngredients();
  20.  
  21. try {
  22. FileInputStream fileInputStream = new FileInputStream("/home/lukibeni/magyaros");
  23. ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
  24. uberData = (Map<String, Set<String>>)objectInputStream.readObject();
  25.  
  26. } catch (FileNotFoundException e) {
  27. e.printStackTrace();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. } catch (ClassNotFoundException e) {
  31. e.printStackTrace();
  32. }
  33.  
  34. /*File[] filesInFolder = new File("/home/lukibeni/www.mindmegette.hu/").listFiles();
  35. List<String> filesNames = new ArrayList<>();
  36. if (filesInFolder != null) {
  37. for (File file : filesInFolder) {
  38. if (!file.isDirectory()) {
  39. filesNames.add(file.getName());
  40. }
  41. }
  42. }
  43.  
  44. int i = 0;
  45. for (String filename : filesNames) {
  46. if (filename.matches(".+recept\\.html")) {
  47. try {
  48. byte[] encoded = Files.readAllBytes(Paths.get("/home/lukibeni/www.mindmegette.hu/" + filename));
  49.  
  50. String tmp = new String(encoded, StandardCharsets.UTF_8);
  51. Document doc = Jsoup.parse(tmp);
  52. String set = doc.body().text();
  53. String[] split1 = set.split("Hozzávalók: ");
  54. String[] split2;
  55. Set<String> ingredients = new HashSet<>();
  56. if (split1.length >= 2) {
  57. split2 = split1[1].split(" Bevásárlólista");
  58. if (split2.length >= 2) {
  59. String[] split3 = split2[0].split(" ");
  60. for (String ingredient : split3) {
  61. if (!ingredient.matches(".*\\d+.*") && !notIngredients.contains(ingredient)) {
  62. ingredients.add(ingredient);
  63. }
  64. }
  65. }
  66. }
  67.  
  68. Set<String> tags = new HashSet<>();
  69. split1 = set.split("Elküldöm ");
  70. if (split1.length >= 2) {
  71. split2 = split1[1].split(" Ajánlott");
  72. if (split2.length >= 2) {
  73. String[] split3 = split2[0].split(" ");
  74. for (String tag : split3) {
  75. tags.add(tag.toUpperCase());
  76. }
  77. }
  78. }
  79.  
  80. if (tags.contains("MAGYAR") || tags.contains("MAGYAROS")) {
  81. try {
  82. uberData.put(filename.split("\\.")[0], ingredients);
  83. } catch (ArrayIndexOutOfBoundsException e) {
  84. System.out.println(filename);
  85. }
  86. }
  87. } catch (IOException e) {
  88. e.printStackTrace();
  89. }
  90. }
  91. }
  92.  
  93. System.out.println("#Receipt: " + filesNames.size() + ", Parsed data: " + uberData.size());
  94.  
  95. try{
  96. FileOutputStream fos= new FileOutputStream("/home/lukibeni/magyaros");
  97. ObjectOutputStream oos= new ObjectOutputStream(fos);
  98. oos.writeObject(uberData);
  99. oos.close();
  100. fos.close();
  101. }catch(IOException ioe){
  102. ioe.printStackTrace();
  103. }
  104.  
  105. */
  106.  
  107. for (Set<String> value : uberData.values()) {
  108. for (String ingredient : value) {
  109. if (ingredientCount.containsKey(ingredient)) {
  110. ingredientCount.put(ingredient, ingredientCount.get(ingredient) + 1);
  111. } else {
  112. ingredientCount.put(ingredient, 1);
  113. }
  114. }
  115. }
  116.  
  117. /*for (String key : ingredientCount.keySet()) {
  118. if (ingredientCount.get(key) > 200 && !notIngredients.contains(key)) {
  119. System.out.println(key + ": " + ingredientCount.get(key));
  120. }
  121. }*/
  122.  
  123. List<String> keys = new ArrayList<>(ingredientCount.keySet());
  124.  
  125. Collections.sort(keys, new Comparator<String>() {
  126. @Override
  127. public int compare(String o1, String o2) {
  128. return ingredientCount.get(o2).compareTo(ingredientCount.get(o1));
  129. }
  130. });
  131.  
  132. int y = 0;
  133. for (String key : keys) {
  134. if (!notIngredients.contains(key) && ingredientCount.get(key) > 50) {
  135. ++y;
  136. System.out.println(key + ": " + ingredientCount.get(key));
  137. }
  138. }
  139. System.out.println(y);
  140. }
  141.  
  142. public static Set<String> notIngredients;
  143.  
  144. public static Map<String, Integer> ingredientCount = new HashMap<>();
  145.  
  146. private static void initNotIngredients() {
  147. notIngredients = new HashSet<>();
  148. String[] ni = {
  149. "kis",
  150. "nagy",
  151. "kevés",
  152. "sok",
  153. "ek",
  154. "csipet",
  155. "dl",
  156. "ml",
  157. "l",
  158. "g",
  159. "dkg",
  160. "kg",
  161. "fej",
  162. "db",
  163. "púpozott",
  164. "evőkanál",
  165. "kávéskanál",
  166. "hegyes",
  167. "erős",
  168. "teáskanál",
  169. "kanál",
  170. "friss",
  171. "őrölt",
  172. "szelet",
  173. "nagyobb",
  174. "szárított",
  175. "gerezd",
  176. "csomag",
  177. "darált",
  178. "csipet",
  179. "A",
  180. "vagy",
  181. "és",
  182. "csokor",
  183. "ízlés",
  184. "füstölt",
  185. "egész",
  186. "reszelt",
  187. "közepes",
  188. "tésztához",
  189. "pohár",
  190. "szál",
  191. "a",
  192. "szerint",
  193. "tésztához:",
  194. "kb.",
  195. "fél",
  196. "pici",
  197. "csip",
  198. "poh",
  199. "csípős",
  200. "főtt",
  201. "héja",
  202. "töltelékhez:",
  203. "kisebb",
  204. "tetejére:",
  205. "savanyú",
  206. "mokkáskanál",
  207. "ísz",
  208. "csapott",
  209. "szem",
  210. "száraz",
  211. "házi",
  212. "(vagy",
  213. "húsos",
  214. "tészta",
  215. "+",
  216. "Valamint:",
  217. "is",
  218. "liter",
  219. "sütéshez",
  220. "vágott",
  221. "fehér",
  222. "Arany",
  223. "Piros",
  224. "vágva",
  225. "pár",
  226. "arany",
  227. "csipetnyi",
  228. "-",
  229. "késhegynyi",
  230. "krémhez",
  231. "apróra",
  232. "doboz",
  233. "üveg",
  234. "is)",
  235. "(lehet"
  236. };
  237. notIngredients.addAll(Arrays.asList(ni));
  238. }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement