Hi!
I’m going to explain to you in details of what I want to achieve.
I have 2 programs about dictionaries.
The code for program 1 is here:

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))

This program has following advantages:
- You can write more than one sentence
- You get the first letter capitalized after “.”
- The program “append” the untranslated word to the output (“translation = []”)
Here is the code for program 2:

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))

With this code you can translate idiomatic expressions or
“the book” to “boka”.
Here is how the program proceeds the codes.
This is the output:

1
('i',)


0
()
1
('read', 'the')
0
('read',)


1
('the', 'book')

[]
0
()
Jeg leste boka

What I want is to implement some of the codes from program 1 into program 2.
I have tried many times with no success… :(
Here is my dream…: ;)
If I change the text to the following…:

text = "I read the book. I read the book! I read the book? I read the book.".lower().split()

I want the output to be:

Jeg leste boka. Jeg leste boka! Jeg leste boka? Jeg leste boka.

So please, tweak your brain and help me with a solution… :icon_biggrin:
I appreciate any reply very much!
Thank you very much in advance!

Recommended Answers

All 2 Replies

If you want to go further, you must write functions in your program. Here is a function which splits the sentences in a text, assuming that a sentence ends with one or more ".!?".

import re
end_of_sentence = re.compile(r"([\.\!\?]+)")

def split_sentences(text):
    """split_sentences(text) --> list of pairs (sentence, punctuation)"""
    L = end_of_sentence.split(text)
    result = []
    for i in range(0, len(L)-1, 2):
        result.append((L[i].strip(), L[i+1]))
    if len(L) % 2:
        if L[-1].strip():
            result.append((L[-1].strip(), ""))
    return result

def main():
   """This program's main function"""
    text = "I read the book. I read the book! I read the book? I read the book."
    pairs = split_sentences(text)
    print(pairs)

if __name__ == "__main__":
    main()

""" my output ---->
[('I read the book', '.'), ('I read the book', '!'), ('I read the book', '?'), ('I read the book', '.')]
"""

If you want to go further, you must write functions in your program. Here is a function which splits the sentences in a text, assuming that a sentence ends with one or more ".!?".

import re
end_of_sentence = re.compile(r"([\.\!\?]+)")

def split_sentences(text):
    """split_sentences(text) --> list of pairs (sentence, punctuation)"""
    L = end_of_sentence.split(text)
    result = []
    for i in range(0, len(L)-1, 2):
        result.append((L[i].strip(), L[i+1]))
    if len(L) % 2:
        if L[-1].strip():
            result.append((L[-1].strip(), ""))
    return result

def main():
   """This program's main function"""
    text = "I read the book. I read the book! I read the book? I read the book."
    pairs = split_sentences(text)
    print(pairs)

if __name__ == "__main__":
    main()

""" my output ---->
[('I read the book', '.'), ('I read the book', '!'), ('I read the book', '?'), ('I read the book', '.')]
"""

Hi!
I want to thank you for a very good reply!

I will try to "finish" your solution and post it back here.
I am a "newbie", but I will try my best... :icon_smile:
Perhaps you could check back and see how it is going?

Thanx again!

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.