Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Splitting Tree

Reply
Thread Tools

Splitting Tree

 
 
subhabangalore@gmail.com
Guest
Posts: n/a
 
      12-02-2012
Dear Group,

I am using NLTK and I used the following command,

chunk=nltk.ne_chunk(tag)
print "The Chunk of the Line Is:",chunk


The Chunk of the Line Is: (S
''/''
It/PRP
is/VBZ
virtually/RB
a/DT
homecoming/NN
,/,
''/''
said/VBD
(PERSON Gen/NNP Singh/NNP)
on/IN
arrival/NN)

Now I am trying to split the output preferably by ",/,".

But how would I split a Tree object in python.

If I use command like,
chunk_word=chunk.split()

It is giving me the error as,

File "C:/Python27/docstructure1.py", line 38, in document_structure1
chunk1=chunk.split()
AttributeError: 'Tree' object has no attribute 'split'

If anyone of the learned members of the room can kindly help.

Regards,
Subhabrata.
 
Reply With Quote
 
 
 
 
subhabangalore@gmail.com
Guest
Posts: n/a
 
      12-02-2012
On Sunday, December 2, 2012 5:39:32 PM UTC+5:30, (E-Mail Removed) wrote:
> Dear Group,
>
>
>
> I am using NLTK and I used the following command,
>
>
>
> chunk=nltk.ne_chunk(tag)
>
> print "The Chunk of the Line Is:",chunk
>
>
>
>
>
> The Chunk of the Line Is: (S
>
> ''/''
>
> It/PRP
>
> is/VBZ
>
> virtually/RB
>
> a/DT
>
> homecoming/NN
>
> ,/,
>
> ''/''
>
> said/VBD
>
> (PERSON Gen/NNP Singh/NNP)
>
> on/IN
>
> arrival/NN)
>
>
>
> Now I am trying to split the output preferably by ",/,".
>
>
>
> But how would I split a Tree object in python.
>
>
>
> If I use command like,
>
> chunk_word=chunk.split()
>
>
>
> It is giving me the error as,
>
>
>
> File "C:/Python27/docstructure1.py", line 38, in document_structure1
>
> chunk1=chunk.split()
>
> AttributeError: 'Tree' object has no attribute 'split'
>
>
>
> If anyone of the learned members of the room can kindly help.
>
>
>
> Regards,
>
> Subhabrata.


Sorry to ask this. I converted in string and then splitted it.
 
Reply With Quote
 
 
 
 
Cameron Simpson
Guest
Posts: n/a
 
      12-02-2012
On 02Dec2012 07:02, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:
| On Sunday, December 2, 2012 5:39:32 PM UTC+5:30, (E-Mail Removed) wrote:
| > I am using NLTK and I used the following command,
| > chunk=nltk.ne_chunk(tag)
| >
| > print "The Chunk of the Line Is:",chunk
| >
| > The Chunk of the Line Is: (S
| > ''/''
| > It/PRP
[...]
| > Now I am trying to split the output preferably by ",/,".
[...]
|
| Sorry to ask this. I converted in string and then splitted it.

I'm glad you solved your problem, but I would like to point out that
this is generally a risky way of manipulating data.

The problem arises if the string you're splitting on occurs as a literal
piece of text, but _not_ in the sense you intend. It may be the case
that it will not happen in your particular situation, but in general the
procedure:
- convert structure to string somehow
- perfect simple text manipulation
- unconvert
is at risk of simplistic parsing of the string.

A common example is with CSV data. Supposing you wanted the the third
column from an array of tuples:

rows = [ (1,2,"A",4),
(5,6,"B",,
(9,10,"C,D",12),
]

and you wanted [ "A", "B", "C,D" ]. If one went with the "convert to
text" approach, and decided that converting each tuple to a CSV style
data row was a good idea you might write:

column_3 = []
for row in rows:
csv_string = ",".join( str(item) for item in row )
item3 = csv_string.split(",")[2]
column_3.append(item3)

The (simplistic) code above with give you "C" from the third row, not
"C,D". Because it naively assumes there are no commas in the data, and
then does a simplistic textual split to find the third column.

Obviously you woldn't really do that for something this simple; it is to
show the issue. But your situation where manipulating a tree was tricky
and you converted it to a string is very similar conceptually.

Hoping this shows you the issue,
--
Cameron Simpson <(E-Mail Removed)>

I'm not making any of this up you know. - Anna Russell
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
B tree, B+ tree and B* tree Stub C Programming 3 11-12-2003 01:51 PM
Tree splitting/merging William Ahern XML 3 11-04-2003 11:39 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Dibling C++ 0 07-19-2003 04:41 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? Mark C++ 0 07-19-2003 04:24 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Ericson C++ 0 07-19-2003 04:03 PM



Advertisments