Skip to content

Commit

Permalink
Algorithm complexity text working
Browse files Browse the repository at this point in the history
Algorithm complexity working and updated help.txt
Tidied ProcessInput class
  • Loading branch information
albertopoljak committed Nov 24, 2017
1 parent 424eda9 commit 315b429
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 43 deletions.
9 changes: 5 additions & 4 deletions src/main/InputConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ArrayList<String> extractInput(String input ,char wordSeparator){

for (int i=0; i<inputLength; i++) {
currentChar = input.charAt(i);
if (currentChar!=wordSeparator && !compareCharAndStringForNewline(currentChar, newLine) )
if (currentChar!=wordSeparator && !compareCharForNewline(currentChar, newLine) )
tempWord+=input.charAt(i);
else{
if (tempWord.length()>0)
Expand All @@ -43,15 +43,15 @@ public ArrayList<String> extractInput(String input ,char wordSeparator){
}

//Add last word if there is NO newline at the end
if ( !compareCharAndStringForNewline( input.charAt( inputLength-1 ) , newLine) )
if ( !compareCharForNewline( input.charAt( inputLength-1 ) , newLine) )
returnedWords.add(tempWord);

Main.txtrLog.appendDebug("Extracted input: " + Arrays.toString(returnedWords.toArray()) );

return returnedWords;
}

private boolean compareCharAndStringForNewline( char charInput , String stringInput ){
private boolean compareCharForNewline( char charInput , String stringInput ){
String temp = Character.toString(charInput);
//Check if contains because newline(stringInput) can be 2 chars (on windows \r\n)
if ( stringInput.contains(temp)){
Expand All @@ -62,6 +62,7 @@ private boolean compareCharAndStringForNewline( char charInput , String stringIn
}



/*
* Returns a string of all characters that are between char 'between'
* Example input is: word\\ab"prefix" ,and between is '"'
Expand All @@ -70,7 +71,7 @@ private boolean compareCharAndStringForNewline( char charInput , String stringIn
* If char between doesn't exist it returns empty string ""
* If there is an odd number of chars between it returns empty string ""
*/
public static String getCharactersFromOptionsS(String text, char between){
public static String getCharactersFromOptions(String text, char between){
int i;
int j;
String temp = "";
Expand Down
34 changes: 27 additions & 7 deletions src/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class Main {
private static Settings windowSettings;
private static InputConsole txtWords;
public static Logger txtrLog;
public static JLabel txtComplexity;
private static JLabel textCurrentMemUsage;
private static PrintWriter out;
private static Timer timer;
Expand Down Expand Up @@ -175,7 +176,7 @@ public void mouseReleased(MouseEvent e) {
try{
openPrintWriter("wordList.txt");
List<List<String>> tempOutput = new ArrayList<List<String>>();
tempOutput = ProcessInput.buildWords( txtWords.extractInput(txtWords.getText() , Settings.wordSeparator), Settings.wordCombinator , Settings.optionSeparator ) ;
tempOutput = ProcessInput.buildWords( txtWords.extractInput(txtWords.getText() , Settings.wordSeparator) ) ;
CombinatoricsPermutations.combinations2D( Helpers.convertListToStringArray(tempOutput) );
closePrintWriter();
txtrLog.appendToTextPanel("Finished!" , 'G');
Expand All @@ -192,10 +193,10 @@ public void mouseReleased(MouseEvent e) {

JLabel txtrOptions = new JLabel();
txtrOptions.setForeground(Color.LIGHT_GRAY);
txtrOptions.setText("Universal options:");
txtrOptions.setText("Post-process options:");
txtrOptions.setOpaque(false);
txtrOptions.setFont(new Font("Tahoma", Font.PLAIN, 11));
txtrOptions.setBounds(14, 179, 100, 18);
txtrOptions.setBounds(14, 179, 120, 18);
frmDecypherInStyle.getContentPane().add(txtrOptions);

JLabel lblMemoryUsage = new JLabel("Current RAM usage:");
Expand All @@ -215,7 +216,7 @@ public void mouseReleased(MouseEvent e) {
txtrT.setBackground(Color.LIGHT_GRAY);
txtrT.setToolTipText("Example: \\\\sbc\"1234567890AAa\" \\\\sec\"1234567890AAa\"");
txtrT.setFont(new Font("Monospaced", Font.PLAIN, 12));
txtrT.setBounds(124, 179, 640, 22);
txtrT.setBounds(134, 179, 630, 22);
frmDecypherInStyle.getContentPane().add(txtrT);

JLabel lblAlgorithmComplexity = new JLabel("Algorithm complexity:");
Expand All @@ -224,10 +225,16 @@ public void mouseReleased(MouseEvent e) {
lblAlgorithmComplexity.setBounds(14, 208, 110, 14);
frmDecypherInStyle.getContentPane().add(lblAlgorithmComplexity);

JLabel txtComplexity = new JLabel("Not working, to do in following updates");
txtComplexity = new JLabel("Click to refresh or click \"Refresh variables\"");
txtComplexity.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
updateComplexityText();
}
});
txtComplexity.setForeground(Color.LIGHT_GRAY);
txtComplexity.setFont(new Font("Monospaced", Font.PLAIN, 14));
txtComplexity.setBounds(124, 204, 640, 25);
txtComplexity.setBounds(134, 204, 630, 25);
frmDecypherInStyle.getContentPane().add(txtComplexity);

JLabel lblEstimatedMemoryUsage = new JLabel("Estimated filesize on disk:");
Expand All @@ -243,14 +250,15 @@ public void mouseReleased(MouseEvent e) {
frmDecypherInStyle.getContentPane().add(txtEstimatedMemUsage);

JButton btnRefreshVariables = new JButton("Refresh variables");
btnRefreshVariables.setEnabled(false);
btnRefreshVariables.setBackground(Color.DARK_GRAY);
btnRefreshVariables.setForeground(Color.GRAY);
btnRefreshVariables.setFocusPainted(false);
btnRefreshVariables.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
textCurrentMemUsage.setText("MB: " + getMemoryUsage());
updateComplexityText();

}
});
btnRefreshVariables.setFont(new Font("Tahoma", Font.PLAIN, 11));
Expand Down Expand Up @@ -341,4 +349,16 @@ public static void autoMemoryDetection(){
}


public static void updateComplexityText(){
try{
List<List<String>> tempOutput = new ArrayList<List<String>>();
tempOutput = ProcessInput.buildWords( txtWords.extractInput(txtWords.getText() , Settings.wordSeparator) ) ;
long tempCount = Helpers.totalExpectedGeneratedWords(tempOutput);
txtComplexity.setText( Helpers.getAlgorithmComplexityString(tempCount) + " Words:" + tempCount );
}catch(Exception exc){
txtrLog.append("Can't update algorithm complexity text, error: "+exc , 'E');
}
}


}
14 changes: 7 additions & 7 deletions src/main/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ This would generate: lovejesus , jesuslove , loveJesus , Jesuslove , loveicecrea



f) Universal options
f) Post-process options
These options only apply to ALREADY generated words.

--sb-%S Add specific string %S at beginning of each generated word example:
Expand Down Expand Up @@ -158,9 +158,9 @@ TO DO
SECTION 3: Explained complexity chart

Word count Complexity
Impossible
Very high
High
Medium
Low
Very low
>1000000000 Impossible
<1000000000 Very high
<500000000 High
<50000000 Medium
<5000000 Low
<100000 Very low
4 changes: 2 additions & 2 deletions src/main/methods/CombinatoricsPermutations.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public class CombinatoricsPermutations {
private CombinatoricsPermutations(){};

/*
* You have to pass a clean string array, with no nulls
* Number of generated words: n = multiply number of words in 1D array block with each number of words from next block
* Method requires a clean string array (with no nulls)
* Number of generated words: multiply number of words in 1D array block with each number of words from next block
* Example input {{"apple","Apple"},{"banana","Banana",}} will result in: applebanana , appleBanana , Applebanana , AppleBanana
*/
public static void combinations2D(String [][] g) {
Expand Down
46 changes: 45 additions & 1 deletion src/main/methods/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Calendar;
import java.util.List;
import main.Help;
import main.Main;

public class Helpers {

Expand Down Expand Up @@ -74,7 +75,6 @@ private static ArrayList<String> clearList() {
*/

public static String[][] convertListToStringArray( List<List<String>> theStrings ){
//List<List<String>> theStrings ... coming from somewhere
String[][] stringsAsArray = new String[theStrings.size()][];
for (int i=0; i<theStrings.size();i++) {
List<String> aList = theStrings.get(i);
Expand All @@ -84,6 +84,50 @@ public static String[][] convertListToStringArray( List<List<String>> theStrings
}


public static long totalNumOfElementsIn2DList( List<List<String>> input, int rows ){
long counter = 0;
for( int i=0; i<rows; i++)
counter+=input.get(i).size();
return counter;
}


public static long factorial( long n ){
if(n==0)
return 1;
else return (n*factorial(n-1));

}


public static long totalExpectedGeneratedWords( List<List<String>> input ){
long multiplier = 1;
int rows = input.size();
for( int i=0; i<rows; i++){
multiplier*=input.get(i).size();
}
return factorial(rows)*multiplier;
}


public static String getAlgorithmComplexityString( long totalWords ){
if( totalWords<100000 )
return "Very low";
else if( totalWords<5000000 )
return "Low";
else if( totalWords<50000000 )
return "Medium";
else if( totalWords<500000000 )
return "High";
else if( totalWords<1000000000 )
return "Very High";
else
return "Almost Impossible!";
}




public static String getDate(){
return new SimpleDateFormat("[HH:mm:ss] ").format(Calendar.getInstance().getTime());
}
Expand Down
53 changes: 31 additions & 22 deletions src/main/methods/ProcessInput.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,51 @@
package main.methods;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import main.Main;
import main.Settings;

public class ProcessInput {

private ProcessInput(){};
private static char combiningSeparator = Settings.wordCombinator ;
private static char optionSeparator = Settings.optionSeparator;

/*
* Build words from options
* NOTE:
* Total number of generated words is: X * Y
* Where X is number of words generated by combinations2D and Y same but from heapPermutation
* Total number of generated words is: a * b
* Example: Input is {{"sad","SAD"},{"najebo","NAJEBO"},{"sam","SAM"},{"test","TEST", "Test", "ayy"}}
* X would then be 2*2*2*4 = 32
* Y would be 4! (1*2*3*4) because we have 4 blocks (4 words that make a sentence)
* a would then be 2*2*2*4 = 32
* b would be 4! (1*2*3*4) because we have 4 blocks (4 words that make a sentence)
* Total = 768
*/

//int Y = inputWords.size(); daje nullPointer exception ako je input prazan
public static List<List<String>> buildWords( ArrayList<String> inputWords , char combiningSeparator, char optionSeparator){
public static List<List<String>> buildWords( ArrayList<String> inputWords ){
int i;
int X;
int Y = inputWords.size();
List<List<String>> tempOutput = new ArrayList<List<String>>();
updateSeparators();

//Add Y words and start adding words and their options
for( i=0; i<Y; i++){
tempOutput.add(new ArrayList<String>());
extractWords( tempOutput, i, inputWords.get(i).toString(), combiningSeparator, optionSeparator );
extractWords( tempOutput, i, inputWords.get(i).toString() );
}

Main.txtrLog.appendDebug( "Base words that were extracted from input(without their options):" );
for( i=0; i<Y; i++){
for (String value : tempOutput.get(i)) {
Main.txtrLog.appendDebug( (i+1) + ":" + value );
}
if( Settings.chckbxDebugMode.isSelected() ){
long tempSize = Helpers.totalNumOfElementsIn2DList(tempOutput, Y);
if( tempSize<50 )
for( i=0; i<Y; i++){
for (String value : tempOutput.get(i)) {
Main.txtrLog.appendDebug( (i+1) + ":" + value );
}
}
else
Main.txtrLog.appendDebug( "Too many base words to display. I counted "+tempSize+" words.");
}

return tempOutput;
Expand All @@ -51,7 +58,7 @@ public static List<List<String>> buildWords( ArrayList<String> inputWords , char
* Extract words from string
*/

public static void extractWords( List<List<String>> inputList , int index , String extractString , char combiningSeparator, char optionSeparator ){
public static void extractWords( List<List<String>> inputList , int index , String extractString ){
String baseString;
String optionString;
int indexCombining = extractString.indexOf(combiningSeparator);
Expand All @@ -66,12 +73,12 @@ public static void extractWords( List<List<String>> inputList , int index , Stri
StringBuilder temp = new StringBuilder();
for( int i=0; i<tempInput.length(); i++ ){
if( tempInput.charAt(i)==combiningSeparator ){
extractWords( inputList, index, temp.toString() , combiningSeparator, optionSeparator );
extractWords( inputList, index, temp.toString() );
temp = new StringBuilder();

}else if( i==tempInput.length()-1 ){
temp.append( tempInput.charAt(i) );
extractWords( inputList, index, temp.toString() , combiningSeparator, optionSeparator );
extractWords( inputList, index, temp.toString() );
//temp = new StringBuilder();
}else{
temp.append( tempInput.charAt(i) );
Expand All @@ -98,7 +105,7 @@ public static void extractWords( List<List<String>> inputList , int index , Stri
*/
String optionSeparatorDouble = optionSeparator+""+optionSeparator;
String[] word = optionString.split(optionSeparatorDouble);
word = removeEmpty(word);
word = removeEmptyWordsFromStringArray(word);

Main.txtrLog.appendDebug("Printing option array for word: '" + extractString + "' in format (ID:option) :" );
for(int k =0; k<word.length;k++)
Expand All @@ -108,8 +115,6 @@ public static void extractWords( List<List<String>> inputList , int index , Stri
/*
* Eliminate empty words produced by optionString.split
*/
//log.append("Test:"+optionPart+"\n");
//if(!optionPart.isEmpty()){
/*
* Before calling the option method first extract sub-options
*/
Expand All @@ -118,16 +123,14 @@ public static void extractWords( List<List<String>> inputList , int index , Stri
/*
* Now remove empty words from sub-options produced by optionString.split
*/
subOptions = removeEmpty(subOptions);
subOptions = removeEmptyWordsFromStringArray(subOptions);
Main.txtrLog.appendDebug("Printing suboptions (ID:suboption) where ID=0 means base option:" );
for(int k =0; k<subOptions.length;k++)
Main.txtrLog.appendDebug(k+":"+subOptions[k] );
/*
* String in first index of finalSubOptions is the option while rest are subOptions
*/
//Log.writeDebug("baseString: "+baseString+" ,optionPart:"+optionPart+" ,subOptions: (printed above)");
WordOptions.extractOptions( inputList, index, baseString, subOptions );
//}
}
return;
}else{
Expand All @@ -150,11 +153,17 @@ public static void extractWords( List<List<String>> inputList , int index , Stri
* Words are considered empty if they are null or have 0 characters
*/

public static String[] removeEmpty(String[] input){
private static String[] removeEmptyWordsFromStringArray(String[] input){
List<String> list = new ArrayList<String>(Arrays.asList(input));
list.removeAll(Arrays.asList("", null));
return list.toArray(new String[0]);
}


private static void updateSeparators(){
combiningSeparator = Settings.wordCombinator ;
optionSeparator = Settings.optionSeparator;
}


}

0 comments on commit 315b429

Please sign in to comment.