hello everyone,

im having a little bit of trouble with my python and i was wondering if someone could please give me a hand with it :)

basically, what i am trying to achieve is exactly what is going on in this thread http://ubuntuforums.org/archive/index.php/t-803060.html except in my example, the dict words have multiple words inside them, so parsing through a split string 1 word at a time is not possible. i know it will have something to do with checking the dictionary first, comparing it to the string then replacing it... but im just having a bit of trouble putting that into code form :icon_razz:

this is my reflections array:

reflections = \
[
 ["yourself",       "myself"],
 ["your",           "my"],
 ["Your",           "my"],
 
 ["myself",         "yourself"],
 ["my",             "your"],
 ["My",             "your"],
 
 ["you and I",      "we"], # In some contexts "we" should be "us"
 ["You and I are",  "We are"],
 ["You and I",      "we"],
 
 ["you and me",     "us"],
 ["You and me",     "we"], # Incorrect, but colloquially common, grammar
 ["me and you",     "us"],
 ["Me and you",     "we"],
 
 # Grammar: The first person singular pronoun is "I" when it's a
 # subject and "me" when it's an object.
 #
 ["to you",         "to me"], # Some person as object cases
 ["for you",        "for me"],
 ["with you",       "with me"],
 ["at you",         "at me"],
 ["see you",        "see me"],
 ["keep you",       "keep me"],
 ["stop you",       "stop me"],
 ["love you",       "love me"],
 ["like you",       "like me"],
 ["hit you",        "hit me"],
 ["loves you",      "loves me"],
 ["likes you",      "likes me"],
 ["thank you",      "thank me"],
 ["thanks you",     "thanks me"],
 ["hate you",       "hate me"],
 ["hates you",      "hates me"],
 ["about you",      "about me"],
 
 ["you",            "I"], # Otherwise assume person is subject
 ["You",            "I"],
 
 ["I",              "you"],
 ["I'm",            "you are"],
 ["I've",           "you have"],
 
 ["me",             "you"],

 # Grammar: "Are" (and its derivatives "am", "is", "were", "was",
 # etc) is an irregular verb with an exceptionally complicated set
 # of conjugation rules.  The "reflections" appearing below are
 # totally inadequate to account for its full complexity!
 #
 ["am",             "are"],
 ["Am",             "are"],

 ["you are",        "I am"],
 ["you're",         "I am"],
 ["You are",        "I am"],
 ["You're",         "I am"],
 ["we are",         "we are"], # These are here to prevent changes to "are"!
 ["We are",         "we are"],
 ["they are",       "they are"],
 ["They are",       "they are"],
 ["Are they",       "are they"],
 ["Are we",         "are we"],
 ["are",            "am"], # In plural contexts "are" should be left unchanged
 ["Are",            "am"],

 ["he was",         "he was"], # Some singular cases
 ["she was",        "she was"],
 ["He was",         "he was"],
 ["She was",        "she was"],
 
 ["was",            "were"],
 ["were",           "was"],
 ["Was",            "were"],
 ["Were",           "was"],

 ["yours",          "mine"],
 ["mine",           "yours"]

 ]

thanks in advance! im just feeling pretty lost at where to begin with it

why are you using a list not a dictionary?

thats just how it was given to me in the data

data_dict = dict(reflections) will assign it to a dict for me to use. i can get it working with the 1 word at a time method but when it comes to using dict items with more then 1 word it gets a bit more complex.

cheers for the reply :)

I wrote a little code which takes your data and builds a regular expression to find the expressions in your list. The following code snippet will print the regex, so that you can see it and allow you to test it when you enter sentences.

# add reflections = [ ... here
import re

