Skip to content

Instantly share code, notes, and snippets.

@ajdapretnar
Last active June 7, 2023 19:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajdapretnar/e66e1dbecef3bb59abd4137bf8c2ab77 to your computer and use it in GitHub Desktop.
Save ajdapretnar/e66e1dbecef3bb59abd4137bf8c2ab77 to your computer and use it in GitHub Desktop.
Example Word Finder widget for Orange3-Text
from Orange.data import Table
from Orange.widgets import gui
from Orange.widgets.widget import OWWidget
from AnyQt.QtWidgets import QApplication, QLabel
from orangecontrib.text import Corpus
class OWWordFinder(OWWidget):
name = "Word Finder"
description = "Display whether a word is in a text or not."
icon = "icons/WordFinder.svg"
priority = 1
inputs = [('Corpus', Table, 'set_data')]
# This widget will have no output, but in case you want one, you define it as below.
# outputs = [('Selected Documents', Table, )]
want_control_area = False
def __init__(self):
super().__init__()
self.corpus = None
self.word = ""
gui.widgetBox(self.mainArea, orientation="vertical")
self.input = gui.lineEdit(self.mainArea, self, '',
orientation="horizontal",
label='Query:')
self.input.setFocus()
self.input.textChanged.connect(self.search)
self.view = QLabel()
self.mainArea.layout().addWidget(self.view)
def set_data(self, data=None):
if data is not None and not isinstance(data, Corpus):
self.corpus = Corpus.from_table(data.domain, data)
self.corpus = data
self.search()
def search(self):
self.word = self.input.text()
result = any(self.word in doc for doc in self.corpus.tokens)
self.view.setText(str(result))
if __name__ == '__main__':
app = QApplication([])
widget = OWWordFinder()
corpus = Corpus.from_file('bookexcerpts')
corpus = corpus[:3]
widget.set_data(corpus)
widget.show()
app.exec()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment