Forums

Generating sentences from a grammar using Feature-structures in Python

Am trying to generate sentences from a defined grammar with python, to avoide agreement problem I used feature structures, This is the code I have done so far: (python 3.6)

   >>> from __future__ import print_function
   >>> import nltk
   >>> from nltk.featstruct import FeatStruct
   >>> from nltk import grammar, parse
   >>> from nltk.parse.generate import generate
   >>> from nltk import CFG
   >>> g = """
    % start DP
    DP-> D[AGR=[NUM='sg', PERS=3, GND='m']] N[AGR=[NUM='sg', GND='m']]
    D[AGR=[NUM='sg', PERS=3, GND='f']] -> 'une' | 'la'
    D[AGR=[NUM='sg', PERS=3, GND='m']] -> 'un' | 'le'
    D[AGR=[NUM='pl', PERS=3]] -> 'des' | 'les'
    N[AGR=[NUM='sg', GND='m']] -> 'garçon'
    N[AGR=[NUM='pl', GND='m']] -> 'garçons'
    N[AGR=[NUM='sg', GND='f']] -> 'fille'
    N[AGR=[NUM='pl', GND='f']] -> 'filles'
    """
        >>> for sentence in generate(grammar, n=30):
            print(''.join(sentence))

This is the output am getting:

unegarçon
 unegarçons
 unefille
 unefilles
 lagarçon
 lagarçons
 lafille
lafilles
 ungarçon 
ungarçons
 unfille 
unfilles 
legarçon 
legarçons
lefille 
lefilles 
desgarçon 
desgarçons
 desfille
 desfilles 
lesgarçon
lesgarçons 
lesfille 
lesfilles

While am supposed to have an output like this:

 un garçon
 le garçon

*The problems I have are:

1/ The agreement is not working out, am having sentences that does not respect the agreement

*2/ There is no space between the two words in the sentence.

What is that I can't see? Advance Thanks

Hi there, I'm not sure any of us here know enough about NLTK to help -- perhaps ask on an NLTK mailing list? Or on stackoverflow?

It looks like your last line

print(''.join(sentence))

is missing a space between the apostrophies before your join call. You want:

print(' '.join(sentence))

This will add a space between each word in your output.