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.
-
2What is the question/problem?lalo– lalo2018年12月12日 14:13:47 +00:00Commented Dec 12, 2018 at 14:13
-
i don't know how to find similliar wordsYunus Berk KAYNAK– Yunus Berk KAYNAK2018年12月12日 14:15:34 +00:00Commented Dec 12, 2018 at 14:15
-
"Similar" is not very precise. You need to figure out what it means to be "similar". Is it enough to use String.startsWith()? Or String.contains()? Those might be good places to start at least.jwismar– jwismar2018年12月12日 14:17:46 +00:00Commented Dec 12, 2018 at 14:17
-
try contains() method on String then.A_C– A_C2018年12月12日 14:21:43 +00:00Commented Dec 12, 2018 at 14:21
-
1i want to find words like. "Input: Car", Shows "Car" and "Carhood","Carlight" etc. words that contains input word.Yunus Berk KAYNAK– Yunus Berk KAYNAK2018年12月12日 14:22:43 +00:00Commented Dec 12, 2018 at 14:22
2 Answers 2
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);
Comments
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