Reputation: 11
I am trying to generate similar kind of sentences using Stanford parser. Steps :
1.Parse input sentence using Stanford library.
2.Generate production rules from parsed sentences
3.Replace some terminal values
4.Regenerate sentences using generate() function from nltk.parse.generate
I have three doubts:
1.Does StanfordParser always generate finite production rules?
2.While using generate function, for few sentences I am getting the following error.
"RuntimeError: maximum recursion depth exceeded while calling a Python object"
When I set recursion limit using sys.setrecursionlimit(0x10000000), I got Error: " Segmentation fault: 11"
2.1 Can I increase Recursion limit?
2.2 How can I solve this problem?
3 Is this the right approach? How can I improve it?
Upvotes: 0
Views: 311
Reputation: 50190
The fuction nltk.parse.generate.generate()
is meant to generate all the productions of the grammar. This means that even a single recursive rule, such as an NP inside an NP (which happens all the time) will give you infinite recursion. As the documentation indicates, you can control it by limiting the depth of recursion (e.g., depth=20
). If you wish you can also specify the number of trees you want to generate (n=100
).
The limits will solve the infinite recursion problem, but they don't guarantee that you'll like the results. generate()
inserts all possible expansions in order, so if the first rule is recursive, it will be used again and again. You'll probably get better results if you select the 50th tree generated, for example; but you'll certainly get the best results if you examine the structure of the grammar you generated, and code your own generator that makes random choices among the possible expansions.
Upvotes: 2