Yunus Berk KAYNAK
Yunus Berk KAYNAK

Reputation: 23

JAVA - Finding a similar word in String ArrayList

I'm working for a school project. Purpose of the program looking for the "Same" and "similar" words via input. I don't know how to find similar words numbers or just words.

FOR EXAMPLE:

INPUT: Car

OUTPUT: FOUND: CAR SIMILAR WORDS: 3(or the Words like 'carhood').

    boolean cnt = false;
    while(!cnt){
        // Variables
        ArrayList<String> WordList = 
                                new ArrayList<>(Arrays.asList("açık",...."zorunda"));
        String Word      = "";
        int WordIndex    = 0;
        int WordListSize = WordList.size();
        boolean result = false;
        System.out.print("Kelime Giriniz: ");
        String Input = new Scanner(System.in).nextLine();

        // Search Loop
        for (int i = 0; i < WordListSize; i++) {

            // Temporary variables
            Word      = WordList.get(i);
            WordIndex = WordList.indexOf(Word);

            // If there is word in word list
            if(Word.equalsIgnoreCase(Input)) {

                System.out.println("Kelime bulundu.");
                System.out.println("Kelime : "+Word);
                System.out.println("Kayıt Numarası : "+WordIndex);

                // Comparison number
                System.out.println(WordIndex+" Kelime ile karşılaştırıldı.");

                result = true;
                break;
            }
        }
        if(!result) {
            System.out.print("Kelime tarandı:");
            System.out.println((WordList.size()));
            System.out.println("Kelime bulunamadı.");
        }
        System.out.println("--");
        System.out.println("Devam etmek ister misiniz ?");
        System.out.println("'1' Evet, '0' Hayır.");
        System.out.println("--");
        int answer = new Scanner(System.in).nextInt();
        if(answer ==0){
            System.out.println("Çıkış yapaılıyor..");
            cnt= true;
        }
    }

it is Turkish btw.

Upvotes: 1

Views: 2961

Answers (2)

Akceptor
Akceptor

Reputation: 1932

Actually there are lots of algorithms to check string similarity. You can read about lots of them and chose the favorite one using this link: https://github.com/tdebatty/java-string-similarity

There also is a maven dependency:

<dependency>
    <groupId>info.debatty</groupId>
    <artifactId>java-string-similarity</artifactId>
    <version>RELEASE</version>
</dependency>

and a couple of usage examples there.

In short you should set some kind of similarity coefficient and based on that algorithm will decide if strings are similar enough or not based on metrics like https://en.wikipedia.org/wiki/Levenshtein_distance

Upvotes: 1

Nicholas K
Nicholas K

Reputation: 15423

You can do so easily using java-8 :

// Assuming this is your list
List<String> myList = Arrays.asList("Carhood", "Carlight", "Tree", "Red", "Carrom");

// using a filter and then collecting 'similar words' to a list
List<String> collect = myList.stream()
                             .filter(s -> s.contains("Car"))
                             .collect(Collectors.toList());

// to print it out
collect.forEach(System.out::println);

Upvotes: 3

Related Questions