1

I am having a long text which i want to split in to small sentences.The following is my text.

I tried Count words in a string method? but there the solution given was to split the string return trim.split("\\s+").length;. In my text i don't have any spaces.

సోషలిజం,అనే,మాటను,గబ్బు,పట్టించడమే,కాంగ్రెసు,వారి,వ్యూహమనీ,అంతవరకూ,ప్రజలలో,సోషలిజం,యెడవున్న,అభిమానాన్ని,ఎక్స్‌ప్లాయిట్,చెయ్యడం,దాని,ఎత్తుగడ,అనీ,అంటే,ఓ,మాటు,నా,మీద,పడిపోయావు,గుర్తుందా.

I know split() is used to split the string. But i don't know how to split above text as there is no space or any other regular expression to split with.

The following code works for splitting the text

String string = "1234,56,789,10,1111111,1111112,12";
char[] ch = string.toCharArray();  
int comma_limit = 3;
int comma_count = 0;
for(int i=0;i<ch.length;i++) 
if (ch[i] == ',') {
    comma_count = comma_count + 1;

if (comma_count % comma_limit == 0)
{
ch[i] = '.';
System.out.println(ch);

     }
  }
16
  • 3
    Can you elaborate what "small sentences" means? Also I have noticed that the text is comma separated. So why not use comma for the split condition? Commented Feb 21, 2019 at 6:29
  • Small sentences means i want to split the above sentence into 2-3 sentences. Commented Feb 21, 2019 at 6:32
  • How do you define sentences if there is no space or any other regular expression to split with? Commented Feb 21, 2019 at 6:33
  • To be honest I do not know what language the sample text is thus I do not know how a sentence can end in that language. What I mean is for example in English language a normal sentence can end in a punctuation marks (period, question mark, etc). Maybe you can use that idea to split your text Commented Feb 21, 2019 at 6:34
  • is there anyway we can take word count and split the text accordingly? Commented Feb 21, 2019 at 6:34

9 Answers 9

2

Use split method with comma separator it will return array of separated strings then by using length method, get its size

System.out.println(yourString.split(",").length);
Sign up to request clarification or add additional context in comments.

2 Comments

While this code may answer the question, please explain why it answers the question.
here spaces are not there in between two words so we need to use split("$character") otherwise directly we could use the split method
1
static IEnumerable<string> Split(string str, int chunkSize)
{
    return Enumerable.Range(0, str.Length / chunkSize)
        .Select(i => str.Substring(i * chunkSize, chunkSize));
}

You need to check for corner cases.

Comments

0

Check this once you will get 3 Strings inside loop, You can increase split count..

    public void splitStr(){
        String str = "";
        String[] split_str = str.split(",");

        int len = split_str.length;

        int split_len = len/3;

        for (int i = 0; i< len; i++){
            String f1 ="";
            if(i == split_len){
                // first string 
                f1 = split_str[i];

                // You will get 3   f1 strings
                split_len += split_len+ i;
            }
        }
    }

1 Comment

Thanks @satyan_android i will try and let you know.
0

In the split use the "," to split based on the comma character. return trim.split(",").length;

3 Comments

Also based on what are you trying to create the small sentences?
The text is too large to synthesize and the synthesizer crashes. That's why now i am trying to split the text into 2 or 3
In the split use an addition parameter to limit the number of split, .split(",", n); n is an example it is used for splitting n times. If the text is too large then divide the whole thing into two or three parts using the substring method.
0

You can split using the comma character

String text = "సోషలిజం,అనే,మాటను,గబ్బు,పట్టించడమే,కాంగ్రెసు,వారి,వ్యూహమనీ,అంతవరకూ,ప్రజలలో,సోషలిజం,యెడవున్న,అభిమానాన్ని,ఎక్స్\u200Cప్లాయిట్,చెయ్యడం,దాని,ఎత్తుగడ,అనీ,అంటే,ఓ,మాటు,నా,మీద,పడిపోయావు,గుర్తుందా.";
String[] lines = text.split(",");
for (int i = 0; i < lines.length; i++) {
    System.out.println("SENTANCE  " + i + "  : "+ lines[i]);
}

Comments

0

Use String Builder instead of string

StringBuilder sb = new StringBuilder();
sb.append("సోషలిజం,అనే,మాటను,గబ్బు,పట్టించడమే,కాంగ్రెసు,వారి,వ్యూహమనీ,అంతవరకూ,ప్రజలలో,సోషలిజం,యెడవున్న,అభిమానాన్ని,ఎక్స్‌ప్లాయిట్,చెయ్యడం,దాని,ఎత్తుగడ,అనీ,అంటే,ఓ,మాటు,నా,మీద,పడిపోయావు,గుర్తుందా");

int totalString = sb.toString().splitby(",").lenght();

1 Comment

Try StringBuilder because it can store more data than string
0

Here is substring example you have asked.

String longText = "సోషలిజం,అనే,మాటను,గబ్బు,పట్టించడమే,కాంగ్రెసు,వారి,వ్యూహమనీ,అంతవరకూ,ప్రజలలో,సోషలిజం,యెడవున్న,అభిమానాన్ని,ఎక్స్\u200Cప్లాయిట్,చెయ్యడం,దాని,ఎత్తుగడ,అనీ,అంటే,ఓ,మాటు,నా,మీద,పడిపోయావు,గుర్తుందా.";
int longTextLength = longText.length();
int partLength  = (int) longTextLength / 3;
String part1 = longText.substring(0, partLength);
String part2 = longText.substring(partLength, 2*(partLength));
String part3 = longText.substring(2*(partLength), longTextLength);

Comments

0

If you want to split it into specific parts use this.

String text = "SAMPLEs"; // <- this will contain the large text
int numberOfParts = 2; // the number of split parts

int partLength = text.length() / numberOfParts;

ArrayList<String> parts = new ArrayList<>();

for (int i = 0; i < numberOfParts; i++) {
    int start = partLength * i;
    int end = start + partLength;
    parts.add(text.substring(start, end));
    if (text.length() - end < partLength) {
        parts.add(text.substring(end, text.length()));
    }
}

for (int i = 0; i < parts.size(); i++) {
    System.out.println("PART " + i + " contains : " + parts.get(i));
}

Result:

PART 0 contains : SAM
PART 1 contains : PLE
PART 2 contains : s

Comments

0

I am confused by what you are asking. Assuming that you are looking to implement some sort of word wrapping, you can do as follows. It may not be the Best way to do it, but it is a way to do it.

divideString("This is my sentence! I would like to split this into 3 Strings with about the same length.", 3);

public static void divideString(String raw, int numberOfDivides) {
    int charsPerString = raw.length()/numberOfDivides;
    String[] refined = new String[charsPerString];
    for(int i=1; i < (raw.length()/charsPerString)+1; i++) {
        refined[i] = raw.substring((charsPerString*i)-charsPerString, charsPerString*i);
        System.out.println(refined[i]);
    }
}

Which would output the following:

This is my sentence! I would l
ike to split this into 3 Strin
gs with about the same length.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.