0

I have a code for dependency parsing which gives output in the form of arcs. Is there any other way to display the parse tree for a paragraph? Because for a paragraph, the parse tree is huge. Is there a better way to display the parse tree for a paragraph?

0x5050
  • 1,221
  • 1
  • 17
  • 32

1 Answers1

1

First of all, setting the compact flag in Displacy will reduce the size of tree shown.

options = {'compact': True} 
svg = displacy.render(doc, style='dep',options=options)

But only this won't work for large paragraphs. What I'll suggest is, instead of viewing the dependency parse of the whole paragraph, break the paragraph into sentences first. Then parse each sentence and view them. You can save the parse trees of each sentence as a SVG file and then see them one by one. Here is the code for saving SVG:

svg = displacy.render(doc, style='dep',options=options)
f = open('sample.svg', 'w')
f.write(svg)
f.close()

Alternatively, you can save the whole parse tree of the paragraph as SVG and open it in a browser. Then you can easily view it with zoom and scroll.

0x5050
  • 1,221
  • 1
  • 17
  • 32
  • can you send a sample code to break the paragraph into sentences and then parsing and viewing them? how to save the parse them one by one and view it? – Nikita Ramesh Rao Jul 10 '18 at 09:25
  • Take a look at this. https://stackoverflow.com/questions/46290313/how-to-break-up-document-by-sentences-with-with-spacy – 0x5050 Jul 10 '18 at 10:45
  • thanks. It worked. I have one more query. now that I have the svg file,is there a way to get the path of the svg file in postman as json response? how do i return the path name in postman? I am not able to get the path in postman it comes as window path is not json serializable. – Nikita Ramesh Rao Jul 12 '18 at 06:11
  • That is a different question. Post it in SO with appropriate tags – 0x5050 Jul 12 '18 at 06:21
  • how to get the file path of where the svg file is being stored? not in postman. just in the normal output? – Nikita Ramesh Rao Jul 12 '18 at 09:20
  • The code written in the ans. writes the file to the directory where the python file is stored. you can also specify full pathnames inside the open() method – 0x5050 Jul 12 '18 at 09:23
  • ya that I understood that it saves the file where the python file is there. but I need to print that path as output. so how do I get it? – Nikita Ramesh Rao Jul 12 '18 at 09:25
  • SO is not a place to give customized solutions. Try the problem first, if it doesn't work, post it in a separate question. – 0x5050 Jul 12 '18 at 09:29