Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.66 KB | None | 0 0
  1.  
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import java.util.Scanner;
  9.  
  10. /*
  11. * To change this license header, choose License Headers in Project Properties.
  12. * To change this template file, choose Tools | Templates
  13. * and open the template in the editor.
  14. */
  15.  
  16.  
  17. /**
  18. *
  19. * @author Windows 10 Home
  20. */
  21.  
  22. class Node{
  23. int number_Match;
  24. int index_Match;
  25. int word_distace;
  26.  
  27. String name_product;
  28. public Node(String name_product){
  29. this.name_product = name_product;
  30.  
  31. }
  32. }
  33.  
  34. public class Homework2 {
  35.  
  36. /**
  37. * @param args the command line arguments
  38. */
  39. public static void main(String[] args) throws FileNotFoundException {
  40. // TODO code application logic here
  41.  
  42. int Show;
  43. if(args.length == 0){
  44. Show = 10;
  45. }else{
  46. Show = Integer.parseInt(args[0]);
  47. }
  48.  
  49. Scanner s = new Scanner(new File("C:\\Users\\Windows 10 Home\\Documents\\NetBeansProjects\\Homework2\\src\\product.txt"));
  50. ArrayList<String> text_list = new ArrayList<String>();
  51. while(s.hasNextLine()){
  52. text_list.add(s.nextLine());
  53. }
  54. while(true){
  55. Scanner reader = new Scanner(System.in);
  56. System.out.print("Product Search - Input your keyword (s): ");
  57. String pattern_input = reader.nextLine();
  58. String[] pattern_parts = pattern_input.split(" ");
  59.  
  60. long Start_time = System.currentTimeMillis();
  61.  
  62.  
  63. ArrayList<Node> Match = new ArrayList<>();
  64. for (int i=0; i<text_list.size();i++){
  65. Node get = new Node(text_list.get(i));
  66. ArrayList<Integer> Index = new ArrayList<Integer>();
  67. for (int j=0; j<pattern_parts.length;j++){
  68. int found = KMP(text_list.get(i).toLowerCase().toCharArray(),pattern_parts[j].toLowerCase().toCharArray());
  69. if(found != -1) {
  70. Index.add(found);
  71. if (j==0)
  72. get.index_Match = found;
  73. get.number_Match++;
  74. }
  75. }
  76. Integer Min_distance = Integer.MAX_VALUE;
  77. for (int a = 0; a<Index.size(); a++){
  78. for (int b=a+1; b<Index.size();b++){
  79. if (Math.abs(Index.get(a)- Index.get(b)) < Min_distance ){
  80. Min_distance = (Math.abs(Index.get(a)- Index.get(b)));
  81. }
  82. }
  83. if(Min_distance > 100) Min_distance = 100;
  84. get.word_distace = Min_distance;
  85. Match.add(get);
  86. }
  87.  
  88. }
  89.  
  90. int[] Radix_int = new int[1000];
  91. String[] Radix_string = new String[1000];
  92.  
  93. for(int i = 0;i<Match.size();i++){
  94. Radix_int[i] = ((10 - Match.get(i).number_Match)*1000) + (Match.get(i).index_Match*100) + (Match.get(i).word_distace);
  95. Radix_string[i] = Match.get(i).name_product;
  96. }
  97.  
  98. int Match_count = Match.size();
  99. ArrayList<String> Show_name = new ArrayList<String>();
  100. radix(Radix_int,Radix_int.length,Radix_string);
  101. String Check = "Check";
  102. System.out.println("Search Result is:");
  103. for(int i = 0;i<Radix_int.length;i++){
  104. if(Check == Radix_string[i]) Match_count--;
  105. if(Radix_int[i] != 0 && Check != Radix_string[i]){
  106. Show_name.add(Radix_string[i]);
  107. Check = Radix_string[i];
  108. }
  109. }
  110.  
  111. int count=0;
  112. for(int i = 0;i<Show_name.size();i++){
  113. System.out.println("- " + Show_name.get(i));
  114. count++;
  115. if(count == Show) break;
  116. }
  117.  
  118. System.out.println(Match_count + " product(s) matched ");
  119. long End_time = System.currentTimeMillis();
  120. long run_time = End_time - Start_time;
  121. System.out.println("run time: " + run_time + " ms");
  122. }
  123.  
  124. }
  125. static int getMax(int arr[], int n)
  126. {
  127. int mx = arr[0];
  128. for (int i = 1; i < n; i++)
  129. if (arr[i] > mx)
  130. mx = arr[i];
  131. return mx;
  132. }
  133.  
  134. static void countSort(int arr[], int n, int exp, String[] product)
  135. {
  136. int output[] = new int[n];
  137. String[] output2 = new String[n];
  138.  
  139. int i;
  140. int count[] = new int[10];
  141. Arrays.fill(count,0);
  142.  
  143.  
  144. for (i = 0; i < n; i++)
  145. count[ (arr[i]/exp)%10 ]++;
  146.  
  147.  
  148. for (i = 1; i < 10; i++)
  149. count[i] += count[i - 1];
  150.  
  151.  
  152. for (i = n - 1; i >= 0; i--)
  153. {
  154. output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
  155. output2[count[ (arr[i]/exp)%10 ] -1] = product[i];
  156.  
  157. count[ (arr[i]/exp)%10 ]--;
  158. }
  159.  
  160.  
  161. for (i = 0; i < n; i++) {
  162. arr[i] = output[i];
  163. product[i] = output2[i];
  164. }
  165. }
  166.  
  167. static void radix(int arr[], int n, String[] product)
  168. {
  169.  
  170. int m = getMax(arr, n);
  171.  
  172.  
  173. for (int exp = 1; m/exp > 0; exp *= 10)
  174. countSort(arr, n, exp,product);
  175. }
  176.  
  177. public static int BoyerMoore(char[ ] text, char[ ] pattern) {
  178. int n = text.length;
  179. int m = pattern.length;
  180. if (m == 0) return 0;
  181. Map<Character,Integer> last = new HashMap<>();
  182. for (int i=0; i < n; i++)
  183. last.put(text[i], -1);
  184. for (int k=0; k < m; k++)
  185. last.put(pattern[k], k);
  186.  
  187. int i=m-1;
  188. int k=m-1;
  189. while (i < n) {
  190. if (text[i] == pattern[k]) {
  191. if (k == 0) return i;
  192. i--;
  193. k--;
  194. } else {
  195. i += m - Math.min(k, 1 + last.get(text[i]));
  196. k=m -1;
  197. }
  198. }
  199. return -1;
  200. }
  201.  
  202. public static int BruteForce(char[] text,char[] pattern){
  203. int n = text.length;
  204. int m = pattern.length;
  205. for(int i = 0; i <= n - m;i++){
  206. int k = 0;
  207. while(k < m && text[i+k] == pattern[k])
  208. k++;
  209. if(k == m)
  210. return i;
  211. }
  212. return -1;
  213. }
  214.  
  215. public static int KMP(char[] text,char[] pattern){
  216. int n = text.length;
  217. int m = pattern.length;
  218. if(m == 0) return 0;
  219. int[] fail = computeFailKMP(pattern);
  220. int j = 0;
  221. int k = 0;
  222. while(j<n){
  223. if(text[j] == pattern[k]){
  224. if(k == m - 1)return j - m + 1;
  225. j++;
  226. k++;
  227. }else if(k>0){
  228. k = fail[k-1];
  229. }else{
  230. j++;
  231. }
  232. }
  233. return -1;
  234. }
  235.  
  236. private static int[ ] computeFailKMP(char[ ] pattern) {
  237. int m = pattern.length;
  238. int[] fail = new int[m];
  239. int j = 1;
  240. int k = 0;
  241. while(j < m){
  242. if(pattern[j] == pattern[k]){
  243. fail[j] = k + 1;
  244. j++;
  245. k++;
  246. }else if(k > 0){
  247. k = fail[k - 1];
  248. }else{
  249. j++;
  250. }
  251. }
  252. return fail;
  253. }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement