Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

looking simple regexp for arff file format

by thealienz1 (Pilgrim)
on Nov 29, 2005 at 06:26 UTC ( #512534=perlquestion: print w/replies, xml ) Need Help??

thealienz1 has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks

I am currently working on a data mining algorithm program in Perl, which requires the need to read the arff file format. Currently, I have gotten as far as I can in a regular expression I am working on.

The regexp is being designed to load from the arff file the @attribute directive. Currently I am trying to implement the different possible formats of this into a single regexp.

possible values
  • @attribute 'Sex' { Male, Female}
  • @attribute 'Feature 0' real
  • @attribute 'Feature 1' real
  • @attribute 'Feature 2' real
  • @attribute Malic_acid REAL
  • @attribute Ash REAL

With my regexp I am trying to pull the name and type from directive line. So I would get the name=Sex and type=Male, Female or name=Feature 0 and type=real, etc... Currently this the code I have written for testing.

my @lines = ( "@attribute 'Sex' { Male, Female}", "@attribute 'Feature 0' real", "@attribute 'Feature 1' real", "@attribute 'Feature 2' real", "@attribute Malic_acid REAL", "@attribute Ash REAL" ); foreach my $line (@lines) { $line =~ /^@attribute\s+[']?(.*?)[']?\s+[{]?(.*?)[}]?$/; print "relation name: $1\n"; print "type: $2\n"; }

With the current output of the program being

perl test.pl relation name: Sex type: Male, Female relation name: Feature type: 0' real relation name: Feature type: 1' real relation name: Feature type: 2' real relation name: Malic_acid type: REAL relation name: Ash type: REAL

As you can see the relation name for the Feature 0,1,2 are not being correctly read. I no I am so close, but it is getting late, and I have no idea where I am going with this regexp. I though I'd leave it open for the monks to tinker with.

Night

Replies are listed 'Best First'.
Re: looking simple regexp for arff file format
by l.frankline (Hermit) on Nov 29, 2005 at 07:29 UTC

    Hi,

    I have made a small change.. it works..

    my @lines = (
        "@attribute 'Sex' { Male, Female}",
        "@attribute 'Feature 0' real",
        "@attribute 'Feature 1' real",
        "@attribute 'Feature 2' real",
        "@attribute Malic_acid REAL",
        "@attribute Ash REAL"
        );
    foreach my $line (@lines) {
        $line =~ /^@attribute\s+([']?(?:[^\']*)[']?)\s+[{]?(.*?)[}]?$/;
        print "relation name: $1\n";
        print "type: $2\n";
    }

    Regards
    Franklin

    Don't put off till tomorrow, what you can do today.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://512534]
Approved by spiritway
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2019-10-23 06:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?