I have a set of topics and each topic consists of a set of words. I want to make meaningful English sentences from these words. Each topic consist of 5 to 10 words and these words are relevant to each other, like {code, language, test, write and function} and {class, public, method, string, int} are two sets. I want to generate a sentence from these set of words using API.

  • 2
    $\begingroup$ Could you clarify what you are aiming at for "meaningful"? With latest ML techniques on their own you can aim for grammatically correct and some rough semantic correctness (e.g. people nouns taking active roles, objects often their typical properties). However, the results are often nonsense - entertaining perhaps, but devoid of any real meaning and more like abstract poetry. Would that sort of thing meet your goals? Perhaps some more explanation of what you are looking for and some examples would help if you added them to the question $\endgroup$ Mar 23, 2019 at 21:25
  • 1
    $\begingroup$ What would be the source of"meaning"? The words of each topic do not suggest any specific meaning. If you want to construct formally correct sentences, you'd need to build a model of the syntactic and semantic roles of the words, and create sentences in which the words take on the given roles. The choice of programming language is irrelevant, and it pretty unclear what you expect from an API here... $\endgroup$ Mar 23, 2019 at 23:42
  • $\begingroup$ a simpler method (but likely not as powerful as @NeilSlater 's solution) might simply construct random sentences based off a list of syntax trees, but admittedly i don't know enough linguistics to evaluate this claim; also iirc there is a lot of difficulty of even evaluating the part of speech of words for many sentences $\endgroup$ Feb 27, 2020 at 1:35
  • $\begingroup$ Sounds a lot like something I wrote in high-school in the late 1960s, a Fortran program with valid English sentence syntaxes and a sets of words classified by grammatical type. It produced "meaningful" sentences like: "their man to their good warmth expands the crowds on its any hands", "my faces smell a lot of me", "every baby of you grows very orange", "his girl kisses into the blue women", "the girls burp her dress again again more", "the face of few sadistic desires chews her boys down a softly quick crowd", "the girls smell the desire also", and "my baby laughs sadly". $\endgroup$ Jun 26, 2020 at 13:23

1 Answer 1


Two Approaches:

  • Naive Bayes
  • LSTM

Train Naive Bayes on a whole dataset learning the probability of the next word given a word.

You can even go with any LSTM approaches, but I'd bet on Naive Bayes.


text: hello how are you hello how are you hello No how

to get the suggestion of next word depending on current word - hello

p(how | hello) = 3/4

p(No | hello) = 1/4

take argmax of probabilities.

Also remember to smooth, and train on huge dataset. Training is just finding the probabilities before hand.

Hope it helps ;)


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .