Commit 02a3b8a7 authored by Bruno Guillaume's avatar Bruno Guillaume

In dev:

- webview
- treeview
- file watching
parents
import itertools
import tempfile
import subprocess
def load_conll (file):
fo = open(file, "r")
lines = fo.read().splitlines()
sentences = [list(x[1]) for x in itertools.groupby(lines, lambda x: x=='') if not x[0]]
fo.close()
return sentences
def new_id():
global cpt
cpt+=1
return (str (cpt))
def get_sentid(sentence):
sent_id_line = next(l for l in sentence if l[0]=="#" and "# sent_id =" in l)
return (sent_id_line.split (" = "))[1]
def get_text(sentence):
sent_id_line = next(l for l in sentence if l[0]=="#" and "# text =" in l)
return (sent_id_line.split (" = "))[1]
def to_svg(sentence):
with tempfile.NamedTemporaryFile('w', suffix=".conllu") as temp:
conllu=temp.name
svg=conllu.replace('conllu', 'svg')
for l in sentence:
temp.write(l+"\n")
temp.flush()
subprocess.call (["dep2pict",conllu,svg])
return svg
seq = load_conll ("sequoia.surf.conll")
to_svg (seq[123])
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, QSlider, QSplitter, QAbstractItemView,
QHBoxLayout, QVBoxLayout, QApplication, QTextEdit, QScrollArea, QTreeView)
from PyQt5.QtWebKitWidgets import QWebView , QWebPage
from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtCore import QUrl, Qt, QFileSystemWatcher
from PyQt5.QtGui import QStandardItemModel
import network
# 2 next lines: quit on Ctrl-C in terminal (stackoverflow.com/questions/4938723)
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
global watcher
watcher = None
class Example(QWidget):
def __init__(self):
super().__init__()
self.start()
self.initUI()
def initUI(self):
vbox = QVBoxLayout()
hbox = QHBoxLayout()
hbox.setContentsMargins(0,0,0,0)
loadButton = QPushButton("Load")
nextButton = QPushButton("Next")
hbox.addWidget(loadButton)
hbox.addWidget(nextButton)
vbox.addLayout(hbox)
loadButton.clicked.connect(self.load_fct)
nextButton.clicked.connect(self.next_fct)
self.dataView = QTreeView()
self.dataView.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.create_model(self)
self.dataView.setModel(self.model)
for i in range (1000):
self.add(self.model, 'item %d' % i)
slider1 = QSlider(Qt.Vertical)
slider1.setMaximum(300)
slider1.setMinimum(50)
slider1.setValue(100)
self.webView_1 = QWebView()
split = QSplitter(Qt.Horizontal)
hbox1 = QHBoxLayout()
#hbox1.setContentsMargins(left, top, right, bottom)
hbox1.setContentsMargins(0,0,0,0)
right_widget = QWidget()
right_widget.setLayout(hbox1)
split.addWidget(self.dataView)
split.addWidget(right_widget)
split.setStretchFactor(1, 10)
hbox1.addWidget(self.webView_1)
hbox1.addWidget(slider1)
vbox.addWidget(split)
slider1.valueChanged.connect(lambda v:self.webView_1.setZoomFactor(v/100))
self.dataView.clicked.connect(self.test)
self.refresh()
self.setLayout(vbox)
self.setGeometry(0, 0, 1920, 600)
self.setWindowTitle('Dep2pict')
self.show()
def test(self, index):
# http://doc.qt.io/qt-5/qstandarditem.html
print (index.model().itemFromIndex(index).text())
print (index.model().itemFromIndex(index).row())
def create_model(self,parent):
self.model = QStandardItemModel(0, 1, parent)
self.model.setHeaderData(0, Qt.Horizontal, "Sent_id")
def add(self,model, sentid):
self.model.insertRow(0)
self.model.setData(model.index(0, 0), sentid)
def fileChanged(filename):
"""Called whenever the global filesystem watcher detects a change."""
print (filename)
def start(self):
"""Start the document watcher."""
global watcher
if watcher is None:
watcher = QFileSystemWatcher()
watcher.addPath("/Users/guillaum/code/pyqt/WebKit/wh.svg")
watcher.fileChanged.connect(self.fileChanged)
watcher.fileChanged.connect(self.refresh)
def refresh (self):
self.webView_1.setUrl(QUrl("file:///Users/guillaum/gitlab/dep2pict/pyqt/clickable.svg"))
def draw (self,filename):
self.webView_1.setUrl(QUrl("file://"+filename))
def load_fct(self):
req = {
"command": "load_corpus",
"files": ["/users/guillaum/gitlab/deep-sequoia/trunk/sequoia.surf.conll"]
}
reply = network.send_and_receive(req)
print (reply)
__cpt = 123
def next_fct(self):
req = {
"command": "svg_corpus",
"corpus_index": 1,
"position": self.__cpt,
}
self.__cpt += 1
reply = network.send_and_receive(req)
self.draw(reply)
print (reply)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
network.init()
sys.exit(app.exec_())
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment