Java :- String search in proximity manner
Asked Answered
M

1

-3

I need a pure Java program to search on a given string to "find words near each other" - each-other distance need to be specified. More specifically said :- finds word1 and word2 in any order, as long as they occur within a certain distance of each other.

For example :- to search for a "cancer" and "problems" within 3 words of each other in a given string - if found return "true" else return "false".

String term = "cancer problems"; String text = "doctors found many cancer related chest problems in japan during second world war."; int distance = 3; // distance may vary

I prefer pure Java solution rather regex solution.

Maxilliped answered 11/8, 2017 at 9:17 Comment(8)
"I prefer pure Java solution rather regex solution." - and we prefer you showing us what you have tried so far so we can help you fix a problem. We are not solving the entire task for you.Mutinous
@Mutinous All OP has is shown here, I guess.Bristow
Please follow the String text = "doctors found many cancer related problems in japan durring second world war."; regex approach 1 : - \\bcancer\\W+(?:\\w+\\W+){1,6}?problems\\b regex approach 2 :- \b(?:(?>cancer()|problems()|(?>\1|\2)\w+)\b\W*?){0,2}\1\2\bMaxilliped
@Wiktor Stribiżew Thanks for your mention. your noted it previously.Maxilliped
And your question is still unclear, sorry.Bristow
@Wiktor Stribiżew - Please leave it to other community people if you can't understand. Thanks.Maxilliped
I have. Others think your question is too broad and they gave you 2 downvotes. Not me.Bristow
@Wiktor Stribiżew it's Ok I'm not counting reputation it doesn't matter for me no hard feeling about. I'm actually stuck on this Java problem that's why I'm asking community support. I already have php solution but I need implement it on Java platform too.Maxilliped
I
1

Here is a very naive way without regex.

public class NotElegant {

    public static void main(String[] args){
        String text = "doctors found many cancer related chest problems in japan during second world war.";
        String term = "cancer problems";
        System.out.println(getWordsNearEachOther(text,term,3));
    }
    public static String getWordsNearEachOther(String text, String term, int distance){
        String word1= term.split(" ")[0];
        String word2= term.split(" ")[1];
        String firstWord = text.indexOf(word1)<text.indexOf(word2)?word1:word2;
        String secondWord = text.indexOf(word1)<text.indexOf(word2)?word2:word1;
        if(!(text.contains(word1) && text.contains(word2))){
            return null;
        }        
        else if(text.substring(text.indexOf(firstWord), text.indexOf(secondWord)+secondWord.length()).split(" ").length>distance+1){
            return null;
        }
        return text.substring(text.indexOf(firstWord), text.indexOf(secondWord)+secondWord.length());
    }
}
Incautious answered 11/8, 2017 at 10:8 Comment(1)
This is actually acceptable. may be now I have some good approach. it's actually better one.Thanks for @IncautiousMaxilliped

© 2022 - 2024 — McMap. All rights reserved.