Thursday 7 April 2016

Sudoku Solver : Solve any level of Sudoku

Sudoku Solver :
This is implemented by simple back-tracking algorithm (Backtracking Ref) in Javacript.

Simple steps to solve your Sudoku :
     1. Download the file  Sudoku Solver
     2. Make sure the downloaded file should be save as .html  file, else rename it to <filename>.html
     3. Open downloaded file in browser

Algorithm :
Input : A 2D 9x9 array (arr) with having valid and solvable input values.
            Valid - No number (1-9) is repeated in a same row, same column or same 3x3 box.
            Solvable - Even though the input is valid, but there is a chance of not having the solution for a                               particular input if that is randomly generated.

boolean solveSudoku(arr[9][9])
               Find an EMPTY cell (r, c) from the array 
               If no EMPTY cell found then
                         return TRUE ; // i.e the array is filled i.e. solved
               else
                         for num from 1 to 9
                                 check if arr[r][c] is SAFE to put num
                                 if SAFE then
                                            arr[r][c] = num; //update cell with num, consider this is a part of the goal
                                            // call recursively solveSudoku(arr) to solve next EMPTY cell
                                            result = solveSudoku(arr) 
                                            if result is FALSE then   // No solution  exist with arr[r][c] = num 
                                                      arr[r][c] = 0;  // Revert back the considered solution
                                            else
                                                      return TRUE; // We reached at the goal by taking arr[r][c] =num
                       end for
                       return FALSE;   // No solution exists, since no number from 1 to 9 can be placed at(r,c)

For SAFE checking :
boolean isSafe(arr[9][9], roe, col, num)
        if num is present in same row or same column or same inner 3x3 box then
                 return FALSE
        else
                return TRUE


Get Source Code here.


Thank You for Visiting the blog.

Saturday 2 April 2016

Text to Speech in Java

Converting text-to-speech is very simple, just by using FreeTTS library.

Include library by maven dependency :
<dependency>
<groupId>org.mobicents.external.freetts</groupId>
<artifactId>freetts</artifactId>
<version>1.0</version>
</dependency>

OR
Download library (jar file) from here and include into project directly.

How to use FreeTTS API ?
Step - 1 : Get the instance of com.sun.speech.freetts.VoiceManager class.
               VoiceManager provides access to all voices available in FreeTTS
               VoiceManager voiceManager = VoiceManager.getInstance();

Step - 2 : Get com.sun.speech.freetts.Voice instance using voiceManager by supplying the voice name
               Voice voice = voiceManager.getVoice("kevin16");
               Here "kevin16" is one of the voice name available in FreeTTS library.
               You can also get all available voice names like below :
               
              Voice voices1[] = voiceManager.getVoices();  
              System.out.println("Available Voices");
              for (int i = 0; i < voices1.length; i++)
            System.out.println(voices1[i].getName());

Step - 3 : Set voice rate (i.e no of words per minute)
              voice.setRate(120);

Step - 4 : call speak( ) method by providing the text to speech
              voice.speak("<text to speech>");
              This speak( ) is overloaded to support various types of inputs like text, inputStream etc.
              You can also read a file by providing the fileInputStream as below : 
              FileInputStream fileInputStream = new FileInputStream("d:/ttsInput.txt");
              voice.speak(fileInputStream);

Here is the complete code to convert a text file to speech : 
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
import java.io.FileInputStream;

public class TextToSpeech {
    private static final String voiceName = "kevin16"; // this is one of the voice name available in default
    private static void doSpeak() {
        try {
            VoiceManager voiceManager = VoiceManager.getInstance();
            Voice voice = voiceManager.getVoice(voiceName);

            if (voice != null) {
                voice.allocate();
                // Set 120 words per minute
                voice.setRate(120);
                FileInputStream fileInputStream = new                                                                FileInputStream("d:/ttsInput.txt");
                voice.speak(fileInputStream);
                voice.deallocate();
            } else {
                System.out.println("No Voice Available");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        doSpeak();
    }
}

Here is a demonstration of a Speaking Alarm Clock application using this FreeTTS library.
Functionalities of application : 
1. class AlarmClockThis class creates the alarm clock UI using Java Swing API, having feature to set alarm for a specific time and triggers the TimeSpeaker to speak the time on the time for which alarm is set.
2. class TimeSpeaker : This class converts any time into word form
                            e.g  05:15 -> quarter past five
                                  03:30 -> half past three
                                  11:40 -> twenty minutes to twelve
3. class TextToSpeech : This is the class responsible of converting text to speech using freetts (Free Text To Speech) library

Download OR fork complete project from here .


Thank You for Visiting the blog. Please feel free to share yout comment/suggestion.