]>
Softwares of Agnibho - anagram.git/blob - anagram/CheckAnagram.java
2 Anagram- Find anagrams of a word
3 Copyright (C) 2013 Agnibho Mondal
5 This file is part of Anagram.
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.
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.
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/>.
20 package com
.agnibho
.code
.anagram
;
22 import javax
.swing
.SwingWorker
;
28 public class CheckAnagram
extends SwingWorker
<String
, Double
> {
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
;
36 private double total
=0; //Progress calculation
37 private double done
=0;
38 private double progress
=0;
40 CheckAnagram(String in
, boolean multi
){
46 public String
doInBackground() throws InterruptedException
{
48 if(!isMulti
) singleWord();
53 public boolean isWord(){
57 private void singleWord(){ //[Finds Anagrams for single word search]
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)
68 result
=result
+matched
;
72 letters
=letters
+Character
.toString(input
.charAt(i
));
77 private void multiWord() throws InterruptedException
{ //[Finds Anagrams for multi word search]
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)
90 wordList
=wordList
+matched
;
95 letters
=letters
+Character
.toString(input
.charAt(i
));
98 result
=potentialSentence(wordList
); //(Function call)
101 private String
singleMatch(String file
){ //[Finds Anagrams of the input from the specified file]
105 read
=new java
.util
.Scanner(new java
.io
.BufferedInputStream(java
.lang
.ClassLoader
.getSystemResourceAsStream(file
)));
106 while(read
.hasNext()){
108 word
=word
.toLowerCase();
109 if(isAnagram
.check(input
, word
)){
110 if(!word
.equals(input
)){
112 if(!found
.contains(word
)){
113 found
=found
+word
+"/";
121 }catch(Exception ex
){
122 result
=ex
.toString();
131 private String
multiMatch(String file
){ //[Finds the list of words for calculation of multi-word Anagrams of the input]
135 read
=new java
.util
.Scanner(new java
.io
.BufferedInputStream(java
.lang
.ClassLoader
.getSystemResourceAsStream(file
)));
136 while(read
.hasNext()){
138 word
=word
.toLowerCase();
139 if(potentialWord(word
)){ //(Function Call)
141 if(!found
.contains(word
)){
142 found
=found
+word
+" ";
148 }catch(Exception ex
){
149 result
=ex
.toString();
158 private boolean potentialWord(String word
){ //[Finds out if the word in question is a candidate for the multiworded Anagram search of the input]
161 for(int i
=0; i
<word
.length(); i
++){
162 if(!(ref
.contains(Character
.toString(word
.charAt(i
))))){
166 else ref
=ref
.replaceFirst(Character
.toString(word
.charAt(i
)), " ");
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(" ");
175 int[] index
=new int[input
.length()];
179 if(list
.length
<3) //Progress calculation
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));
184 for(int i
=0; i
<list
.length
&&index
[0]<list
.length
; i
++){
187 for(int j
=0; j
<=count
; j
++){
188 checkLength
=checkLength
+list
[index
[j
]].length();
191 if(checkLength
==input
.length()){
192 for(int j
=0; j
<=count
; j
++){
193 holder
=holder
+" "+list
[index
[j
]];
195 if(new IsAnagram().check(holder
, input
)){
196 if(!qualified
.contains(holder
)) qualified
=qualified
+holder
+"/";
200 else if(checkLength
<input
.length()){
205 if(i
==(list
.length
-1)&&(index
[0]<list
.length
)){
206 while((index
[count
]>=(list
.length
-1))&&(count
>0)){
212 if(Thread
.interrupted())
213 throw new InterruptedException();
215 if(index
.length
<3) //Progress calculation
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();
225 publish((double)100);
229 private double calcProgress(){ //[Calculates the progress]
230 return (double)(done
*100)/total
;