-2

I want to beak a sentence which has no spaces between next words.

Example :

String str = "johncancomewithme";

I want to print the string as follows:

"john can come with me"

My main concern is about occurrence of proper noun

Ilham
  • 33
  • 1
  • 1
  • 6
  • 1
    You need to have a big list of all the words to achieve this. – moritzg Jun 12 '17 at 06:43
  • I have big list of words. but i don't have any idea about how to do this in java. – Ilham Jun 12 '17 at 06:44
  • 2
    You need to put in effort and try something on your own. – cs95 Jun 12 '17 at 06:44
  • my main concern is about the occurrence of proper noun. – Ilham Jun 12 '17 at 06:45
  • 1
    Don't do this in Java first or any language for that matter. First, figure out the algorithm with pencil and paper or using a whiteboard. – Michael Markidis Jun 12 '17 at 06:46
  • i think you could make a loop too scan a sub string is a word – Felix Jun 12 '17 at 06:46
  • Voted to close. – Michael Markidis Jun 12 '17 at 06:51
  • 2
    the problem is bigger than you think. consider `catsmile` is it `cat smile` or `cats mile` the example may look convoluted but the point is that putting words together may create other combinations – Sharon Ben Asher Jun 12 '17 at 07:04
  • 1
    One would need much more than a flat list of known words to disambiguate such a phrase (see famous examples "childrenslaughter", "burntherapist", etc), especially if requirements such as "detect proper nouns" are unstated in main question and appear in comments. Voting to close as too broad. – Hugues M. Jun 12 '17 at 07:07

1 Answers1

0

There are many factors to consider. Can the string have multiple valid sentences (herowedashore = he rowed ashore AND hero wed a shore)? Do you have a list of all the words you need, including names and proper nouns? If so, this should be easily solvable.

First you'll probably want to load the list of words in memory, or if it's too large, have some sort of caching system in place for frequently used words. I think a Java HashMap would be the quickest way to store your bank of words.

Start by splitting your String into an array of one character Strings (there are many alternatives here, this is just one way):

String[] splitStr = str.split("");

Then, you want to loop thorugh your array from index 0. Loop through each letter adding it to a temporary String until your temp string forms a complete word. Then, bank it in a stack implementation (you could use another array as well, perhaps a Java ArrayList):

String word = "";
for (int i = 0; i < splitStr.length; i++) {
    word = word + splitStr[i];
    if (mapOfValidWords.get(word) != null ){ 
        sentence.push(word); //Assuming sentence is a stack implementation
        word = "";
    }

Continue doing this until you get to the end of the String. If at the end of the loop you have any remaining characters (word != "") and it isn't a match for your list of words, you can assume that one or more of the words were not what this sentence requires.

If the original string was "hewillwalktowardsthecastle", your first iteration will give you "he will walk to ward sthecastle""

You'll need to pop the previously accepted word off of your stack "ward", decrement your counter by the length of that word, and skip over your previously deemed valid word to see if there's another valid word that longer. If not, you'll have to go further back and pop the next word of the stack, doing the same. In this case, you'll get "he will walk to wardsthecastle", then finally "he will walk towards the castle".

Assuming a valid input, eventually you'll reach a point where your sentence is separated into different values in the stack, and you can reconstruct it from there.

This is only returning one answer. If you need to return multiple answers then once you reach the end of your first answer, you'll need to go through your string again, and every "hit" of a word from the previous iteration needs to be tried by going one letter again, this time by continuing past the point where you stopped on your first go through.

This could likely be done through recursion, as you'll have a tree like structure. Your first sentence will be the root case, and each level deeper will attempt to go further with a word that succeeded in the first sentence. If that level of recursion finds a match, it will for it's own branch of recursion where this happens again. Eventually you'll reach a point where no more sentences can be formed from the String.

jmclaughlin
  • 57
  • 1
  • 5