]> Softwares of Agnibho - anagram.git/blob - com/agnibho/code/anagram/CheckAnagram.java
7de5a59643b725cd1a014b5a568dda11bcb88b9f
[anagram.git] / com / agnibho / code / anagram / CheckAnagram.java
1 /*
2 Anagram- Find anagrams of a word
3 Copyright (C) 2013 Agnibho Mondal
4
5 This file is part of Anagram.
6
7 Anagram is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 Anagram is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Anagram. If not, see <http://www.gnu.org/licenses/>.
19 */
20 package com.agnibho.code.anagram;
21
22 import javax.swing.SwingWorker;
23
24 /**
25 *
26 * @author SPARK
27 */
28 public class CheckAnagram extends SwingWorker <String, Double> {
29 private String input;
30 private String result;
31 private IsAnagram isAnagram= new IsAnagram();
32 private java.util.Scanner read=null;
33 private boolean inDict;
34 private boolean isMulti;
35
36 private double total=0; //Progress calculation
37 private double done=0;
38 private double progress=0;
39
40 CheckAnagram(String in, boolean multi){
41 input=in;
42 isMulti=multi;
43 }
44
45 @Override
46 public String doInBackground() throws InterruptedException{
47 result="";
48 if(!isMulti) singleWord();
49 else multiWord();
50 return result;
51 }
52
53 public boolean isWord(){
54 return inDict;
55 }
56
57 private void singleWord(){ //[Finds Anagrams for single word search]
58 String letters="";
59 String filename;
60 String matched;
61 inDict=false;
62 for(int i=0; i<input.length(); i++){
63 if(!letters.contains(Character.toString(input.charAt(i)))){
64 filename="com/agnibho/code/anagram/dictionary/"+Character.toString(input.charAt(i)).toUpperCase()+Integer.toString(input.length())+".txt";
65 matched=singleMatch(filename); //(Function call)
66 if(matched!=null){
67 if(result!=null)
68 result=result+matched;
69 else
70 result=matched;
71 }
72 letters=letters+Character.toString(input.charAt(i));
73 }
74 }
75 }
76
77 private void multiWord() throws InterruptedException{ //[Finds Anagrams for multi word search]
78 String letters="";
79 String filename;
80 String matched;
81 String wordList=null;
82 input=input.replaceAll("\\s", "");
83 for(int i=0; i<input.length(); i++){
84 if(!letters.contains(Character.toString(input.charAt(i)))){
85 for(int j=0; j<30; j++){
86 filename="com/agnibho/code/anagram/dictionary/"+Character.toString(input.charAt(i)).toUpperCase()+Integer.toString(j)+".txt";
87 matched=multiMatch(filename); //(Function Call)
88 if(matched!=null){
89 if(wordList!=null)
90 wordList=wordList+matched;
91 else
92 wordList=matched;
93 }
94 }
95 letters=letters+Character.toString(input.charAt(i));
96 }
97 }
98 result=potentialSentence(wordList); //(Function call)
99 }
100
101 private String singleMatch(String file){ //[Finds Anagrams of the input from the specified file]
102 String found=null;
103 String word;
104 try{
105 read=new java.util.Scanner(new java.io.BufferedInputStream(java.lang.ClassLoader.getSystemResourceAsStream(file)));
106 while(read.hasNext()){
107 word=read.next();
108 word=word.toLowerCase();
109 if(isAnagram.check(input, word)){
110 if(!word.equals(input)){
111 if(found!=null){
112 if(!found.contains(word)){
113 found=found+word+"/";
114 }
115 }
116 else found=word+"/";
117 }
118 else inDict=true;
119 }
120 }
121 }catch(Exception ex){
122 result=ex.toString();
123 }
124 finally{
125 if(read!=null)
126 read.close();
127 }
128 return found;
129 }
130
131 private String multiMatch(String file){ //[Finds the list of words for calculation of multi-word Anagrams of the input]
132 String found=null;
133 String word;
134 try{
135 read=new java.util.Scanner(new java.io.BufferedInputStream(java.lang.ClassLoader.getSystemResourceAsStream(file)));
136 while(read.hasNext()){
137 word=read.next();
138 word=word.toLowerCase();
139 if(potentialWord(word)){ //(Function Call)
140 if(found!=null){
141 if(!found.contains(word)){
142 found=found+word+" ";
143 }
144 }
145 else found=word+" ";
146 }
147 }
148 }catch(Exception ex){
149 result=ex.toString();
150 }
151 finally{
152 if(read!=null)
153 read.close();
154 }
155 return found;
156 }
157
158 private boolean potentialWord(String word){ //[Finds out if the word in question is a candidate for the multiworded Anagram search of the input]
159 String ref=input;
160 boolean flag=true;
161 for(int i=0; i<word.length(); i++){
162 if(!(ref.contains(Character.toString(word.charAt(i))))){
163 flag=false;
164 break;
165 }
166 else ref=ref.replaceFirst(Character.toString(word.charAt(i)), " ");
167 }
168 return flag;
169 }
170
171 private String potentialSentence(String words) throws InterruptedException{ //[Finds out the multi-word Anagrams of the input from the word list]
172 String[] list=words.split(" ");
173 String holder="";
174 String qualified="";
175 int[] index=new int[input.length()];
176 int count=0;
177 int checkLength;
178
179 if(list.length<3) //Progress calculation
180 total=list.length;
181 else
182 total=(list.length*java.lang.Math.pow(list.length, 2)+list.length*java.lang.Math.pow(list.length, 1)+list.length*java.lang.Math.pow(list.length, 0));
183
184 for(int i=0; i<list.length&&index[0]<list.length; i++){
185 index[count]=i;
186 checkLength=0;
187 for(int j=0; j<=count; j++){
188 checkLength=checkLength+list[index[j]].length();
189 }
190
191 if(checkLength==input.length()){
192 for(int j=0; j<=count; j++){
193 holder=holder+" "+list[index[j]];
194 }
195 if(new IsAnagram().check(holder, input)){
196 if(!qualified.contains(holder)) qualified=qualified+holder+"/";
197 }
198 holder="";
199 }
200 else if(checkLength<input.length()){
201 count++;
202 i=-1;
203 }
204
205 if(i==(list.length-1)&&(index[0]<list.length)){
206 while((index[count]>=(list.length-1))&&(count>0)){
207 count--;
208 }
209 i=index[count];
210 }
211
212 if(Thread.interrupted())
213 throw new InterruptedException();
214
215 if(index.length<3) //Progress calculation
216 done=index[0];
217 else
218 done=(index[0]*java.lang.Math.pow(list.length, 2)+index[1]*java.lang.Math.pow(list.length, 1)+index[2]*java.lang.Math.pow(list.length, 0));
219 if(calcProgress()>progress){
220 progress=calcProgress();
221 publish(progress);
222 }
223
224 }
225 publish((double)100);
226 return qualified;
227 }
228
229 private double calcProgress(){ //[Calculates the progress]
230 return (double)(done*100)/total;
231 }
232 }