Re: [Tutor] extracting phrases and their memberships from syntax

2009-02-14 Thread عماد نوفل
On Fri, Feb 13, 2009 at 10:20 AM, Paul McGuire  wrote:

> Pyparsing has a built-in helper called nestedExpr that fits neatly in with
> this data.  Here is the whole script:
>
> from pyparsing import nestedExpr
>
> syntax_tree = nestedExpr()
> results = syntax_tree.parseString(st_data)
>
> from pprint import pprint
> pprint(results.asList())
>
>
> Prints:
>
> [[['S',
>   ['NP-SBJ-1',
>['NP', ['NNP', 'Rudolph'], ['NNP', 'Agnew']],
>[',', ','],
>['UCP',
> ['ADJP', ['NP', ['CD', '55'], ['NNS', 'years']], ['JJ', 'old']],
> ['CC', 'and'],
> ['NP',
>  ['NP', ['JJ', 'former'], ['NN', 'chairman']],
>  ['PP',
>   ['IN', 'of'],
>   ['NP',
>['NNP', 'Consolidated'],
>['NNP', 'Gold'],
>['NNP', 'Fields'],
>['NNP', 'PLC'],
>[',', ',']],
>   ['VP',
>['VBD', 'was'],
>['VP',
> ['VBN', 'named'],
> ['S',
>  ['NP-SBJ', ['-NONE-', '*-1']],
>  ['NP-PRD',
>   ['NP', ['DT', 'a'], ['JJ', 'nonexecutive'], ['NN', 'director']],
>   ['PP',
>['IN', 'of'],
>['NP',
> ['DT', 'this'],
> ['JJ', 'British'],
> ['JJ', 'industrial'],
> ['NN', 'conglomerate']]],
>   ['.', '.'
>
> If you want to delve deeper into this, you could, since the content of the
> () groups is so regular.  You in essence reconstruct nestedExpr in your own
> code, but you do get some increased control and visibility to the parsed
> content.
>
> Since this is a recursive syntax, you will need to use pyparsing's
> mechanism
> for recursion, which is the Forward class.  Forward is sort of a "I can't
> define the whole thing yet, just create a placeholder" placeholder.
>
> syntax_element = Forward()
> LPAR,RPAR = map(Suppress,"()")
> syntax_tree = LPAR + syntax_element + RPAR
>
> Now in your example, a syntax_element can be one of 4 things:
> - a punctuation mark, twice
> - a syntax marker followed by one or more syntax_trees
> - a syntax marker followed by a word
> - a syntax tree
>
> Here is how I define those:
>
> marker = oneOf("VBD ADJP VBN JJ DT PP NN UCP NP-PRD "
>"NP NNS NNP CC NP-SBJ-1 CD VP -NONE- "
>"IN NP-SBJ S")
> punc = oneOf(", . ! ?")
>
> wordchars = printables.replace("(","").replace(")","")
>
> syntax_element << (
>punc + punc |
>marker + OneOrMore(Group(syntax_tree)) |
>marker + Word(wordchars) |
>syntax_tree )
>
> Note that we use '<<' operator to "inject" the definition of a
> syntax_element - we can't use '=' or we would get a different expression
> than the one we used to define syntax_tree.
>
> Now parse the string, and voila!  Same as before.
>
> Here is the entire script:
>
> from pyparsing import nestedExpr, Suppress, oneOf, Forward, OneOrMore,
> Word,
> printables, Group
>
> syntax_element = Forward()
> LPAR,RPAR = map(Suppress,"()")
> syntax_tree = LPAR + syntax_element + RPAR
>
> marker = oneOf("VBD ADJP VBN JJ DT PP NN UCP NP-PRD "
>"NP NNS NNP CC NP-SBJ-1 CD VP -NONE- "
>"IN NP-SBJ S")
> punc = oneOf(", . ! ?")
>
> wordchars = printables.replace("(","").replace(")","")
>
> syntax_element << (
>punc + punc |
>marker + OneOrMore(Group(syntax_tree)) |
>marker + Word(wordchars) |
>syntax_tree )
>
> results = syntax_tree.parseString(st_data)
> from pprint import pprint
> pprint(results.asList())
>
> -- Paul
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


Thank you so much Paul, Kent, and Hoftkamp.
I was asking what the right tools were, and I got two fully-functional
scripts back. Much more than I had expected.
I'm planning to use these scripts instead of the Perl one. I've also started
with PyParsing as it seems to be a little easier to understand than PLY.
 Thank you again,
-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] extracting phrases and their memberships from syntax

2009-02-14 Thread Paul McGuire
-Original Message-
From: Emad Nawfal (عماد نوفل) [mailto:emadnaw...@gmail.com] 
Sent: Saturday, February 14, 2009 8:59 AM
To: Paul McGuire
Cc: tutor@python.org
Subject: Re: [Tutor] extracting phrases and their memberships from syntax


Thank you so much Paul, Kent, and Hoftkamp.
I was asking what the right tools were, and I got two fully-functional
scripts back. Much more than I had expected.

I'm planning to use these scripts instead of the Perl one. I've also started
with PyParsing as it seems to be a little easier to understand than PLY.


---
Glad to help - actually, I sent 2 working scripts, and Kent sent 1, so I
count THREE fully-functional scripts! ;)  Also, Kent's recursive example
code for traversing the returned parse tree should also work with the data
structure that the pyparsing example returns.

Please make use of the pyparsing samples and documentation available at the
pyparsing wiki - pyparsing.wikispaces.com.  Also, if you use the Windows
binary or easy_install installations, you don't get the help and example
directories - download the source ZIP distribution, and you get a lot more
helpful information.

-- Paul

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Keeping Dictonary Entries Ordered

2009-02-14 Thread Wayne Watson
Title: Signature.html




It looks like a secondary list should do the trick. I'm not adverse to
keeping one. My list is maybe 30-40 items at most. When I posted
originally, I was just trying to find if I had overlooked something. It
appears that without a secondary list, it gets pretty tricky. 

Alan Gauld wrote:

"Eric Dorsey"  wrote 
  

  
config_names
  

  

{'start_time': '18:00:00', 'gray_scale': True, 'long': 120.0}


  
for i, x in config_names.items():
  

  

 print i, x



start_time 18:00:00

gray_scale True

long 120.0

  
  
Thats pretty much a happy coincidence, there is no guarantee that it
will work like that for all dictionaries:
  
  
  

  d = {1:2,3:4,8:9,5:7}

for i,x in d.items(): print i,x

  

  
 8 9
  
1 2
  
3 4
  
5 7
  
  
Alan G.
  
  
___
  
Tutor maillist  -  Tutor@python.org
  
http://mail.python.org/mailman/listinfo/tutor
  
  


-- 


   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)

S, quiet. I'm thinking about filling this space. 





Web Page: 




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] extracting phrases and their memberships from syntax

2009-02-14 Thread عماد نوفل
2009/2/14 Paul McGuire 

> -Original Message-
> From: Emad Nawfal (عماد نوفل) [mailto:emadnaw...@gmail.com]
> Sent: Saturday, February 14, 2009 8:59 AM
> To: Paul McGuire
> Cc: tutor@python.org
> Subject: Re: [Tutor] extracting phrases and their memberships from syntax
>
>
> Thank you so much Paul, Kent, and Hoftkamp.
> I was asking what the right tools were, and I got two fully-functional
> scripts back. Much more than I had expected.
>
> I'm planning to use these scripts instead of the Perl one. I've also
> started
> with PyParsing as it seems to be a little easier to understand than PLY.
>
> 
> ---
> Glad to help - actually, I sent 2 working scripts, and Kent sent 1, so I
> count THREE fully-functional scripts! ;)  Also, Kent's recursive example
> code for traversing the returned parse tree should also work with the data
> structure that the pyparsing example returns.
>
> Please make use of the pyparsing samples and documentation available at the
> pyparsing wiki - pyparsing.wikispaces.com.  Also, if you use the Windows
> binary or easy_install installations, you don't get the help and example
> directories - download the source ZIP distribution, and you get a lot more
> helpful information.
>
> -- Paul
>
> Thanks Paul.
Yesterday I downloaded PyParsing and downloaded "Getting Started with
Pyparsing" from O'Reilly. It is really good. What I like most about
Pyparsing is that it is very easy to understand. Suitable for an
ever-amateur like me. This is all I can say. I have no means of telling what
the difference between it and PLY is.


-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Extract image from RTF file

2009-02-14 Thread Bryan Fodness
I have a large amount of RTF files where the only thing in them is an
image.  I would like to extract them an save them as a png.
Eventually, I would like to also grab some text that is on the image.
I think PIL has something for this.

Does anyone have any suggestion on how to start this?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Extract image from RTF file

2009-02-14 Thread Alan Gauld


"Bryan Fodness"  wrote

I have a large amount of RTF files where the only thing in them is an
image.  I would like to extract them an save them as a png.


The link I posted on another thread claims to
handle images in RTF files


http://www.nava.de/2005/04/06/pyrtf/

HTH.


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: Extract image from RTF file

2009-02-14 Thread Marc Tompkins
Forgot to Reply All.

-- Forwarded message --
From: Marc Tompkins 
Date: Sat, Feb 14, 2009 at 11:35 AM
Subject: Re: [Tutor] Extract image from RTF file
To: Bryan Fodness 


On Sat, Feb 14, 2009 at 8:40 AM, Bryan Fodness wrote:

> I have a large amount of RTF files where the only thing in them is an
> image.  I would like to extract them an save them as a png.
> Eventually, I would like to also grab some text that is on the image.
> I think PIL has something for this.
>
> Does anyone have any suggestion on how to start this?
>

I'm no kind of expert, but I do have a pointer or two...  RTF files are text
with lots and lots of funky-looking formatting, but generally not "binary"
in the sense of requiring special handling (although, now that I just read
about how pictures are stored in them, it seems there might be some
exceptions...)  There's a Python library for dealing with RTF files (
http://www.nava.de/2005/04/06/pyrtf/) but I haven't tried it; if you're
comfortable opening text files and handling their contents, it might be
simpler to roll your own for this task.

You'll want to look at the Microsoft RTF specification, the latest version
of which (1.6) is available here:

http://msdn.microsoft.com/en-us/library/aa140277(office.10).aspx

In particular, you'll be interested in the section on Pictures, which I'll
excerpt here:
Pictures

An RTF file can include pictures created with other applications. These
pictures can be in hexadecimal (the default) or binary format. Pictures are
destinations, and begin with the \*pict* control word. The *\pict* keyword
is preceded by* \*\shppict* destination control keyword as described in the
following example. A picture destination has the following syntax:
  '{' *\pict* (? & ? &  &  &
?)  '}'   |* \emfblip* |* \pngblip*
|*\jpegblip | \macpict
* | *\pmmetafile* | *\wmetafile* | *\dibitmap*  | *\wbitmap *
   *\wbmbitspixel *& *\wbmplanes* & *\wbmwidthbytes*
 (\*picw* & *\pich*) \*picwgoal*? & \*pichgoal*? *\picscalex*? & *
\picscaley*? & *\picscaled*? & *\piccropt*? & *\piccropb*? & *\piccropr*? &
*\piccropl*?   *\picbmp *& *\picbpp*   (\*bin* #BDATA) |
#SDATA


Basically, it looks like you can search for "{\pict", then search for the
closing "}".  Everything in between will be your picture, plus metadata that
tells you how to decode it.

Now that you've caught your rabbit... I'm out of advice; I've never used PIL
(though I used to listen to them all the time.)

-- 
www.fsrtechnologies.com



-- 
www.fsrtechnologies.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Copy file function works, but not with adding the date copied

2009-02-14 Thread cclpianos

Hi Bob,

I found out from Alan Gauld that the file i was attempting to open  
was a binary file. And because of that the strangeness occurred. I  
haven't yet learned to work with the binaries as of yet.


Thanks!
Pat

4. Re: Copy file function works,but not with adding the date
  copied (Alan Gauld) Tutor Digest Vol 60, Issue 69
On Feb 13, 2009, at 6:16 PM, bob gailer wrote:





cclpia...@comcast.net wrote:

Hello,

"but can't open with the application TextEdit"
What are you doing to open it? What does "can't open" mean"? Error  
messages or what?


Could this be a resource fork issue? AFAIK Python does not  
reproduce the resource fork.


--
Bob Gailer
Chapel Hill NC
919-636-4239


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Copy file function works, but not with adding the date copied

2009-02-14 Thread bob gailer
Please don't attach a reply of this nature to comments I made that you 
have not responded to.


If you want our help, please respond to our comments and questions.

cclpia...@comcast.net wrote:

Hi Bob,

I found out from Alan Gauld that the file i was attempting to open was 
a binary file. And because of that the strangeness occurred. I haven't 
yet learned to work with the binaries as of yet.


Thanks!
Pat

4. Re: Copy file function works,but not with adding the date
  copied (Alan Gauld) Tutor Digest Vol 60, Issue 69
On Feb 13, 2009, at 6:16 PM, bob gailer wrote:





cclpia...@comcast.net wrote:

Hello,

"but can't open with the application TextEdit"
What are you doing to open it? What does "can't open" mean"? Error 
messages or what?


Could this be a resource fork issue? AFAIK Python does not reproduce 
the resource fork.


--
Bob Gailer
Chapel Hill NC
919-636-4239


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor




--
Bob Gailer
Chapel Hill NC
919-636-4239
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Loops

2009-02-14 Thread pa yo
Thanks Alan.

after a few hours scratching my head I finally fixed it by removing
all the indents and remaking them in the correct position.

Payo


On Sat, Feb 14, 2009 at 1:52 AM, Alan Gauld  wrote:
> "pa yo"  wrote
>
>> The way I have come up with to solve this problem is to put the
>> following loop in:
>>
 rawfeed = feedparser(http//twitter.com/foobar)
 feedstring = rawfeed.split('=',1)#this splits the feed at the
 first "="
 headline = feedstring[0]#this is the text infront of
 the "=" sign.
 if len(headlinestring) == len(rawfeed)#ie. if the split hasn't
 worked they will be exactly the same >>> body = "\x7b\x7bEmpty\x7d\x7d"
#this adds an "{{Empty}}" template to the wiki
 else:
body = feedstring[1]
>
> Sorry, where is the loop?
>
>> But I can't get it to work - I have tried various indents and break
>> and continues - but I don't know enough about loops to identify what I
>> have done wrong.
>
> I'm confused, there is no loop only a conditional statement (if/else)
> The only loops supported in Python are for and while (OK you
> could count list comprehensions/generator expressions too) and
> neither of them are in your code.
>
> However you don't tell us what is happening that you don;t expect.
> Are you getting an error message? If so what (all of it please)?
> Or is it just the output that is different to what you expect?
> if so what did you expect and what did you get?
>
> And finally which version of Python and which OS please?
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor