<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'Problems with nested loops…' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'Problems with nested loops…' posted on the 'Python' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2012 Programmers Heaven</copyright>
    <pubDate>Wed, 23 May 2012 23:55:31 -0700</pubDate>
    <lastBuildDate>Wed, 23 May 2012 23:55:31 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>Problems with nested loops…</title>
      <link>http://www.programmersheaven.com/mb/python/414045/414045/problems-with-nested-loops/</link>
      <description>Hi!&lt;br /&gt;
I’m going to explain to you in details of what I want to achieve.&lt;br /&gt;
I have 2 programs about dictionaries.&lt;br /&gt;
The code for program 1 is here:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;import re
words = {'i':'jeg','am':'er','happy':'glad'}

text = "I am happy.".split()
translation = []

for word in text:
    word_mod = re.sub('[^a-z0-9]', '', word.lower())
    punctuation = word[-1] if word[-1].lower() != word_mod[-1] else ''
    if word_mod in words:
        translation.append(words[word_mod] + punctuation)
    else:
        translation.append(word)
translation = ' '.join(translation).split('. ')
print('. '.join(s.capitalize() for s in translation))

&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
This program has following advantages:&lt;br /&gt;
-	You can write more than one sentence&lt;br /&gt;
-	You get the first letter capitalized after “.”&lt;br /&gt;
-	The program “append” the untranslated word to the output (“translation = []”)&lt;br /&gt;
Here is the code for program 2:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;words = {('i',): 'jeg', ('read',): 'leste', ('the', 'book'): 'boka'}
max_group = len(max(words))

text = "I read the book".lower().split()
translation = []

position = 0
while text:
    for m in range(max_group - 1, -1, -1):
        word_mod = tuple(text[:position + m])
        if word_mod in words:
            translation.append(words[word_mod])
            text = text[position + m:]
    position += 1

translation = ' '.join(translation).split('. ')
print('. '.join(s.capitalize() for s in translation))
&lt;/pre&gt;&lt;br /&gt;
With this code you can translate idiomatic expressions or &lt;br /&gt;
“the book” to “boka”.&lt;br /&gt;
Here is how the program proceeds the codes.&lt;br /&gt;
This is the output:&lt;br /&gt;
1&lt;br /&gt;
('i',)&lt;br /&gt;
['jeg']&lt;br /&gt;
['read', 'the', 'book']&lt;br /&gt;
0&lt;br /&gt;
()&lt;br /&gt;
1&lt;br /&gt;
('read', 'the')&lt;br /&gt;
0&lt;br /&gt;
('read',)&lt;br /&gt;
['jeg', 'leste']&lt;br /&gt;
['the', 'book']&lt;br /&gt;
1&lt;br /&gt;
('the', 'book')&lt;br /&gt;
['jeg', 'leste', 'boka']&lt;br /&gt;
[]&lt;br /&gt;
0&lt;br /&gt;
()&lt;br /&gt;
Jeg leste boka&lt;br /&gt;
What I want is to implement some of the codes from program 1 into program 2.&lt;br /&gt;
I have tried many times with no success…&lt;br /&gt;
Here is my dream…:&lt;br /&gt;
If I change the text to the following…:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;text = "I read the book. I read the book! I read the book? I read the book.".lower().split()&lt;/pre&gt;&lt;br /&gt;
I want the output to be:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;Jeg leste boka. Jeg leste boka! Jeg leste boka? Jeg leste boka.&lt;/pre&gt;&lt;br /&gt;
So please, tweak your brain and help me with a solution…&lt;br /&gt;
I appreciate any reply very much!&lt;br /&gt;
Thank you very much in advance!&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/python/414045/414045/problems-with-nested-loops/</guid>
      <pubDate>Mon, 01 Mar 2010 07:57:35 -0700</pubDate>
      <category>Python</category>
    </item>
  </channel>
</rss>
