Git Product home page Git Product logo

text-generator's Introduction

Text-Generator

Overview

This script was done as part of JetBrains Academy's Python Core track, as an implementation of one of it's educational projects (Text Generator).

Project Description

The task was to implement a script that generates ten random sentences from a given text file(corpus), and each sentence should be:
    1. No less than five words
    2. Starts with a capital letter
    3. Ends with a sentence-ending punctuation(?, ., !)
    4. On a separate line.

Solution Approach

Using a WhitespaceTokenizer the text is split into a list of words, which will be fed to the model that creates trigrams; from every three consecutive words in the corpus; the model maps the first two words(head) as one token with the third word (tail) as the second token, the model will also store how often every trigram has appeared. Then the first word of each sentence is chosen randomly from the list of heads, according to the rules mentioned in the project description. Then the following words will be selected also randomly but based on the previous word in the sentence considering the frequency of the trigram as the weight of this trigram in the corpus. A sentence that has ended with sentence-ending punctuation and is less than 5 words in length will be discarded to avoid an infinite loop as a valid sentence won't be found.

How to run

Type: python text_generator.py in the terminal then type the text file name.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.