[Tutor] swapping list elements based on some criterion

Emad Nawfal (عمـ نوفل ـاد) emadnawfal at gmail.com
Sat Oct 8 06:22:47 CEST 2011


Hello Tutors,
It's been quite some time since I last posted something here, and now I'm
back with a question:

I want to re-structure English so that the adjectives appear after the
nouns, instead of before.
If I have a sentence like:

The tall man plays well
I need to change it to
The man tall plays well
So far, I have thought of the following function, but I'm not sure it's the
best solution. It assumes that the sentence has been augmented with part of
speech tags
(Also plz note that there may be multiple adjectives)

######################
test = "the/DET tall/ADJ man/N plays/VBZ well/ADV"

def swapAdjNoun(sentence):
    sentence = sentence.split()
    new = []

    x = enumerate(sentence)
    for idx, lex in x:
        if lex.endswith("/N") and sentence[idx-1].endswith("/ADJ"):
            idx = idx - 1
        elif lex.endswith("/ADJ") and sentence[idx+1].endswith("/N"):
            idx = idx + 1
        new.append((idx, lex))
    new.sort()
    new = [ elm[1] for elm in new]
    return  new

####################

I'd appreciate if if you  gave me some advice on how to do this the right
way.
-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal

--------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111008/138722a6/attachment-0001.html>


More information about the Tutor mailing list