-4

Very straightforward question: how can I measure the distance between two words in a text in Java? For example, the text could be:

The color of the car is blue.

How can I get the distance of the word color and blue in this sentence? I know that the distance from color to blue is 5. How can I get this distance of 5 in Java?

Thanks in advance.

modarwish
  • 72
  • 1
  • 8
  • 2
    On what basis are you saying that the distance between color and blue is 5? It is either 4 or 15. Nevertheless, where is the code you've attempted and what doesn't seem to be working? – Rahul Nov 20 '13 at 10:18
  • No, the distance is 5, but the way he's asking is weird – Fabinout Nov 20 '13 at 10:18
  • You could do some research before - possible duplicate of [How to compare almost similar Strings in Java? (String distance measure)](http://stackoverflow.com/questions/2084730/how-to-compare-almost-similar-strings-in-java-string-distance-measure) – Eel Lee Nov 20 '13 at 10:19
  • Split the sentence by empty space and work with indexes to find out the so called distance. – Konstantin Yovkov Nov 20 '13 at 10:19
  • @R.J yes , and when there is zero word between two words, the distance between them is 1. – Fabinout Nov 20 '13 at 10:22
  • @Fabinout That's weird.. but you can add 1 to the final result according to my solution. – Maroun Nov 20 '13 at 10:26
  • @R.J That's not weird that's how distance has always been calculated. 'a' and 'b' are 1unit of distance appart in the alphabet, etc. – Fabinout Nov 20 '13 at 10:28
  • 1
    @Fabinout the way I am asking is not weird. I am simply asking about the distance between two word entities in a corpus. What is so weird about that? You saying its weird makes you weird. – modarwish Nov 20 '13 at 10:35
  • "you're the weirdo", "no you're the weirdo". Reddit all the way down. – Fabinout Nov 20 '13 at 10:45

3 Answers3

6

Here is what you can do:

  1. Split the array according to whitespaces.
  2. Get the index of the first word.
  3. Get the index of the second word.
  4. Subtract the indexes, that's the "distance".

Translating this to Java is straightforward.. I leave it for you.

Maroun
  • 94,125
  • 30
  • 188
  • 241
3

This could be an approximation to the solution:

public static void main(String[] args) {
    final String strWords = "The color of the car is blue.";
    final String word1 = "color";
    final String word2 = "blue";

    // Remove any special chars from string
    final String strOnlyWords = strWords.replace(",", "").replace(".", "");

    final List<String> words = Arrays.asList(strOnlyWords.split(" "));
    final int index1 = words.indexOf(word1);
    final int index2 = words.indexOf(word2);
    int distance = -1;

    // Check index of two words
    if (index1 != -1 && index2 != - 1) {
        distance = index2 - index1;
    }

    System.out.println(distance);
}
fjtorres
  • 266
  • 6
  • 13
1

You can do it by following code:

    String s = "The color of the car is blue";
    String[] arr = s.split(" ");
    int startIndex = -1;
    int endIndex = -1;
    for(int i=0; i<arr.length; i++){
        if(arr[i].equals("color")){
            startIndex = i;
        }
        else if(arr[i].equals("blue")){
            endIndex = i;
        }
    }
    System.out.println("distance is: " + (endIndex-startIndex));
Meet
  • 242
  • 1
  • 4
  • 13
  • 3
    Spoon feeding an answer to the OP(specially when there is no effort shown) is discouraged here. – Rahul Nov 20 '13 at 10:26
  • @R.J He is giving the OP a concrete & specific answer. The OP if they want to use it for anything (hint, their homework) will have to generalize it, wrap it in a method, and perhaps optimize it (the given answer is O(n) but you can have it be O(1) if one doesn't count preprocessing). – Lan Nov 20 '13 at 10:29
  • @John - How difficult would it be to put this code in a method? Ahh, see another answer below, within a method now. That's exactly why such a practice is discouraged when there is absolutely no effort shown by the OP. – Rahul Nov 20 '13 at 10:31
  • 1
    @R.J I think code example helps for novice users – Meet Nov 20 '13 at 10:33
  • 1
    @Meet - Trust me. This may help them this time, but later on, they're going to struggle a lot. And it will only encourage them to ask more such questions, which is neither good for the OP or the community. – Rahul Nov 20 '13 at 10:35
  • If you've sometime, read this [open letter to the students](http://meta.programmers.stackexchange.com/q/6166) and a few answers posted for it. It might help you understand a bit more of what I'm trying to say here. – Rahul Nov 20 '13 at 10:37
  • @R.J You may find this interesting: http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html . It eludes to the "struggle later" you point to, and parallels part of what you linked to. – Lan Nov 20 '13 at 10:42
  • The question was not clear. My bad, I will make a new one.. – modarwish Nov 20 '13 at 14:09