class Group(set):
    def __init__(self, *args, **kwd):
        set.__init__(self, *args, **kwd)
        self.optional = 0
        self.subgroups = None

    def group(self):
        self.subgroups = {}
        for s in self:
            if s[0] not in self.subgroups:
                self.subgroups[s[0]] = Group()
            if len(s) == 1:
                self.subgroups[s[0]].optional = 1
            else:
                self.subgroups[s[0]].add(s[1:])
        for k, g in self.subgroups.items():
            g.group()
        self.shrink()
        
    def shrink(self):
        D = {}
        for x, g in self.subgroups.items():
            if len(g.subgroups) == 1:
                k, h = g.subgroups.popitem()
                D[x+k] = h
                h.optional &= g.optional
            else:
                D[x] = g
        self.subgroups = D
        
    def regex(self):
        self.group()
        L = []
        for x, g in self.subgroups.items():
            if not len(g):
                L.append(x)
                continue
            L.append(x+"(?:%s)"%g.regex() + ("","?")[g.optional])
        return "|".join(L)

G = Group()
spaces = re.compile(r"\s+")
for x in reflections:
    m= " ".join(spaces.split(x[0]))
    G.add(m)

pat = "\\s+".join(G.regex().split())
print pat
pattern = re.compile(pat)


def test():
    while True:
        sentence = raw_input("Enter a sentence: ")
        m = pattern.findall(sentence)
        print m

test()

There is a little bug in the shrink function, replace it by

def shrink(self):
        D = {}
        for x, g in self.subgroups.items():
            if len(g.subgroups) == 1 and not g.optional:
                k, h = g.subgroups.popitem()
                D[x+k] = h
                h.optional &= g.optional
            else:
                D[x] = g
        self.subgroups = D

I'll give you some clues about the algorithm later if you're interested.

oh wow, cheers for posting!! i think its a little more complex then what we are after though, i dont think we are allowed regular expressions :( this is just an introductory unit into python (its for my assignment, im not gonna hide that haha) so im not after the complete kinda code, just a pointer to some built in methods or some code examples i could adapt and change into my own, cos at the moment in just at a loss on how to go about it

cheers for the help though :) really appreciate it

ok, so build your own method. Anyway I think my code is a little buggy :)

yeah im sure ill work it out eventually, its just been driving me insane so i thought id post anyways haha

:) thanks

hey, i did this. This works fine for me.

dic = {'like you': 'like me', "You're": 'I am', 'You are': 'I am', 'She was': 'she was',
       'you are': 'I am', 'about you': 'about me', 'am': 'are', 'she was': 'she was',
       'yourself': 'myself', 'are': 'am', 'You': 'I', 'Was': 'were', 'your': 'my',
       'keep you': 'keep me', 'You and I are': 'We are', "you're": 'I am', "I've": 'you have',
       'stop you': 'stop me', 'hates you': 'hates me', 'Are we': 'are we', 'Are they': 'are they',
       'You and I': 'we', 'You and me': 'we', 'loves you': 'loves me', 'They are': 'they are',
       'hate you': 'hate me', 'you': 'I', 'was': 'were', 'see you': 'see me', 'likes you': 'likes me',
       'Me and you': 'we', 'we are': 'we are', ' I ': 'you', 'for you': 'for me', 'me and you': 'us',
       'mine': 'yours', 'We are': 'we are', 'love you': 'love me', 'thanks you': 'thanks me',
       'you and me': 'us', 'Were': 'was', "I'm": 'you are', 'My': 'your', 'me': 'you', 'He was': 'he was',
       'myself': 'yourself', 'hit you': 'hit me', 'they are': 'they are', 'Am': 'are', 'yours': 'mine',
       'thank you': 'thank me', 'he was': 'he was', 'at you': 'at me', 'Are': 'am', 'were': 'was',
       'to you': 'to me', 'with you': 'with me', 'my': 'your', 'Your': 'my', 'you and I': 'we'}

line = raw_input("Enter your line")

def changeLine(line):
    line = line.lower()
    for f in dic.keys():
        f = f.lower()
        if f in line:
         
            index = line.index(f)
            new = dic[f]
           
            
            line = line[:index]+new.upper()+line[index+len(dic[f])+1:]
           
    return line
line = changeLine(line)
print "Line:",line
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.