Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Modifying code
#1
Is there a way to modify this code for it to search for the sentences generated to:
— always start with capitalized words ("This is beautiful.", "You are a great programmer!", etc.);
— not start with a word that ends with a sentence-ending punctuation mark ("Okay.", "Nice.", "Good.", "Look!", "Jon!", etc.);
— always end with a sentence-ending punctuation mark like ., !, or ?;
— should not be shorter than 5 tokens.

My code:

sentence_count = 10
while sentence_count != 0:
    generated_sentence = []
    current_word = random.choice(text1)
    generated_sentence.append(current_word)
    count = 9
    while count != 0:
        next_word = Counter(head_tail[current_word]).most_common(1)[0][0]
        generated_sentence.append(next_word)
        current_word = next_word
        count -= 1
    print(" ".join(generated_sentence))
    sentence_count -= 1
Yoriz write Dec-12-2021, 08:40 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Quote:Is there a way to modify this code for it to search for the sentences generated to:
Yes, it is possible. Did you try? Show your tries and we can comment.

Quote:— always start with capitalized words ("This is beautiful.", "You are a great programmer!", etc.);
Do you know how to capitalize?

Quote:— not start with a word that ends with a sentence-ending punctuation mark ("Okay.", "Nice.", "Good.", "Look!", "Jon!", etc.);
Do you know how to check the last character in a string?

Quote:— always end with a sentence-ending punctuation mark like ., !, or ?;
Same question as above. Also do you know how to join strings together?

Quote:— should not be shorter than 5 tokens.
Does this even happen with your current code? You did not post all of it, so I can't run it for myself here.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Modifying Lists RedSkeleton007 12 6,716 Nov-22-2017, 04:31 AM
Last Post: heiner55
  Assistance Modifying Code chuco61 2 3,439 Apr-30-2017, 03:35 PM
Last Post: michaelcollier
  win32 package: self-modifying source code??? Orthoducks 3 4,416 Jan-13-2017, 09:29 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020