6 Replies - 643 Views - Last Post: 27 November 2012 - 05:12 PM Rate Topic: -----

#1 shad0wk1   User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 139
  • Joined: 01-October 10

Split string

Posted 27 November 2012 - 08:34 AM

I am in the middle of programming rules of inference... thingy.

Anyway, I want to split word like these:

"If it snows today, then we will go skiiing." -> "it snows today" , "we will go skiing".
"It is not Friday." -> "It is Friday"

Currently I know about using split() to split string. But, I don't know how to split the way I want to. the one below just my test class to find it.

public class test2
{
    public static void main(String [] args)
    {
        String str = "If it snows today, then we will go skiing";
        String [] result = str.split(" ");
        for (String string : result)
            System.out.println(string);
    }
}



I planned to make the program find pattern of if p then q, not p, p implies q, p if only if q and etc. But, I don't think I can use StringTokenizer to find pattern.

This post has been edited by shad0wk1: 27 November 2012 - 08:43 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Split string

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Split string

Posted 27 November 2012 - 08:42 AM

Splitting on " " will produce an array with each word in the sentence. You want to split on your logical terms - and, or, not, if, then.
Not sure I'd really see split as the best way to go here, but for what it's worth,
"If it snows today, then we will go skiing".split("then") returns the array
{""If it snows today, ", "we will go skiing"}

This post has been edited by jon.kiparsky: 27 November 2012 - 08:42 AM

Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Split string

Posted 27 November 2012 - 10:28 AM

May be g00se Regex will have the magic pattern :)
Was This Post Helpful? 0
  • +
  • -

#4 oQMr FoxQo   User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 123
  • Joined: 16-August 09

Re: Split string

Posted 27 November 2012 - 10:34 AM

View Postpbl, on 27 November 2012 - 10:28 AM, said:

May be g00se Regex will have the magic pattern :)/>

I agree with pbl, and moreover you should create a library of the words you want them to be used as a separator in your sentence
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Split string

Posted 27 November 2012 - 10:39 AM

Parsing natural language is a slippery slope. Have you written the back end for this yet? If you assume the parser works and pass in "parsed" expression objects, does it do what you want?
That's probably where I'd be concentrating now. Later on you can think about how to get the expressions in. (for example, define a little metalanguage for your users, something as simple as "put logical operators in ALL CAPS", or use a GUI front end of some sort)
Was This Post Helpful? 0
  • +
  • -

#6 shad0wk1   User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 139
  • Joined: 01-October 10

Re: Split string

Posted 27 November 2012 - 01:09 PM

View Postjon.kiparsky, on 27 November 2012 - 08:42 AM, said:

Splitting on " " will produce an array with each word in the sentence. You want to split on your logical terms - and, or, not, if, then. Not sure I'd really see split as the best way to go here, but for what it's worth, "If it snows today, then we will go skiing".split("then") returns the array {""If it snows today, ", "we will go skiing"}

Yeah, but just for now, I only know this. I got an idea from your reply.
public class test2
{
    public static void main(String [] args)
    {
        String str = "If it snows today, then we will go skiing";
        System.out.println("str="+str);
        if (str.regionMatches(true, str.indexOf("If"), "if", 0, 2) && str.regionMatches(true, str.indexOf("then"), "then", 0, 4))
        {
            System.out.println("Found if-then");
            String [] result = str.split(", ");
            String [] result2 = result[0].split("If ");
            System.out.println("p="+result2[1]);                  
            String [] result3 = result[1].split("then ");
            System.out.println("q="+result3[1]);
        }
    }
}



View Postpbl, on 27 November 2012 - 10:28 AM, said:

May be g00se Regex will have the magic pattern :)/>

I guess I need to learn the new class.

View PostoQMr FoxQo, on 27 November 2012 - 10:34 AM, said:

View Postpbl, on 27 November 2012 - 10:28 AM, said:

May be g00se Regex will have the magic pattern :)/>/>
I agree with pbl, and moreover you should create a library of the words you want them to be used as a separator in your sentence

Library? I do want to store the Pattern in an ArrayList or something.

This is a group project, and I am at present finding how to store the statements and splitting them. Main program shall be in GUI, but that's from my teammate.

Different part I am planning:
0. Main program (receive input hypothesis in form of English sentences). I planned to use only simple expression for now to find right algorithm.
1. Store Statement(a custom class)
2. Methods to recognize pattern
3. Methods to split and store into statements. I will set my custom class Statement to automatically assigned to a literal [from 'a' to 'z' for now]. I think it is more appropriate I store facts I split from hypothesis.
4. Store the literals pattern of statement. Or I can set it in custom class Statement, but I still no general idea about it.
5. Methods to evaluate statements/hypothesis in form of literals to find conclusion from all hypothesis.

I'll be back after learning and trying Regex[Matcher and Pattern].
Was This Post Helpful? 0
  • +
  • -

#7 shad0wk1   User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 139
  • Joined: 01-October 10

Re: Split string

Posted 27 November 2012 - 05:12 PM

I got for IF_THEN pattern for now.

//Fig 30.24: RegexMatches.java
//Demonstrating Classes Pattern and Matchers.
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches
{
    public static void main(String [] args)
    {
        //create regular expression
        Pattern expression = Pattern.compile( "J.*\\d[0-35-9]-\\d\\d-\\d\\d");
        String string1 = "Jane's Birthday is 05-12-75\n" +
            "Dave's Birthday is 11-04-68\n" +
            "John's Birthday is 04-28-73\n" +
            "Joe's Birthday is 12-17-77\n";
            
        //match regular expression to string and print matches
        Matcher matcher = expression.matcher(string1);
        while(matcher.find())
            System.out.println(matcher.group());
        /*------------------------------------------------------------------------*/    
        System.out.println("***********");
        
        Pattern IF = Pattern.compile("IF.*THEN.*",Pattern.CASE_INSENSITIVE);
        String str = "If it snows today, then we will go skiing\n"
        +"It snows today.";
        System.out.println("str="+str);
        matcher = IF.matcher(str);
        while(matcher.find())
            System.out.println("Matcher: "+matcher.group());
        /*------------------------------------------------------------------------*/    
        System.out.println("***********");
            
        String str2 = "It snows today.";
        System.out.println("str2="+str2);
        matcher = IF.matcher(str2);
        while(matcher.find())
            System.out.println("Matcher: "+matcher.group());
    }//end main
}//end class RegexMatches

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1