I have a piece of text that I wish to present as a graph using pytextrank. The code (copied from source) is
import spacy
nlp = spacy.load("en_core_web_sm")
import pytextrank
import graphviz
tr = pytextrank.TextRank()
nlp.add_pipe(tr.PipelineComponent, name='textrank', last=True)
line = "the ballistic nuclear threat can be thwarted by building a nuclear shield"
doc = nlp(line)
tr.write_dot(path="graph.dot")
"it" writes something to the file "graph.dot". This looks like a json file with as first field "digraph {}". At this point I'm lost. How do I create a nice graph of the text (or a graph at all, for that matter)
thanks,
Andreas
using ubuntu 20.04.1LTS, python 3.8, pytextrank 2.0.3
There are updates in the new online documentation for PyTextRank, and in particular see the "Getting Started" page at https://derwen.ai/docs/ptr/start/ for example code. Similar code is also shown in the
sample.py
script in the GitHub repo.BTW, the most recent release is 3.0.1, which is tracking the new
spaCy
3.x updates.Here's a simple usage:
The output would be:
If you want to visualize the lemma graph in
Graphviz
or other libraries which read theDOT
file format, you can add:That will write output to a
"graph.dot"
file. See theGraphviz
docs for examples of how to read and render.FWIW, we are currently working on integration of the
kglab
library, which will open up a much broader range of graph manipulation and visualization capabilities, since it integrates with may other libraries and file formats.Also, if you have any suggestions or requests in terms of how you'd like to visualize results from PyTextRank, it's really helpful to create an issue at https://github.com/DerwenAI/pytextrank/issues and our developer community can help more there.
My apologies if I'm not interpreting correctly about "present the text as a graph", since another way to think about that would be to use the
displaCy
dependency visualizer which shows a grammatical dependency graph of tokens in a sentence. There's an example given in the spaCy tuTorial.