Splitting Tree

Discussion in 'Python' started by subhabangalore@gmail.com, Dec 2, 2012.

  1. Guest

    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.
     
    , Dec 2, 2012
    #1
    1. Advertising

  2. Guest

    On Sunday, December 2, 2012 5:39:32 PM UTC+5:30, 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.
     
    , Dec 2, 2012
    #2
    1. Advertising

  3. On 02Dec2012 07:02, <> wrote:
    | On Sunday, December 2, 2012 5:39:32 PM UTC+5:30, 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",8),
    (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 <>

    I'm not making any of this up you know. - Anna Russell
     
    Cameron Simpson, Dec 2, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Ahern

    Tree splitting/merging

    William Ahern, Nov 4, 2003, in forum: XML
    Replies:
    3
    Views:
    722
    sylvain.loiseau
    Nov 4, 2003
  2. John Ericson
    Replies:
    0
    Views:
    427
    John Ericson
    Jul 19, 2003
  3. Mark
    Replies:
    0
    Views:
    442
  4. John Dibling
    Replies:
    0
    Views:
    417
    John Dibling
    Jul 19, 2003
  5. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,131
Loading...

Share This Page