techprotrade commited on
Commit
27c650d
·
verified ·
1 Parent(s): 5bf6dc9

Add loan-advisor-agent

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +5 -0
  2. loan-advisor-agent/.venv/Lib/site-packages/nltk/__init__.py +207 -0
  3. loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/__init__.cpython-312.pyc +0 -0
  4. loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/collections.cpython-312.pyc +0 -0
  5. loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/collocations.cpython-312.pyc +0 -0
  6. loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/data.cpython-312.pyc +0 -0
  7. loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/decorators.cpython-312.pyc +0 -0
  8. loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/downloader.cpython-312.pyc +3 -0
  9. loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/featstruct.cpython-312.pyc +3 -0
  10. loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/grammar.cpython-312.pyc +0 -0
  11. loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/help.cpython-312.pyc +0 -0
  12. loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/internals.cpython-312.pyc +0 -0
  13. loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/jsontags.cpython-312.pyc +0 -0
  14. loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/lazyimport.cpython-312.pyc +0 -0
  15. loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/pathsec.cpython-312.pyc +0 -0
  16. loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/picklesec.cpython-312.pyc +0 -0
  17. loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/probability.cpython-312.pyc +3 -0
  18. loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/text.cpython-312.pyc +0 -0
  19. loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/toolbox.cpython-312.pyc +0 -0
  20. loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/util.cpython-312.pyc +0 -0
  21. loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/wsd.cpython-312.pyc +0 -0
  22. loan-advisor-agent/.venv/Lib/site-packages/nltk/app/__init__.py +47 -0
  23. loan-advisor-agent/.venv/Lib/site-packages/nltk/app/chartparser_app.py +2570 -0
  24. loan-advisor-agent/.venv/Lib/site-packages/nltk/app/chunkparser_app.py +1500 -0
  25. loan-advisor-agent/.venv/Lib/site-packages/nltk/app/collocations_app.py +438 -0
  26. loan-advisor-agent/.venv/Lib/site-packages/nltk/app/concordance_app.py +709 -0
  27. loan-advisor-agent/.venv/Lib/site-packages/nltk/app/nemo_app.py +163 -0
  28. loan-advisor-agent/.venv/Lib/site-packages/nltk/app/rdparser_app.py +1052 -0
  29. loan-advisor-agent/.venv/Lib/site-packages/nltk/app/srparser_app.py +937 -0
  30. loan-advisor-agent/.venv/Lib/site-packages/nltk/app/wordfreq_app.py +36 -0
  31. loan-advisor-agent/.venv/Lib/site-packages/nltk/app/wordnet_app.py +1003 -0
  32. loan-advisor-agent/.venv/Lib/site-packages/nltk/book.py +213 -0
  33. loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/__init__.py +34 -0
  34. loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/__pycache__/__init__.cpython-312.pyc +0 -0
  35. loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/__pycache__/api.cpython-312.pyc +0 -0
  36. loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/__pycache__/chart.cpython-312.pyc +0 -0
  37. loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/__pycache__/combinator.cpython-312.pyc +0 -0
  38. loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/__pycache__/lexicon.cpython-312.pyc +0 -0
  39. loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/__pycache__/logic.cpython-312.pyc +0 -0
  40. loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/api.py +358 -0
  41. loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/chart.py +480 -0
  42. loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/combinator.py +340 -0
  43. loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/lexicon.py +338 -0
  44. loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/logic.py +63 -0
  45. loan-advisor-agent/.venv/Lib/site-packages/nltk/chat/__init__.py +48 -0
  46. loan-advisor-agent/.venv/Lib/site-packages/nltk/chat/eliza.py +337 -0
  47. loan-advisor-agent/.venv/Lib/site-packages/nltk/chat/iesha.py +160 -0
  48. loan-advisor-agent/.venv/Lib/site-packages/nltk/chat/rude.py +125 -0
  49. loan-advisor-agent/.venv/Lib/site-packages/nltk/chat/suntsu.py +140 -0
  50. loan-advisor-agent/.venv/Lib/site-packages/nltk/chat/util.py +124 -0
.gitattributes CHANGED
@@ -437,3 +437,8 @@ loan-advisor-agent/.venv/Lib/site-packages/litellm/proxy/_experimental/out/asset
437
  loan-advisor-agent/.venv/Lib/site-packages/litellm/proxy/_experimental/out/assets/logos/dataforseo.png filter=lfs diff=lfs merge=lfs -text
438
  loan-advisor-agent/.venv/Lib/site-packages/litellm/proxy/_experimental/out/assets/logos/s3_vector.png filter=lfs diff=lfs merge=lfs -text
439
  loan-advisor-agent/.venv/Lib/site-packages/litellm/proxy/_experimental/out/assets/logos/sap.png filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
437
  loan-advisor-agent/.venv/Lib/site-packages/litellm/proxy/_experimental/out/assets/logos/dataforseo.png filter=lfs diff=lfs merge=lfs -text
438
  loan-advisor-agent/.venv/Lib/site-packages/litellm/proxy/_experimental/out/assets/logos/s3_vector.png filter=lfs diff=lfs merge=lfs -text
439
  loan-advisor-agent/.venv/Lib/site-packages/litellm/proxy/_experimental/out/assets/logos/sap.png filter=lfs diff=lfs merge=lfs -text
440
+ loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/downloader.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
441
+ loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/featstruct.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
442
+ loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/probability.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
443
+ loan-advisor-agent/.venv/Lib/site-packages/nltk/corpus/reader/__pycache__/framenet.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
444
+ loan-advisor-agent/.venv/Lib/site-packages/nltk/corpus/reader/__pycache__/wordnet.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
loan-advisor-agent/.venv/Lib/site-packages/nltk/__init__.py ADDED
@@ -0,0 +1,207 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit (NLTK)
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Authors: Steven Bird <stevenbird1@gmail.com>
5
+ # Edward Loper <edloper@gmail.com>
6
+ # URL: <https://www.nltk.org/>
7
+ # For license information, see LICENSE.TXT
8
+
9
+ """
10
+ The Natural Language Toolkit (NLTK) is an open source Python library
11
+ for Natural Language Processing. A free online book is available.
12
+ (If you use the library for academic research, please cite the book.)
13
+
14
+ Steven Bird, Ewan Klein, and Edward Loper (2009).
15
+ Natural Language Processing with Python. O'Reilly Media Inc.
16
+ https://www.nltk.org/book/
17
+
18
+ isort:skip_file
19
+ """
20
+
21
+ import os
22
+ import importlib
23
+
24
+ # //////////////////////////////////////////////////////
25
+ # Metadata
26
+ # //////////////////////////////////////////////////////
27
+
28
+ # Version. For each new release, the version number should be updated
29
+ # in the file VERSION.
30
+ try:
31
+ # If a VERSION file exists, use it!
32
+ version_file = os.path.join(os.path.dirname(__file__), "VERSION")
33
+ with open(version_file) as infile:
34
+ __version__ = infile.read().strip()
35
+ except NameError:
36
+ __version__ = "unknown (running code interactively?)"
37
+ except OSError as ex:
38
+ __version__ = "unknown (%s)" % ex
39
+
40
+ if __doc__ is not None: # fix for the ``python -OO``
41
+ __doc__ += "\n@version: " + __version__
42
+
43
+
44
+ # Copyright notice
45
+ __copyright__ = """\
46
+ Copyright (C) 2001-2026 NLTK Project.
47
+
48
+ Distributed and Licensed under the Apache License, Version 2.0,
49
+ which is included by reference.
50
+ """
51
+
52
+ __license__ = "Apache License, Version 2.0"
53
+ # Description of the toolkit, keywords, and the project's primary URL.
54
+ __longdescr__ = """\
55
+ The Natural Language Toolkit (NLTK) is a Python package for
56
+ natural language processing. NLTK requires Python 3.10, 3.11, 3.12 or 3.13."""
57
+ __keywords__ = [
58
+ "NLP",
59
+ "CL",
60
+ "natural language processing",
61
+ "computational linguistics",
62
+ "parsing",
63
+ "tagging",
64
+ "tokenizing",
65
+ "syntax",
66
+ "linguistics",
67
+ "language",
68
+ "natural language",
69
+ "text analytics",
70
+ ]
71
+ __url__ = "https://www.nltk.org/"
72
+
73
+ # Maintainer, contributors, etc.
74
+ __maintainer__ = "NLTK Team"
75
+ __maintainer_email__ = "nltk.team@gmail.com"
76
+ __author__ = __maintainer__
77
+ __author_email__ = __maintainer_email__
78
+
79
+ # "Trove" classifiers for Python Package Index.
80
+ __classifiers__ = [
81
+ "Development Status :: 5 - Production/Stable",
82
+ "Intended Audience :: Developers",
83
+ "Intended Audience :: Education",
84
+ "Intended Audience :: Information Technology",
85
+ "Intended Audience :: Science/Research",
86
+ "License :: OSI Approved :: Apache Software License",
87
+ "Operating System :: OS Independent",
88
+ "Programming Language :: Python :: 3.10",
89
+ "Programming Language :: Python :: 3.11",
90
+ "Programming Language :: Python :: 3.12",
91
+ "Programming Language :: Python :: 3.13",
92
+ "Topic :: Scientific/Engineering",
93
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
94
+ "Topic :: Scientific/Engineering :: Human Machine Interfaces",
95
+ "Topic :: Scientific/Engineering :: Information Analysis",
96
+ "Topic :: Text Processing",
97
+ "Topic :: Text Processing :: Filters",
98
+ "Topic :: Text Processing :: General",
99
+ "Topic :: Text Processing :: Indexing",
100
+ "Topic :: Text Processing :: Linguistic",
101
+ ]
102
+
103
+ from nltk.internals import config_java
104
+
105
+ # support numpy from pypy
106
+ try:
107
+ import numpypy
108
+ except ImportError:
109
+ pass
110
+
111
+ # Override missing methods on environments where it cannot be used like GAE.
112
+ import subprocess
113
+
114
+ if not hasattr(subprocess, "PIPE"):
115
+
116
+ def _fake_PIPE(*args, **kwargs):
117
+ raise NotImplementedError("subprocess.PIPE is not supported.")
118
+
119
+ subprocess.PIPE = _fake_PIPE
120
+ if not hasattr(subprocess, "Popen"):
121
+
122
+ def _fake_Popen(*args, **kwargs):
123
+ raise NotImplementedError("subprocess.Popen is not supported.")
124
+
125
+ subprocess.Popen = _fake_Popen
126
+
127
+ ###########################################################
128
+ # TOP-LEVEL MODULES
129
+ ###########################################################
130
+
131
+ # Import top-level functionality into top-level namespace
132
+
133
+ from nltk.collocations import *
134
+ from nltk.decorators import decorator, memoize
135
+ from nltk.featstruct import *
136
+ from nltk.grammar import *
137
+ from nltk.probability import *
138
+ from nltk.text import *
139
+ from nltk.util import *
140
+ from nltk.jsontags import *
141
+
142
+ ###########################################################
143
+ # PACKAGES
144
+ ###########################################################
145
+
146
+ from nltk.chunk import *
147
+ from nltk.classify import *
148
+ from nltk.inference import *
149
+ from nltk.metrics import *
150
+ from nltk.parse import *
151
+ from nltk.tag import *
152
+ from nltk.tokenize import *
153
+ from nltk.translate import *
154
+ from nltk.tree import *
155
+ from nltk.sem import *
156
+ from nltk.stem import *
157
+
158
+ # Packages which can be lazily imported
159
+ # (a) we don't import *
160
+ # (b) they're slow to import or have run-time dependencies
161
+ # that can safely fail at run time
162
+
163
+ from nltk import lazyimport
164
+
165
+ app = lazyimport.LazyModule("app", locals(), globals())
166
+ chat = lazyimport.LazyModule("chat", locals(), globals())
167
+ corpus = lazyimport.LazyModule("corpus", locals(), globals())
168
+ draw = lazyimport.LazyModule("draw", locals(), globals())
169
+ toolbox = lazyimport.LazyModule("toolbox", locals(), globals())
170
+
171
+ # Optional loading
172
+ try:
173
+ import numpy
174
+ except ImportError:
175
+ pass
176
+ else:
177
+ from nltk import cluster
178
+
179
+ from nltk.downloader import download, download_shell
180
+
181
+ # Check if tkinter exists without importing it to avoid crashes after
182
+ # forks on macOS. Only nltk.app, nltk.draw, and demo modules should
183
+ # have top-level tkinter imports. See #2949 for more details.
184
+ if importlib.util.find_spec("tkinter"):
185
+ try:
186
+ from nltk.downloader import download_gui
187
+ except RuntimeError as e:
188
+ import warnings
189
+
190
+ warnings.warn(
191
+ "Corpus downloader GUI not loaded "
192
+ "(RuntimeError during import: %s)" % str(e)
193
+ )
194
+
195
+ # explicitly import all top-level modules (ensuring
196
+ # they override the same names inadvertently imported
197
+ # from a subpackage)
198
+
199
+ from nltk import ccg, chunk, classify, collocations
200
+ from nltk import data, featstruct, grammar, help, inference, metrics
201
+ from nltk import misc, parse, probability, sem, stem, wsd
202
+ from nltk import tag, tbl, text, tokenize, translate, tree, util
203
+
204
+
205
+ # FIXME: override any accidentally imported demo, see https://github.com/nltk/nltk/issues/2116
206
+ def demo():
207
+ print("To run the demo code for a module, type nltk.module.demo()")
loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (5.99 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/collections.cpython-312.pyc ADDED
Binary file (31.8 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/collocations.cpython-312.pyc ADDED
Binary file (20 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/data.cpython-312.pyc ADDED
Binary file (63.4 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/decorators.cpython-312.pyc ADDED
Binary file (8.49 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/downloader.cpython-312.pyc ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1df5baa7554d8fdd6e70df6c7840d5deec76ce700c499ed21a04cea16e5f07ac
3
+ size 114732
loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/featstruct.cpython-312.pyc ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:13051378c3ac8334b6a1ceece51cc18096a8c8eed70fddefdf4f18053dbcd7c6
3
+ size 106983
loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/grammar.cpython-312.pyc ADDED
Binary file (73.2 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/help.cpython-312.pyc ADDED
Binary file (2.32 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/internals.cpython-312.pyc ADDED
Binary file (41.2 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/jsontags.cpython-312.pyc ADDED
Binary file (3.61 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/lazyimport.cpython-312.pyc ADDED
Binary file (4.84 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/pathsec.cpython-312.pyc ADDED
Binary file (12.7 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/picklesec.cpython-312.pyc ADDED
Binary file (3.28 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/probability.cpython-312.pyc ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5822c5a583c8bc99f558fd08dcd996d467ac0498376d32b335cbefec9fdd2742
3
+ size 110722
loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/text.cpython-312.pyc ADDED
Binary file (37.4 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/toolbox.cpython-312.pyc ADDED
Binary file (22.1 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/util.cpython-312.pyc ADDED
Binary file (46.3 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/__pycache__/wsd.cpython-312.pyc ADDED
Binary file (2.13 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/app/__init__.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: Applications package
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Author: Edward Loper <edloper@gmail.com>
5
+ # Steven Bird <stevenbird1@gmail.com>
6
+ # URL: <https://www.nltk.org/>
7
+ # For license information, see LICENSE.TXT
8
+
9
+ """
10
+ Interactive NLTK Applications:
11
+
12
+ chartparser: Chart Parser
13
+ chunkparser: Regular-Expression Chunk Parser
14
+ collocations: Find collocations in text
15
+ concordance: Part-of-speech concordancer
16
+ nemo: Finding (and Replacing) Nemo regular expression tool
17
+ rdparser: Recursive Descent Parser
18
+ srparser: Shift-Reduce Parser
19
+ wordnet: WordNet Browser
20
+ """
21
+
22
+
23
+ # Import Tkinter-based modules if Tkinter is installed
24
+ try:
25
+ import tkinter
26
+ except ImportError:
27
+ import warnings
28
+
29
+ warnings.warn("nltk.app package not loaded (please install Tkinter library).")
30
+ else:
31
+ from nltk.app.chartparser_app import app as chartparser
32
+ from nltk.app.chunkparser_app import app as chunkparser
33
+ from nltk.app.collocations_app import app as collocations
34
+ from nltk.app.concordance_app import app as concordance
35
+ from nltk.app.nemo_app import app as nemo
36
+ from nltk.app.rdparser_app import app as rdparser
37
+ from nltk.app.srparser_app import app as srparser
38
+ from nltk.app.wordnet_app import app as wordnet
39
+
40
+ try:
41
+ from matplotlib import pylab
42
+ except ImportError:
43
+ import warnings
44
+
45
+ warnings.warn("nltk.app.wordfreq not loaded (requires the matplotlib library).")
46
+ else:
47
+ from nltk.app.wordfreq_app import app as wordfreq
loan-advisor-agent/.venv/Lib/site-packages/nltk/app/chartparser_app.py ADDED
@@ -0,0 +1,2570 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: Chart Parser Application
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Author: Edward Loper <edloper@gmail.com>
5
+ # Jean Mark Gawron <gawron@mail.sdsu.edu>
6
+ # Steven Bird <stevenbird1@gmail.com>
7
+ # URL: <https://www.nltk.org/>
8
+ # For license information, see LICENSE.TXT
9
+
10
+ """
11
+ A graphical tool for exploring chart parsing.
12
+
13
+ Chart parsing is a flexible parsing algorithm that uses a data
14
+ structure called a "chart" to record hypotheses about syntactic
15
+ constituents. Each hypothesis is represented by a single "edge" on
16
+ the chart. A set of "chart rules" determine when new edges can be
17
+ added to the chart. This set of rules controls the overall behavior
18
+ of the parser (e.g. whether it parses top-down or bottom-up).
19
+
20
+ The chart parsing tool demonstrates the process of parsing a single
21
+ sentence, with a given grammar and lexicon. Its display is divided
22
+ into three sections: the bottom section displays the chart; the middle
23
+ section displays the sentence; and the top section displays the
24
+ partial syntax tree corresponding to the selected edge. Buttons along
25
+ the bottom of the window are used to control the execution of the
26
+ algorithm.
27
+
28
+ The chart parsing tool allows for flexible control of the parsing
29
+ algorithm. At each step of the algorithm, you can select which rule
30
+ or strategy you wish to apply. This allows you to experiment with
31
+ mixing different strategies (e.g. top-down and bottom-up). You can
32
+ exercise fine-grained control over the algorithm by selecting which
33
+ edge you wish to apply a rule to.
34
+ """
35
+
36
+ # At some point, we should rewrite this tool to use the new canvas
37
+ # widget system.
38
+
39
+
40
+ import os.path
41
+ import pickle
42
+ from tkinter import (
43
+ Button,
44
+ Canvas,
45
+ Checkbutton,
46
+ Frame,
47
+ IntVar,
48
+ Label,
49
+ Menu,
50
+ Scrollbar,
51
+ Tk,
52
+ Toplevel,
53
+ )
54
+ from tkinter.filedialog import askopenfilename, asksaveasfilename
55
+ from tkinter.font import Font
56
+ from tkinter.messagebox import showerror, showinfo
57
+
58
+ from nltk.draw import CFGEditor, TreeSegmentWidget, tree_to_treesegment
59
+ from nltk.draw.util import (
60
+ CanvasFrame,
61
+ ColorizedList,
62
+ EntryDialog,
63
+ MutableOptionMenu,
64
+ ShowText,
65
+ SymbolWidget,
66
+ )
67
+ from nltk.grammar import CFG, Nonterminal
68
+ from nltk.parse.chart import (
69
+ BottomUpPredictCombineRule,
70
+ BottomUpPredictRule,
71
+ Chart,
72
+ LeafEdge,
73
+ LeafInitRule,
74
+ SingleEdgeFundamentalRule,
75
+ SteppingChartParser,
76
+ TopDownInitRule,
77
+ TopDownPredictRule,
78
+ TreeEdge,
79
+ )
80
+ from nltk.picklesec import pickle_load
81
+ from nltk.tree import Tree
82
+ from nltk.util import in_idle
83
+
84
+ # Known bug: ChartView doesn't handle edges generated by epsilon
85
+ # productions (e.g., [Production: PP -> ]) very well.
86
+
87
+ #######################################################################
88
+ # Edge List
89
+ #######################################################################
90
+
91
+
92
+ class EdgeList(ColorizedList):
93
+ ARROW = SymbolWidget.SYMBOLS["rightarrow"]
94
+
95
+ def _init_colortags(self, textwidget, options):
96
+ textwidget.tag_config("terminal", foreground="#006000")
97
+ textwidget.tag_config("arrow", font="symbol", underline="0")
98
+ textwidget.tag_config("dot", foreground="#000000")
99
+ textwidget.tag_config(
100
+ "nonterminal", foreground="blue", font=("helvetica", -12, "bold")
101
+ )
102
+
103
+ def _item_repr(self, item):
104
+ contents = []
105
+ contents.append(("%s\t" % item.lhs(), "nonterminal"))
106
+ contents.append((self.ARROW, "arrow"))
107
+ for i, elt in enumerate(item.rhs()):
108
+ if i == item.dot():
109
+ contents.append((" *", "dot"))
110
+ if isinstance(elt, Nonterminal):
111
+ contents.append((" %s" % elt.symbol(), "nonterminal"))
112
+ else:
113
+ contents.append((" %r" % elt, "terminal"))
114
+ if item.is_complete():
115
+ contents.append((" *", "dot"))
116
+ return contents
117
+
118
+
119
+ #######################################################################
120
+ # Chart Matrix View
121
+ #######################################################################
122
+
123
+
124
+ class ChartMatrixView:
125
+ """
126
+ A view of a chart that displays the contents of the corresponding matrix.
127
+ """
128
+
129
+ def __init__(
130
+ self, parent, chart, toplevel=True, title="Chart Matrix", show_numedges=False
131
+ ):
132
+ self._chart = chart
133
+ self._cells = []
134
+ self._marks = []
135
+
136
+ self._selected_cell = None
137
+
138
+ if toplevel:
139
+ self._root = Toplevel(parent)
140
+ self._root.title(title)
141
+ self._root.bind("<Control-q>", self.destroy)
142
+ self._init_quit(self._root)
143
+ else:
144
+ self._root = Frame(parent)
145
+
146
+ self._init_matrix(self._root)
147
+ self._init_list(self._root)
148
+ if show_numedges:
149
+ self._init_numedges(self._root)
150
+ else:
151
+ self._numedges_label = None
152
+
153
+ self._callbacks = {}
154
+
155
+ self._num_edges = 0
156
+
157
+ self.draw()
158
+
159
+ def _init_quit(self, root):
160
+ quit = Button(root, text="Quit", command=self.destroy)
161
+ quit.pack(side="bottom", expand=0, fill="none")
162
+
163
+ def _init_matrix(self, root):
164
+ cframe = Frame(root, border=2, relief="sunken")
165
+ cframe.pack(expand=0, fill="none", padx=1, pady=3, side="top")
166
+ self._canvas = Canvas(cframe, width=200, height=200, background="white")
167
+ self._canvas.pack(expand=0, fill="none")
168
+
169
+ def _init_numedges(self, root):
170
+ self._numedges_label = Label(root, text="0 edges")
171
+ self._numedges_label.pack(expand=0, fill="none", side="top")
172
+
173
+ def _init_list(self, root):
174
+ self._list = EdgeList(root, [], width=20, height=5)
175
+ self._list.pack(side="top", expand=1, fill="both", pady=3)
176
+
177
+ def cb(edge, self=self):
178
+ self._fire_callbacks("select", edge)
179
+
180
+ self._list.add_callback("select", cb)
181
+ self._list.focus()
182
+
183
+ def destroy(self, *e):
184
+ if self._root is None:
185
+ return
186
+ try:
187
+ self._root.destroy()
188
+ except Exception:
189
+ pass
190
+ self._root = None
191
+
192
+ def set_chart(self, chart):
193
+ if chart is not self._chart:
194
+ self._chart = chart
195
+ self._num_edges = 0
196
+ self.draw()
197
+
198
+ def update(self):
199
+ if self._root is None:
200
+ return
201
+
202
+ # Count the edges in each cell
203
+ N = len(self._cells)
204
+ cell_edges = [[0 for i in range(N)] for j in range(N)]
205
+ for edge in self._chart:
206
+ cell_edges[edge.start()][edge.end()] += 1
207
+
208
+ # Color the cells correspondingly.
209
+ for i in range(N):
210
+ for j in range(i, N):
211
+ if cell_edges[i][j] == 0:
212
+ color = "gray20"
213
+ else:
214
+ color = "#00{:02x}{:02x}".format(
215
+ min(255, 50 + 128 * cell_edges[i][j] / 10),
216
+ max(0, 128 - 128 * cell_edges[i][j] / 10),
217
+ )
218
+ cell_tag = self._cells[i][j]
219
+ self._canvas.itemconfig(cell_tag, fill=color)
220
+ if (i, j) == self._selected_cell:
221
+ self._canvas.itemconfig(cell_tag, outline="#00ffff", width=3)
222
+ self._canvas.tag_raise(cell_tag)
223
+ else:
224
+ self._canvas.itemconfig(cell_tag, outline="black", width=1)
225
+
226
+ # Update the edge list.
227
+ edges = list(self._chart.select(span=self._selected_cell))
228
+ self._list.set(edges)
229
+
230
+ # Update our edge count.
231
+ self._num_edges = self._chart.num_edges()
232
+ if self._numedges_label is not None:
233
+ self._numedges_label["text"] = "%d edges" % self._num_edges
234
+
235
+ def activate(self):
236
+ self._canvas.itemconfig("inactivebox", state="hidden")
237
+ self.update()
238
+
239
+ def inactivate(self):
240
+ self._canvas.itemconfig("inactivebox", state="normal")
241
+ self.update()
242
+
243
+ def add_callback(self, event, func):
244
+ self._callbacks.setdefault(event, {})[func] = 1
245
+
246
+ def remove_callback(self, event, func=None):
247
+ if func is None:
248
+ del self._callbacks[event]
249
+ else:
250
+ try:
251
+ del self._callbacks[event][func]
252
+ except KeyError:
253
+ pass
254
+
255
+ def _fire_callbacks(self, event, *args):
256
+ if event not in self._callbacks:
257
+ return
258
+ for cb_func in list(self._callbacks[event].keys()):
259
+ cb_func(*args)
260
+
261
+ def select_cell(self, i, j):
262
+ if self._root is None:
263
+ return
264
+
265
+ # If the cell is already selected (and the chart contents
266
+ # haven't changed), then do nothing.
267
+ if (i, j) == self._selected_cell and self._chart.num_edges() == self._num_edges:
268
+ return
269
+
270
+ self._selected_cell = (i, j)
271
+ self.update()
272
+
273
+ # Fire the callback.
274
+ self._fire_callbacks("select_cell", i, j)
275
+
276
+ def deselect_cell(self):
277
+ if self._root is None:
278
+ return
279
+ self._selected_cell = None
280
+ self._list.set([])
281
+ self.update()
282
+
283
+ def _click_cell(self, i, j):
284
+ if self._selected_cell == (i, j):
285
+ self.deselect_cell()
286
+ else:
287
+ self.select_cell(i, j)
288
+
289
+ def view_edge(self, edge):
290
+ self.select_cell(*edge.span())
291
+ self._list.view(edge)
292
+
293
+ def mark_edge(self, edge):
294
+ if self._root is None:
295
+ return
296
+ self.select_cell(*edge.span())
297
+ self._list.mark(edge)
298
+
299
+ def unmark_edge(self, edge=None):
300
+ if self._root is None:
301
+ return
302
+ self._list.unmark(edge)
303
+
304
+ def markonly_edge(self, edge):
305
+ if self._root is None:
306
+ return
307
+ self.select_cell(*edge.span())
308
+ self._list.markonly(edge)
309
+
310
+ def draw(self):
311
+ if self._root is None:
312
+ return
313
+ LEFT_MARGIN = BOT_MARGIN = 15
314
+ TOP_MARGIN = 5
315
+ c = self._canvas
316
+ c.delete("all")
317
+ N = self._chart.num_leaves() + 1
318
+ dx = (int(c["width"]) - LEFT_MARGIN) / N
319
+ dy = (int(c["height"]) - TOP_MARGIN - BOT_MARGIN) / N
320
+
321
+ c.delete("all")
322
+
323
+ # Labels and dotted lines
324
+ for i in range(N):
325
+ c.create_text(
326
+ LEFT_MARGIN - 2, i * dy + dy / 2 + TOP_MARGIN, text=repr(i), anchor="e"
327
+ )
328
+ c.create_text(
329
+ i * dx + dx / 2 + LEFT_MARGIN,
330
+ N * dy + TOP_MARGIN + 1,
331
+ text=repr(i),
332
+ anchor="n",
333
+ )
334
+ c.create_line(
335
+ LEFT_MARGIN,
336
+ dy * (i + 1) + TOP_MARGIN,
337
+ dx * N + LEFT_MARGIN,
338
+ dy * (i + 1) + TOP_MARGIN,
339
+ dash=".",
340
+ )
341
+ c.create_line(
342
+ dx * i + LEFT_MARGIN,
343
+ TOP_MARGIN,
344
+ dx * i + LEFT_MARGIN,
345
+ dy * N + TOP_MARGIN,
346
+ dash=".",
347
+ )
348
+
349
+ # A box around the whole thing
350
+ c.create_rectangle(
351
+ LEFT_MARGIN, TOP_MARGIN, LEFT_MARGIN + dx * N, dy * N + TOP_MARGIN, width=2
352
+ )
353
+
354
+ # Cells
355
+ self._cells = [[None for i in range(N)] for j in range(N)]
356
+ for i in range(N):
357
+ for j in range(i, N):
358
+ t = c.create_rectangle(
359
+ j * dx + LEFT_MARGIN,
360
+ i * dy + TOP_MARGIN,
361
+ (j + 1) * dx + LEFT_MARGIN,
362
+ (i + 1) * dy + TOP_MARGIN,
363
+ fill="gray20",
364
+ )
365
+ self._cells[i][j] = t
366
+
367
+ def cb(event, self=self, i=i, j=j):
368
+ self._click_cell(i, j)
369
+
370
+ c.tag_bind(t, "<Button-1>", cb)
371
+
372
+ # Inactive box
373
+ xmax, ymax = int(c["width"]), int(c["height"])
374
+ t = c.create_rectangle(
375
+ -100,
376
+ -100,
377
+ xmax + 100,
378
+ ymax + 100,
379
+ fill="gray50",
380
+ state="hidden",
381
+ tag="inactivebox",
382
+ )
383
+ c.tag_lower(t)
384
+
385
+ # Update the cells.
386
+ self.update()
387
+
388
+ def pack(self, *args, **kwargs):
389
+ self._root.pack(*args, **kwargs)
390
+
391
+
392
+ #######################################################################
393
+ # Chart Results View
394
+ #######################################################################
395
+
396
+
397
+ class ChartResultsView:
398
+ def __init__(self, parent, chart, grammar, toplevel=True):
399
+ self._chart = chart
400
+ self._grammar = grammar
401
+ self._trees = []
402
+ self._y = 10
403
+ self._treewidgets = []
404
+ self._selection = None
405
+ self._selectbox = None
406
+
407
+ if toplevel:
408
+ self._root = Toplevel(parent)
409
+ self._root.title("Chart Parser Application: Results")
410
+ self._root.bind("<Control-q>", self.destroy)
411
+ else:
412
+ self._root = Frame(parent)
413
+
414
+ # Buttons
415
+ if toplevel:
416
+ buttons = Frame(self._root)
417
+ buttons.pack(side="bottom", expand=0, fill="x")
418
+ Button(buttons, text="Quit", command=self.destroy).pack(side="right")
419
+ Button(buttons, text="Print All", command=self.print_all).pack(side="left")
420
+ Button(buttons, text="Print Selection", command=self.print_selection).pack(
421
+ side="left"
422
+ )
423
+
424
+ # Canvas frame.
425
+ self._cframe = CanvasFrame(self._root, closeenough=20)
426
+ self._cframe.pack(side="top", expand=1, fill="both")
427
+
428
+ # Initial update
429
+ self.update()
430
+
431
+ def update(self, edge=None):
432
+ if self._root is None:
433
+ return
434
+ # If the edge isn't a parse edge, do nothing.
435
+ if edge is not None:
436
+ if edge.lhs() != self._grammar.start():
437
+ return
438
+ if edge.span() != (0, self._chart.num_leaves()):
439
+ return
440
+
441
+ for parse in self._chart.parses(self._grammar.start()):
442
+ if parse not in self._trees:
443
+ self._add(parse)
444
+
445
+ def _add(self, parse):
446
+ # Add it to self._trees.
447
+ self._trees.append(parse)
448
+
449
+ # Create a widget for it.
450
+ c = self._cframe.canvas()
451
+ treewidget = tree_to_treesegment(c, parse)
452
+
453
+ # Add it to the canvas frame.
454
+ self._treewidgets.append(treewidget)
455
+ self._cframe.add_widget(treewidget, 10, self._y)
456
+
457
+ # Register callbacks.
458
+ treewidget.bind_click(self._click)
459
+
460
+ # Update y.
461
+ self._y = treewidget.bbox()[3] + 10
462
+
463
+ def _click(self, widget):
464
+ c = self._cframe.canvas()
465
+ if self._selection is not None:
466
+ c.delete(self._selectbox)
467
+ self._selection = widget
468
+ (x1, y1, x2, y2) = widget.bbox()
469
+ self._selectbox = c.create_rectangle(x1, y1, x2, y2, width=2, outline="#088")
470
+
471
+ def _color(self, treewidget, color):
472
+ treewidget.label()["color"] = color
473
+ for child in treewidget.subtrees():
474
+ if isinstance(child, TreeSegmentWidget):
475
+ self._color(child, color)
476
+ else:
477
+ child["color"] = color
478
+
479
+ def print_all(self, *e):
480
+ if self._root is None:
481
+ return
482
+ self._cframe.print_to_file()
483
+
484
+ def print_selection(self, *e):
485
+ if self._root is None:
486
+ return
487
+ if self._selection is None:
488
+ showerror("Print Error", "No tree selected")
489
+ else:
490
+ c = self._cframe.canvas()
491
+ for widget in self._treewidgets:
492
+ if widget is not self._selection:
493
+ self._cframe.destroy_widget(widget)
494
+ c.delete(self._selectbox)
495
+ (x1, y1, x2, y2) = self._selection.bbox()
496
+ self._selection.move(10 - x1, 10 - y1)
497
+ c["scrollregion"] = f"0 0 {x2 - x1 + 20} {y2 - y1 + 20}"
498
+ self._cframe.print_to_file()
499
+
500
+ # Restore our state.
501
+ self._treewidgets = [self._selection]
502
+ self.clear()
503
+ self.update()
504
+
505
+ def clear(self):
506
+ if self._root is None:
507
+ return
508
+ for treewidget in self._treewidgets:
509
+ self._cframe.destroy_widget(treewidget)
510
+ self._trees = []
511
+ self._treewidgets = []
512
+ if self._selection is not None:
513
+ self._cframe.canvas().delete(self._selectbox)
514
+ self._selection = None
515
+ self._y = 10
516
+
517
+ def set_chart(self, chart):
518
+ self.clear()
519
+ self._chart = chart
520
+ self.update()
521
+
522
+ def set_grammar(self, grammar):
523
+ self.clear()
524
+ self._grammar = grammar
525
+ self.update()
526
+
527
+ def destroy(self, *e):
528
+ if self._root is None:
529
+ return
530
+ try:
531
+ self._root.destroy()
532
+ except Exception:
533
+ pass
534
+ self._root = None
535
+
536
+ def pack(self, *args, **kwargs):
537
+ self._root.pack(*args, **kwargs)
538
+
539
+
540
+ #######################################################################
541
+ # Chart Comparer
542
+ #######################################################################
543
+
544
+
545
+ class ChartComparer:
546
+ """
547
+
548
+ :ivar _root: The root window
549
+
550
+ :ivar _charts: A dictionary mapping names to charts. When
551
+ charts are loaded, they are added to this dictionary.
552
+
553
+ :ivar _left_chart: The left ``Chart``.
554
+ :ivar _left_name: The name ``_left_chart`` (derived from filename)
555
+ :ivar _left_matrix: The ``ChartMatrixView`` for ``_left_chart``
556
+ :ivar _left_selector: The drop-down ``MutableOptionsMenu`` used
557
+ to select ``_left_chart``.
558
+
559
+ :ivar _right_chart: The right ``Chart``.
560
+ :ivar _right_name: The name ``_right_chart`` (derived from filename)
561
+ :ivar _right_matrix: The ``ChartMatrixView`` for ``_right_chart``
562
+ :ivar _right_selector: The drop-down ``MutableOptionsMenu`` used
563
+ to select ``_right_chart``.
564
+
565
+ :ivar _out_chart: The out ``Chart``.
566
+ :ivar _out_name: The name ``_out_chart`` (derived from filename)
567
+ :ivar _out_matrix: The ``ChartMatrixView`` for ``_out_chart``
568
+ :ivar _out_label: The label for ``_out_chart``.
569
+
570
+ :ivar _op_label: A Label containing the most recent operation.
571
+ """
572
+
573
+ _OPSYMBOL = {
574
+ "-": "-",
575
+ "and": SymbolWidget.SYMBOLS["intersection"],
576
+ "or": SymbolWidget.SYMBOLS["union"],
577
+ }
578
+
579
+ def __init__(self, *chart_filenames):
580
+ # This chart is displayed when we don't have a value (eg
581
+ # before any chart is loaded).
582
+ faketok = [""] * 8
583
+ self._emptychart = Chart(faketok)
584
+
585
+ # The left & right charts start out empty.
586
+ self._left_name = "None"
587
+ self._right_name = "None"
588
+ self._left_chart = self._emptychart
589
+ self._right_chart = self._emptychart
590
+
591
+ # The charts that have been loaded.
592
+ self._charts = {"None": self._emptychart}
593
+
594
+ # The output chart.
595
+ self._out_chart = self._emptychart
596
+
597
+ # The most recent operation
598
+ self._operator = None
599
+
600
+ # Set up the root window.
601
+ self._root = Tk()
602
+ self._root.title("Chart Comparison")
603
+ self._root.bind("<Control-q>", self.destroy)
604
+ self._root.bind("<Control-x>", self.destroy)
605
+
606
+ # Initialize all widgets, etc.
607
+ self._init_menubar(self._root)
608
+ self._init_chartviews(self._root)
609
+ self._init_divider(self._root)
610
+ self._init_buttons(self._root)
611
+ self._init_bindings(self._root)
612
+
613
+ # Load any specified charts.
614
+ for filename in chart_filenames:
615
+ self.load_chart(filename)
616
+
617
+ def destroy(self, *e):
618
+ if self._root is None:
619
+ return
620
+ try:
621
+ self._root.destroy()
622
+ except Exception:
623
+ pass
624
+ self._root = None
625
+
626
+ def mainloop(self, *args, **kwargs):
627
+ return
628
+ self._root.mainloop(*args, **kwargs)
629
+
630
+ # ////////////////////////////////////////////////////////////
631
+ # Initialization
632
+ # ////////////////////////////////////////////////////////////
633
+
634
+ def _init_menubar(self, root):
635
+ menubar = Menu(root)
636
+
637
+ # File menu
638
+ filemenu = Menu(menubar, tearoff=0)
639
+ filemenu.add_command(
640
+ label="Load Chart",
641
+ accelerator="Ctrl-o",
642
+ underline=0,
643
+ command=self.load_chart_dialog,
644
+ )
645
+ filemenu.add_command(
646
+ label="Save Output",
647
+ accelerator="Ctrl-s",
648
+ underline=0,
649
+ command=self.save_chart_dialog,
650
+ )
651
+ filemenu.add_separator()
652
+ filemenu.add_command(
653
+ label="Exit", underline=1, command=self.destroy, accelerator="Ctrl-x"
654
+ )
655
+ menubar.add_cascade(label="File", underline=0, menu=filemenu)
656
+
657
+ # Compare menu
658
+ opmenu = Menu(menubar, tearoff=0)
659
+ opmenu.add_command(
660
+ label="Intersection", command=self._intersection, accelerator="+"
661
+ )
662
+ opmenu.add_command(label="Union", command=self._union, accelerator="*")
663
+ opmenu.add_command(
664
+ label="Difference", command=self._difference, accelerator="-"
665
+ )
666
+ opmenu.add_separator()
667
+ opmenu.add_command(label="Swap Charts", command=self._swapcharts)
668
+ menubar.add_cascade(label="Compare", underline=0, menu=opmenu)
669
+
670
+ # Add the menu
671
+ self._root.config(menu=menubar)
672
+
673
+ def _init_divider(self, root):
674
+ divider = Frame(root, border=2, relief="sunken")
675
+ divider.pack(side="top", fill="x", ipady=2)
676
+
677
+ def _init_chartviews(self, root):
678
+ opfont = ("symbol", -36) # Font for operator.
679
+ eqfont = ("helvetica", -36) # Font for equals sign.
680
+
681
+ frame = Frame(root, background="#c0c0c0")
682
+ frame.pack(side="top", expand=1, fill="both")
683
+
684
+ # The left matrix.
685
+ cv1_frame = Frame(frame, border=3, relief="groove")
686
+ cv1_frame.pack(side="left", padx=8, pady=7, expand=1, fill="both")
687
+ self._left_selector = MutableOptionMenu(
688
+ cv1_frame, list(self._charts.keys()), command=self._select_left
689
+ )
690
+ self._left_selector.pack(side="top", pady=5, fill="x")
691
+ self._left_matrix = ChartMatrixView(
692
+ cv1_frame, self._emptychart, toplevel=False, show_numedges=True
693
+ )
694
+ self._left_matrix.pack(side="bottom", padx=5, pady=5, expand=1, fill="both")
695
+ self._left_matrix.add_callback("select", self.select_edge)
696
+ self._left_matrix.add_callback("select_cell", self.select_cell)
697
+ self._left_matrix.inactivate()
698
+
699
+ # The operator.
700
+ self._op_label = Label(
701
+ frame, text=" ", width=3, background="#c0c0c0", font=opfont
702
+ )
703
+ self._op_label.pack(side="left", padx=5, pady=5)
704
+
705
+ # The right matrix.
706
+ cv2_frame = Frame(frame, border=3, relief="groove")
707
+ cv2_frame.pack(side="left", padx=8, pady=7, expand=1, fill="both")
708
+ self._right_selector = MutableOptionMenu(
709
+ cv2_frame, list(self._charts.keys()), command=self._select_right
710
+ )
711
+ self._right_selector.pack(side="top", pady=5, fill="x")
712
+ self._right_matrix = ChartMatrixView(
713
+ cv2_frame, self._emptychart, toplevel=False, show_numedges=True
714
+ )
715
+ self._right_matrix.pack(side="bottom", padx=5, pady=5, expand=1, fill="both")
716
+ self._right_matrix.add_callback("select", self.select_edge)
717
+ self._right_matrix.add_callback("select_cell", self.select_cell)
718
+ self._right_matrix.inactivate()
719
+
720
+ # The equals sign
721
+ Label(frame, text="=", width=3, background="#c0c0c0", font=eqfont).pack(
722
+ side="left", padx=5, pady=5
723
+ )
724
+
725
+ # The output matrix.
726
+ out_frame = Frame(frame, border=3, relief="groove")
727
+ out_frame.pack(side="left", padx=8, pady=7, expand=1, fill="both")
728
+ self._out_label = Label(out_frame, text="Output")
729
+ self._out_label.pack(side="top", pady=9)
730
+ self._out_matrix = ChartMatrixView(
731
+ out_frame, self._emptychart, toplevel=False, show_numedges=True
732
+ )
733
+ self._out_matrix.pack(side="bottom", padx=5, pady=5, expand=1, fill="both")
734
+ self._out_matrix.add_callback("select", self.select_edge)
735
+ self._out_matrix.add_callback("select_cell", self.select_cell)
736
+ self._out_matrix.inactivate()
737
+
738
+ def _init_buttons(self, root):
739
+ buttons = Frame(root)
740
+ buttons.pack(side="bottom", pady=5, fill="x", expand=0)
741
+ Button(buttons, text="Intersection", command=self._intersection).pack(
742
+ side="left"
743
+ )
744
+ Button(buttons, text="Union", command=self._union).pack(side="left")
745
+ Button(buttons, text="Difference", command=self._difference).pack(side="left")
746
+ Frame(buttons, width=20).pack(side="left")
747
+ Button(buttons, text="Swap Charts", command=self._swapcharts).pack(side="left")
748
+
749
+ Button(buttons, text="Detach Output", command=self._detach_out).pack(
750
+ side="right"
751
+ )
752
+
753
+ def _init_bindings(self, root):
754
+ # root.bind('<Control-s>', self.save_chart)
755
+ root.bind("<Control-o>", self.load_chart_dialog)
756
+ # root.bind('<Control-r>', self.reset)
757
+
758
+ # ////////////////////////////////////////////////////////////
759
+ # Input Handling
760
+ # ////////////////////////////////////////////////////////////
761
+
762
+ def _select_left(self, name):
763
+ self._left_name = name
764
+ self._left_chart = self._charts[name]
765
+ self._left_matrix.set_chart(self._left_chart)
766
+ if name == "None":
767
+ self._left_matrix.inactivate()
768
+ self._apply_op()
769
+
770
+ def _select_right(self, name):
771
+ self._right_name = name
772
+ self._right_chart = self._charts[name]
773
+ self._right_matrix.set_chart(self._right_chart)
774
+ if name == "None":
775
+ self._right_matrix.inactivate()
776
+ self._apply_op()
777
+
778
+ def _apply_op(self):
779
+ if self._operator == "-":
780
+ self._difference()
781
+ elif self._operator == "or":
782
+ self._union()
783
+ elif self._operator == "and":
784
+ self._intersection()
785
+
786
+ # ////////////////////////////////////////////////////////////
787
+ # File
788
+ # ////////////////////////////////////////////////////////////
789
+ CHART_FILE_TYPES = [("Pickle file", ".pickle"), ("All files", "*")]
790
+
791
+ def save_chart_dialog(self, *args):
792
+ filename = asksaveasfilename(
793
+ filetypes=self.CHART_FILE_TYPES, defaultextension=".pickle"
794
+ )
795
+ if not filename:
796
+ return
797
+ try:
798
+ with open(filename, "wb") as outfile:
799
+ pickle.dump(self._out_chart, outfile)
800
+ except Exception as e:
801
+ showerror("Error Saving Chart", f"Unable to open file: {filename!r}\n{e}")
802
+
803
+ def load_chart_dialog(self, *args):
804
+ filename = askopenfilename(
805
+ filetypes=self.CHART_FILE_TYPES, defaultextension=".pickle"
806
+ )
807
+ if not filename:
808
+ return
809
+ try:
810
+ self.load_chart(filename)
811
+ except Exception as e:
812
+ showerror("Error Loading Chart", f"Unable to open file: {filename!r}\n{e}")
813
+
814
+ def load_chart(self, filename):
815
+ with open(filename, "rb") as infile:
816
+ chart = pickle_load(infile)
817
+ name = os.path.basename(filename)
818
+ if name.endswith(".pickle"):
819
+ name = name[:-7]
820
+ if name.endswith(".chart"):
821
+ name = name[:-6]
822
+ self._charts[name] = chart
823
+ self._left_selector.add(name)
824
+ self._right_selector.add(name)
825
+
826
+ # If either left_matrix or right_matrix is empty, then
827
+ # display the new chart.
828
+ if self._left_chart is self._emptychart:
829
+ self._left_selector.set(name)
830
+ elif self._right_chart is self._emptychart:
831
+ self._right_selector.set(name)
832
+
833
+ def _update_chartviews(self):
834
+ self._left_matrix.update()
835
+ self._right_matrix.update()
836
+ self._out_matrix.update()
837
+
838
+ # ////////////////////////////////////////////////////////////
839
+ # Selection
840
+ # ////////////////////////////////////////////////////////////
841
+
842
+ def select_edge(self, edge):
843
+ if edge in self._left_chart:
844
+ self._left_matrix.markonly_edge(edge)
845
+ else:
846
+ self._left_matrix.unmark_edge()
847
+ if edge in self._right_chart:
848
+ self._right_matrix.markonly_edge(edge)
849
+ else:
850
+ self._right_matrix.unmark_edge()
851
+ if edge in self._out_chart:
852
+ self._out_matrix.markonly_edge(edge)
853
+ else:
854
+ self._out_matrix.unmark_edge()
855
+
856
+ def select_cell(self, i, j):
857
+ self._left_matrix.select_cell(i, j)
858
+ self._right_matrix.select_cell(i, j)
859
+ self._out_matrix.select_cell(i, j)
860
+
861
+ # ////////////////////////////////////////////////////////////
862
+ # Operations
863
+ # ////////////////////////////////////////////////////////////
864
+
865
+ def _difference(self):
866
+ if not self._checkcompat():
867
+ return
868
+
869
+ out_chart = Chart(self._left_chart.tokens())
870
+ for edge in self._left_chart:
871
+ if edge not in self._right_chart:
872
+ out_chart.insert(edge, [])
873
+
874
+ self._update("-", out_chart)
875
+
876
+ def _intersection(self):
877
+ if not self._checkcompat():
878
+ return
879
+
880
+ out_chart = Chart(self._left_chart.tokens())
881
+ for edge in self._left_chart:
882
+ if edge in self._right_chart:
883
+ out_chart.insert(edge, [])
884
+
885
+ self._update("and", out_chart)
886
+
887
+ def _union(self):
888
+ if not self._checkcompat():
889
+ return
890
+
891
+ out_chart = Chart(self._left_chart.tokens())
892
+ for edge in self._left_chart:
893
+ out_chart.insert(edge, [])
894
+ for edge in self._right_chart:
895
+ out_chart.insert(edge, [])
896
+
897
+ self._update("or", out_chart)
898
+
899
+ def _swapcharts(self):
900
+ left, right = self._left_name, self._right_name
901
+ self._left_selector.set(right)
902
+ self._right_selector.set(left)
903
+
904
+ def _checkcompat(self):
905
+ if (
906
+ self._left_chart.tokens() != self._right_chart.tokens()
907
+ or self._left_chart.property_names() != self._right_chart.property_names()
908
+ or self._left_chart == self._emptychart
909
+ or self._right_chart == self._emptychart
910
+ ):
911
+ # Clear & inactivate the output chart.
912
+ self._out_chart = self._emptychart
913
+ self._out_matrix.set_chart(self._out_chart)
914
+ self._out_matrix.inactivate()
915
+ self._out_label["text"] = "Output"
916
+ # Issue some other warning?
917
+ return False
918
+ else:
919
+ return True
920
+
921
+ def _update(self, operator, out_chart):
922
+ self._operator = operator
923
+ self._op_label["text"] = self._OPSYMBOL[operator]
924
+ self._out_chart = out_chart
925
+ self._out_matrix.set_chart(out_chart)
926
+ self._out_label["text"] = "{} {} {}".format(
927
+ self._left_name,
928
+ self._operator,
929
+ self._right_name,
930
+ )
931
+
932
+ def _clear_out_chart(self):
933
+ self._out_chart = self._emptychart
934
+ self._out_matrix.set_chart(self._out_chart)
935
+ self._op_label["text"] = " "
936
+ self._out_matrix.inactivate()
937
+
938
+ def _detach_out(self):
939
+ ChartMatrixView(self._root, self._out_chart, title=self._out_label["text"])
940
+
941
+
942
+ #######################################################################
943
+ # Chart View
944
+ #######################################################################
945
+
946
+
947
+ class ChartView:
948
+ """
949
+ A component for viewing charts. This is used by ``ChartParserApp`` to
950
+ allow students to interactively experiment with various chart
951
+ parsing techniques. It is also used by ``Chart.draw()``.
952
+
953
+ :ivar _chart: The chart that we are giving a view of. This chart
954
+ may be modified; after it is modified, you should call
955
+ ``update``.
956
+ :ivar _sentence: The list of tokens that the chart spans.
957
+
958
+ :ivar _root: The root window.
959
+ :ivar _chart_canvas: The canvas we're using to display the chart
960
+ itself.
961
+ :ivar _tree_canvas: The canvas we're using to display the tree
962
+ that each edge spans. May be None, if we're not displaying
963
+ trees.
964
+ :ivar _sentence_canvas: The canvas we're using to display the sentence
965
+ text. May be None, if we're not displaying the sentence text.
966
+ :ivar _edgetags: A dictionary mapping from edges to the tags of
967
+ the canvas elements (lines, etc) used to display that edge.
968
+ The values of this dictionary have the form
969
+ ``(linetag, rhstag1, dottag, rhstag2, lhstag)``.
970
+ :ivar _treetags: A list of all the tags that make up the tree;
971
+ used to erase the tree (without erasing the loclines).
972
+ :ivar _chart_height: The height of the chart canvas.
973
+ :ivar _sentence_height: The height of the sentence canvas.
974
+ :ivar _tree_height: The height of the tree
975
+
976
+ :ivar _text_height: The height of a text string (in the normal
977
+ font).
978
+
979
+ :ivar _edgelevels: A list of edges at each level of the chart (the
980
+ top level is the 0th element). This list is used to remember
981
+ where edges should be drawn; and to make sure that no edges
982
+ are overlapping on the chart view.
983
+
984
+ :ivar _unitsize: Pixel size of one unit (from the location). This
985
+ is determined by the span of the chart's location, and the
986
+ width of the chart display canvas.
987
+
988
+ :ivar _fontsize: The current font size
989
+
990
+ :ivar _marks: A dictionary from edges to marks. Marks are
991
+ strings, specifying colors (e.g. 'green').
992
+ """
993
+
994
+ _LEAF_SPACING = 10
995
+ _MARGIN = 10
996
+ _TREE_LEVEL_SIZE = 12
997
+ _CHART_LEVEL_SIZE = 40
998
+
999
+ def __init__(self, chart, root=None, **kw):
1000
+ """
1001
+ Construct a new ``Chart`` display.
1002
+ """
1003
+ # Process keyword args.
1004
+ draw_tree = kw.get("draw_tree", 0)
1005
+ draw_sentence = kw.get("draw_sentence", 1)
1006
+ self._fontsize = kw.get("fontsize", -12)
1007
+
1008
+ # The chart!
1009
+ self._chart = chart
1010
+
1011
+ # Callback functions
1012
+ self._callbacks = {}
1013
+
1014
+ # Keep track of drawn edges
1015
+ self._edgelevels = []
1016
+ self._edgetags = {}
1017
+
1018
+ # Keep track of which edges are marked.
1019
+ self._marks = {}
1020
+
1021
+ # These are used to keep track of the set of tree tokens
1022
+ # currently displayed in the tree canvas.
1023
+ self._treetoks = []
1024
+ self._treetoks_edge = None
1025
+ self._treetoks_index = 0
1026
+
1027
+ # Keep track of the tags used to draw the tree
1028
+ self._tree_tags = []
1029
+
1030
+ # Put multiple edges on each level?
1031
+ self._compact = 0
1032
+
1033
+ # If they didn't provide a main window, then set one up.
1034
+ if root is None:
1035
+ top = Tk()
1036
+ top.title("Chart View")
1037
+
1038
+ def destroy1(e, top=top):
1039
+ top.destroy()
1040
+
1041
+ def destroy2(top=top):
1042
+ top.destroy()
1043
+
1044
+ top.bind("q", destroy1)
1045
+ b = Button(top, text="Done", command=destroy2)
1046
+ b.pack(side="bottom")
1047
+ self._root = top
1048
+ else:
1049
+ self._root = root
1050
+
1051
+ # Create some fonts.
1052
+ self._init_fonts(root)
1053
+
1054
+ # Create the chart canvas.
1055
+ (self._chart_sb, self._chart_canvas) = self._sb_canvas(self._root)
1056
+ self._chart_canvas["height"] = 300
1057
+ self._chart_canvas["closeenough"] = 15
1058
+
1059
+ # Create the sentence canvas.
1060
+ if draw_sentence:
1061
+ cframe = Frame(self._root, relief="sunk", border=2)
1062
+ cframe.pack(fill="both", side="bottom")
1063
+ self._sentence_canvas = Canvas(cframe, height=50)
1064
+ self._sentence_canvas["background"] = "#e0e0e0"
1065
+ self._sentence_canvas.pack(fill="both")
1066
+ # self._sentence_canvas['height'] = self._sentence_height
1067
+ else:
1068
+ self._sentence_canvas = None
1069
+
1070
+ # Create the tree canvas.
1071
+ if draw_tree:
1072
+ (sb, canvas) = self._sb_canvas(self._root, "n", "x")
1073
+ (self._tree_sb, self._tree_canvas) = (sb, canvas)
1074
+ self._tree_canvas["height"] = 200
1075
+ else:
1076
+ self._tree_canvas = None
1077
+
1078
+ # Do some analysis to figure out how big the window should be
1079
+ self._analyze()
1080
+ self.draw()
1081
+ self._resize()
1082
+ self._grow()
1083
+
1084
+ # Set up the configure callback, which will be called whenever
1085
+ # the window is resized.
1086
+ self._chart_canvas.bind("<Configure>", self._configure)
1087
+
1088
+ def _init_fonts(self, root):
1089
+ self._boldfont = Font(family="helvetica", weight="bold", size=self._fontsize)
1090
+ self._font = Font(family="helvetica", size=self._fontsize)
1091
+ # See: <http://www.astro.washington.edu/owen/ROTKFolklore.html>
1092
+ self._sysfont = Font(font=Button()["font"])
1093
+ root.option_add("*Font", self._sysfont)
1094
+
1095
+ def _sb_canvas(self, root, expand="y", fill="both", side="bottom"):
1096
+ """
1097
+ Helper for __init__: construct a canvas with a scrollbar.
1098
+ """
1099
+ cframe = Frame(root, relief="sunk", border=2)
1100
+ cframe.pack(fill=fill, expand=expand, side=side)
1101
+ canvas = Canvas(cframe, background="#e0e0e0")
1102
+
1103
+ # Give the canvas a scrollbar.
1104
+ sb = Scrollbar(cframe, orient="vertical")
1105
+ sb.pack(side="right", fill="y")
1106
+ canvas.pack(side="left", fill=fill, expand="yes")
1107
+
1108
+ # Connect the scrollbars to the canvas.
1109
+ sb["command"] = canvas.yview
1110
+ canvas["yscrollcommand"] = sb.set
1111
+
1112
+ return (sb, canvas)
1113
+
1114
+ def scroll_up(self, *e):
1115
+ self._chart_canvas.yview("scroll", -1, "units")
1116
+
1117
+ def scroll_down(self, *e):
1118
+ self._chart_canvas.yview("scroll", 1, "units")
1119
+
1120
+ def page_up(self, *e):
1121
+ self._chart_canvas.yview("scroll", -1, "pages")
1122
+
1123
+ def page_down(self, *e):
1124
+ self._chart_canvas.yview("scroll", 1, "pages")
1125
+
1126
+ def _grow(self):
1127
+ """
1128
+ Grow the window, if necessary
1129
+ """
1130
+ # Grow, if need-be
1131
+ N = self._chart.num_leaves()
1132
+ width = max(
1133
+ int(self._chart_canvas["width"]), N * self._unitsize + ChartView._MARGIN * 2
1134
+ )
1135
+
1136
+ # It won't resize without the second (height) line, but I
1137
+ # don't understand why not.
1138
+ self._chart_canvas.configure(width=width)
1139
+ self._chart_canvas.configure(height=self._chart_canvas["height"])
1140
+
1141
+ self._unitsize = (width - 2 * ChartView._MARGIN) / N
1142
+
1143
+ # Reset the height for the sentence window.
1144
+ if self._sentence_canvas is not None:
1145
+ self._sentence_canvas["height"] = self._sentence_height
1146
+
1147
+ def set_font_size(self, size):
1148
+ self._font.configure(size=-abs(size))
1149
+ self._boldfont.configure(size=-abs(size))
1150
+ self._sysfont.configure(size=-abs(size))
1151
+ self._analyze()
1152
+ self._grow()
1153
+ self.draw()
1154
+
1155
+ def get_font_size(self):
1156
+ return abs(self._fontsize)
1157
+
1158
+ def _configure(self, e):
1159
+ """
1160
+ The configure callback. This is called whenever the window is
1161
+ resized. It is also called when the window is first mapped.
1162
+ It figures out the unit size, and redraws the contents of each
1163
+ canvas.
1164
+ """
1165
+ N = self._chart.num_leaves()
1166
+ self._unitsize = (e.width - 2 * ChartView._MARGIN) / N
1167
+ self.draw()
1168
+
1169
+ def update(self, chart=None):
1170
+ """
1171
+ Draw any edges that have not been drawn. This is typically
1172
+ called when a after modifies the canvas that a CanvasView is
1173
+ displaying. ``update`` will cause any edges that have been
1174
+ added to the chart to be drawn.
1175
+
1176
+ If update is given a ``chart`` argument, then it will replace
1177
+ the current chart with the given chart.
1178
+ """
1179
+ if chart is not None:
1180
+ self._chart = chart
1181
+ self._edgelevels = []
1182
+ self._marks = {}
1183
+ self._analyze()
1184
+ self._grow()
1185
+ self.draw()
1186
+ self.erase_tree()
1187
+ self._resize()
1188
+ else:
1189
+ for edge in self._chart:
1190
+ if edge not in self._edgetags:
1191
+ self._add_edge(edge)
1192
+ self._resize()
1193
+
1194
+ def _edge_conflict(self, edge, lvl):
1195
+ """
1196
+ Return True if the given edge overlaps with any edge on the given
1197
+ level. This is used by _add_edge to figure out what level a
1198
+ new edge should be added to.
1199
+ """
1200
+ (s1, e1) = edge.span()
1201
+ for otheredge in self._edgelevels[lvl]:
1202
+ (s2, e2) = otheredge.span()
1203
+ if (s1 <= s2 < e1) or (s2 <= s1 < e2) or (s1 == s2 == e1 == e2):
1204
+ return True
1205
+ return False
1206
+
1207
+ def _analyze_edge(self, edge):
1208
+ """
1209
+ Given a new edge, recalculate:
1210
+
1211
+ - _text_height
1212
+ - _unitsize (if the edge text is too big for the current
1213
+ _unitsize, then increase _unitsize)
1214
+ """
1215
+ c = self._chart_canvas
1216
+
1217
+ if isinstance(edge, TreeEdge):
1218
+ lhs = edge.lhs()
1219
+ rhselts = []
1220
+ for elt in edge.rhs():
1221
+ if isinstance(elt, Nonterminal):
1222
+ rhselts.append(str(elt.symbol()))
1223
+ else:
1224
+ rhselts.append(repr(elt))
1225
+ rhs = " ".join(rhselts)
1226
+ else:
1227
+ lhs = edge.lhs()
1228
+ rhs = ""
1229
+
1230
+ for s in (lhs, rhs):
1231
+ tag = c.create_text(
1232
+ 0, 0, text=s, font=self._boldfont, anchor="nw", justify="left"
1233
+ )
1234
+ bbox = c.bbox(tag)
1235
+ c.delete(tag)
1236
+ width = bbox[2] # + ChartView._LEAF_SPACING
1237
+ edgelen = max(edge.length(), 1)
1238
+ self._unitsize = max(self._unitsize, width / edgelen)
1239
+ self._text_height = max(self._text_height, bbox[3] - bbox[1])
1240
+
1241
+ def _add_edge(self, edge, minlvl=0):
1242
+ """
1243
+ Add a single edge to the ChartView:
1244
+
1245
+ - Call analyze_edge to recalculate display parameters
1246
+ - Find an available level
1247
+ - Call _draw_edge
1248
+ """
1249
+ # Do NOT show leaf edges in the chart.
1250
+ if isinstance(edge, LeafEdge):
1251
+ return
1252
+
1253
+ if edge in self._edgetags:
1254
+ return
1255
+ self._analyze_edge(edge)
1256
+ self._grow()
1257
+
1258
+ if not self._compact:
1259
+ self._edgelevels.append([edge])
1260
+ lvl = len(self._edgelevels) - 1
1261
+ self._draw_edge(edge, lvl)
1262
+ self._resize()
1263
+ return
1264
+
1265
+ # Figure out what level to draw the edge on.
1266
+ lvl = 0
1267
+ while True:
1268
+ # If this level doesn't exist yet, create it.
1269
+ while lvl >= len(self._edgelevels):
1270
+ self._edgelevels.append([])
1271
+ self._resize()
1272
+
1273
+ # Check if we can fit the edge in this level.
1274
+ if lvl >= minlvl and not self._edge_conflict(edge, lvl):
1275
+ # Go ahead and draw it.
1276
+ self._edgelevels[lvl].append(edge)
1277
+ break
1278
+
1279
+ # Try the next level.
1280
+ lvl += 1
1281
+
1282
+ self._draw_edge(edge, lvl)
1283
+
1284
+ def view_edge(self, edge):
1285
+ level = None
1286
+ for i in range(len(self._edgelevels)):
1287
+ if edge in self._edgelevels[i]:
1288
+ level = i
1289
+ break
1290
+ if level is None:
1291
+ return
1292
+ # Try to view the new edge..
1293
+ y = (level + 1) * self._chart_level_size
1294
+ dy = self._text_height + 10
1295
+ self._chart_canvas.yview("moveto", 1.0)
1296
+ if self._chart_height != 0:
1297
+ self._chart_canvas.yview("moveto", (y - dy) / self._chart_height)
1298
+
1299
+ def _draw_edge(self, edge, lvl):
1300
+ """
1301
+ Draw a single edge on the ChartView.
1302
+ """
1303
+ c = self._chart_canvas
1304
+
1305
+ # Draw the arrow.
1306
+ x1 = edge.start() * self._unitsize + ChartView._MARGIN
1307
+ x2 = edge.end() * self._unitsize + ChartView._MARGIN
1308
+ if x2 == x1:
1309
+ x2 += max(4, self._unitsize / 5)
1310
+ y = (lvl + 1) * self._chart_level_size
1311
+ linetag = c.create_line(x1, y, x2, y, arrow="last", width=3)
1312
+
1313
+ # Draw a label for the edge.
1314
+ if isinstance(edge, TreeEdge):
1315
+ rhs = []
1316
+ for elt in edge.rhs():
1317
+ if isinstance(elt, Nonterminal):
1318
+ rhs.append(str(elt.symbol()))
1319
+ else:
1320
+ rhs.append(repr(elt))
1321
+ pos = edge.dot()
1322
+ else:
1323
+ rhs = []
1324
+ pos = 0
1325
+
1326
+ rhs1 = " ".join(rhs[:pos])
1327
+ rhs2 = " ".join(rhs[pos:])
1328
+ rhstag1 = c.create_text(x1 + 3, y, text=rhs1, font=self._font, anchor="nw")
1329
+ dotx = c.bbox(rhstag1)[2] + 6
1330
+ doty = (c.bbox(rhstag1)[1] + c.bbox(rhstag1)[3]) / 2
1331
+ dottag = c.create_oval(dotx - 2, doty - 2, dotx + 2, doty + 2)
1332
+ rhstag2 = c.create_text(dotx + 6, y, text=rhs2, font=self._font, anchor="nw")
1333
+ lhstag = c.create_text(
1334
+ (x1 + x2) / 2, y, text=str(edge.lhs()), anchor="s", font=self._boldfont
1335
+ )
1336
+
1337
+ # Keep track of the edge's tags.
1338
+ self._edgetags[edge] = (linetag, rhstag1, dottag, rhstag2, lhstag)
1339
+
1340
+ # Register a callback for clicking on the edge.
1341
+ def cb(event, self=self, edge=edge):
1342
+ self._fire_callbacks("select", edge)
1343
+
1344
+ c.tag_bind(rhstag1, "<Button-1>", cb)
1345
+ c.tag_bind(rhstag2, "<Button-1>", cb)
1346
+ c.tag_bind(linetag, "<Button-1>", cb)
1347
+ c.tag_bind(dottag, "<Button-1>", cb)
1348
+ c.tag_bind(lhstag, "<Button-1>", cb)
1349
+
1350
+ self._color_edge(edge)
1351
+
1352
+ def _color_edge(self, edge, linecolor=None, textcolor=None):
1353
+ """
1354
+ Color in an edge with the given colors.
1355
+ If no colors are specified, use intelligent defaults
1356
+ (dependent on selection, etc.)
1357
+ """
1358
+ if edge not in self._edgetags:
1359
+ return
1360
+ c = self._chart_canvas
1361
+
1362
+ if linecolor is not None and textcolor is not None:
1363
+ if edge in self._marks:
1364
+ linecolor = self._marks[edge]
1365
+ tags = self._edgetags[edge]
1366
+ c.itemconfig(tags[0], fill=linecolor)
1367
+ c.itemconfig(tags[1], fill=textcolor)
1368
+ c.itemconfig(tags[2], fill=textcolor, outline=textcolor)
1369
+ c.itemconfig(tags[3], fill=textcolor)
1370
+ c.itemconfig(tags[4], fill=textcolor)
1371
+ return
1372
+ else:
1373
+ N = self._chart.num_leaves()
1374
+ if edge in self._marks:
1375
+ self._color_edge(self._marks[edge])
1376
+ if edge.is_complete() and edge.span() == (0, N):
1377
+ self._color_edge(edge, "#084", "#042")
1378
+ elif isinstance(edge, LeafEdge):
1379
+ self._color_edge(edge, "#48c", "#246")
1380
+ else:
1381
+ self._color_edge(edge, "#00f", "#008")
1382
+
1383
+ def mark_edge(self, edge, mark="#0df"):
1384
+ """
1385
+ Mark an edge
1386
+ """
1387
+ self._marks[edge] = mark
1388
+ self._color_edge(edge)
1389
+
1390
+ def unmark_edge(self, edge=None):
1391
+ """
1392
+ Unmark an edge (or all edges)
1393
+ """
1394
+ if edge is None:
1395
+ old_marked_edges = list(self._marks.keys())
1396
+ self._marks = {}
1397
+ for edge in old_marked_edges:
1398
+ self._color_edge(edge)
1399
+ else:
1400
+ del self._marks[edge]
1401
+ self._color_edge(edge)
1402
+
1403
+ def markonly_edge(self, edge, mark="#0df"):
1404
+ self.unmark_edge()
1405
+ self.mark_edge(edge, mark)
1406
+
1407
+ def _analyze(self):
1408
+ """
1409
+ Analyze the sentence string, to figure out how big a unit needs
1410
+ to be, How big the tree should be, etc.
1411
+ """
1412
+ # Figure out the text height and the unit size.
1413
+ unitsize = 70 # min unitsize
1414
+ text_height = 0
1415
+ c = self._chart_canvas
1416
+
1417
+ # Check against all tokens
1418
+ for leaf in self._chart.leaves():
1419
+ tag = c.create_text(
1420
+ 0, 0, text=repr(leaf), font=self._font, anchor="nw", justify="left"
1421
+ )
1422
+ bbox = c.bbox(tag)
1423
+ c.delete(tag)
1424
+ width = bbox[2] + ChartView._LEAF_SPACING
1425
+ unitsize = max(width, unitsize)
1426
+ text_height = max(text_height, bbox[3] - bbox[1])
1427
+
1428
+ self._unitsize = unitsize
1429
+ self._text_height = text_height
1430
+ self._sentence_height = self._text_height + 2 * ChartView._MARGIN
1431
+
1432
+ # Check against edges.
1433
+ for edge in self._chart.edges():
1434
+ self._analyze_edge(edge)
1435
+
1436
+ # Size of chart levels
1437
+ self._chart_level_size = self._text_height * 2
1438
+
1439
+ # Default tree size..
1440
+ self._tree_height = 3 * (ChartView._TREE_LEVEL_SIZE + self._text_height)
1441
+
1442
+ # Resize the scrollregions.
1443
+ self._resize()
1444
+
1445
+ def _resize(self):
1446
+ """
1447
+ Update the scroll-regions for each canvas. This ensures that
1448
+ everything is within a scroll-region, so the user can use the
1449
+ scrollbars to view the entire display. This does *not*
1450
+ resize the window.
1451
+ """
1452
+ c = self._chart_canvas
1453
+
1454
+ # Reset the chart scroll region
1455
+ width = self._chart.num_leaves() * self._unitsize + ChartView._MARGIN * 2
1456
+
1457
+ levels = len(self._edgelevels)
1458
+ self._chart_height = (levels + 2) * self._chart_level_size
1459
+ c["scrollregion"] = (0, 0, width, self._chart_height)
1460
+
1461
+ # Reset the tree scroll region
1462
+ if self._tree_canvas:
1463
+ self._tree_canvas["scrollregion"] = (0, 0, width, self._tree_height)
1464
+
1465
+ def _draw_loclines(self):
1466
+ """
1467
+ Draw location lines. These are vertical gridlines used to
1468
+ show where each location unit is.
1469
+ """
1470
+ BOTTOM = 50000
1471
+ c1 = self._tree_canvas
1472
+ c2 = self._sentence_canvas
1473
+ c3 = self._chart_canvas
1474
+ margin = ChartView._MARGIN
1475
+ self._loclines = []
1476
+ for i in range(0, self._chart.num_leaves() + 1):
1477
+ x = i * self._unitsize + margin
1478
+
1479
+ if c1:
1480
+ t1 = c1.create_line(x, 0, x, BOTTOM)
1481
+ c1.tag_lower(t1)
1482
+ if c2:
1483
+ t2 = c2.create_line(x, 0, x, self._sentence_height)
1484
+ c2.tag_lower(t2)
1485
+ t3 = c3.create_line(x, 0, x, BOTTOM)
1486
+ c3.tag_lower(t3)
1487
+ t4 = c3.create_text(x + 2, 0, text=repr(i), anchor="nw", font=self._font)
1488
+ c3.tag_lower(t4)
1489
+ # if i % 4 == 0:
1490
+ # if c1: c1.itemconfig(t1, width=2, fill='gray60')
1491
+ # if c2: c2.itemconfig(t2, width=2, fill='gray60')
1492
+ # c3.itemconfig(t3, width=2, fill='gray60')
1493
+ if i % 2 == 0:
1494
+ if c1:
1495
+ c1.itemconfig(t1, fill="gray60")
1496
+ if c2:
1497
+ c2.itemconfig(t2, fill="gray60")
1498
+ c3.itemconfig(t3, fill="gray60")
1499
+ else:
1500
+ if c1:
1501
+ c1.itemconfig(t1, fill="gray80")
1502
+ if c2:
1503
+ c2.itemconfig(t2, fill="gray80")
1504
+ c3.itemconfig(t3, fill="gray80")
1505
+
1506
+ def _draw_sentence(self):
1507
+ """Draw the sentence string."""
1508
+ if self._chart.num_leaves() == 0:
1509
+ return
1510
+ c = self._sentence_canvas
1511
+ margin = ChartView._MARGIN
1512
+ y = ChartView._MARGIN
1513
+
1514
+ for i, leaf in enumerate(self._chart.leaves()):
1515
+ x1 = i * self._unitsize + margin
1516
+ x2 = x1 + self._unitsize
1517
+ x = (x1 + x2) / 2
1518
+ tag = c.create_text(
1519
+ x, y, text=repr(leaf), font=self._font, anchor="n", justify="left"
1520
+ )
1521
+ bbox = c.bbox(tag)
1522
+ rt = c.create_rectangle(
1523
+ x1 + 2,
1524
+ bbox[1] - (ChartView._LEAF_SPACING / 2),
1525
+ x2 - 2,
1526
+ bbox[3] + (ChartView._LEAF_SPACING / 2),
1527
+ fill="#f0f0f0",
1528
+ outline="#f0f0f0",
1529
+ )
1530
+ c.tag_lower(rt)
1531
+
1532
+ def erase_tree(self):
1533
+ for tag in self._tree_tags:
1534
+ self._tree_canvas.delete(tag)
1535
+ self._treetoks = []
1536
+ self._treetoks_edge = None
1537
+ self._treetoks_index = 0
1538
+
1539
+ def draw_tree(self, edge=None):
1540
+ if edge is None and self._treetoks_edge is None:
1541
+ return
1542
+ if edge is None:
1543
+ edge = self._treetoks_edge
1544
+
1545
+ # If it's a new edge, then get a new list of treetoks.
1546
+ if self._treetoks_edge != edge:
1547
+ self._treetoks = [t for t in self._chart.trees(edge) if isinstance(t, Tree)]
1548
+ self._treetoks_edge = edge
1549
+ self._treetoks_index = 0
1550
+
1551
+ # Make sure there's something to draw.
1552
+ if len(self._treetoks) == 0:
1553
+ return
1554
+
1555
+ # Erase the old tree.
1556
+ for tag in self._tree_tags:
1557
+ self._tree_canvas.delete(tag)
1558
+
1559
+ # Draw the new tree.
1560
+ tree = self._treetoks[self._treetoks_index]
1561
+ self._draw_treetok(tree, edge.start())
1562
+
1563
+ # Show how many trees are available for the edge.
1564
+ self._draw_treecycle()
1565
+
1566
+ # Update the scroll region.
1567
+ w = self._chart.num_leaves() * self._unitsize + 2 * ChartView._MARGIN
1568
+ h = tree.height() * (ChartView._TREE_LEVEL_SIZE + self._text_height)
1569
+ self._tree_canvas["scrollregion"] = (0, 0, w, h)
1570
+
1571
+ def cycle_tree(self):
1572
+ self._treetoks_index = (self._treetoks_index + 1) % len(self._treetoks)
1573
+ self.draw_tree(self._treetoks_edge)
1574
+
1575
+ def _draw_treecycle(self):
1576
+ if len(self._treetoks) <= 1:
1577
+ return
1578
+
1579
+ # Draw the label.
1580
+ label = "%d Trees" % len(self._treetoks)
1581
+ c = self._tree_canvas
1582
+ margin = ChartView._MARGIN
1583
+ right = self._chart.num_leaves() * self._unitsize + margin - 2
1584
+ tag = c.create_text(right, 2, anchor="ne", text=label, font=self._boldfont)
1585
+ self._tree_tags.append(tag)
1586
+ _, _, _, y = c.bbox(tag)
1587
+
1588
+ # Draw the triangles.
1589
+ for i in range(len(self._treetoks)):
1590
+ x = right - 20 * (len(self._treetoks) - i - 1)
1591
+ if i == self._treetoks_index:
1592
+ fill = "#084"
1593
+ else:
1594
+ fill = "#fff"
1595
+ tag = c.create_polygon(
1596
+ x, y + 10, x - 5, y, x - 10, y + 10, fill=fill, outline="black"
1597
+ )
1598
+ self._tree_tags.append(tag)
1599
+
1600
+ # Set up a callback: show the tree if they click on its
1601
+ # triangle.
1602
+ def cb(event, self=self, i=i):
1603
+ self._treetoks_index = i
1604
+ self.draw_tree()
1605
+
1606
+ c.tag_bind(tag, "<Button-1>", cb)
1607
+
1608
+ def _draw_treetok(self, treetok, index, depth=0):
1609
+ """
1610
+ :param index: The index of the first leaf in the tree.
1611
+ :return: The index of the first leaf after the tree.
1612
+ """
1613
+ c = self._tree_canvas
1614
+ margin = ChartView._MARGIN
1615
+
1616
+ # Draw the children
1617
+ child_xs = []
1618
+ for child in treetok:
1619
+ if isinstance(child, Tree):
1620
+ child_x, index = self._draw_treetok(child, index, depth + 1)
1621
+ child_xs.append(child_x)
1622
+ else:
1623
+ child_xs.append((2 * index + 1) * self._unitsize / 2 + margin)
1624
+ index += 1
1625
+
1626
+ # If we have children, then get the node's x by averaging their
1627
+ # node x's. Otherwise, make room for ourselves.
1628
+ if child_xs:
1629
+ nodex = sum(child_xs) / len(child_xs)
1630
+ else:
1631
+ # [XX] breaks for null productions.
1632
+ nodex = (2 * index + 1) * self._unitsize / 2 + margin
1633
+ index += 1
1634
+
1635
+ # Draw the node
1636
+ nodey = depth * (ChartView._TREE_LEVEL_SIZE + self._text_height)
1637
+ tag = c.create_text(
1638
+ nodex,
1639
+ nodey,
1640
+ anchor="n",
1641
+ justify="center",
1642
+ text=str(treetok.label()),
1643
+ fill="#042",
1644
+ font=self._boldfont,
1645
+ )
1646
+ self._tree_tags.append(tag)
1647
+
1648
+ # Draw lines to the children.
1649
+ childy = nodey + ChartView._TREE_LEVEL_SIZE + self._text_height
1650
+ for childx, child in zip(child_xs, treetok):
1651
+ if isinstance(child, Tree) and child:
1652
+ # A "real" tree token:
1653
+ tag = c.create_line(
1654
+ nodex,
1655
+ nodey + self._text_height,
1656
+ childx,
1657
+ childy,
1658
+ width=2,
1659
+ fill="#084",
1660
+ )
1661
+ self._tree_tags.append(tag)
1662
+ if isinstance(child, Tree) and not child:
1663
+ # An unexpanded tree token:
1664
+ tag = c.create_line(
1665
+ nodex,
1666
+ nodey + self._text_height,
1667
+ childx,
1668
+ childy,
1669
+ width=2,
1670
+ fill="#048",
1671
+ dash="2 3",
1672
+ )
1673
+ self._tree_tags.append(tag)
1674
+ if not isinstance(child, Tree):
1675
+ # A leaf:
1676
+ tag = c.create_line(
1677
+ nodex,
1678
+ nodey + self._text_height,
1679
+ childx,
1680
+ 10000,
1681
+ width=2,
1682
+ fill="#084",
1683
+ )
1684
+ self._tree_tags.append(tag)
1685
+
1686
+ return nodex, index
1687
+
1688
+ def draw(self):
1689
+ """
1690
+ Draw everything (from scratch).
1691
+ """
1692
+ if self._tree_canvas:
1693
+ self._tree_canvas.delete("all")
1694
+ self.draw_tree()
1695
+
1696
+ if self._sentence_canvas:
1697
+ self._sentence_canvas.delete("all")
1698
+ self._draw_sentence()
1699
+
1700
+ self._chart_canvas.delete("all")
1701
+ self._edgetags = {}
1702
+
1703
+ # Redraw any edges we erased.
1704
+ for lvl in range(len(self._edgelevels)):
1705
+ for edge in self._edgelevels[lvl]:
1706
+ self._draw_edge(edge, lvl)
1707
+
1708
+ for edge in self._chart:
1709
+ self._add_edge(edge)
1710
+
1711
+ self._draw_loclines()
1712
+
1713
+ def add_callback(self, event, func):
1714
+ self._callbacks.setdefault(event, {})[func] = 1
1715
+
1716
+ def remove_callback(self, event, func=None):
1717
+ if func is None:
1718
+ del self._callbacks[event]
1719
+ else:
1720
+ try:
1721
+ del self._callbacks[event][func]
1722
+ except KeyError:
1723
+ pass
1724
+
1725
+ def _fire_callbacks(self, event, *args):
1726
+ if event not in self._callbacks:
1727
+ return
1728
+ for cb_func in list(self._callbacks[event].keys()):
1729
+ cb_func(*args)
1730
+
1731
+
1732
+ #######################################################################
1733
+ # Edge Rules
1734
+ #######################################################################
1735
+ # These version of the chart rules only apply to a specific edge.
1736
+ # This lets the user select an edge, and then apply a rule.
1737
+
1738
+
1739
+ class EdgeRule:
1740
+ """
1741
+ To create an edge rule, make an empty base class that uses
1742
+ EdgeRule as the first base class, and the basic rule as the
1743
+ second base class. (Order matters!)
1744
+ """
1745
+
1746
+ def __init__(self, edge):
1747
+ super = self.__class__.__bases__[1]
1748
+ self._edge = edge
1749
+ self.NUM_EDGES = super.NUM_EDGES - 1
1750
+
1751
+ def apply(self, chart, grammar, *edges):
1752
+ super = self.__class__.__bases__[1]
1753
+ edges += (self._edge,)
1754
+ yield from super.apply(self, chart, grammar, *edges)
1755
+
1756
+ def __str__(self):
1757
+ super = self.__class__.__bases__[1]
1758
+ return super.__str__(self)
1759
+
1760
+
1761
+ class TopDownPredictEdgeRule(EdgeRule, TopDownPredictRule):
1762
+ pass
1763
+
1764
+
1765
+ class BottomUpEdgeRule(EdgeRule, BottomUpPredictRule):
1766
+ pass
1767
+
1768
+
1769
+ class BottomUpLeftCornerEdgeRule(EdgeRule, BottomUpPredictCombineRule):
1770
+ pass
1771
+
1772
+
1773
+ class FundamentalEdgeRule(EdgeRule, SingleEdgeFundamentalRule):
1774
+ pass
1775
+
1776
+
1777
+ #######################################################################
1778
+ # Chart Parser Application
1779
+ #######################################################################
1780
+
1781
+
1782
+ class ChartParserApp:
1783
+ def __init__(self, grammar, tokens, title="Chart Parser Application"):
1784
+ # Initialize the parser
1785
+ self._init_parser(grammar, tokens)
1786
+
1787
+ self._root = None
1788
+ try:
1789
+ # Create the root window.
1790
+ self._root = Tk()
1791
+ self._root.title(title)
1792
+ self._root.bind("<Control-q>", self.destroy)
1793
+
1794
+ # Set up some frames.
1795
+ frame3 = Frame(self._root)
1796
+ frame2 = Frame(self._root)
1797
+ frame1 = Frame(self._root)
1798
+ frame3.pack(side="bottom", fill="none")
1799
+ frame2.pack(side="bottom", fill="x")
1800
+ frame1.pack(side="bottom", fill="both", expand=1)
1801
+
1802
+ self._init_fonts(self._root)
1803
+ self._init_animation()
1804
+ self._init_chartview(frame1)
1805
+ self._init_rulelabel(frame2)
1806
+ self._init_buttons(frame3)
1807
+ self._init_menubar()
1808
+
1809
+ self._matrix = None
1810
+ self._results = None
1811
+
1812
+ # Set up keyboard bindings.
1813
+ self._init_bindings()
1814
+
1815
+ except Exception:
1816
+ print("Error creating Tree View")
1817
+ self.destroy()
1818
+ raise
1819
+
1820
+ def destroy(self, *args):
1821
+ if self._root is None:
1822
+ return
1823
+ self._root.destroy()
1824
+ self._root = None
1825
+
1826
+ def mainloop(self, *args, **kwargs):
1827
+ """
1828
+ Enter the Tkinter mainloop. This function must be called if
1829
+ this demo is created from a non-interactive program (e.g.
1830
+ from a secript); otherwise, the demo will close as soon as
1831
+ the script completes.
1832
+ """
1833
+ if in_idle():
1834
+ return
1835
+ self._root.mainloop(*args, **kwargs)
1836
+
1837
+ # ////////////////////////////////////////////////////////////
1838
+ # Initialization Helpers
1839
+ # ////////////////////////////////////////////////////////////
1840
+
1841
+ def _init_parser(self, grammar, tokens):
1842
+ self._grammar = grammar
1843
+ self._tokens = tokens
1844
+ self._reset_parser()
1845
+
1846
+ def _reset_parser(self):
1847
+ self._cp = SteppingChartParser(self._grammar)
1848
+ self._cp.initialize(self._tokens)
1849
+ self._chart = self._cp.chart()
1850
+
1851
+ # Insert LeafEdges before the parsing starts.
1852
+ for _new_edge in LeafInitRule().apply(self._chart, self._grammar):
1853
+ pass
1854
+
1855
+ # The step iterator -- use this to generate new edges
1856
+ self._cpstep = self._cp.step()
1857
+
1858
+ # The currently selected edge
1859
+ self._selection = None
1860
+
1861
+ def _init_fonts(self, root):
1862
+ # See: <http://www.astro.washington.edu/owen/ROTKFolklore.html>
1863
+ self._sysfont = Font(font=Button()["font"])
1864
+ root.option_add("*Font", self._sysfont)
1865
+
1866
+ # TWhat's our font size (default=same as sysfont)
1867
+ self._size = IntVar(root)
1868
+ self._size.set(self._sysfont.cget("size"))
1869
+
1870
+ self._boldfont = Font(family="helvetica", weight="bold", size=self._size.get())
1871
+ self._font = Font(family="helvetica", size=self._size.get())
1872
+
1873
+ def _init_animation(self):
1874
+ # Are we stepping? (default=yes)
1875
+ self._step = IntVar(self._root)
1876
+ self._step.set(1)
1877
+
1878
+ # What's our animation speed (default=fast)
1879
+ self._animate = IntVar(self._root)
1880
+ self._animate.set(3) # Default speed = fast
1881
+
1882
+ # Are we currently animating?
1883
+ self._animating = 0
1884
+
1885
+ def _init_chartview(self, parent):
1886
+ self._cv = ChartView(self._chart, parent, draw_tree=1, draw_sentence=1)
1887
+ self._cv.add_callback("select", self._click_cv_edge)
1888
+
1889
+ def _init_rulelabel(self, parent):
1890
+ ruletxt = "Last edge generated by:"
1891
+
1892
+ self._rulelabel1 = Label(parent, text=ruletxt, font=self._boldfont)
1893
+ self._rulelabel2 = Label(
1894
+ parent, width=40, relief="groove", anchor="w", font=self._boldfont
1895
+ )
1896
+ self._rulelabel1.pack(side="left")
1897
+ self._rulelabel2.pack(side="left")
1898
+ step = Checkbutton(parent, variable=self._step, text="Step")
1899
+ step.pack(side="right")
1900
+
1901
+ def _init_buttons(self, parent):
1902
+ frame1 = Frame(parent)
1903
+ frame2 = Frame(parent)
1904
+ frame1.pack(side="bottom", fill="x")
1905
+ frame2.pack(side="top", fill="none")
1906
+
1907
+ Button(
1908
+ frame1,
1909
+ text="Reset\nParser",
1910
+ background="#90c0d0",
1911
+ foreground="black",
1912
+ command=self.reset,
1913
+ ).pack(side="right")
1914
+ # Button(frame1, text='Pause',
1915
+ # background='#90c0d0', foreground='black',
1916
+ # command=self.pause).pack(side='left')
1917
+
1918
+ Button(
1919
+ frame1,
1920
+ text="Top Down\nStrategy",
1921
+ background="#90c0d0",
1922
+ foreground="black",
1923
+ command=self.top_down_strategy,
1924
+ ).pack(side="left")
1925
+ Button(
1926
+ frame1,
1927
+ text="Bottom Up\nStrategy",
1928
+ background="#90c0d0",
1929
+ foreground="black",
1930
+ command=self.bottom_up_strategy,
1931
+ ).pack(side="left")
1932
+ Button(
1933
+ frame1,
1934
+ text="Bottom Up\nLeft-Corner Strategy",
1935
+ background="#90c0d0",
1936
+ foreground="black",
1937
+ command=self.bottom_up_leftcorner_strategy,
1938
+ ).pack(side="left")
1939
+
1940
+ Button(
1941
+ frame2,
1942
+ text="Top Down Init\nRule",
1943
+ background="#90f090",
1944
+ foreground="black",
1945
+ command=self.top_down_init,
1946
+ ).pack(side="left")
1947
+ Button(
1948
+ frame2,
1949
+ text="Top Down Predict\nRule",
1950
+ background="#90f090",
1951
+ foreground="black",
1952
+ command=self.top_down_predict,
1953
+ ).pack(side="left")
1954
+ Frame(frame2, width=20).pack(side="left")
1955
+
1956
+ Button(
1957
+ frame2,
1958
+ text="Bottom Up Predict\nRule",
1959
+ background="#90f090",
1960
+ foreground="black",
1961
+ command=self.bottom_up,
1962
+ ).pack(side="left")
1963
+ Frame(frame2, width=20).pack(side="left")
1964
+
1965
+ Button(
1966
+ frame2,
1967
+ text="Bottom Up Left-Corner\nPredict Rule",
1968
+ background="#90f090",
1969
+ foreground="black",
1970
+ command=self.bottom_up_leftcorner,
1971
+ ).pack(side="left")
1972
+ Frame(frame2, width=20).pack(side="left")
1973
+
1974
+ Button(
1975
+ frame2,
1976
+ text="Fundamental\nRule",
1977
+ background="#90f090",
1978
+ foreground="black",
1979
+ command=self.fundamental,
1980
+ ).pack(side="left")
1981
+
1982
+ def _init_bindings(self):
1983
+ self._root.bind("<Up>", self._cv.scroll_up)
1984
+ self._root.bind("<Down>", self._cv.scroll_down)
1985
+ self._root.bind("<Prior>", self._cv.page_up)
1986
+ self._root.bind("<Next>", self._cv.page_down)
1987
+ self._root.bind("<Control-q>", self.destroy)
1988
+ self._root.bind("<Control-x>", self.destroy)
1989
+ self._root.bind("<F1>", self.help)
1990
+
1991
+ self._root.bind("<Control-s>", self.save_chart)
1992
+ self._root.bind("<Control-o>", self.load_chart)
1993
+ self._root.bind("<Control-r>", self.reset)
1994
+
1995
+ self._root.bind("t", self.top_down_strategy)
1996
+ self._root.bind("b", self.bottom_up_strategy)
1997
+ self._root.bind("c", self.bottom_up_leftcorner_strategy)
1998
+ self._root.bind("<space>", self._stop_animation)
1999
+
2000
+ self._root.bind("<Control-g>", self.edit_grammar)
2001
+ self._root.bind("<Control-t>", self.edit_sentence)
2002
+
2003
+ # Animation speed control
2004
+ self._root.bind("-", lambda e, a=self._animate: a.set(1))
2005
+ self._root.bind("=", lambda e, a=self._animate: a.set(2))
2006
+ self._root.bind("+", lambda e, a=self._animate: a.set(3))
2007
+
2008
+ # Step control
2009
+ self._root.bind("s", lambda e, s=self._step: s.set(not s.get()))
2010
+
2011
+ def _init_menubar(self):
2012
+ menubar = Menu(self._root)
2013
+
2014
+ filemenu = Menu(menubar, tearoff=0)
2015
+ filemenu.add_command(
2016
+ label="Save Chart",
2017
+ underline=0,
2018
+ command=self.save_chart,
2019
+ accelerator="Ctrl-s",
2020
+ )
2021
+ filemenu.add_command(
2022
+ label="Load Chart",
2023
+ underline=0,
2024
+ command=self.load_chart,
2025
+ accelerator="Ctrl-o",
2026
+ )
2027
+ filemenu.add_command(
2028
+ label="Reset Chart", underline=0, command=self.reset, accelerator="Ctrl-r"
2029
+ )
2030
+ filemenu.add_separator()
2031
+ filemenu.add_command(label="Save Grammar", command=self.save_grammar)
2032
+ filemenu.add_command(label="Load Grammar", command=self.load_grammar)
2033
+ filemenu.add_separator()
2034
+ filemenu.add_command(
2035
+ label="Exit", underline=1, command=self.destroy, accelerator="Ctrl-x"
2036
+ )
2037
+ menubar.add_cascade(label="File", underline=0, menu=filemenu)
2038
+
2039
+ editmenu = Menu(menubar, tearoff=0)
2040
+ editmenu.add_command(
2041
+ label="Edit Grammar",
2042
+ underline=5,
2043
+ command=self.edit_grammar,
2044
+ accelerator="Ctrl-g",
2045
+ )
2046
+ editmenu.add_command(
2047
+ label="Edit Text",
2048
+ underline=5,
2049
+ command=self.edit_sentence,
2050
+ accelerator="Ctrl-t",
2051
+ )
2052
+ menubar.add_cascade(label="Edit", underline=0, menu=editmenu)
2053
+
2054
+ viewmenu = Menu(menubar, tearoff=0)
2055
+ viewmenu.add_command(
2056
+ label="Chart Matrix", underline=6, command=self.view_matrix
2057
+ )
2058
+ viewmenu.add_command(label="Results", underline=0, command=self.view_results)
2059
+ menubar.add_cascade(label="View", underline=0, menu=viewmenu)
2060
+
2061
+ rulemenu = Menu(menubar, tearoff=0)
2062
+ rulemenu.add_command(
2063
+ label="Top Down Strategy",
2064
+ underline=0,
2065
+ command=self.top_down_strategy,
2066
+ accelerator="t",
2067
+ )
2068
+ rulemenu.add_command(
2069
+ label="Bottom Up Strategy",
2070
+ underline=0,
2071
+ command=self.bottom_up_strategy,
2072
+ accelerator="b",
2073
+ )
2074
+ rulemenu.add_command(
2075
+ label="Bottom Up Left-Corner Strategy",
2076
+ underline=0,
2077
+ command=self.bottom_up_leftcorner_strategy,
2078
+ accelerator="c",
2079
+ )
2080
+ rulemenu.add_separator()
2081
+ rulemenu.add_command(label="Bottom Up Rule", command=self.bottom_up)
2082
+ rulemenu.add_command(
2083
+ label="Bottom Up Left-Corner Rule", command=self.bottom_up_leftcorner
2084
+ )
2085
+ rulemenu.add_command(label="Top Down Init Rule", command=self.top_down_init)
2086
+ rulemenu.add_command(
2087
+ label="Top Down Predict Rule", command=self.top_down_predict
2088
+ )
2089
+ rulemenu.add_command(label="Fundamental Rule", command=self.fundamental)
2090
+ menubar.add_cascade(label="Apply", underline=0, menu=rulemenu)
2091
+
2092
+ animatemenu = Menu(menubar, tearoff=0)
2093
+ animatemenu.add_checkbutton(
2094
+ label="Step", underline=0, variable=self._step, accelerator="s"
2095
+ )
2096
+ animatemenu.add_separator()
2097
+ animatemenu.add_radiobutton(
2098
+ label="No Animation", underline=0, variable=self._animate, value=0
2099
+ )
2100
+ animatemenu.add_radiobutton(
2101
+ label="Slow Animation",
2102
+ underline=0,
2103
+ variable=self._animate,
2104
+ value=1,
2105
+ accelerator="-",
2106
+ )
2107
+ animatemenu.add_radiobutton(
2108
+ label="Normal Animation",
2109
+ underline=0,
2110
+ variable=self._animate,
2111
+ value=2,
2112
+ accelerator="=",
2113
+ )
2114
+ animatemenu.add_radiobutton(
2115
+ label="Fast Animation",
2116
+ underline=0,
2117
+ variable=self._animate,
2118
+ value=3,
2119
+ accelerator="+",
2120
+ )
2121
+ menubar.add_cascade(label="Animate", underline=1, menu=animatemenu)
2122
+
2123
+ zoommenu = Menu(menubar, tearoff=0)
2124
+ zoommenu.add_radiobutton(
2125
+ label="Tiny",
2126
+ variable=self._size,
2127
+ underline=0,
2128
+ value=10,
2129
+ command=self.resize,
2130
+ )
2131
+ zoommenu.add_radiobutton(
2132
+ label="Small",
2133
+ variable=self._size,
2134
+ underline=0,
2135
+ value=12,
2136
+ command=self.resize,
2137
+ )
2138
+ zoommenu.add_radiobutton(
2139
+ label="Medium",
2140
+ variable=self._size,
2141
+ underline=0,
2142
+ value=14,
2143
+ command=self.resize,
2144
+ )
2145
+ zoommenu.add_radiobutton(
2146
+ label="Large",
2147
+ variable=self._size,
2148
+ underline=0,
2149
+ value=18,
2150
+ command=self.resize,
2151
+ )
2152
+ zoommenu.add_radiobutton(
2153
+ label="Huge",
2154
+ variable=self._size,
2155
+ underline=0,
2156
+ value=24,
2157
+ command=self.resize,
2158
+ )
2159
+ menubar.add_cascade(label="Zoom", underline=0, menu=zoommenu)
2160
+
2161
+ helpmenu = Menu(menubar, tearoff=0)
2162
+ helpmenu.add_command(label="About", underline=0, command=self.about)
2163
+ helpmenu.add_command(
2164
+ label="Instructions", underline=0, command=self.help, accelerator="F1"
2165
+ )
2166
+ menubar.add_cascade(label="Help", underline=0, menu=helpmenu)
2167
+
2168
+ self._root.config(menu=menubar)
2169
+
2170
+ # ////////////////////////////////////////////////////////////
2171
+ # Selection Handling
2172
+ # ////////////////////////////////////////////////////////////
2173
+
2174
+ def _click_cv_edge(self, edge):
2175
+ if edge != self._selection:
2176
+ # Clicking on a new edge selects it.
2177
+ self._select_edge(edge)
2178
+ else:
2179
+ # Repeated clicks on one edge cycle its trees.
2180
+ self._cv.cycle_tree()
2181
+ # [XX] this can get confused if animation is running
2182
+ # faster than the callbacks...
2183
+
2184
+ def _select_matrix_edge(self, edge):
2185
+ self._select_edge(edge)
2186
+ self._cv.view_edge(edge)
2187
+
2188
+ def _select_edge(self, edge):
2189
+ self._selection = edge
2190
+ # Update the chart view.
2191
+ self._cv.markonly_edge(edge, "#f00")
2192
+ self._cv.draw_tree(edge)
2193
+ # Update the matrix view.
2194
+ if self._matrix:
2195
+ self._matrix.markonly_edge(edge)
2196
+ if self._matrix:
2197
+ self._matrix.view_edge(edge)
2198
+
2199
+ def _deselect_edge(self):
2200
+ self._selection = None
2201
+ # Update the chart view.
2202
+ self._cv.unmark_edge()
2203
+ self._cv.erase_tree()
2204
+ # Update the matrix view
2205
+ if self._matrix:
2206
+ self._matrix.unmark_edge()
2207
+
2208
+ def _show_new_edge(self, edge):
2209
+ self._display_rule(self._cp.current_chartrule())
2210
+ # Update the chart view.
2211
+ self._cv.update()
2212
+ self._cv.draw_tree(edge)
2213
+ self._cv.markonly_edge(edge, "#0df")
2214
+ self._cv.view_edge(edge)
2215
+ # Update the matrix view.
2216
+ if self._matrix:
2217
+ self._matrix.update()
2218
+ if self._matrix:
2219
+ self._matrix.markonly_edge(edge)
2220
+ if self._matrix:
2221
+ self._matrix.view_edge(edge)
2222
+ # Update the results view.
2223
+ if self._results:
2224
+ self._results.update(edge)
2225
+
2226
+ # ////////////////////////////////////////////////////////////
2227
+ # Help/usage
2228
+ # ////////////////////////////////////////////////////////////
2229
+
2230
+ def help(self, *e):
2231
+ self._animating = 0
2232
+ # The default font's not very legible; try using 'fixed' instead.
2233
+ try:
2234
+ ShowText(
2235
+ self._root,
2236
+ "Help: Chart Parser Application",
2237
+ (__doc__ or "").strip(),
2238
+ width=75,
2239
+ font="fixed",
2240
+ )
2241
+ except Exception:
2242
+ ShowText(
2243
+ self._root,
2244
+ "Help: Chart Parser Application",
2245
+ (__doc__ or "").strip(),
2246
+ width=75,
2247
+ )
2248
+
2249
+ def about(self, *e):
2250
+ ABOUT = "NLTK Chart Parser Application\n" + "Written by Edward Loper"
2251
+ showinfo("About: Chart Parser Application", ABOUT)
2252
+
2253
+ # ////////////////////////////////////////////////////////////
2254
+ # File Menu
2255
+ # ////////////////////////////////////////////////////////////
2256
+
2257
+ CHART_FILE_TYPES = [("Pickle file", ".pickle"), ("All files", "*")]
2258
+ GRAMMAR_FILE_TYPES = [
2259
+ ("Plaintext grammar file", ".cfg"),
2260
+ ("Pickle file", ".pickle"),
2261
+ ("All files", "*"),
2262
+ ]
2263
+
2264
+ def load_chart(self, *args):
2265
+ "Load a chart from a pickle file"
2266
+ filename = askopenfilename(
2267
+ filetypes=self.CHART_FILE_TYPES, defaultextension=".pickle"
2268
+ )
2269
+ if not filename:
2270
+ return
2271
+ try:
2272
+ with open(filename, "rb") as infile:
2273
+ chart = pickle_load(infile)
2274
+ self._chart = chart
2275
+ self._cv.update(chart)
2276
+ if self._matrix:
2277
+ self._matrix.set_chart(chart)
2278
+ if self._matrix:
2279
+ self._matrix.deselect_cell()
2280
+ if self._results:
2281
+ self._results.set_chart(chart)
2282
+ self._cp.set_chart(chart)
2283
+ except Exception as e:
2284
+ raise
2285
+ showerror("Error Loading Chart", "Unable to open file: %r" % filename)
2286
+
2287
+ def save_chart(self, *args):
2288
+ "Save a chart to a pickle file"
2289
+ filename = asksaveasfilename(
2290
+ filetypes=self.CHART_FILE_TYPES, defaultextension=".pickle"
2291
+ )
2292
+ if not filename:
2293
+ return
2294
+ try:
2295
+ with open(filename, "wb") as outfile:
2296
+ pickle.dump(self._chart, outfile)
2297
+ except Exception as e:
2298
+ raise
2299
+ showerror("Error Saving Chart", "Unable to open file: %r" % filename)
2300
+
2301
+ def load_grammar(self, *args):
2302
+ "Load a grammar from a pickle file"
2303
+ filename = askopenfilename(
2304
+ filetypes=self.GRAMMAR_FILE_TYPES, defaultextension=".cfg"
2305
+ )
2306
+ if not filename:
2307
+ return
2308
+ try:
2309
+ if filename.endswith(".pickle"):
2310
+ with open(filename, "rb") as infile:
2311
+ grammar = pickle_load(infile)
2312
+ else:
2313
+ with open(filename) as infile:
2314
+ grammar = CFG.fromstring(infile.read())
2315
+ self.set_grammar(grammar)
2316
+ except Exception as e:
2317
+ showerror("Error Loading Grammar", "Unable to open file: %r" % filename)
2318
+
2319
+ def save_grammar(self, *args):
2320
+ filename = asksaveasfilename(
2321
+ filetypes=self.GRAMMAR_FILE_TYPES, defaultextension=".cfg"
2322
+ )
2323
+ if not filename:
2324
+ return
2325
+ try:
2326
+ if filename.endswith(".pickle"):
2327
+ with open(filename, "wb") as outfile:
2328
+ pickle.dump((self._chart, self._tokens), outfile)
2329
+ else:
2330
+ with open(filename, "w") as outfile:
2331
+ prods = self._grammar.productions()
2332
+ start = [p for p in prods if p.lhs() == self._grammar.start()]
2333
+ rest = [p for p in prods if p.lhs() != self._grammar.start()]
2334
+ for prod in start:
2335
+ outfile.write("%s\n" % prod)
2336
+ for prod in rest:
2337
+ outfile.write("%s\n" % prod)
2338
+ except Exception as e:
2339
+ showerror("Error Saving Grammar", "Unable to open file: %r" % filename)
2340
+
2341
+ def reset(self, *args):
2342
+ self._animating = 0
2343
+ self._reset_parser()
2344
+ self._cv.update(self._chart)
2345
+ if self._matrix:
2346
+ self._matrix.set_chart(self._chart)
2347
+ if self._matrix:
2348
+ self._matrix.deselect_cell()
2349
+ if self._results:
2350
+ self._results.set_chart(self._chart)
2351
+
2352
+ # ////////////////////////////////////////////////////////////
2353
+ # Edit
2354
+ # ////////////////////////////////////////////////////////////
2355
+
2356
+ def edit_grammar(self, *e):
2357
+ CFGEditor(self._root, self._grammar, self.set_grammar)
2358
+
2359
+ def set_grammar(self, grammar):
2360
+ self._grammar = grammar
2361
+ self._cp.set_grammar(grammar)
2362
+ if self._results:
2363
+ self._results.set_grammar(grammar)
2364
+
2365
+ def edit_sentence(self, *e):
2366
+ sentence = " ".join(self._tokens)
2367
+ title = "Edit Text"
2368
+ instr = "Enter a new sentence to parse."
2369
+ EntryDialog(self._root, sentence, instr, self.set_sentence, title)
2370
+
2371
+ def set_sentence(self, sentence):
2372
+ self._tokens = list(sentence.split())
2373
+ self.reset()
2374
+
2375
+ # ////////////////////////////////////////////////////////////
2376
+ # View Menu
2377
+ # ////////////////////////////////////////////////////////////
2378
+
2379
+ def view_matrix(self, *e):
2380
+ if self._matrix is not None:
2381
+ self._matrix.destroy()
2382
+ self._matrix = ChartMatrixView(self._root, self._chart)
2383
+ self._matrix.add_callback("select", self._select_matrix_edge)
2384
+
2385
+ def view_results(self, *e):
2386
+ if self._results is not None:
2387
+ self._results.destroy()
2388
+ self._results = ChartResultsView(self._root, self._chart, self._grammar)
2389
+
2390
+ # ////////////////////////////////////////////////////////////
2391
+ # Zoom Menu
2392
+ # ////////////////////////////////////////////////////////////
2393
+
2394
+ def resize(self):
2395
+ self._animating = 0
2396
+ self.set_font_size(self._size.get())
2397
+
2398
+ def set_font_size(self, size):
2399
+ self._cv.set_font_size(size)
2400
+ self._font.configure(size=-abs(size))
2401
+ self._boldfont.configure(size=-abs(size))
2402
+ self._sysfont.configure(size=-abs(size))
2403
+
2404
+ def get_font_size(self):
2405
+ return abs(self._size.get())
2406
+
2407
+ # ////////////////////////////////////////////////////////////
2408
+ # Parsing
2409
+ # ////////////////////////////////////////////////////////////
2410
+
2411
+ def apply_strategy(self, strategy, edge_strategy=None):
2412
+ # If we're animating, then stop.
2413
+ if self._animating:
2414
+ self._animating = 0
2415
+ return
2416
+
2417
+ # Clear the rule display & mark.
2418
+ self._display_rule(None)
2419
+ # self._cv.unmark_edge()
2420
+
2421
+ if self._step.get():
2422
+ selection = self._selection
2423
+ if (selection is not None) and (edge_strategy is not None):
2424
+ # Apply the given strategy to the selected edge.
2425
+ self._cp.set_strategy([edge_strategy(selection)])
2426
+ newedge = self._apply_strategy()
2427
+
2428
+ # If it failed, then clear the selection.
2429
+ if newedge is None:
2430
+ self._cv.unmark_edge()
2431
+ self._selection = None
2432
+ else:
2433
+ self._cp.set_strategy(strategy)
2434
+ self._apply_strategy()
2435
+
2436
+ else:
2437
+ self._cp.set_strategy(strategy)
2438
+ if self._animate.get():
2439
+ self._animating = 1
2440
+ self._animate_strategy()
2441
+ else:
2442
+ for edge in self._cpstep:
2443
+ if edge is None:
2444
+ break
2445
+ self._cv.update()
2446
+ if self._matrix:
2447
+ self._matrix.update()
2448
+ if self._results:
2449
+ self._results.update()
2450
+
2451
+ def _stop_animation(self, *e):
2452
+ self._animating = 0
2453
+
2454
+ def _animate_strategy(self, speed=1):
2455
+ if self._animating == 0:
2456
+ return
2457
+ if self._apply_strategy() is not None:
2458
+ if self._animate.get() == 0 or self._step.get() == 1:
2459
+ return
2460
+ if self._animate.get() == 1:
2461
+ self._root.after(3000, self._animate_strategy)
2462
+ elif self._animate.get() == 2:
2463
+ self._root.after(1000, self._animate_strategy)
2464
+ else:
2465
+ self._root.after(20, self._animate_strategy)
2466
+
2467
+ def _apply_strategy(self):
2468
+ new_edge = next(self._cpstep)
2469
+
2470
+ if new_edge is not None:
2471
+ self._show_new_edge(new_edge)
2472
+ return new_edge
2473
+
2474
+ def _display_rule(self, rule):
2475
+ if rule is None:
2476
+ self._rulelabel2["text"] = ""
2477
+ else:
2478
+ name = str(rule)
2479
+ self._rulelabel2["text"] = name
2480
+ size = self._cv.get_font_size()
2481
+
2482
+ # ////////////////////////////////////////////////////////////
2483
+ # Parsing Strategies
2484
+ # ////////////////////////////////////////////////////////////
2485
+
2486
+ # Basic rules:
2487
+ _TD_INIT = [TopDownInitRule()]
2488
+ _TD_PREDICT = [TopDownPredictRule()]
2489
+ _BU_RULE = [BottomUpPredictRule()]
2490
+ _BU_LC_RULE = [BottomUpPredictCombineRule()]
2491
+ _FUNDAMENTAL = [SingleEdgeFundamentalRule()]
2492
+
2493
+ # Complete strategies:
2494
+ _TD_STRATEGY = _TD_INIT + _TD_PREDICT + _FUNDAMENTAL
2495
+ _BU_STRATEGY = _BU_RULE + _FUNDAMENTAL
2496
+ _BU_LC_STRATEGY = _BU_LC_RULE + _FUNDAMENTAL
2497
+
2498
+ # Button callback functions:
2499
+ def top_down_init(self, *e):
2500
+ self.apply_strategy(self._TD_INIT, None)
2501
+
2502
+ def top_down_predict(self, *e):
2503
+ self.apply_strategy(self._TD_PREDICT, TopDownPredictEdgeRule)
2504
+
2505
+ def bottom_up(self, *e):
2506
+ self.apply_strategy(self._BU_RULE, BottomUpEdgeRule)
2507
+
2508
+ def bottom_up_leftcorner(self, *e):
2509
+ self.apply_strategy(self._BU_LC_RULE, BottomUpLeftCornerEdgeRule)
2510
+
2511
+ def fundamental(self, *e):
2512
+ self.apply_strategy(self._FUNDAMENTAL, FundamentalEdgeRule)
2513
+
2514
+ def bottom_up_strategy(self, *e):
2515
+ self.apply_strategy(self._BU_STRATEGY, BottomUpEdgeRule)
2516
+
2517
+ def bottom_up_leftcorner_strategy(self, *e):
2518
+ self.apply_strategy(self._BU_LC_STRATEGY, BottomUpLeftCornerEdgeRule)
2519
+
2520
+ def top_down_strategy(self, *e):
2521
+ self.apply_strategy(self._TD_STRATEGY, TopDownPredictEdgeRule)
2522
+
2523
+
2524
+ def app():
2525
+ grammar = CFG.fromstring(
2526
+ """
2527
+ # Grammatical productions.
2528
+ S -> NP VP
2529
+ VP -> VP PP | V NP | V
2530
+ NP -> Det N | NP PP
2531
+ PP -> P NP
2532
+ # Lexical productions.
2533
+ NP -> 'John' | 'I'
2534
+ Det -> 'the' | 'my' | 'a'
2535
+ N -> 'dog' | 'cookie' | 'table' | 'cake' | 'fork'
2536
+ V -> 'ate' | 'saw'
2537
+ P -> 'on' | 'under' | 'with'
2538
+ """
2539
+ )
2540
+
2541
+ sent = "John ate the cake on the table with a fork"
2542
+ sent = "John ate the cake on the table"
2543
+ tokens = list(sent.split())
2544
+
2545
+ print("grammar= (")
2546
+ for rule in grammar.productions():
2547
+ print((" ", repr(rule) + ","))
2548
+ print(")")
2549
+ print("tokens = %r" % tokens)
2550
+ print('Calling "ChartParserApp(grammar, tokens)"...')
2551
+ ChartParserApp(grammar, tokens).mainloop()
2552
+
2553
+
2554
+ if __name__ == "__main__":
2555
+ app()
2556
+
2557
+ # Chart comparer:
2558
+ # charts = ['/tmp/earley.pickle',
2559
+ # '/tmp/topdown.pickle',
2560
+ # '/tmp/bottomup.pickle']
2561
+ # ChartComparer(*charts).mainloop()
2562
+
2563
+ # import profile
2564
+ # profile.run('demo2()', '/tmp/profile.out')
2565
+ # import pstats
2566
+ # p = pstats.Stats('/tmp/profile.out')
2567
+ # p.strip_dirs().sort_stats('time', 'cum').print_stats(60)
2568
+ # p.strip_dirs().sort_stats('cum', 'time').print_stats(60)
2569
+
2570
+ __all__ = ["app"]
loan-advisor-agent/.venv/Lib/site-packages/nltk/app/chunkparser_app.py ADDED
@@ -0,0 +1,1500 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: Regexp Chunk Parser Application
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Author: Edward Loper <edloper@gmail.com>
5
+ # URL: <https://www.nltk.org/>
6
+ # For license information, see LICENSE.TXT
7
+
8
+ """
9
+ A graphical tool for exploring the regular expression based chunk
10
+ parser ``nltk.chunk.RegexpChunkParser``.
11
+ """
12
+
13
+ # Todo: Add a way to select the development set from the menubar. This
14
+ # might just need to be a selection box (conll vs treebank etc) plus
15
+ # configuration parameters to select what's being chunked (eg VP vs NP)
16
+ # and what part of the data is being used as the development set.
17
+
18
+ import random
19
+ import re
20
+ import textwrap
21
+ import time
22
+ from tkinter import (
23
+ Button,
24
+ Canvas,
25
+ Checkbutton,
26
+ Frame,
27
+ IntVar,
28
+ Label,
29
+ Menu,
30
+ Scrollbar,
31
+ Text,
32
+ Tk,
33
+ )
34
+ from tkinter.filedialog import askopenfilename, asksaveasfilename
35
+ from tkinter.font import Font
36
+
37
+ from nltk.chunk import ChunkScore, RegexpChunkParser
38
+ from nltk.chunk.regexp import RegexpChunkRule
39
+ from nltk.corpus import conll2000, treebank_chunk
40
+ from nltk.draw.util import ShowText
41
+ from nltk.tree import Tree
42
+ from nltk.util import in_idle
43
+
44
+
45
+ class RegexpChunkApp:
46
+ """
47
+ A graphical tool for exploring the regular expression based chunk
48
+ parser ``nltk.chunk.RegexpChunkParser``.
49
+
50
+ See ``HELP`` for instructional text.
51
+ """
52
+
53
+ ##/////////////////////////////////////////////////////////////////
54
+ ## Help Text
55
+ ##/////////////////////////////////////////////////////////////////
56
+
57
+ #: A dictionary mapping from part of speech tags to descriptions,
58
+ #: which is used in the help text. (This should probably live with
59
+ #: the conll and/or treebank corpus instead.)
60
+ TAGSET = {
61
+ "CC": "Coordinating conjunction",
62
+ "PRP$": "Possessive pronoun",
63
+ "CD": "Cardinal number",
64
+ "RB": "Adverb",
65
+ "DT": "Determiner",
66
+ "RBR": "Adverb, comparative",
67
+ "EX": "Existential there",
68
+ "RBS": "Adverb, superlative",
69
+ "FW": "Foreign word",
70
+ "RP": "Particle",
71
+ "JJ": "Adjective",
72
+ "TO": "to",
73
+ "JJR": "Adjective, comparative",
74
+ "UH": "Interjection",
75
+ "JJS": "Adjective, superlative",
76
+ "VB": "Verb, base form",
77
+ "LS": "List item marker",
78
+ "VBD": "Verb, past tense",
79
+ "MD": "Modal",
80
+ "NNS": "Noun, plural",
81
+ "NN": "Noun, singular or mass",
82
+ "VBN": "Verb, past participle",
83
+ "VBZ": "Verb,3rd ps. sing. present",
84
+ "NNP": "Proper noun, singular",
85
+ "NNPS": "Proper noun plural",
86
+ "WDT": "wh-determiner",
87
+ "PDT": "Predeterminer",
88
+ "WP": "wh-pronoun",
89
+ "POS": "Possessive ending",
90
+ "WP$": "Possessive wh-pronoun",
91
+ "PRP": "Personal pronoun",
92
+ "WRB": "wh-adverb",
93
+ "(": "open parenthesis",
94
+ ")": "close parenthesis",
95
+ "``": "open quote",
96
+ ",": "comma",
97
+ "''": "close quote",
98
+ ".": "period",
99
+ "#": "pound sign (currency marker)",
100
+ "$": "dollar sign (currency marker)",
101
+ "IN": "Preposition/subord. conjunction",
102
+ "SYM": "Symbol (mathematical or scientific)",
103
+ "VBG": "Verb, gerund/present participle",
104
+ "VBP": "Verb, non-3rd ps. sing. present",
105
+ ":": "colon",
106
+ }
107
+
108
+ #: Contents for the help box. This is a list of tuples, one for
109
+ #: each help page, where each tuple has four elements:
110
+ #: - A title (displayed as a tab)
111
+ #: - A string description of tabstops (see Tkinter.Text for details)
112
+ #: - The text contents for the help page. You can use expressions
113
+ #: like <red>...</red> to colorize the text; see ``HELP_AUTOTAG``
114
+ #: for a list of tags you can use for colorizing.
115
+ HELP = [
116
+ (
117
+ "Help",
118
+ "20",
119
+ "Welcome to the regular expression chunk-parser grammar editor. "
120
+ "You can use this editor to develop and test chunk parser grammars "
121
+ "based on NLTK's RegexpChunkParser class.\n\n"
122
+ # Help box.
123
+ "Use this box ('Help') to learn more about the editor; click on the "
124
+ "tabs for help on specific topics:"
125
+ "<indent>\n"
126
+ "Rules: grammar rule types\n"
127
+ "Regexps: regular expression syntax\n"
128
+ "Tags: part of speech tags\n</indent>\n"
129
+ # Grammar.
130
+ "Use the upper-left box ('Grammar') to edit your grammar. "
131
+ "Each line of your grammar specifies a single 'rule', "
132
+ "which performs an action such as creating a chunk or merging "
133
+ "two chunks.\n\n"
134
+ # Dev set.
135
+ "The lower-left box ('Development Set') runs your grammar on the "
136
+ "development set, and displays the results. "
137
+ "Your grammar's chunks are <highlight>highlighted</highlight>, and "
138
+ "the correct (gold standard) chunks are "
139
+ "<underline>underlined</underline>. If they "
140
+ "match, they are displayed in <green>green</green>; otherwise, "
141
+ "they are displayed in <red>red</red>. The box displays a single "
142
+ "sentence from the development set at a time; use the scrollbar or "
143
+ "the next/previous buttons view additional sentences.\n\n"
144
+ # Performance
145
+ "The lower-right box ('Evaluation') tracks the performance of "
146
+ "your grammar on the development set. The 'precision' axis "
147
+ "indicates how many of your grammar's chunks are correct; and "
148
+ "the 'recall' axis indicates how many of the gold standard "
149
+ "chunks your system generated. Typically, you should try to "
150
+ "design a grammar that scores high on both metrics. The "
151
+ "exact precision and recall of the current grammar, as well "
152
+ "as their harmonic mean (the 'f-score'), are displayed in "
153
+ "the status bar at the bottom of the window.",
154
+ ),
155
+ (
156
+ "Rules",
157
+ "10",
158
+ "<h1>{...regexp...}</h1>"
159
+ "<indent>\nChunk rule: creates new chunks from words matching "
160
+ "regexp.</indent>\n\n"
161
+ "<h1>}...regexp...{</h1>"
162
+ "<indent>\nStrip rule: removes words matching regexp from existing "
163
+ "chunks.</indent>\n\n"
164
+ "<h1>...regexp1...}{...regexp2...</h1>"
165
+ "<indent>\nSplit rule: splits chunks that match regexp1 followed by "
166
+ "regexp2 in two.</indent>\n\n"
167
+ "<h1>...regexp...{}...regexp...</h1>"
168
+ "<indent>\nMerge rule: joins consecutive chunks that match regexp1 "
169
+ "and regexp2</indent>\n",
170
+ ),
171
+ (
172
+ "Regexps",
173
+ "10 60",
174
+ # "Regular Expression Syntax Summary:\n\n"
175
+ "<h1>Pattern\t\tMatches...</h1>\n"
176
+ "<hangindent>"
177
+ "\t<<var>T</var>>\ta word with tag <var>T</var> "
178
+ "(where <var>T</var> may be a regexp).\n"
179
+ "\t<var>x</var>?\tan optional <var>x</var>\n"
180
+ "\t<var>x</var>+\ta sequence of 1 or more <var>x</var>'s\n"
181
+ "\t<var>x</var>*\ta sequence of 0 or more <var>x</var>'s\n"
182
+ "\t<var>x</var>|<var>y</var>\t<var>x</var> or <var>y</var>\n"
183
+ "\t.\tmatches any character\n"
184
+ "\t(<var>x</var>)\tTreats <var>x</var> as a group\n"
185
+ "\t# <var>x...</var>\tTreats <var>x...</var> "
186
+ "(to the end of the line) as a comment\n"
187
+ "\t\\<var>C</var>\tmatches character <var>C</var> "
188
+ "(useful when <var>C</var> is a special character "
189
+ "like + or #)\n"
190
+ "</hangindent>"
191
+ "\n<h1>Examples:</h1>\n"
192
+ "<hangindent>"
193
+ "\t<regexp><NN></regexp>\n"
194
+ '\t\tMatches <match>"cow/NN"</match>\n'
195
+ '\t\tMatches <match>"green/NN"</match>\n'
196
+ "\t<regexp><VB.*></regexp>\n"
197
+ '\t\tMatches <match>"eating/VBG"</match>\n'
198
+ '\t\tMatches <match>"ate/VBD"</match>\n'
199
+ "\t<regexp><IN><DT><NN></regexp>\n"
200
+ '\t\tMatches <match>"on/IN the/DT car/NN"</match>\n'
201
+ "\t<regexp><RB>?<VBD></regexp>\n"
202
+ '\t\tMatches <match>"ran/VBD"</match>\n'
203
+ '\t\tMatches <match>"slowly/RB ate/VBD"</match>\n'
204
+ r"\t<regexp><\#><CD> # This is a comment...</regexp>\n"
205
+ '\t\tMatches <match>"#/# 100/CD"</match>\n'
206
+ "</hangindent>",
207
+ ),
208
+ (
209
+ "Tags",
210
+ "10 60",
211
+ "<h1>Part of Speech Tags:</h1>\n"
212
+ + "<hangindent>"
213
+ + "<<TAGSET>>"
214
+ + "</hangindent>\n", # this gets auto-substituted w/ self.TAGSET
215
+ ),
216
+ ]
217
+
218
+ HELP_AUTOTAG = [
219
+ ("red", dict(foreground="#a00")),
220
+ ("green", dict(foreground="#080")),
221
+ ("highlight", dict(background="#ddd")),
222
+ ("underline", dict(underline=True)),
223
+ ("h1", dict(underline=True)),
224
+ ("indent", dict(lmargin1=20, lmargin2=20)),
225
+ ("hangindent", dict(lmargin1=0, lmargin2=60)),
226
+ ("var", dict(foreground="#88f")),
227
+ ("regexp", dict(foreground="#ba7")),
228
+ ("match", dict(foreground="#6a6")),
229
+ ]
230
+
231
+ ##/////////////////////////////////////////////////////////////////
232
+ ## Config Parameters
233
+ ##/////////////////////////////////////////////////////////////////
234
+
235
+ _EVAL_DELAY = 1
236
+ """If the user has not pressed any key for this amount of time (in
237
+ seconds), and the current grammar has not been evaluated, then
238
+ the eval demon will evaluate it."""
239
+
240
+ _EVAL_CHUNK = 15
241
+ """The number of sentences that should be evaluated by the eval
242
+ demon each time it runs."""
243
+ _EVAL_FREQ = 0.2
244
+ """The frequency (in seconds) at which the eval demon is run"""
245
+ _EVAL_DEMON_MIN = 0.02
246
+ """The minimum amount of time that the eval demon should take each time
247
+ it runs -- if it takes less than this time, _EVAL_CHUNK will be
248
+ modified upwards."""
249
+ _EVAL_DEMON_MAX = 0.04
250
+ """The maximum amount of time that the eval demon should take each time
251
+ it runs -- if it takes more than this time, _EVAL_CHUNK will be
252
+ modified downwards."""
253
+
254
+ _GRAMMARBOX_PARAMS = dict(
255
+ width=40,
256
+ height=12,
257
+ background="#efe",
258
+ highlightbackground="#efe",
259
+ highlightthickness=1,
260
+ relief="groove",
261
+ border=2,
262
+ wrap="word",
263
+ )
264
+ _HELPBOX_PARAMS = dict(
265
+ width=15,
266
+ height=15,
267
+ background="#efe",
268
+ highlightbackground="#efe",
269
+ foreground="#555",
270
+ highlightthickness=1,
271
+ relief="groove",
272
+ border=2,
273
+ wrap="word",
274
+ )
275
+ _DEVSETBOX_PARAMS = dict(
276
+ width=70,
277
+ height=10,
278
+ background="#eef",
279
+ highlightbackground="#eef",
280
+ highlightthickness=1,
281
+ relief="groove",
282
+ border=2,
283
+ wrap="word",
284
+ tabs=(30,),
285
+ )
286
+ _STATUS_PARAMS = dict(background="#9bb", relief="groove", border=2)
287
+ _FONT_PARAMS = dict(family="helvetica", size=-20)
288
+ _FRAME_PARAMS = dict(background="#777", padx=2, pady=2, border=3)
289
+ _EVALBOX_PARAMS = dict(
290
+ background="#eef",
291
+ highlightbackground="#eef",
292
+ highlightthickness=1,
293
+ relief="groove",
294
+ border=2,
295
+ width=300,
296
+ height=280,
297
+ )
298
+ _BUTTON_PARAMS = dict(
299
+ background="#777", activebackground="#777", highlightbackground="#777"
300
+ )
301
+ _HELPTAB_BG_COLOR = "#aba"
302
+ _HELPTAB_FG_COLOR = "#efe"
303
+
304
+ _HELPTAB_FG_PARAMS = dict(background="#efe")
305
+ _HELPTAB_BG_PARAMS = dict(background="#aba")
306
+ _HELPTAB_SPACER = 6
307
+
308
+ def normalize_grammar(self, grammar):
309
+ # Strip comments
310
+ grammar = re.sub(r"((\\.|[^#])*)(#.*)?", r"\1", grammar)
311
+ # Normalize whitespace
312
+ grammar = re.sub(" +", " ", grammar)
313
+ grammar = re.sub(r"\n\s+", r"\n", grammar)
314
+ grammar = grammar.strip()
315
+ # [xx] Hack: automatically backslash $!
316
+ grammar = re.sub(r"([^\\])\$", r"\1\\$", grammar)
317
+ return grammar
318
+
319
+ def __init__(
320
+ self,
321
+ devset_name="conll2000",
322
+ devset=None,
323
+ grammar="",
324
+ chunk_label="NP",
325
+ tagset=None,
326
+ ):
327
+ """
328
+ :param devset_name: The name of the development set; used for
329
+ display & for save files. If either the name 'treebank'
330
+ or the name 'conll2000' is used, and devset is None, then
331
+ devset will be set automatically.
332
+ :param devset: A list of chunked sentences
333
+ :param grammar: The initial grammar to display.
334
+ :param tagset: Dictionary from tags to string descriptions, used
335
+ for the help page. Defaults to ``self.TAGSET``.
336
+ """
337
+ self._chunk_label = chunk_label
338
+
339
+ if tagset is None:
340
+ tagset = self.TAGSET
341
+ self.tagset = tagset
342
+
343
+ # Named development sets:
344
+ if devset is None:
345
+ if devset_name == "conll2000":
346
+ devset = conll2000.chunked_sents("train.txt") # [:100]
347
+ elif devset == "treebank":
348
+ devset = treebank_chunk.chunked_sents() # [:100]
349
+ else:
350
+ raise ValueError("Unknown development set %s" % devset_name)
351
+
352
+ self.chunker = None
353
+ """The chunker built from the grammar string"""
354
+
355
+ self.grammar = grammar
356
+ """The unparsed grammar string"""
357
+
358
+ self.normalized_grammar = None
359
+ """A normalized version of ``self.grammar``."""
360
+
361
+ self.grammar_changed = 0
362
+ """The last time() that the grammar was changed."""
363
+
364
+ self.devset = devset
365
+ """The development set -- a list of chunked sentences."""
366
+
367
+ self.devset_name = devset_name
368
+ """The name of the development set (for save files)."""
369
+
370
+ self.devset_index = -1
371
+ """The index into the development set of the first instance
372
+ that's currently being viewed."""
373
+
374
+ self._last_keypress = 0
375
+ """The time() when a key was most recently pressed"""
376
+
377
+ self._history = []
378
+ """A list of (grammar, precision, recall, fscore) tuples for
379
+ grammars that the user has already tried."""
380
+
381
+ self._history_index = 0
382
+ """When the user is scrolling through previous grammars, this
383
+ is used to keep track of which grammar they're looking at."""
384
+
385
+ self._eval_grammar = None
386
+ """The grammar that is being currently evaluated by the eval
387
+ demon."""
388
+
389
+ self._eval_normalized_grammar = None
390
+ """A normalized copy of ``_eval_grammar``."""
391
+
392
+ self._eval_index = 0
393
+ """The index of the next sentence in the development set that
394
+ should be looked at by the eval demon."""
395
+
396
+ self._eval_score = ChunkScore(chunk_label=chunk_label)
397
+ """The ``ChunkScore`` object that's used to keep track of the score
398
+ of the current grammar on the development set."""
399
+
400
+ # Set up the main window.
401
+ top = self.top = Tk()
402
+ top.geometry("+50+50")
403
+ top.title("Regexp Chunk Parser App")
404
+ top.bind("<Control-q>", self.destroy)
405
+
406
+ # Variable that restricts how much of the devset we look at.
407
+ self._devset_size = IntVar(top)
408
+ self._devset_size.set(100)
409
+
410
+ # Set up all the tkinter widgets
411
+ self._init_fonts(top)
412
+ self._init_widgets(top)
413
+ self._init_bindings(top)
414
+ self._init_menubar(top)
415
+ self.grammarbox.focus()
416
+
417
+ # If a grammar was given, then display it.
418
+ if grammar:
419
+ self.grammarbox.insert("end", grammar + "\n")
420
+ self.grammarbox.mark_set("insert", "1.0")
421
+
422
+ # Display the first item in the development set
423
+ self.show_devset(0)
424
+ self.update()
425
+
426
+ def _init_bindings(self, top):
427
+ top.bind("<Control-n>", self._devset_next)
428
+ top.bind("<Control-p>", self._devset_prev)
429
+ top.bind("<Control-t>", self.toggle_show_trace)
430
+ top.bind("<KeyPress>", self.update)
431
+ top.bind("<Control-s>", lambda e: self.save_grammar())
432
+ top.bind("<Control-o>", lambda e: self.load_grammar())
433
+ self.grammarbox.bind("<Control-t>", self.toggle_show_trace)
434
+ self.grammarbox.bind("<Control-n>", self._devset_next)
435
+ self.grammarbox.bind("<Control-p>", self._devset_prev)
436
+
437
+ # Redraw the eval graph when the window size changes
438
+ self.evalbox.bind("<Configure>", self._eval_plot)
439
+
440
+ def _init_fonts(self, top):
441
+ # TWhat's our font size (default=same as sysfont)
442
+ self._size = IntVar(top)
443
+ self._size.set(20)
444
+ self._font = Font(family="helvetica", size=-self._size.get())
445
+ self._smallfont = Font(
446
+ family="helvetica", size=-(int(self._size.get() * 14 // 20))
447
+ )
448
+
449
+ def _init_menubar(self, parent):
450
+ menubar = Menu(parent)
451
+
452
+ filemenu = Menu(menubar, tearoff=0)
453
+ filemenu.add_command(label="Reset Application", underline=0, command=self.reset)
454
+ filemenu.add_command(
455
+ label="Save Current Grammar",
456
+ underline=0,
457
+ accelerator="Ctrl-s",
458
+ command=self.save_grammar,
459
+ )
460
+ filemenu.add_command(
461
+ label="Load Grammar",
462
+ underline=0,
463
+ accelerator="Ctrl-o",
464
+ command=self.load_grammar,
465
+ )
466
+
467
+ filemenu.add_command(
468
+ label="Save Grammar History", underline=13, command=self.save_history
469
+ )
470
+
471
+ filemenu.add_command(
472
+ label="Exit", underline=1, command=self.destroy, accelerator="Ctrl-q"
473
+ )
474
+ menubar.add_cascade(label="File", underline=0, menu=filemenu)
475
+
476
+ viewmenu = Menu(menubar, tearoff=0)
477
+ viewmenu.add_radiobutton(
478
+ label="Tiny",
479
+ variable=self._size,
480
+ underline=0,
481
+ value=10,
482
+ command=self.resize,
483
+ )
484
+ viewmenu.add_radiobutton(
485
+ label="Small",
486
+ variable=self._size,
487
+ underline=0,
488
+ value=16,
489
+ command=self.resize,
490
+ )
491
+ viewmenu.add_radiobutton(
492
+ label="Medium",
493
+ variable=self._size,
494
+ underline=0,
495
+ value=20,
496
+ command=self.resize,
497
+ )
498
+ viewmenu.add_radiobutton(
499
+ label="Large",
500
+ variable=self._size,
501
+ underline=0,
502
+ value=24,
503
+ command=self.resize,
504
+ )
505
+ viewmenu.add_radiobutton(
506
+ label="Huge",
507
+ variable=self._size,
508
+ underline=0,
509
+ value=34,
510
+ command=self.resize,
511
+ )
512
+ menubar.add_cascade(label="View", underline=0, menu=viewmenu)
513
+
514
+ devsetmenu = Menu(menubar, tearoff=0)
515
+ devsetmenu.add_radiobutton(
516
+ label="50 sentences",
517
+ variable=self._devset_size,
518
+ value=50,
519
+ command=self.set_devset_size,
520
+ )
521
+ devsetmenu.add_radiobutton(
522
+ label="100 sentences",
523
+ variable=self._devset_size,
524
+ value=100,
525
+ command=self.set_devset_size,
526
+ )
527
+ devsetmenu.add_radiobutton(
528
+ label="200 sentences",
529
+ variable=self._devset_size,
530
+ value=200,
531
+ command=self.set_devset_size,
532
+ )
533
+ devsetmenu.add_radiobutton(
534
+ label="500 sentences",
535
+ variable=self._devset_size,
536
+ value=500,
537
+ command=self.set_devset_size,
538
+ )
539
+ menubar.add_cascade(label="Development-Set", underline=0, menu=devsetmenu)
540
+
541
+ helpmenu = Menu(menubar, tearoff=0)
542
+ helpmenu.add_command(label="About", underline=0, command=self.about)
543
+ menubar.add_cascade(label="Help", underline=0, menu=helpmenu)
544
+
545
+ parent.config(menu=menubar)
546
+
547
+ def toggle_show_trace(self, *e):
548
+ if self._showing_trace:
549
+ self.show_devset()
550
+ else:
551
+ self.show_trace()
552
+ return "break"
553
+
554
+ _SCALE_N = 5 # center on the last 5 examples.
555
+ _DRAW_LINES = False
556
+
557
+ def _eval_plot(self, *e, **config):
558
+ width = config.get("width", self.evalbox.winfo_width())
559
+ height = config.get("height", self.evalbox.winfo_height())
560
+
561
+ # Clear the canvas
562
+ self.evalbox.delete("all")
563
+
564
+ # Draw the precision & recall labels.
565
+ tag = self.evalbox.create_text(
566
+ 10, height // 2 - 10, justify="left", anchor="w", text="Precision"
567
+ )
568
+ left, right = self.evalbox.bbox(tag)[2] + 5, width - 10
569
+ tag = self.evalbox.create_text(
570
+ left + (width - left) // 2,
571
+ height - 10,
572
+ anchor="s",
573
+ text="Recall",
574
+ justify="center",
575
+ )
576
+ top, bot = 10, self.evalbox.bbox(tag)[1] - 10
577
+
578
+ # Draw masks for clipping the plot.
579
+ bg = self._EVALBOX_PARAMS["background"]
580
+ self.evalbox.lower(
581
+ self.evalbox.create_rectangle(0, 0, left - 1, 5000, fill=bg, outline=bg)
582
+ )
583
+ self.evalbox.lower(
584
+ self.evalbox.create_rectangle(0, bot + 1, 5000, 5000, fill=bg, outline=bg)
585
+ )
586
+
587
+ # Calculate the plot's scale.
588
+ if self._autoscale.get() and len(self._history) > 1:
589
+ max_precision = max_recall = 0
590
+ min_precision = min_recall = 1
591
+ for i in range(1, min(len(self._history), self._SCALE_N + 1)):
592
+ grammar, precision, recall, fmeasure = self._history[-i]
593
+ min_precision = min(precision, min_precision)
594
+ min_recall = min(recall, min_recall)
595
+ max_precision = max(precision, max_precision)
596
+ max_recall = max(recall, max_recall)
597
+ # if max_precision-min_precision > max_recall-min_recall:
598
+ # min_recall -= (max_precision-min_precision)/2
599
+ # max_recall += (max_precision-min_precision)/2
600
+ # else:
601
+ # min_precision -= (max_recall-min_recall)/2
602
+ # max_precision += (max_recall-min_recall)/2
603
+ # if min_recall < 0:
604
+ # max_recall -= min_recall
605
+ # min_recall = 0
606
+ # if min_precision < 0:
607
+ # max_precision -= min_precision
608
+ # min_precision = 0
609
+ min_precision = max(min_precision - 0.01, 0)
610
+ min_recall = max(min_recall - 0.01, 0)
611
+ max_precision = min(max_precision + 0.01, 1)
612
+ max_recall = min(max_recall + 0.01, 1)
613
+ else:
614
+ min_precision = min_recall = 0
615
+ max_precision = max_recall = 1
616
+
617
+ # Draw the axis lines & grid lines
618
+ for i in range(11):
619
+ x = left + (right - left) * (
620
+ (i / 10.0 - min_recall) / (max_recall - min_recall)
621
+ )
622
+ y = bot - (bot - top) * (
623
+ (i / 10.0 - min_precision) / (max_precision - min_precision)
624
+ )
625
+ if left < x < right:
626
+ self.evalbox.create_line(x, top, x, bot, fill="#888")
627
+ if top < y < bot:
628
+ self.evalbox.create_line(left, y, right, y, fill="#888")
629
+ self.evalbox.create_line(left, top, left, bot)
630
+ self.evalbox.create_line(left, bot, right, bot)
631
+
632
+ # Display the plot's scale
633
+ self.evalbox.create_text(
634
+ left - 3,
635
+ bot,
636
+ justify="right",
637
+ anchor="se",
638
+ text="%d%%" % (100 * min_precision),
639
+ )
640
+ self.evalbox.create_text(
641
+ left - 3,
642
+ top,
643
+ justify="right",
644
+ anchor="ne",
645
+ text="%d%%" % (100 * max_precision),
646
+ )
647
+ self.evalbox.create_text(
648
+ left,
649
+ bot + 3,
650
+ justify="center",
651
+ anchor="nw",
652
+ text="%d%%" % (100 * min_recall),
653
+ )
654
+ self.evalbox.create_text(
655
+ right,
656
+ bot + 3,
657
+ justify="center",
658
+ anchor="ne",
659
+ text="%d%%" % (100 * max_recall),
660
+ )
661
+
662
+ # Display the scores.
663
+ prev_x = prev_y = None
664
+ for i, (_, precision, recall, fscore) in enumerate(self._history):
665
+ x = left + (right - left) * (
666
+ (recall - min_recall) / (max_recall - min_recall)
667
+ )
668
+ y = bot - (bot - top) * (
669
+ (precision - min_precision) / (max_precision - min_precision)
670
+ )
671
+ if i == self._history_index:
672
+ self.evalbox.create_oval(
673
+ x - 2, y - 2, x + 2, y + 2, fill="#0f0", outline="#000"
674
+ )
675
+ self.status["text"] = (
676
+ "Precision: %.2f%%\t" % (precision * 100)
677
+ + "Recall: %.2f%%\t" % (recall * 100)
678
+ + "F-score: %.2f%%" % (fscore * 100)
679
+ )
680
+ else:
681
+ self.evalbox.lower(
682
+ self.evalbox.create_oval(
683
+ x - 2, y - 2, x + 2, y + 2, fill="#afa", outline="#8c8"
684
+ )
685
+ )
686
+ if prev_x is not None and self._eval_lines.get():
687
+ self.evalbox.lower(
688
+ self.evalbox.create_line(prev_x, prev_y, x, y, fill="#8c8")
689
+ )
690
+ prev_x, prev_y = x, y
691
+
692
+ _eval_demon_running = False
693
+
694
+ def _eval_demon(self):
695
+ if self.top is None:
696
+ return
697
+ if self.chunker is None:
698
+ self._eval_demon_running = False
699
+ return
700
+
701
+ # Note our starting time.
702
+ t0 = time.time()
703
+
704
+ # If are still typing, then wait for them to finish.
705
+ if (
706
+ time.time() - self._last_keypress < self._EVAL_DELAY
707
+ and self.normalized_grammar != self._eval_normalized_grammar
708
+ ):
709
+ self._eval_demon_running = True
710
+ return self.top.after(int(self._EVAL_FREQ * 1000), self._eval_demon)
711
+
712
+ # If the grammar changed, restart the evaluation.
713
+ if self.normalized_grammar != self._eval_normalized_grammar:
714
+ # Check if we've seen this grammar already. If so, then
715
+ # just use the old evaluation values.
716
+ for g, p, r, f in self._history:
717
+ if self.normalized_grammar == self.normalize_grammar(g):
718
+ self._history.append((g, p, r, f))
719
+ self._history_index = len(self._history) - 1
720
+ self._eval_plot()
721
+ self._eval_demon_running = False
722
+ self._eval_normalized_grammar = None
723
+ return
724
+ self._eval_index = 0
725
+ self._eval_score = ChunkScore(chunk_label=self._chunk_label)
726
+ self._eval_grammar = self.grammar
727
+ self._eval_normalized_grammar = self.normalized_grammar
728
+
729
+ # If the grammar is empty, the don't bother evaluating it, or
730
+ # recording it in history -- the score will just be 0.
731
+ if self.normalized_grammar.strip() == "":
732
+ # self._eval_index = self._devset_size.get()
733
+ self._eval_demon_running = False
734
+ return
735
+
736
+ # Score the next set of examples
737
+ for gold in self.devset[
738
+ self._eval_index : min(
739
+ self._eval_index + self._EVAL_CHUNK, self._devset_size.get()
740
+ )
741
+ ]:
742
+ guess = self._chunkparse(gold.leaves())
743
+ self._eval_score.score(gold, guess)
744
+
745
+ # update our index in the devset.
746
+ self._eval_index += self._EVAL_CHUNK
747
+
748
+ # Check if we're done
749
+ if self._eval_index >= self._devset_size.get():
750
+ self._history.append(
751
+ (
752
+ self._eval_grammar,
753
+ self._eval_score.precision(),
754
+ self._eval_score.recall(),
755
+ self._eval_score.f_measure(),
756
+ )
757
+ )
758
+ self._history_index = len(self._history) - 1
759
+ self._eval_plot()
760
+ self._eval_demon_running = False
761
+ self._eval_normalized_grammar = None
762
+ else:
763
+ progress = 100 * self._eval_index / self._devset_size.get()
764
+ self.status["text"] = "Evaluating on Development Set (%d%%)" % progress
765
+ self._eval_demon_running = True
766
+ self._adaptively_modify_eval_chunk(time.time() - t0)
767
+ self.top.after(int(self._EVAL_FREQ * 1000), self._eval_demon)
768
+
769
+ def _adaptively_modify_eval_chunk(self, t):
770
+ """
771
+ Modify _EVAL_CHUNK to try to keep the amount of time that the
772
+ eval demon takes between _EVAL_DEMON_MIN and _EVAL_DEMON_MAX.
773
+
774
+ :param t: The amount of time that the eval demon took.
775
+ """
776
+ if t > self._EVAL_DEMON_MAX and self._EVAL_CHUNK > 5:
777
+ self._EVAL_CHUNK = min(
778
+ self._EVAL_CHUNK - 1,
779
+ max(
780
+ int(self._EVAL_CHUNK * (self._EVAL_DEMON_MAX / t)),
781
+ self._EVAL_CHUNK - 10,
782
+ ),
783
+ )
784
+ elif t < self._EVAL_DEMON_MIN:
785
+ self._EVAL_CHUNK = max(
786
+ self._EVAL_CHUNK + 1,
787
+ min(
788
+ int(self._EVAL_CHUNK * (self._EVAL_DEMON_MIN / t)),
789
+ self._EVAL_CHUNK + 10,
790
+ ),
791
+ )
792
+
793
+ def _init_widgets(self, top):
794
+ frame0 = Frame(top, **self._FRAME_PARAMS)
795
+ frame0.grid_columnconfigure(0, weight=4)
796
+ frame0.grid_columnconfigure(3, weight=2)
797
+ frame0.grid_rowconfigure(1, weight=1)
798
+ frame0.grid_rowconfigure(5, weight=1)
799
+
800
+ # The grammar
801
+ self.grammarbox = Text(frame0, font=self._font, **self._GRAMMARBOX_PARAMS)
802
+ self.grammarlabel = Label(
803
+ frame0,
804
+ font=self._font,
805
+ text="Grammar:",
806
+ highlightcolor="black",
807
+ background=self._GRAMMARBOX_PARAMS["background"],
808
+ )
809
+ self.grammarlabel.grid(column=0, row=0, sticky="SW")
810
+ self.grammarbox.grid(column=0, row=1, sticky="NEWS")
811
+
812
+ # Scroll bar for grammar
813
+ grammar_scrollbar = Scrollbar(frame0, command=self.grammarbox.yview)
814
+ grammar_scrollbar.grid(column=1, row=1, sticky="NWS")
815
+ self.grammarbox.config(yscrollcommand=grammar_scrollbar.set)
816
+
817
+ # grammar buttons
818
+ bg = self._FRAME_PARAMS["background"]
819
+ frame3 = Frame(frame0, background=bg)
820
+ frame3.grid(column=0, row=2, sticky="EW")
821
+ Button(
822
+ frame3,
823
+ text="Prev Grammar",
824
+ command=self._history_prev,
825
+ **self._BUTTON_PARAMS,
826
+ ).pack(side="left")
827
+ Button(
828
+ frame3,
829
+ text="Next Grammar",
830
+ command=self._history_next,
831
+ **self._BUTTON_PARAMS,
832
+ ).pack(side="left")
833
+
834
+ # Help box
835
+ self.helpbox = Text(frame0, font=self._smallfont, **self._HELPBOX_PARAMS)
836
+ self.helpbox.grid(column=3, row=1, sticky="NEWS")
837
+ self.helptabs = {}
838
+ bg = self._FRAME_PARAMS["background"]
839
+ helptab_frame = Frame(frame0, background=bg)
840
+ helptab_frame.grid(column=3, row=0, sticky="SW")
841
+ for i, (tab, tabstops, text) in enumerate(self.HELP):
842
+ label = Label(helptab_frame, text=tab, font=self._smallfont)
843
+ label.grid(column=i * 2, row=0, sticky="S")
844
+ # help_frame.grid_columnconfigure(i, weight=1)
845
+ # label.pack(side='left')
846
+ label.bind("<ButtonPress>", lambda e, tab=tab: self.show_help(tab))
847
+ self.helptabs[tab] = label
848
+ Frame(
849
+ helptab_frame, height=1, width=self._HELPTAB_SPACER, background=bg
850
+ ).grid(column=i * 2 + 1, row=0)
851
+ self.helptabs[self.HELP[0][0]].configure(font=self._font)
852
+ self.helpbox.tag_config("elide", elide=True)
853
+ for tag, params in self.HELP_AUTOTAG:
854
+ self.helpbox.tag_config("tag-%s" % tag, **params)
855
+ self.show_help(self.HELP[0][0])
856
+
857
+ # Scroll bar for helpbox
858
+ help_scrollbar = Scrollbar(frame0, command=self.helpbox.yview)
859
+ self.helpbox.config(yscrollcommand=help_scrollbar.set)
860
+ help_scrollbar.grid(column=4, row=1, sticky="NWS")
861
+
862
+ # The dev set
863
+ frame4 = Frame(frame0, background=self._FRAME_PARAMS["background"])
864
+ self.devsetbox = Text(frame4, font=self._font, **self._DEVSETBOX_PARAMS)
865
+ self.devsetbox.pack(expand=True, fill="both")
866
+ self.devsetlabel = Label(
867
+ frame0,
868
+ font=self._font,
869
+ text="Development Set:",
870
+ justify="right",
871
+ background=self._DEVSETBOX_PARAMS["background"],
872
+ )
873
+ self.devsetlabel.grid(column=0, row=4, sticky="SW")
874
+ frame4.grid(column=0, row=5, sticky="NEWS")
875
+
876
+ # dev set scrollbars
877
+ self.devset_scroll = Scrollbar(frame0, command=self._devset_scroll)
878
+ self.devset_scroll.grid(column=1, row=5, sticky="NWS")
879
+ self.devset_xscroll = Scrollbar(
880
+ frame4, command=self.devsetbox.xview, orient="horiz"
881
+ )
882
+ self.devsetbox["xscrollcommand"] = self.devset_xscroll.set
883
+ self.devset_xscroll.pack(side="bottom", fill="x")
884
+
885
+ # dev set buttons
886
+ bg = self._FRAME_PARAMS["background"]
887
+ frame1 = Frame(frame0, background=bg)
888
+ frame1.grid(column=0, row=7, sticky="EW")
889
+ Button(
890
+ frame1,
891
+ text="Prev Example (Ctrl-p)",
892
+ command=self._devset_prev,
893
+ **self._BUTTON_PARAMS,
894
+ ).pack(side="left")
895
+ Button(
896
+ frame1,
897
+ text="Next Example (Ctrl-n)",
898
+ command=self._devset_next,
899
+ **self._BUTTON_PARAMS,
900
+ ).pack(side="left")
901
+ self.devset_button = Button(
902
+ frame1,
903
+ text="Show example",
904
+ command=self.show_devset,
905
+ state="disabled",
906
+ **self._BUTTON_PARAMS,
907
+ )
908
+ self.devset_button.pack(side="right")
909
+ self.trace_button = Button(
910
+ frame1, text="Show trace", command=self.show_trace, **self._BUTTON_PARAMS
911
+ )
912
+ self.trace_button.pack(side="right")
913
+
914
+ # evaluation box
915
+ self.evalbox = Canvas(frame0, **self._EVALBOX_PARAMS)
916
+ label = Label(
917
+ frame0,
918
+ font=self._font,
919
+ text="Evaluation:",
920
+ justify="right",
921
+ background=self._EVALBOX_PARAMS["background"],
922
+ )
923
+ label.grid(column=3, row=4, sticky="SW")
924
+ self.evalbox.grid(column=3, row=5, sticky="NEWS", columnspan=2)
925
+
926
+ # evaluation box buttons
927
+ bg = self._FRAME_PARAMS["background"]
928
+ frame2 = Frame(frame0, background=bg)
929
+ frame2.grid(column=3, row=7, sticky="EW")
930
+ self._autoscale = IntVar(self.top)
931
+ self._autoscale.set(False)
932
+ Checkbutton(
933
+ frame2,
934
+ variable=self._autoscale,
935
+ command=self._eval_plot,
936
+ text="Zoom",
937
+ **self._BUTTON_PARAMS,
938
+ ).pack(side="left")
939
+ self._eval_lines = IntVar(self.top)
940
+ self._eval_lines.set(False)
941
+ Checkbutton(
942
+ frame2,
943
+ variable=self._eval_lines,
944
+ command=self._eval_plot,
945
+ text="Lines",
946
+ **self._BUTTON_PARAMS,
947
+ ).pack(side="left")
948
+ Button(frame2, text="History", **self._BUTTON_PARAMS).pack(side="right")
949
+
950
+ # The status label
951
+ self.status = Label(frame0, font=self._font, **self._STATUS_PARAMS)
952
+ self.status.grid(column=0, row=9, sticky="NEW", padx=3, pady=2, columnspan=5)
953
+
954
+ # Help box & devset box can't be edited.
955
+ self.helpbox["state"] = "disabled"
956
+ self.devsetbox["state"] = "disabled"
957
+
958
+ # Spacers
959
+ bg = self._FRAME_PARAMS["background"]
960
+ Frame(frame0, height=10, width=0, background=bg).grid(column=0, row=3)
961
+ Frame(frame0, height=0, width=10, background=bg).grid(column=2, row=0)
962
+ Frame(frame0, height=6, width=0, background=bg).grid(column=0, row=8)
963
+
964
+ # pack the frame.
965
+ frame0.pack(fill="both", expand=True)
966
+
967
+ # Set up colors for the devset box
968
+ self.devsetbox.tag_config("true-pos", background="#afa", underline="True")
969
+ self.devsetbox.tag_config("false-neg", underline="True", foreground="#800")
970
+ self.devsetbox.tag_config("false-pos", background="#faa")
971
+ self.devsetbox.tag_config("trace", foreground="#666", wrap="none")
972
+ self.devsetbox.tag_config("wrapindent", lmargin2=30, wrap="none")
973
+ self.devsetbox.tag_config("error", foreground="#800")
974
+
975
+ # And for the grammarbox
976
+ self.grammarbox.tag_config("error", background="#fec")
977
+ self.grammarbox.tag_config("comment", foreground="#840")
978
+ self.grammarbox.tag_config("angle", foreground="#00f")
979
+ self.grammarbox.tag_config("brace", foreground="#0a0")
980
+ self.grammarbox.tag_config("hangindent", lmargin1=0, lmargin2=40)
981
+
982
+ _showing_trace = False
983
+
984
+ def show_trace(self, *e):
985
+ self._showing_trace = True
986
+ self.trace_button["state"] = "disabled"
987
+ self.devset_button["state"] = "normal"
988
+
989
+ self.devsetbox["state"] = "normal"
990
+ # self.devsetbox['wrap'] = 'none'
991
+ self.devsetbox.delete("1.0", "end")
992
+ self.devsetlabel["text"] = "Development Set (%d/%d)" % (
993
+ (self.devset_index + 1, self._devset_size.get())
994
+ )
995
+
996
+ if self.chunker is None:
997
+ self.devsetbox.insert("1.0", "Trace: waiting for a valid grammar.")
998
+ self.devsetbox.tag_add("error", "1.0", "end")
999
+ return # can't do anything more
1000
+
1001
+ gold_tree = self.devset[self.devset_index]
1002
+ rules = self.chunker.rules()
1003
+
1004
+ # Calculate the tag sequence
1005
+ tagseq = "\t"
1006
+ charnum = [1]
1007
+ for wordnum, (word, pos) in enumerate(gold_tree.leaves()):
1008
+ tagseq += "%s " % pos
1009
+ charnum.append(len(tagseq))
1010
+ self.charnum = {
1011
+ (i, j): charnum[j]
1012
+ for i in range(len(rules) + 1)
1013
+ for j in range(len(charnum))
1014
+ }
1015
+ self.linenum = {i: i * 2 + 2 for i in range(len(rules) + 1)}
1016
+
1017
+ for i in range(len(rules) + 1):
1018
+ if i == 0:
1019
+ self.devsetbox.insert("end", "Start:\n")
1020
+ self.devsetbox.tag_add("trace", "end -2c linestart", "end -2c")
1021
+ else:
1022
+ self.devsetbox.insert("end", "Apply %s:\n" % rules[i - 1])
1023
+ self.devsetbox.tag_add("trace", "end -2c linestart", "end -2c")
1024
+ # Display the tag sequence.
1025
+ self.devsetbox.insert("end", tagseq + "\n")
1026
+ self.devsetbox.tag_add("wrapindent", "end -2c linestart", "end -2c")
1027
+ # Run a partial parser, and extract gold & test chunks
1028
+ chunker = RegexpChunkParser(rules[:i])
1029
+ test_tree = self._chunkparse(gold_tree.leaves())
1030
+ gold_chunks = self._chunks(gold_tree)
1031
+ test_chunks = self._chunks(test_tree)
1032
+ # Compare them.
1033
+ for chunk in gold_chunks.intersection(test_chunks):
1034
+ self._color_chunk(i, chunk, "true-pos")
1035
+ for chunk in gold_chunks - test_chunks:
1036
+ self._color_chunk(i, chunk, "false-neg")
1037
+ for chunk in test_chunks - gold_chunks:
1038
+ self._color_chunk(i, chunk, "false-pos")
1039
+ self.devsetbox.insert("end", "Finished.\n")
1040
+ self.devsetbox.tag_add("trace", "end -2c linestart", "end -2c")
1041
+
1042
+ # This is a hack, because the x-scrollbar isn't updating its
1043
+ # position right -- I'm not sure what the underlying cause is
1044
+ # though. (This is on OS X w/ python 2.5)
1045
+ self.top.after(100, self.devset_xscroll.set, 0, 0.3)
1046
+
1047
+ def show_help(self, tab):
1048
+ self.helpbox["state"] = "normal"
1049
+ self.helpbox.delete("1.0", "end")
1050
+ for name, tabstops, text in self.HELP:
1051
+ if name == tab:
1052
+ text = text.replace(
1053
+ "<<TAGSET>>",
1054
+ "\n".join(
1055
+ "\t%s\t%s" % item
1056
+ for item in sorted(
1057
+ list(self.tagset.items()),
1058
+ key=lambda t_w: re.match(r"\w+", t_w[0])
1059
+ and (0, t_w[0])
1060
+ or (1, t_w[0]),
1061
+ )
1062
+ ),
1063
+ )
1064
+
1065
+ self.helptabs[name].config(**self._HELPTAB_FG_PARAMS)
1066
+ self.helpbox.config(tabs=tabstops)
1067
+ self.helpbox.insert("1.0", text + "\n" * 20)
1068
+ C = "1.0 + %d chars"
1069
+ for tag, params in self.HELP_AUTOTAG:
1070
+ pattern = f"(?s)(<{tag}>)(.*?)(</{tag}>)"
1071
+ for m in re.finditer(pattern, text):
1072
+ self.helpbox.tag_add("elide", C % m.start(1), C % m.end(1))
1073
+ self.helpbox.tag_add(
1074
+ "tag-%s" % tag, C % m.start(2), C % m.end(2)
1075
+ )
1076
+ self.helpbox.tag_add("elide", C % m.start(3), C % m.end(3))
1077
+ else:
1078
+ self.helptabs[name].config(**self._HELPTAB_BG_PARAMS)
1079
+ self.helpbox["state"] = "disabled"
1080
+
1081
+ def _history_prev(self, *e):
1082
+ self._view_history(self._history_index - 1)
1083
+ return "break"
1084
+
1085
+ def _history_next(self, *e):
1086
+ self._view_history(self._history_index + 1)
1087
+ return "break"
1088
+
1089
+ def _view_history(self, index):
1090
+ # Bounds & sanity checking:
1091
+ index = max(0, min(len(self._history) - 1, index))
1092
+ if not self._history:
1093
+ return
1094
+ # Already viewing the requested history item?
1095
+ if index == self._history_index:
1096
+ return
1097
+ # Show the requested grammar. It will get added to _history
1098
+ # only if they edit it (causing self.update() to get run.)
1099
+ self.grammarbox["state"] = "normal"
1100
+ self.grammarbox.delete("1.0", "end")
1101
+ self.grammarbox.insert("end", self._history[index][0])
1102
+ self.grammarbox.mark_set("insert", "1.0")
1103
+ self._history_index = index
1104
+ self._syntax_highlight_grammar(self._history[index][0])
1105
+ # Record the normalized grammar & regenerate the chunker.
1106
+ self.normalized_grammar = self.normalize_grammar(self._history[index][0])
1107
+ if self.normalized_grammar:
1108
+ rules = [
1109
+ RegexpChunkRule.fromstring(line)
1110
+ for line in self.normalized_grammar.split("\n")
1111
+ ]
1112
+ else:
1113
+ rules = []
1114
+ self.chunker = RegexpChunkParser(rules)
1115
+ # Show the score.
1116
+ self._eval_plot()
1117
+ # Update the devset box
1118
+ self._highlight_devset()
1119
+ if self._showing_trace:
1120
+ self.show_trace()
1121
+ # Update the grammar label
1122
+ if self._history_index < len(self._history) - 1:
1123
+ self.grammarlabel["text"] = "Grammar {}/{}:".format(
1124
+ self._history_index + 1,
1125
+ len(self._history),
1126
+ )
1127
+ else:
1128
+ self.grammarlabel["text"] = "Grammar:"
1129
+
1130
+ def _devset_next(self, *e):
1131
+ self._devset_scroll("scroll", 1, "page")
1132
+ return "break"
1133
+
1134
+ def _devset_prev(self, *e):
1135
+ self._devset_scroll("scroll", -1, "page")
1136
+ return "break"
1137
+
1138
+ def destroy(self, *e):
1139
+ if self.top is None:
1140
+ return
1141
+ self.top.destroy()
1142
+ self.top = None
1143
+
1144
+ def _devset_scroll(self, command, *args):
1145
+ N = 1 # size of a page -- one sentence.
1146
+ showing_trace = self._showing_trace
1147
+ if command == "scroll" and args[1].startswith("unit"):
1148
+ self.show_devset(self.devset_index + int(args[0]))
1149
+ elif command == "scroll" and args[1].startswith("page"):
1150
+ self.show_devset(self.devset_index + N * int(args[0]))
1151
+ elif command == "moveto":
1152
+ self.show_devset(int(float(args[0]) * self._devset_size.get()))
1153
+ else:
1154
+ assert 0, f"bad scroll command {command} {args}"
1155
+ if showing_trace:
1156
+ self.show_trace()
1157
+
1158
+ def show_devset(self, index=None):
1159
+ if index is None:
1160
+ index = self.devset_index
1161
+
1162
+ # Bounds checking
1163
+ index = min(max(0, index), self._devset_size.get() - 1)
1164
+
1165
+ if index == self.devset_index and not self._showing_trace:
1166
+ return
1167
+ self.devset_index = index
1168
+
1169
+ self._showing_trace = False
1170
+ self.trace_button["state"] = "normal"
1171
+ self.devset_button["state"] = "disabled"
1172
+
1173
+ # Clear the text box.
1174
+ self.devsetbox["state"] = "normal"
1175
+ self.devsetbox["wrap"] = "word"
1176
+ self.devsetbox.delete("1.0", "end")
1177
+ self.devsetlabel["text"] = "Development Set (%d/%d)" % (
1178
+ (self.devset_index + 1, self._devset_size.get())
1179
+ )
1180
+
1181
+ # Add the sentences
1182
+ sample = self.devset[self.devset_index : self.devset_index + 1]
1183
+ self.charnum = {}
1184
+ self.linenum = {0: 1}
1185
+ for sentnum, sent in enumerate(sample):
1186
+ linestr = ""
1187
+ for wordnum, (word, pos) in enumerate(sent.leaves()):
1188
+ self.charnum[sentnum, wordnum] = len(linestr)
1189
+ linestr += f"{word}/{pos} "
1190
+ self.charnum[sentnum, wordnum + 1] = len(linestr)
1191
+ self.devsetbox.insert("end", linestr[:-1] + "\n\n")
1192
+
1193
+ # Highlight chunks in the dev set
1194
+ if self.chunker is not None:
1195
+ self._highlight_devset()
1196
+ self.devsetbox["state"] = "disabled"
1197
+
1198
+ # Update the scrollbar
1199
+ first = self.devset_index / self._devset_size.get()
1200
+ last = (self.devset_index + 2) / self._devset_size.get()
1201
+ self.devset_scroll.set(first, last)
1202
+
1203
+ def _chunks(self, tree):
1204
+ chunks = set()
1205
+ wordnum = 0
1206
+ for child in tree:
1207
+ if isinstance(child, Tree):
1208
+ if child.label() == self._chunk_label:
1209
+ chunks.add((wordnum, wordnum + len(child)))
1210
+ wordnum += len(child)
1211
+ else:
1212
+ wordnum += 1
1213
+ return chunks
1214
+
1215
+ def _syntax_highlight_grammar(self, grammar):
1216
+ if self.top is None:
1217
+ return
1218
+ self.grammarbox.tag_remove("comment", "1.0", "end")
1219
+ self.grammarbox.tag_remove("angle", "1.0", "end")
1220
+ self.grammarbox.tag_remove("brace", "1.0", "end")
1221
+ self.grammarbox.tag_add("hangindent", "1.0", "end")
1222
+ for lineno, line in enumerate(grammar.split("\n")):
1223
+ if not line.strip():
1224
+ continue
1225
+ m = re.match(r"(\\.|[^#])*(#.*)?", line)
1226
+ comment_start = None
1227
+ if m.group(2):
1228
+ comment_start = m.start(2)
1229
+ s = "%d.%d" % (lineno + 1, m.start(2))
1230
+ e = "%d.%d" % (lineno + 1, m.end(2))
1231
+ self.grammarbox.tag_add("comment", s, e)
1232
+ for m in re.finditer("[<>{}]", line):
1233
+ if comment_start is not None and m.start() >= comment_start:
1234
+ break
1235
+ s = "%d.%d" % (lineno + 1, m.start())
1236
+ e = "%d.%d" % (lineno + 1, m.end())
1237
+ if m.group() in "<>":
1238
+ self.grammarbox.tag_add("angle", s, e)
1239
+ else:
1240
+ self.grammarbox.tag_add("brace", s, e)
1241
+
1242
+ def _grammarcheck(self, grammar):
1243
+ if self.top is None:
1244
+ return
1245
+ self.grammarbox.tag_remove("error", "1.0", "end")
1246
+ self._grammarcheck_errs = []
1247
+ for lineno, line in enumerate(grammar.split("\n")):
1248
+ line = re.sub(r"((\\.|[^#])*)(#.*)?", r"\1", line)
1249
+ line = line.strip()
1250
+ if line:
1251
+ try:
1252
+ RegexpChunkRule.fromstring(line)
1253
+ except ValueError as e:
1254
+ self.grammarbox.tag_add(
1255
+ "error", "%s.0" % (lineno + 1), "%s.0 lineend" % (lineno + 1)
1256
+ )
1257
+ self.status["text"] = ""
1258
+
1259
+ def update(self, *event):
1260
+ # Record when update was called (for grammarcheck)
1261
+ if event:
1262
+ self._last_keypress = time.time()
1263
+
1264
+ # Read the grammar from the Text box.
1265
+ self.grammar = grammar = self.grammarbox.get("1.0", "end")
1266
+
1267
+ # If the grammar hasn't changed, do nothing:
1268
+ normalized_grammar = self.normalize_grammar(grammar)
1269
+ if normalized_grammar == self.normalized_grammar:
1270
+ return
1271
+ else:
1272
+ self.normalized_grammar = normalized_grammar
1273
+
1274
+ # If the grammar has changed, and we're looking at history,
1275
+ # then stop looking at history.
1276
+ if self._history_index < len(self._history) - 1:
1277
+ self.grammarlabel["text"] = "Grammar:"
1278
+
1279
+ self._syntax_highlight_grammar(grammar)
1280
+
1281
+ # The grammar has changed; try parsing it. If it doesn't
1282
+ # parse, do nothing. (flag error location?)
1283
+ try:
1284
+ # Note: the normalized grammar has no blank lines.
1285
+ if normalized_grammar:
1286
+ rules = [
1287
+ RegexpChunkRule.fromstring(line)
1288
+ for line in normalized_grammar.split("\n")
1289
+ ]
1290
+ else:
1291
+ rules = []
1292
+ except ValueError as e:
1293
+ # Use the un-normalized grammar for error highlighting.
1294
+ self._grammarcheck(grammar)
1295
+ self.chunker = None
1296
+ return
1297
+
1298
+ self.chunker = RegexpChunkParser(rules)
1299
+ self.grammarbox.tag_remove("error", "1.0", "end")
1300
+ self.grammar_changed = time.time()
1301
+ # Display the results
1302
+ if self._showing_trace:
1303
+ self.show_trace()
1304
+ else:
1305
+ self._highlight_devset()
1306
+ # Start the eval demon
1307
+ if not self._eval_demon_running:
1308
+ self._eval_demon()
1309
+
1310
+ def _highlight_devset(self, sample=None):
1311
+ if sample is None:
1312
+ sample = self.devset[self.devset_index : self.devset_index + 1]
1313
+
1314
+ self.devsetbox.tag_remove("true-pos", "1.0", "end")
1315
+ self.devsetbox.tag_remove("false-neg", "1.0", "end")
1316
+ self.devsetbox.tag_remove("false-pos", "1.0", "end")
1317
+
1318
+ # Run the grammar on the test cases.
1319
+ for sentnum, gold_tree in enumerate(sample):
1320
+ # Run the chunk parser
1321
+ test_tree = self._chunkparse(gold_tree.leaves())
1322
+ # Extract gold & test chunks
1323
+ gold_chunks = self._chunks(gold_tree)
1324
+ test_chunks = self._chunks(test_tree)
1325
+ # Compare them.
1326
+ for chunk in gold_chunks.intersection(test_chunks):
1327
+ self._color_chunk(sentnum, chunk, "true-pos")
1328
+ for chunk in gold_chunks - test_chunks:
1329
+ self._color_chunk(sentnum, chunk, "false-neg")
1330
+ for chunk in test_chunks - gold_chunks:
1331
+ self._color_chunk(sentnum, chunk, "false-pos")
1332
+
1333
+ def _chunkparse(self, words):
1334
+ try:
1335
+ return self.chunker.parse(words)
1336
+ except (ValueError, IndexError) as e:
1337
+ # There's an error somewhere in the grammar, but we're not sure
1338
+ # exactly where, so just mark the whole grammar as bad.
1339
+ # E.g., this is caused by: "({<NN>})"
1340
+ self.grammarbox.tag_add("error", "1.0", "end")
1341
+ # Treat it as tagging nothing:
1342
+ return words
1343
+
1344
+ def _color_chunk(self, sentnum, chunk, tag):
1345
+ start, end = chunk
1346
+ self.devsetbox.tag_add(
1347
+ tag,
1348
+ f"{self.linenum[sentnum]}.{self.charnum[sentnum, start]}",
1349
+ f"{self.linenum[sentnum]}.{self.charnum[sentnum, end] - 1}",
1350
+ )
1351
+
1352
+ def reset(self):
1353
+ # Clear various variables
1354
+ self.chunker = None
1355
+ self.grammar = None
1356
+ self.normalized_grammar = None
1357
+ self.grammar_changed = 0
1358
+ self._history = []
1359
+ self._history_index = 0
1360
+ # Update the on-screen display.
1361
+ self.grammarbox.delete("1.0", "end")
1362
+ self.show_devset(0)
1363
+ self.update()
1364
+ # self._eval_plot()
1365
+
1366
+ SAVE_GRAMMAR_TEMPLATE = (
1367
+ "# Regexp Chunk Parsing Grammar\n"
1368
+ "# Saved %(date)s\n"
1369
+ "#\n"
1370
+ "# Development set: %(devset)s\n"
1371
+ "# Precision: %(precision)s\n"
1372
+ "# Recall: %(recall)s\n"
1373
+ "# F-score: %(fscore)s\n\n"
1374
+ "%(grammar)s\n"
1375
+ )
1376
+
1377
+ def save_grammar(self, filename=None):
1378
+ if not filename:
1379
+ ftypes = [("Chunk Gramamr", ".chunk"), ("All files", "*")]
1380
+ filename = asksaveasfilename(filetypes=ftypes, defaultextension=".chunk")
1381
+ if not filename:
1382
+ return
1383
+ if self._history and self.normalized_grammar == self.normalize_grammar(
1384
+ self._history[-1][0]
1385
+ ):
1386
+ precision, recall, fscore = (
1387
+ "%.2f%%" % (100 * v) for v in self._history[-1][1:]
1388
+ )
1389
+ elif self.chunker is None:
1390
+ precision = recall = fscore = "Grammar not well formed"
1391
+ else:
1392
+ precision = recall = fscore = "Not finished evaluation yet"
1393
+
1394
+ with open(filename, "w") as outfile:
1395
+ outfile.write(
1396
+ self.SAVE_GRAMMAR_TEMPLATE
1397
+ % dict(
1398
+ date=time.ctime(),
1399
+ devset=self.devset_name,
1400
+ precision=precision,
1401
+ recall=recall,
1402
+ fscore=fscore,
1403
+ grammar=self.grammar.strip(),
1404
+ )
1405
+ )
1406
+
1407
+ def load_grammar(self, filename=None):
1408
+ if not filename:
1409
+ ftypes = [("Chunk Gramamr", ".chunk"), ("All files", "*")]
1410
+ filename = askopenfilename(filetypes=ftypes, defaultextension=".chunk")
1411
+ if not filename:
1412
+ return
1413
+ self.grammarbox.delete("1.0", "end")
1414
+ self.update()
1415
+ with open(filename) as infile:
1416
+ grammar = infile.read()
1417
+ grammar = re.sub(
1418
+ r"^\# Regexp Chunk Parsing Grammar[\s\S]*" "F-score:.*\n", "", grammar
1419
+ ).lstrip()
1420
+ self.grammarbox.insert("1.0", grammar)
1421
+ self.update()
1422
+
1423
+ def save_history(self, filename=None):
1424
+ if not filename:
1425
+ ftypes = [("Chunk Gramamr History", ".txt"), ("All files", "*")]
1426
+ filename = asksaveasfilename(filetypes=ftypes, defaultextension=".txt")
1427
+ if not filename:
1428
+ return
1429
+
1430
+ with open(filename, "w") as outfile:
1431
+ outfile.write("# Regexp Chunk Parsing Grammar History\n")
1432
+ outfile.write("# Saved %s\n" % time.ctime())
1433
+ outfile.write("# Development set: %s\n" % self.devset_name)
1434
+ for i, (g, p, r, f) in enumerate(self._history):
1435
+ hdr = (
1436
+ "Grammar %d/%d (precision=%.2f%%, recall=%.2f%%, "
1437
+ "fscore=%.2f%%)"
1438
+ % (i + 1, len(self._history), p * 100, r * 100, f * 100)
1439
+ )
1440
+ outfile.write("\n%s\n" % hdr)
1441
+ outfile.write("".join(" %s\n" % line for line in g.strip().split()))
1442
+
1443
+ if not (
1444
+ self._history
1445
+ and self.normalized_grammar
1446
+ == self.normalize_grammar(self._history[-1][0])
1447
+ ):
1448
+ if self.chunker is None:
1449
+ outfile.write("\nCurrent Grammar (not well-formed)\n")
1450
+ else:
1451
+ outfile.write("\nCurrent Grammar (not evaluated)\n")
1452
+ outfile.write(
1453
+ "".join(" %s\n" % line for line in self.grammar.strip().split())
1454
+ )
1455
+
1456
+ def about(self, *e):
1457
+ ABOUT = "NLTK RegExp Chunk Parser Application\n" + "Written by Edward Loper"
1458
+ TITLE = "About: Regular Expression Chunk Parser Application"
1459
+ try:
1460
+ from tkinter.messagebox import Message
1461
+
1462
+ Message(message=ABOUT, title=TITLE).show()
1463
+ except Exception:
1464
+ ShowText(self.top, TITLE, ABOUT)
1465
+
1466
+ def set_devset_size(self, size=None):
1467
+ if size is not None:
1468
+ self._devset_size.set(size)
1469
+ self._devset_size.set(min(len(self.devset), self._devset_size.get()))
1470
+ self.show_devset(1)
1471
+ self.show_devset(0)
1472
+ # what about history? Evaluated at diff dev set sizes!
1473
+
1474
+ def resize(self, size=None):
1475
+ if size is not None:
1476
+ self._size.set(size)
1477
+ size = self._size.get()
1478
+ self._font.configure(size=-(abs(size)))
1479
+ self._smallfont.configure(size=min(-10, -(abs(size)) * 14 // 20))
1480
+
1481
+ def mainloop(self, *args, **kwargs):
1482
+ """
1483
+ Enter the Tkinter mainloop. This function must be called if
1484
+ this demo is created from a non-interactive program (e.g.
1485
+ from a secript); otherwise, the demo will close as soon as
1486
+ the script completes.
1487
+ """
1488
+ if in_idle():
1489
+ return
1490
+ self.top.mainloop(*args, **kwargs)
1491
+
1492
+
1493
+ def app():
1494
+ RegexpChunkApp().mainloop()
1495
+
1496
+
1497
+ if __name__ == "__main__":
1498
+ app()
1499
+
1500
+ __all__ = ["app"]
loan-advisor-agent/.venv/Lib/site-packages/nltk/app/collocations_app.py ADDED
@@ -0,0 +1,438 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: Collocations Application
2
+ # Much of the GUI code is imported from concordance.py; We intend to merge these tools together
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Author: Sumukh Ghodke <sghodke@csse.unimelb.edu.au>
5
+ # URL: <https://www.nltk.org/>
6
+ # For license information, see LICENSE.TXT
7
+ #
8
+
9
+
10
+ import queue as q
11
+ import threading
12
+ from tkinter import (
13
+ END,
14
+ LEFT,
15
+ SUNKEN,
16
+ Button,
17
+ Frame,
18
+ IntVar,
19
+ Label,
20
+ Menu,
21
+ OptionMenu,
22
+ Scrollbar,
23
+ StringVar,
24
+ Text,
25
+ Tk,
26
+ )
27
+ from tkinter.font import Font
28
+
29
+ from nltk.corpus import (
30
+ alpino,
31
+ brown,
32
+ cess_cat,
33
+ cess_esp,
34
+ floresta,
35
+ indian,
36
+ mac_morpho,
37
+ machado,
38
+ nps_chat,
39
+ sinica_treebank,
40
+ treebank,
41
+ )
42
+ from nltk.probability import FreqDist
43
+ from nltk.util import in_idle
44
+
45
+ CORPUS_LOADED_EVENT = "<<CL_EVENT>>"
46
+ ERROR_LOADING_CORPUS_EVENT = "<<ELC_EVENT>>"
47
+ POLL_INTERVAL = 100
48
+
49
+ _DEFAULT = "English: Brown Corpus (Humor)"
50
+ _CORPORA = {
51
+ "Catalan: CESS-CAT Corpus": lambda: cess_cat.words(),
52
+ "English: Brown Corpus": lambda: brown.words(),
53
+ "English: Brown Corpus (Press)": lambda: brown.words(
54
+ categories=["news", "editorial", "reviews"]
55
+ ),
56
+ "English: Brown Corpus (Religion)": lambda: brown.words(categories="religion"),
57
+ "English: Brown Corpus (Learned)": lambda: brown.words(categories="learned"),
58
+ "English: Brown Corpus (Science Fiction)": lambda: brown.words(
59
+ categories="science_fiction"
60
+ ),
61
+ "English: Brown Corpus (Romance)": lambda: brown.words(categories="romance"),
62
+ "English: Brown Corpus (Humor)": lambda: brown.words(categories="humor"),
63
+ "English: NPS Chat Corpus": lambda: nps_chat.words(),
64
+ "English: Wall Street Journal Corpus": lambda: treebank.words(),
65
+ "Chinese: Sinica Corpus": lambda: sinica_treebank.words(),
66
+ "Dutch: Alpino Corpus": lambda: alpino.words(),
67
+ "Hindi: Indian Languages Corpus": lambda: indian.words(files="hindi.pos"),
68
+ "Portuguese: Floresta Corpus (Portugal)": lambda: floresta.words(),
69
+ "Portuguese: MAC-MORPHO Corpus (Brazil)": lambda: mac_morpho.words(),
70
+ "Portuguese: Machado Corpus (Brazil)": lambda: machado.words(),
71
+ "Spanish: CESS-ESP Corpus": lambda: cess_esp.words(),
72
+ }
73
+
74
+
75
+ class CollocationsView:
76
+ _BACKGROUND_COLOUR = "#FFF" # white
77
+
78
+ def __init__(self):
79
+ self.queue = q.Queue()
80
+ self.model = CollocationsModel(self.queue)
81
+ self.top = Tk()
82
+ self._init_top(self.top)
83
+ self._init_menubar()
84
+ self._init_widgets(self.top)
85
+ self.load_corpus(self.model.DEFAULT_CORPUS)
86
+ self.after = self.top.after(POLL_INTERVAL, self._poll)
87
+
88
+ def _init_top(self, top):
89
+ top.geometry("550x650+50+50")
90
+ top.title("NLTK Collocations List")
91
+ top.bind("<Control-q>", self.destroy)
92
+ top.protocol("WM_DELETE_WINDOW", self.destroy)
93
+ top.minsize(550, 650)
94
+
95
+ def _init_widgets(self, parent):
96
+ self.main_frame = Frame(
97
+ parent, dict(background=self._BACKGROUND_COLOUR, padx=1, pady=1, border=1)
98
+ )
99
+ self._init_corpus_select(self.main_frame)
100
+ self._init_results_box(self.main_frame)
101
+ self._init_paging(self.main_frame)
102
+ self._init_status(self.main_frame)
103
+ self.main_frame.pack(fill="both", expand=True)
104
+
105
+ def _init_corpus_select(self, parent):
106
+ innerframe = Frame(parent, background=self._BACKGROUND_COLOUR)
107
+ self.var = StringVar(innerframe)
108
+ self.var.set(self.model.DEFAULT_CORPUS)
109
+ Label(
110
+ innerframe,
111
+ justify=LEFT,
112
+ text=" Corpus: ",
113
+ background=self._BACKGROUND_COLOUR,
114
+ padx=2,
115
+ pady=1,
116
+ border=0,
117
+ ).pack(side="left")
118
+
119
+ other_corpora = list(self.model.CORPORA.keys()).remove(
120
+ self.model.DEFAULT_CORPUS
121
+ )
122
+ om = OptionMenu(
123
+ innerframe,
124
+ self.var,
125
+ self.model.DEFAULT_CORPUS,
126
+ command=self.corpus_selected,
127
+ *self.model.non_default_corpora()
128
+ )
129
+ om["borderwidth"] = 0
130
+ om["highlightthickness"] = 1
131
+ om.pack(side="left")
132
+ innerframe.pack(side="top", fill="x", anchor="n")
133
+
134
+ def _init_status(self, parent):
135
+ self.status = Label(
136
+ parent,
137
+ justify=LEFT,
138
+ relief=SUNKEN,
139
+ background=self._BACKGROUND_COLOUR,
140
+ border=0,
141
+ padx=1,
142
+ pady=0,
143
+ )
144
+ self.status.pack(side="top", anchor="sw")
145
+
146
+ def _init_menubar(self):
147
+ self._result_size = IntVar(self.top)
148
+ menubar = Menu(self.top)
149
+
150
+ filemenu = Menu(menubar, tearoff=0, borderwidth=0)
151
+ filemenu.add_command(
152
+ label="Exit", underline=1, command=self.destroy, accelerator="Ctrl-q"
153
+ )
154
+ menubar.add_cascade(label="File", underline=0, menu=filemenu)
155
+
156
+ editmenu = Menu(menubar, tearoff=0)
157
+ rescntmenu = Menu(editmenu, tearoff=0)
158
+ rescntmenu.add_radiobutton(
159
+ label="20",
160
+ variable=self._result_size,
161
+ underline=0,
162
+ value=20,
163
+ command=self.set_result_size,
164
+ )
165
+ rescntmenu.add_radiobutton(
166
+ label="50",
167
+ variable=self._result_size,
168
+ underline=0,
169
+ value=50,
170
+ command=self.set_result_size,
171
+ )
172
+ rescntmenu.add_radiobutton(
173
+ label="100",
174
+ variable=self._result_size,
175
+ underline=0,
176
+ value=100,
177
+ command=self.set_result_size,
178
+ )
179
+ rescntmenu.invoke(1)
180
+ editmenu.add_cascade(label="Result Count", underline=0, menu=rescntmenu)
181
+
182
+ menubar.add_cascade(label="Edit", underline=0, menu=editmenu)
183
+ self.top.config(menu=menubar)
184
+
185
+ def set_result_size(self, **kwargs):
186
+ self.model.result_count = self._result_size.get()
187
+
188
+ def _init_results_box(self, parent):
189
+ innerframe = Frame(parent)
190
+ i1 = Frame(innerframe)
191
+ i2 = Frame(innerframe)
192
+ vscrollbar = Scrollbar(i1, borderwidth=1)
193
+ hscrollbar = Scrollbar(i2, borderwidth=1, orient="horiz")
194
+ self.results_box = Text(
195
+ i1,
196
+ font=Font(family="courier", size="16"),
197
+ state="disabled",
198
+ borderwidth=1,
199
+ yscrollcommand=vscrollbar.set,
200
+ xscrollcommand=hscrollbar.set,
201
+ wrap="none",
202
+ width="40",
203
+ height="20",
204
+ exportselection=1,
205
+ )
206
+ self.results_box.pack(side="left", fill="both", expand=True)
207
+ vscrollbar.pack(side="left", fill="y", anchor="e")
208
+ vscrollbar.config(command=self.results_box.yview)
209
+ hscrollbar.pack(side="left", fill="x", expand=True, anchor="w")
210
+ hscrollbar.config(command=self.results_box.xview)
211
+ # there is no other way of avoiding the overlap of scrollbars while using pack layout manager!!!
212
+ Label(i2, text=" ", background=self._BACKGROUND_COLOUR).pack(
213
+ side="left", anchor="e"
214
+ )
215
+ i1.pack(side="top", fill="both", expand=True, anchor="n")
216
+ i2.pack(side="bottom", fill="x", anchor="s")
217
+ innerframe.pack(side="top", fill="both", expand=True)
218
+
219
+ def _init_paging(self, parent):
220
+ innerframe = Frame(parent, background=self._BACKGROUND_COLOUR)
221
+ self.prev = prev = Button(
222
+ innerframe,
223
+ text="Previous",
224
+ command=self.previous,
225
+ width="10",
226
+ borderwidth=1,
227
+ highlightthickness=1,
228
+ state="disabled",
229
+ )
230
+ prev.pack(side="left", anchor="center")
231
+ self.next = next = Button(
232
+ innerframe,
233
+ text="Next",
234
+ command=self.__next__,
235
+ width="10",
236
+ borderwidth=1,
237
+ highlightthickness=1,
238
+ state="disabled",
239
+ )
240
+ next.pack(side="right", anchor="center")
241
+ innerframe.pack(side="top", fill="y")
242
+ self.reset_current_page()
243
+
244
+ def reset_current_page(self):
245
+ self.current_page = -1
246
+
247
+ def _poll(self):
248
+ try:
249
+ event = self.queue.get(block=False)
250
+ except q.Empty:
251
+ pass
252
+ else:
253
+ if event == CORPUS_LOADED_EVENT:
254
+ self.handle_corpus_loaded(event)
255
+ elif event == ERROR_LOADING_CORPUS_EVENT:
256
+ self.handle_error_loading_corpus(event)
257
+ self.after = self.top.after(POLL_INTERVAL, self._poll)
258
+
259
+ def handle_error_loading_corpus(self, event):
260
+ self.status["text"] = "Error in loading " + self.var.get()
261
+ self.unfreeze_editable()
262
+ self.clear_results_box()
263
+ self.freeze_editable()
264
+ self.reset_current_page()
265
+
266
+ def handle_corpus_loaded(self, event):
267
+ self.status["text"] = self.var.get() + " is loaded"
268
+ self.unfreeze_editable()
269
+ self.clear_results_box()
270
+ self.reset_current_page()
271
+ # self.next()
272
+ collocations = self.model.next(self.current_page + 1)
273
+ self.write_results(collocations)
274
+ self.current_page += 1
275
+
276
+ def corpus_selected(self, *args):
277
+ new_selection = self.var.get()
278
+ self.load_corpus(new_selection)
279
+
280
+ def previous(self):
281
+ self.freeze_editable()
282
+ collocations = self.model.prev(self.current_page - 1)
283
+ self.current_page = self.current_page - 1
284
+ self.clear_results_box()
285
+ self.write_results(collocations)
286
+ self.unfreeze_editable()
287
+
288
+ def __next__(self):
289
+ self.freeze_editable()
290
+ collocations = self.model.next(self.current_page + 1)
291
+ self.clear_results_box()
292
+ self.write_results(collocations)
293
+ self.current_page += 1
294
+ self.unfreeze_editable()
295
+
296
+ def load_corpus(self, selection):
297
+ if self.model.selected_corpus != selection:
298
+ self.status["text"] = "Loading " + selection + "..."
299
+ self.freeze_editable()
300
+ self.model.load_corpus(selection)
301
+
302
+ def freeze_editable(self):
303
+ self.prev["state"] = "disabled"
304
+ self.next["state"] = "disabled"
305
+
306
+ def clear_results_box(self):
307
+ self.results_box["state"] = "normal"
308
+ self.results_box.delete("1.0", END)
309
+ self.results_box["state"] = "disabled"
310
+
311
+ def fire_event(self, event):
312
+ # Firing an event so that rendering of widgets happen in the mainloop thread
313
+ self.top.event_generate(event, when="tail")
314
+
315
+ def destroy(self, *e):
316
+ if self.top is None:
317
+ return
318
+ self.top.after_cancel(self.after)
319
+ self.top.destroy()
320
+ self.top = None
321
+
322
+ def mainloop(self, *args, **kwargs):
323
+ if in_idle():
324
+ return
325
+ self.top.mainloop(*args, **kwargs)
326
+
327
+ def unfreeze_editable(self):
328
+ self.set_paging_button_states()
329
+
330
+ def set_paging_button_states(self):
331
+ if self.current_page == -1 or self.current_page == 0:
332
+ self.prev["state"] = "disabled"
333
+ else:
334
+ self.prev["state"] = "normal"
335
+ if self.model.is_last_page(self.current_page):
336
+ self.next["state"] = "disabled"
337
+ else:
338
+ self.next["state"] = "normal"
339
+
340
+ def write_results(self, results):
341
+ self.results_box["state"] = "normal"
342
+ row = 1
343
+ for each in results:
344
+ self.results_box.insert(str(row) + ".0", each[0] + " " + each[1] + "\n")
345
+ row += 1
346
+ self.results_box["state"] = "disabled"
347
+
348
+
349
+ class CollocationsModel:
350
+ def __init__(self, queue):
351
+ self.result_count = None
352
+ self.selected_corpus = None
353
+ self.collocations = None
354
+ self.CORPORA = _CORPORA
355
+ self.DEFAULT_CORPUS = _DEFAULT
356
+ self.queue = queue
357
+ self.reset_results()
358
+
359
+ def reset_results(self):
360
+ self.result_pages = []
361
+ self.results_returned = 0
362
+
363
+ def load_corpus(self, name):
364
+ self.selected_corpus = name
365
+ self.collocations = None
366
+ runner_thread = self.LoadCorpus(name, self)
367
+ runner_thread.start()
368
+ self.reset_results()
369
+
370
+ def non_default_corpora(self):
371
+ copy = []
372
+ copy.extend(list(self.CORPORA.keys()))
373
+ copy.remove(self.DEFAULT_CORPUS)
374
+ copy.sort()
375
+ return copy
376
+
377
+ def is_last_page(self, number):
378
+ if number < len(self.result_pages):
379
+ return False
380
+ return self.results_returned + (
381
+ number - len(self.result_pages)
382
+ ) * self.result_count >= len(self.collocations)
383
+
384
+ def next(self, page):
385
+ if (len(self.result_pages) - 1) < page:
386
+ for i in range(page - (len(self.result_pages) - 1)):
387
+ self.result_pages.append(
388
+ self.collocations[
389
+ self.results_returned : self.results_returned
390
+ + self.result_count
391
+ ]
392
+ )
393
+ self.results_returned += self.result_count
394
+ return self.result_pages[page]
395
+
396
+ def prev(self, page):
397
+ if page == -1:
398
+ return []
399
+ return self.result_pages[page]
400
+
401
+ class LoadCorpus(threading.Thread):
402
+ def __init__(self, name, model):
403
+ threading.Thread.__init__(self)
404
+ self.model, self.name = model, name
405
+
406
+ def run(self):
407
+ try:
408
+ words = self.model.CORPORA[self.name]()
409
+ from operator import itemgetter
410
+
411
+ text = [w for w in words if len(w) > 2]
412
+ fd = FreqDist(tuple(text[i : i + 2]) for i in range(len(text) - 1))
413
+ vocab = FreqDist(text)
414
+ scored = [
415
+ ((w1, w2), fd[(w1, w2)] ** 3 / (vocab[w1] * vocab[w2]))
416
+ for w1, w2 in fd
417
+ ]
418
+ scored.sort(key=itemgetter(1), reverse=True)
419
+ self.model.collocations = list(map(itemgetter(0), scored))
420
+ self.model.queue.put(CORPUS_LOADED_EVENT)
421
+ except Exception as e:
422
+ print(e)
423
+ self.model.queue.put(ERROR_LOADING_CORPUS_EVENT)
424
+
425
+
426
+ # def collocations():
427
+ # colloc_strings = [w1 + ' ' + w2 for w1, w2 in self._collocations[:num]]
428
+
429
+
430
+ def app():
431
+ c = CollocationsView()
432
+ c.mainloop()
433
+
434
+
435
+ if __name__ == "__main__":
436
+ app()
437
+
438
+ __all__ = ["app"]
loan-advisor-agent/.venv/Lib/site-packages/nltk/app/concordance_app.py ADDED
@@ -0,0 +1,709 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: Concordance Application
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Author: Sumukh Ghodke <sghodke@csse.unimelb.edu.au>
5
+ # URL: <https://www.nltk.org/>
6
+ # For license information, see LICENSE.TXT
7
+
8
+ import queue as q
9
+ import re
10
+ import threading
11
+ from tkinter import (
12
+ END,
13
+ LEFT,
14
+ SUNKEN,
15
+ Button,
16
+ Entry,
17
+ Frame,
18
+ IntVar,
19
+ Label,
20
+ Menu,
21
+ OptionMenu,
22
+ Scrollbar,
23
+ StringVar,
24
+ Text,
25
+ Tk,
26
+ )
27
+ from tkinter.font import Font
28
+
29
+ from nltk.corpus import (
30
+ alpino,
31
+ brown,
32
+ cess_cat,
33
+ cess_esp,
34
+ floresta,
35
+ indian,
36
+ mac_morpho,
37
+ nps_chat,
38
+ sinica_treebank,
39
+ treebank,
40
+ )
41
+ from nltk.draw.util import ShowText
42
+ from nltk.util import in_idle
43
+
44
+ WORD_OR_TAG = "[^/ ]+"
45
+ BOUNDARY = r"\b"
46
+
47
+ CORPUS_LOADED_EVENT = "<<CL_EVENT>>"
48
+ SEARCH_TERMINATED_EVENT = "<<ST_EVENT>>"
49
+ SEARCH_ERROR_EVENT = "<<SE_EVENT>>"
50
+ ERROR_LOADING_CORPUS_EVENT = "<<ELC_EVENT>>"
51
+
52
+ POLL_INTERVAL = 50
53
+
54
+ # NB All corpora must be specified in a lambda expression so as not to be
55
+ # loaded when the module is imported.
56
+
57
+ _DEFAULT = "English: Brown Corpus (Humor, simplified)"
58
+ _CORPORA = {
59
+ "Catalan: CESS-CAT Corpus (simplified)": lambda: cess_cat.tagged_sents(
60
+ tagset="universal"
61
+ ),
62
+ "English: Brown Corpus": lambda: brown.tagged_sents(),
63
+ "English: Brown Corpus (simplified)": lambda: brown.tagged_sents(
64
+ tagset="universal"
65
+ ),
66
+ "English: Brown Corpus (Press, simplified)": lambda: brown.tagged_sents(
67
+ categories=["news", "editorial", "reviews"], tagset="universal"
68
+ ),
69
+ "English: Brown Corpus (Religion, simplified)": lambda: brown.tagged_sents(
70
+ categories="religion", tagset="universal"
71
+ ),
72
+ "English: Brown Corpus (Learned, simplified)": lambda: brown.tagged_sents(
73
+ categories="learned", tagset="universal"
74
+ ),
75
+ "English: Brown Corpus (Science Fiction, simplified)": lambda: brown.tagged_sents(
76
+ categories="science_fiction", tagset="universal"
77
+ ),
78
+ "English: Brown Corpus (Romance, simplified)": lambda: brown.tagged_sents(
79
+ categories="romance", tagset="universal"
80
+ ),
81
+ "English: Brown Corpus (Humor, simplified)": lambda: brown.tagged_sents(
82
+ categories="humor", tagset="universal"
83
+ ),
84
+ "English: NPS Chat Corpus": lambda: nps_chat.tagged_posts(),
85
+ "English: NPS Chat Corpus (simplified)": lambda: nps_chat.tagged_posts(
86
+ tagset="universal"
87
+ ),
88
+ "English: Wall Street Journal Corpus": lambda: treebank.tagged_sents(),
89
+ "English: Wall Street Journal Corpus (simplified)": lambda: treebank.tagged_sents(
90
+ tagset="universal"
91
+ ),
92
+ "Chinese: Sinica Corpus": lambda: sinica_treebank.tagged_sents(),
93
+ "Chinese: Sinica Corpus (simplified)": lambda: sinica_treebank.tagged_sents(
94
+ tagset="universal"
95
+ ),
96
+ "Dutch: Alpino Corpus": lambda: alpino.tagged_sents(),
97
+ "Dutch: Alpino Corpus (simplified)": lambda: alpino.tagged_sents(
98
+ tagset="universal"
99
+ ),
100
+ "Hindi: Indian Languages Corpus": lambda: indian.tagged_sents(files="hindi.pos"),
101
+ "Hindi: Indian Languages Corpus (simplified)": lambda: indian.tagged_sents(
102
+ files="hindi.pos", tagset="universal"
103
+ ),
104
+ "Portuguese: Floresta Corpus (Portugal)": lambda: floresta.tagged_sents(),
105
+ "Portuguese: Floresta Corpus (Portugal, simplified)": lambda: floresta.tagged_sents(
106
+ tagset="universal"
107
+ ),
108
+ "Portuguese: MAC-MORPHO Corpus (Brazil)": lambda: mac_morpho.tagged_sents(),
109
+ "Portuguese: MAC-MORPHO Corpus (Brazil, simplified)": lambda: mac_morpho.tagged_sents(
110
+ tagset="universal"
111
+ ),
112
+ "Spanish: CESS-ESP Corpus (simplified)": lambda: cess_esp.tagged_sents(
113
+ tagset="universal"
114
+ ),
115
+ }
116
+
117
+
118
+ class ConcordanceSearchView:
119
+ _BACKGROUND_COLOUR = "#FFF" # white
120
+
121
+ # Colour of highlighted results
122
+ _HIGHLIGHT_WORD_COLOUR = "#F00" # red
123
+ _HIGHLIGHT_WORD_TAG = "HL_WRD_TAG"
124
+
125
+ _HIGHLIGHT_LABEL_COLOUR = "#C0C0C0" # dark grey
126
+ _HIGHLIGHT_LABEL_TAG = "HL_LBL_TAG"
127
+
128
+ # Percentage of text left of the scrollbar position
129
+ _FRACTION_LEFT_TEXT = 0.30
130
+
131
+ def __init__(self):
132
+ self.queue = q.Queue()
133
+ self.model = ConcordanceSearchModel(self.queue)
134
+ self.top = Tk()
135
+ self._init_top(self.top)
136
+ self._init_menubar()
137
+ self._init_widgets(self.top)
138
+ self.load_corpus(self.model.DEFAULT_CORPUS)
139
+ self.after = self.top.after(POLL_INTERVAL, self._poll)
140
+
141
+ def _init_top(self, top):
142
+ top.geometry("950x680+50+50")
143
+ top.title("NLTK Concordance Search")
144
+ top.bind("<Control-q>", self.destroy)
145
+ top.protocol("WM_DELETE_WINDOW", self.destroy)
146
+ top.minsize(950, 680)
147
+
148
+ def _init_widgets(self, parent):
149
+ self.main_frame = Frame(
150
+ parent, dict(background=self._BACKGROUND_COLOUR, padx=1, pady=1, border=1)
151
+ )
152
+ self._init_corpus_select(self.main_frame)
153
+ self._init_query_box(self.main_frame)
154
+ self._init_results_box(self.main_frame)
155
+ self._init_paging(self.main_frame)
156
+ self._init_status(self.main_frame)
157
+ self.main_frame.pack(fill="both", expand=True)
158
+
159
+ def _init_menubar(self):
160
+ self._result_size = IntVar(self.top)
161
+ self._cntx_bf_len = IntVar(self.top)
162
+ self._cntx_af_len = IntVar(self.top)
163
+ menubar = Menu(self.top)
164
+
165
+ filemenu = Menu(menubar, tearoff=0, borderwidth=0)
166
+ filemenu.add_command(
167
+ label="Exit", underline=1, command=self.destroy, accelerator="Ctrl-q"
168
+ )
169
+ menubar.add_cascade(label="File", underline=0, menu=filemenu)
170
+
171
+ editmenu = Menu(menubar, tearoff=0)
172
+ rescntmenu = Menu(editmenu, tearoff=0)
173
+ rescntmenu.add_radiobutton(
174
+ label="20",
175
+ variable=self._result_size,
176
+ underline=0,
177
+ value=20,
178
+ command=self.set_result_size,
179
+ )
180
+ rescntmenu.add_radiobutton(
181
+ label="50",
182
+ variable=self._result_size,
183
+ underline=0,
184
+ value=50,
185
+ command=self.set_result_size,
186
+ )
187
+ rescntmenu.add_radiobutton(
188
+ label="100",
189
+ variable=self._result_size,
190
+ underline=0,
191
+ value=100,
192
+ command=self.set_result_size,
193
+ )
194
+ rescntmenu.invoke(1)
195
+ editmenu.add_cascade(label="Result Count", underline=0, menu=rescntmenu)
196
+
197
+ cntxmenu = Menu(editmenu, tearoff=0)
198
+ cntxbfmenu = Menu(cntxmenu, tearoff=0)
199
+ cntxbfmenu.add_radiobutton(
200
+ label="60 characters",
201
+ variable=self._cntx_bf_len,
202
+ underline=0,
203
+ value=60,
204
+ command=self.set_cntx_bf_len,
205
+ )
206
+ cntxbfmenu.add_radiobutton(
207
+ label="80 characters",
208
+ variable=self._cntx_bf_len,
209
+ underline=0,
210
+ value=80,
211
+ command=self.set_cntx_bf_len,
212
+ )
213
+ cntxbfmenu.add_radiobutton(
214
+ label="100 characters",
215
+ variable=self._cntx_bf_len,
216
+ underline=0,
217
+ value=100,
218
+ command=self.set_cntx_bf_len,
219
+ )
220
+ cntxbfmenu.invoke(1)
221
+ cntxmenu.add_cascade(label="Before", underline=0, menu=cntxbfmenu)
222
+
223
+ cntxafmenu = Menu(cntxmenu, tearoff=0)
224
+ cntxafmenu.add_radiobutton(
225
+ label="70 characters",
226
+ variable=self._cntx_af_len,
227
+ underline=0,
228
+ value=70,
229
+ command=self.set_cntx_af_len,
230
+ )
231
+ cntxafmenu.add_radiobutton(
232
+ label="90 characters",
233
+ variable=self._cntx_af_len,
234
+ underline=0,
235
+ value=90,
236
+ command=self.set_cntx_af_len,
237
+ )
238
+ cntxafmenu.add_radiobutton(
239
+ label="110 characters",
240
+ variable=self._cntx_af_len,
241
+ underline=0,
242
+ value=110,
243
+ command=self.set_cntx_af_len,
244
+ )
245
+ cntxafmenu.invoke(1)
246
+ cntxmenu.add_cascade(label="After", underline=0, menu=cntxafmenu)
247
+
248
+ editmenu.add_cascade(label="Context", underline=0, menu=cntxmenu)
249
+
250
+ menubar.add_cascade(label="Edit", underline=0, menu=editmenu)
251
+
252
+ self.top.config(menu=menubar)
253
+
254
+ def set_result_size(self, **kwargs):
255
+ self.model.result_count = self._result_size.get()
256
+
257
+ def set_cntx_af_len(self, **kwargs):
258
+ self._char_after = self._cntx_af_len.get()
259
+
260
+ def set_cntx_bf_len(self, **kwargs):
261
+ self._char_before = self._cntx_bf_len.get()
262
+
263
+ def _init_corpus_select(self, parent):
264
+ innerframe = Frame(parent, background=self._BACKGROUND_COLOUR)
265
+ self.var = StringVar(innerframe)
266
+ self.var.set(self.model.DEFAULT_CORPUS)
267
+ Label(
268
+ innerframe,
269
+ justify=LEFT,
270
+ text=" Corpus: ",
271
+ background=self._BACKGROUND_COLOUR,
272
+ padx=2,
273
+ pady=1,
274
+ border=0,
275
+ ).pack(side="left")
276
+
277
+ other_corpora = list(self.model.CORPORA.keys()).remove(
278
+ self.model.DEFAULT_CORPUS
279
+ )
280
+ om = OptionMenu(
281
+ innerframe,
282
+ self.var,
283
+ self.model.DEFAULT_CORPUS,
284
+ command=self.corpus_selected,
285
+ *self.model.non_default_corpora()
286
+ )
287
+ om["borderwidth"] = 0
288
+ om["highlightthickness"] = 1
289
+ om.pack(side="left")
290
+ innerframe.pack(side="top", fill="x", anchor="n")
291
+
292
+ def _init_status(self, parent):
293
+ self.status = Label(
294
+ parent,
295
+ justify=LEFT,
296
+ relief=SUNKEN,
297
+ background=self._BACKGROUND_COLOUR,
298
+ border=0,
299
+ padx=1,
300
+ pady=0,
301
+ )
302
+ self.status.pack(side="top", anchor="sw")
303
+
304
+ def _init_query_box(self, parent):
305
+ innerframe = Frame(parent, background=self._BACKGROUND_COLOUR)
306
+ another = Frame(innerframe, background=self._BACKGROUND_COLOUR)
307
+ self.query_box = Entry(another, width=60)
308
+ self.query_box.pack(side="left", fill="x", pady=25, anchor="center")
309
+ self.search_button = Button(
310
+ another,
311
+ text="Search",
312
+ command=self.search,
313
+ borderwidth=1,
314
+ highlightthickness=1,
315
+ )
316
+ self.search_button.pack(side="left", fill="x", pady=25, anchor="center")
317
+ self.query_box.bind("<KeyPress-Return>", self.search_enter_keypress_handler)
318
+ another.pack()
319
+ innerframe.pack(side="top", fill="x", anchor="n")
320
+
321
+ def search_enter_keypress_handler(self, *event):
322
+ self.search()
323
+
324
+ def _init_results_box(self, parent):
325
+ innerframe = Frame(parent)
326
+ i1 = Frame(innerframe)
327
+ i2 = Frame(innerframe)
328
+ vscrollbar = Scrollbar(i1, borderwidth=1)
329
+ hscrollbar = Scrollbar(i2, borderwidth=1, orient="horiz")
330
+ self.results_box = Text(
331
+ i1,
332
+ font=Font(family="courier", size="16"),
333
+ state="disabled",
334
+ borderwidth=1,
335
+ yscrollcommand=vscrollbar.set,
336
+ xscrollcommand=hscrollbar.set,
337
+ wrap="none",
338
+ width="40",
339
+ height="20",
340
+ exportselection=1,
341
+ )
342
+ self.results_box.pack(side="left", fill="both", expand=True)
343
+ self.results_box.tag_config(
344
+ self._HIGHLIGHT_WORD_TAG, foreground=self._HIGHLIGHT_WORD_COLOUR
345
+ )
346
+ self.results_box.tag_config(
347
+ self._HIGHLIGHT_LABEL_TAG, foreground=self._HIGHLIGHT_LABEL_COLOUR
348
+ )
349
+ vscrollbar.pack(side="left", fill="y", anchor="e")
350
+ vscrollbar.config(command=self.results_box.yview)
351
+ hscrollbar.pack(side="left", fill="x", expand=True, anchor="w")
352
+ hscrollbar.config(command=self.results_box.xview)
353
+ # there is no other way of avoiding the overlap of scrollbars while using pack layout manager!!!
354
+ Label(i2, text=" ", background=self._BACKGROUND_COLOUR).pack(
355
+ side="left", anchor="e"
356
+ )
357
+ i1.pack(side="top", fill="both", expand=True, anchor="n")
358
+ i2.pack(side="bottom", fill="x", anchor="s")
359
+ innerframe.pack(side="top", fill="both", expand=True)
360
+
361
+ def _init_paging(self, parent):
362
+ innerframe = Frame(parent, background=self._BACKGROUND_COLOUR)
363
+ self.prev = prev = Button(
364
+ innerframe,
365
+ text="Previous",
366
+ command=self.previous,
367
+ width="10",
368
+ borderwidth=1,
369
+ highlightthickness=1,
370
+ state="disabled",
371
+ )
372
+ prev.pack(side="left", anchor="center")
373
+ self.next = next = Button(
374
+ innerframe,
375
+ text="Next",
376
+ command=self.__next__,
377
+ width="10",
378
+ borderwidth=1,
379
+ highlightthickness=1,
380
+ state="disabled",
381
+ )
382
+ next.pack(side="right", anchor="center")
383
+ innerframe.pack(side="top", fill="y")
384
+ self.current_page = 0
385
+
386
+ def previous(self):
387
+ self.clear_results_box()
388
+ self.freeze_editable()
389
+ self.model.prev(self.current_page - 1)
390
+
391
+ def __next__(self):
392
+ self.clear_results_box()
393
+ self.freeze_editable()
394
+ self.model.next(self.current_page + 1)
395
+
396
+ def about(self, *e):
397
+ ABOUT = "NLTK Concordance Search Demo\n"
398
+ TITLE = "About: NLTK Concordance Search Demo"
399
+ try:
400
+ from tkinter.messagebox import Message
401
+
402
+ Message(message=ABOUT, title=TITLE, parent=self.main_frame).show()
403
+ except Exception:
404
+ ShowText(self.top, TITLE, ABOUT)
405
+
406
+ def _bind_event_handlers(self):
407
+ self.top.bind(CORPUS_LOADED_EVENT, self.handle_corpus_loaded)
408
+ self.top.bind(SEARCH_TERMINATED_EVENT, self.handle_search_terminated)
409
+ self.top.bind(SEARCH_ERROR_EVENT, self.handle_search_error)
410
+ self.top.bind(ERROR_LOADING_CORPUS_EVENT, self.handle_error_loading_corpus)
411
+
412
+ def _poll(self):
413
+ try:
414
+ event = self.queue.get(block=False)
415
+ except q.Empty:
416
+ pass
417
+ else:
418
+ if event == CORPUS_LOADED_EVENT:
419
+ self.handle_corpus_loaded(event)
420
+ elif event == SEARCH_TERMINATED_EVENT:
421
+ self.handle_search_terminated(event)
422
+ elif event == SEARCH_ERROR_EVENT:
423
+ self.handle_search_error(event)
424
+ elif event == ERROR_LOADING_CORPUS_EVENT:
425
+ self.handle_error_loading_corpus(event)
426
+ self.after = self.top.after(POLL_INTERVAL, self._poll)
427
+
428
+ def handle_error_loading_corpus(self, event):
429
+ self.status["text"] = "Error in loading " + self.var.get()
430
+ self.unfreeze_editable()
431
+ self.clear_all()
432
+ self.freeze_editable()
433
+
434
+ def handle_corpus_loaded(self, event):
435
+ self.status["text"] = self.var.get() + " is loaded"
436
+ self.unfreeze_editable()
437
+ self.clear_all()
438
+ self.query_box.focus_set()
439
+
440
+ def handle_search_terminated(self, event):
441
+ # todo: refactor the model such that it is less state sensitive
442
+ results = self.model.get_results()
443
+ self.write_results(results)
444
+ self.status["text"] = ""
445
+ if len(results) == 0:
446
+ self.status["text"] = "No results found for " + self.model.query
447
+ else:
448
+ self.current_page = self.model.last_requested_page
449
+ self.unfreeze_editable()
450
+ self.results_box.xview_moveto(self._FRACTION_LEFT_TEXT)
451
+
452
+ def handle_search_error(self, event):
453
+ self.status["text"] = "Error in query " + self.model.query
454
+ self.unfreeze_editable()
455
+
456
+ def corpus_selected(self, *args):
457
+ new_selection = self.var.get()
458
+ self.load_corpus(new_selection)
459
+
460
+ def load_corpus(self, selection):
461
+ if self.model.selected_corpus != selection:
462
+ self.status["text"] = "Loading " + selection + "..."
463
+ self.freeze_editable()
464
+ self.model.load_corpus(selection)
465
+
466
+ def search(self):
467
+ self.current_page = 0
468
+ self.clear_results_box()
469
+ self.model.reset_results()
470
+ query = self.query_box.get()
471
+ if len(query.strip()) == 0:
472
+ return
473
+ self.status["text"] = "Searching for " + query
474
+ self.freeze_editable()
475
+ self.model.search(query, self.current_page + 1)
476
+
477
+ def write_results(self, results):
478
+ self.results_box["state"] = "normal"
479
+ row = 1
480
+ for each in results:
481
+ sent, pos1, pos2 = each[0].strip(), each[1], each[2]
482
+ if len(sent) != 0:
483
+ if pos1 < self._char_before:
484
+ sent, pos1, pos2 = self.pad(sent, pos1, pos2)
485
+ sentence = sent[pos1 - self._char_before : pos1 + self._char_after]
486
+ if not row == len(results):
487
+ sentence += "\n"
488
+ self.results_box.insert(str(row) + ".0", sentence)
489
+ word_markers, label_markers = self.words_and_labels(sent, pos1, pos2)
490
+ for marker in word_markers:
491
+ self.results_box.tag_add(
492
+ self._HIGHLIGHT_WORD_TAG,
493
+ str(row) + "." + str(marker[0]),
494
+ str(row) + "." + str(marker[1]),
495
+ )
496
+ for marker in label_markers:
497
+ self.results_box.tag_add(
498
+ self._HIGHLIGHT_LABEL_TAG,
499
+ str(row) + "." + str(marker[0]),
500
+ str(row) + "." + str(marker[1]),
501
+ )
502
+ row += 1
503
+ self.results_box["state"] = "disabled"
504
+
505
+ def words_and_labels(self, sentence, pos1, pos2):
506
+ search_exp = sentence[pos1:pos2]
507
+ words, labels = [], []
508
+ labeled_words = search_exp.split(" ")
509
+ index = 0
510
+ for each in labeled_words:
511
+ if each == "":
512
+ index += 1
513
+ else:
514
+ word, label = each.split("/")
515
+ words.append(
516
+ (self._char_before + index, self._char_before + index + len(word))
517
+ )
518
+ index += len(word) + 1
519
+ labels.append(
520
+ (self._char_before + index, self._char_before + index + len(label))
521
+ )
522
+ index += len(label)
523
+ index += 1
524
+ return words, labels
525
+
526
+ def pad(self, sent, hstart, hend):
527
+ if hstart >= self._char_before:
528
+ return sent, hstart, hend
529
+ d = self._char_before - hstart
530
+ sent = "".join([" "] * d) + sent
531
+ return sent, hstart + d, hend + d
532
+
533
+ def destroy(self, *e):
534
+ if self.top is None:
535
+ return
536
+ self.top.after_cancel(self.after)
537
+ self.top.destroy()
538
+ self.top = None
539
+
540
+ def clear_all(self):
541
+ self.query_box.delete(0, END)
542
+ self.model.reset_query()
543
+ self.clear_results_box()
544
+
545
+ def clear_results_box(self):
546
+ self.results_box["state"] = "normal"
547
+ self.results_box.delete("1.0", END)
548
+ self.results_box["state"] = "disabled"
549
+
550
+ def freeze_editable(self):
551
+ self.query_box["state"] = "disabled"
552
+ self.search_button["state"] = "disabled"
553
+ self.prev["state"] = "disabled"
554
+ self.next["state"] = "disabled"
555
+
556
+ def unfreeze_editable(self):
557
+ self.query_box["state"] = "normal"
558
+ self.search_button["state"] = "normal"
559
+ self.set_paging_button_states()
560
+
561
+ def set_paging_button_states(self):
562
+ if self.current_page == 0 or self.current_page == 1:
563
+ self.prev["state"] = "disabled"
564
+ else:
565
+ self.prev["state"] = "normal"
566
+ if self.model.has_more_pages(self.current_page):
567
+ self.next["state"] = "normal"
568
+ else:
569
+ self.next["state"] = "disabled"
570
+
571
+ def fire_event(self, event):
572
+ # Firing an event so that rendering of widgets happen in the mainloop thread
573
+ self.top.event_generate(event, when="tail")
574
+
575
+ def mainloop(self, *args, **kwargs):
576
+ if in_idle():
577
+ return
578
+ self.top.mainloop(*args, **kwargs)
579
+
580
+
581
+ class ConcordanceSearchModel:
582
+ def __init__(self, queue):
583
+ self.queue = queue
584
+ self.CORPORA = _CORPORA
585
+ self.DEFAULT_CORPUS = _DEFAULT
586
+ self.selected_corpus = None
587
+ self.reset_query()
588
+ self.reset_results()
589
+ self.result_count = None
590
+ self.last_sent_searched = 0
591
+
592
+ def non_default_corpora(self):
593
+ copy = []
594
+ copy.extend(list(self.CORPORA.keys()))
595
+ copy.remove(self.DEFAULT_CORPUS)
596
+ copy.sort()
597
+ return copy
598
+
599
+ def load_corpus(self, name):
600
+ self.selected_corpus = name
601
+ self.tagged_sents = []
602
+ runner_thread = self.LoadCorpus(name, self)
603
+ runner_thread.start()
604
+
605
+ def search(self, query, page):
606
+ self.query = query
607
+ self.last_requested_page = page
608
+ self.SearchCorpus(self, page, self.result_count).start()
609
+
610
+ def next(self, page):
611
+ self.last_requested_page = page
612
+ if len(self.results) < page:
613
+ self.search(self.query, page)
614
+ else:
615
+ self.queue.put(SEARCH_TERMINATED_EVENT)
616
+
617
+ def prev(self, page):
618
+ self.last_requested_page = page
619
+ self.queue.put(SEARCH_TERMINATED_EVENT)
620
+
621
+ def reset_results(self):
622
+ self.last_sent_searched = 0
623
+ self.results = []
624
+ self.last_page = None
625
+
626
+ def reset_query(self):
627
+ self.query = None
628
+
629
+ def set_results(self, page, resultset):
630
+ self.results.insert(page - 1, resultset)
631
+
632
+ def get_results(self):
633
+ return self.results[self.last_requested_page - 1]
634
+
635
+ def has_more_pages(self, page):
636
+ if self.results == [] or self.results[0] == []:
637
+ return False
638
+ if self.last_page is None:
639
+ return True
640
+ return page < self.last_page
641
+
642
+ class LoadCorpus(threading.Thread):
643
+ def __init__(self, name, model):
644
+ threading.Thread.__init__(self)
645
+ self.model, self.name = model, name
646
+
647
+ def run(self):
648
+ try:
649
+ ts = self.model.CORPORA[self.name]()
650
+ self.model.tagged_sents = [
651
+ " ".join(w + "/" + t for (w, t) in sent) for sent in ts
652
+ ]
653
+ self.model.queue.put(CORPUS_LOADED_EVENT)
654
+ except Exception as e:
655
+ print(e)
656
+ self.model.queue.put(ERROR_LOADING_CORPUS_EVENT)
657
+
658
+ class SearchCorpus(threading.Thread):
659
+ def __init__(self, model, page, count):
660
+ self.model, self.count, self.page = model, count, page
661
+ threading.Thread.__init__(self)
662
+
663
+ def run(self):
664
+ q = self.processed_query()
665
+ sent_pos, i, sent_count = [], 0, 0
666
+ for sent in self.model.tagged_sents[self.model.last_sent_searched :]:
667
+ try:
668
+ m = re.search(q, sent)
669
+ except re.error:
670
+ self.model.reset_results()
671
+ self.model.queue.put(SEARCH_ERROR_EVENT)
672
+ return
673
+ if m:
674
+ sent_pos.append((sent, m.start(), m.end()))
675
+ i += 1
676
+ if i > self.count:
677
+ self.model.last_sent_searched += sent_count - 1
678
+ break
679
+ sent_count += 1
680
+ if self.count >= len(sent_pos):
681
+ self.model.last_sent_searched += sent_count - 1
682
+ self.model.last_page = self.page
683
+ self.model.set_results(self.page, sent_pos)
684
+ else:
685
+ self.model.set_results(self.page, sent_pos[:-1])
686
+ self.model.queue.put(SEARCH_TERMINATED_EVENT)
687
+
688
+ def processed_query(self):
689
+ new = []
690
+ for term in self.model.query.split():
691
+ term = re.sub(r"\.", r"[^/ ]", term)
692
+ if re.match("[A-Z]+$", term):
693
+ new.append(BOUNDARY + WORD_OR_TAG + "/" + term + BOUNDARY)
694
+ elif "/" in term:
695
+ new.append(BOUNDARY + term + BOUNDARY)
696
+ else:
697
+ new.append(BOUNDARY + term + "/" + WORD_OR_TAG + BOUNDARY)
698
+ return " ".join(new)
699
+
700
+
701
+ def app():
702
+ d = ConcordanceSearchView()
703
+ d.mainloop()
704
+
705
+
706
+ if __name__ == "__main__":
707
+ app()
708
+
709
+ __all__ = ["app"]
loan-advisor-agent/.venv/Lib/site-packages/nltk/app/nemo_app.py ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Finding (and Replacing) Nemo, Version 1.1, Aristide Grange 2006/06/06
2
+ # https://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496783
3
+
4
+ """
5
+ Finding (and Replacing) Nemo
6
+
7
+ Instant Regular Expressions
8
+ Created by Aristide Grange
9
+ """
10
+ import itertools
11
+ import re
12
+ from tkinter import SEL_FIRST, SEL_LAST, Frame, Label, PhotoImage, Scrollbar, Text, Tk
13
+
14
+ windowTitle = "Finding (and Replacing) Nemo"
15
+ initialFind = r"n(.*?)e(.*?)m(.*?)o"
16
+ initialRepl = r"M\1A\2K\3I"
17
+ initialText = """\
18
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
19
+ Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
20
+ Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
21
+ Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
22
+ """
23
+ images = {
24
+ "FIND": "R0lGODlhMAAiAPcAMf/////37//35//n1v97Off///f/9/f37/fexvfOvfeEQvd7QvdrQvdrKfdaKfdSMfdSIe/v9+/v7+/v5+/n3u/e1u/Wxu/Gre+1lO+tnO+thO+Ua+97Y+97Oe97Me9rOe9rMe9jOe9jMe9jIe9aMefe5+fe3ufezuece+eEWudzQudaIedSIedKMedKIedCKedCId7e1t7Wzt7Oxt7Gvd69vd69rd61pd6ljN6UjN6Ue96EY95zY95rUt5rQt5jMd5SId5KIdbn59be3tbGztbGvda1rdaEa9Z7a9Z7WtZzQtZzOdZzMdZjMdZaQtZSOdZSMdZKMdZCKdZCGNY5Ic7W1s7Oxs7Gtc69xs69tc69rc6tpc6llM6clM6cjM6Ue86EY85zWs5rSs5SKc5KKc5KGMa1tcatrcalvcalnMaUpcZ7c8ZzMcZrUsZrOcZrMcZaQsZSOcZSMcZKMcZCKcZCGMYxIcYxGL3Gxr21tb21rb2lpb2crb2cjL2UnL2UlL2UhL2Ec717Wr17Ur1zWr1rMb1jUr1KMb1KIb1CIb0xGLWlrbWlpbWcnLWEe7V7c7VzY7VzUrVSKbVKMbVCMbVCIbU5KbUxIbUxEK2lta2lpa2clK2UjK2MnK2MlK2Ea617e61za61rY61rMa1jSq1aUq1aSq1SQq1KKa0xEKWlnKWcnKWUnKWUhKWMjKWEa6Vza6VrWqVjMaVaUqVaKaVSMaVCMaU5KaUxIaUxGJyclJyMe5yElJyEhJx7e5x7c5xrOZxaQpxSOZxKQpw5IZSMhJSEjJR7c5Rre5RrY5RrUpRSQpRSKZRCOZRCKZQxKZQxIYyEhIx7hIxza4xzY4xrc4xjUoxaa4xaUoxSSoxKQoxCMYw5GIR7c4Rzc4Rre4RjY4RjWoRaa4RSWoRSUoRSMYRKQoRCOYQ5KYQxIXtra3taY3taSntKOXtCMXtCKXNCMXM5MXMxIWtSUmtKSmtKQmtCOWs5MWs5KWs5IWNCKWMxIVIxKUIQCDkhGAAAACH+AS4ALAAAAAAwACIAAAj/AAEIHEiwoMGDCBMqXMiwoUOHMqxIeEiRoZVp7cpZ29WrF4WKIAd208dGAQEVbiTVChUjZMU9+pYQmPmBZpxgvVw+nDdKwQICNVcIXQEkTgKdDdUJ+/nggVAXK1xI3TEA6UIr2uJ8iBqka1cXXTlkqGoVYRZ7iLyqBSs0iiEtZQVKiDGxBI1u3NR6lUpGDKg8MSgEQCphU7Z22vhg0dILXRCpYLuSCcYJT4wqXASBQaBzU7klHxC127OHD7ZDJFpERqRt0x5OnwQpmZmCLEhrbgg4WIHO1RY+nbQ9WRGEDJlmnXwJ+9FBgXMCIzYMVijBBgYMFxIMqJBMSc0Ht7qh/+Gjpte2rnYsYeNlasWIBgQ6yCewIoPCCp/cyP/wgUGbXVu0QcADZNBDnh98gHMLGXYQUw02w61QU3wdbNWDbQVVIIhMMwFF1DaZiPLBAy7E04kafrjSizaK3LFNNc0AAYRQDsAHHQlJ2IDQJ2zE1+EKDjiAijShkECCC8Qgw4cr7ZgyzC2WaHPNLWWoNeNWPiRAw0QFWQFMhz8C+QQ20yAiVSrY+MGOJCsccsst2GCzoHFxxEGGC+8hgs0MB2kyCpgzrUDCbs1Es41UdtATHFFkWELMOtsoQsYcgvRRQw5RSDgGOjZMR1AvPQIq6KCo9AKOJWDd48owQlHR4DXEKP9iyRrK+DNNBTu4RwIPFeTAGUG7hAomkA84gEg1m6ADljy9PBKGGJY4ig0xlsTBRSn98FOFDUC8pwQOPkgHbCGAzhTkA850s0c7j6Hjix9+gBIrMXLeAccWXUCyiRBcBEECdEJ98KtAqtBCYQc/OvDENnl4gYpUxISCIjjzylkGGV9okYUVNogRhAOBuuAEhjG08wOgDYzAgA5bCjIoCe5uwUk80RKTTSppPREGGGCIISOQ9AXBg6cC6WIywvCpoMHAocRBwhP4bHLFLujYkV42xNxBRhAyGrc113EgYtRBerDDDHMoDCyQEL5sE083EkgwQyBhxGFHMM206DUixGxmE0wssbQjCQ4JCaFKFwgQTVAVVhQUwAVPIFJKrHfYYRwi6OCDzzuIJIFhXAD0EccPsYRiSyqKSDpFcWSMIcZRoBMkQyA2BGZDIKSYcggih8TRRg4VxM5QABVYYLxgwiev/PLMCxQQADs=",
25
+ "find": "R0lGODlhMAAiAPQAMf////f39+/v7+fn597e3tbW1s7OzsbGxr29vbW1ta2traWlpZycnJSUlIyMjISEhHt7e3Nzc2tra2NjY1paWlJSUkpKSkJCQjk5OSkpKRgYGAAAAAAAAAAAAAAAAAAAACH+AS4ALAAAAAAwACIAAAX/ICCOZGmeaKquY2AGLiuvMCAUBuHWc48Kh0iFInEYCb4kSQCxPBiMxkMigRQEgJiSFVBYHNGG0RiZOHjblWAiiY4fkDhEYoBp06dAWfyAQyKAgAwDaHgnB0RwgYASgQ0IhDuGJDAIFhMRVFSLEX8QCJJ4AQM5AgQHTZqqjBAOCQQEkWkCDRMUFQsICQ4Vm5maEwwHOAsPDTpKMAsUDlO4CssTcb+2DAp8YGCyNFoCEsZwFQ3QDRTTVBRS0g1QbgsCd5QAAwgIBwYFAwStzQ8UEdCKVchky0yVBw7YuXkAKt4IAg74vXHVagqFBRgXSCAyYWAVCH0SNhDTitCJfSL5/4RbAPKPhQYYjVCYYAvCP0BxEDaD8CheAAHNwqh8MMGPSwgLeJWhwHSjqkYI+xg4MMCEgQjtRvZ7UAYCpghMF7CxONOWJkYR+rCpY4JlVpVxKDwYWEactKW9mhYRtqCTgwgWEMArERSK1j5q//6T8KXonFsShpiJkAECgQYVjykooCVA0JGHEWNiYCHThTFeb3UkoiCCBgwGEKQ1kuAJlhFwhA71h5SukwUM5qqeCSGBgicEWkfNiWSERtBad4JNIBaQBaQah1ToyGZBAnsIuIJs1qnqiAIVjIE2gnAB1T5x0icgzXT79ipgMOOEH6HBbREBMJCeGEY08IoLAkzB1YYFwjxwSUGSNULQJnNUwRYlCcyEkALIxECAP9cNMMABYpRhy3ZsSLDaR70oUAiABGCkAxowCGCAAfDYIQACXoElGRsdXWDBdg2Y90IWktDYGYAB9PWHP0PMdFZaF07SQgAFNDAMAQg0QA1UC8xoZQl22JGFPgWkOUCOL1pZQyhjxinnnCWEAAA7",
26
+ "REPL": "R0lGODlhMAAjAPcAMf/////3//+lOf+UKf+MEPf///f39/f35/fv7/ecQvecOfecKfeUIfeUGPeUEPeUCPeMAO/37+/v9+/v3u/n3u/n1u+9jO+9c++1hO+ta++tY++tWu+tUu+tSu+lUu+lQu+lMe+UMe+UKe+UGO+UEO+UAO+MCOfv5+fvxufn7+fn5+fnzue9lOe9c+e1jOe1e+e1c+e1a+etWuetUuelQuecOeeUUueUCN7e597e3t7e1t7ezt7evd7Wzt7Oxt7Ovd7Otd7Opd7OnN7Gtd7Gpd69lN61hN6ta96lStbextberdbW3tbWztbWxtbOvdbOrda1hNalUtaECM7W1s7Ozs7Oxs7Otc7Gxs7Gvc69tc69rc69pc61jM6lc8bWlMbOvcbGxsbGpca9tca9pca1nMaMAL3OhL3Gtb21vb21tb2tpb2tnL2tlLW9tbW9pbW9e7W1pbWtjLWcKa21nK2tra2tnK2tlK2lpa2llK2ljK2le6WlnKWljKWUe6WUc6WUY5y1QpyclJycjJychJyUc5yMY5StY5SUe5SMhJSMe5SMc5SMWpSEa5SESoyUe4yMhIyEY4SlKYScWoSMe4SEe4SEa4R7c4R7Y3uMY3uEe3t7e3t7c3tza3tzY3trKXtjIXOcAHOUMXOEY3Nzc3NzWnNrSmulCGuUMWuMGGtzWmtrY2taMWtaGGOUOWOMAGNzUmNjWmNjSmNaUmNaQmNaOWNaIWNSCFqcAFpjUlpSMVpSIVpSEFpKKVKMAFJSUlJSSlJSMVJKMVJKGFJKAFI5CEqUAEqEAEpzQkpKIUpCQkpCGEpCAEo5EEoxAEJjOUJCOUJCAEI5IUIxADl7ADlaITlCOTkxMTkxKTkxEDkhADFzADFrGDE5OTExADEpEClrCCkxKSkpKSkpISkpACkhCCkhACkYACFzACFrACEhCCEYGBhjEBhjABghABgYCBgYABgQEBgQABAQABAIAAhjAAhSAAhKAAgIEAgICABaAABCAAAhAAAQAAAIAAAAAAAAACH+AS4ALAAAAAAwACMAAAj/AAEIHEiwoMGDCBMqXMiwocOHAA4cgEixIIIJO3JMmAjADIqKFU/8MHIkg5EgYXx4iaTkI0iHE6wE2TCggYILQayEAgXIy8uGCKz8sDCAQAMRG3iEcXULlJkJPwli3OFjh9UdYYLE6NBhA04UXHoVA2XoTZgfPKBWlOBDphAWOdfMcfMDLloeO3hIMjbWVCQ5Fn6E2UFxgpsgFjYIEBADrZU6luqEEfqjTqpt54z1uuWqTIcgWAk7PECGzIUQDRosDmxlUrVJkwQJkqVuX71v06YZcyUlROAdbnLAJKPFyAYFAhoMwFlnEh0rWkpz8raPHm7dqKKc/KFFkBUrVn1M/ziBcEIeLUEQI8/AYk0i9Be4sqjsrN66c9/OnbobhpR3HkIUoZ0WVnBE0AGLFKKFD0HAFUQe77HQgQI1hRBDEHMcY0899bBzihZuCPILJD8EccEGGzwAQhFaUHHQH82sUkgeNHISDBk8WCCCcsqFUEQWmOyzjz3sUGNNOO5Y48YOEgowAAQhnBScQV00k82V47jzjy9CXZBcjziFoco//4CDiSOyhPMPLkJZkEBqJmRQxA9uZGEQD8Ncmc044/zzDF2IZQBCCDYE8QMZz/iiCSx0neHGI7BIhhhNn+1gxRpokEcQAp7seWU7/PwTyxqG/iCEEVzQmUombnDRxRExzP9nBR2PCKLFD3UJwcMPa/SRqUGNWJmNOVn+M44ukMRB4KGcWDNLVhuUMEIJAlzwA3DJBHMJIXm4sQYhqyxCRQQGLSIsn1qac2UzysQSyzX/hLMGD0F0IMCODYAQBA9W/PKPOcRiw0wzwxTiokF9dLMnuv/Mo+fCZF7jBr0xbDDCACWEYKgb1vzjDp/jZNOMLX0IZxAKq2TZTjtaOjwOsXyG+s8sZJTIQsUdIGHoJPf8w487QI/TDSt5mGwQFZxc406o8HiDJchk/ltLHpSlJwSvz5DpTjvmuGNOM57koelBOaAhiCaaPBLL0wwbm003peRBnBZqJMJL1ECz/HXYYx/NdAIOOVCxQyLorswymU93o0wuwfAiTDNR/xz0MLXU0XdCE+UwSTRZAq2lsSATu+4wkGvt+TjNzPLrQyegAUku2Hij5cd8LhxyM8QIg4w18HgcdC6BTBFSDmfQqsovttveDcG7lFLHI75cE841sARCxeWsnxC4G9HADPK6ywzDCRqBo0EHHWhMgT1IJzziNci1N7PMKnSYfML96/90AiJKey/0KtbLX1QK0rrNnQ541xugQ7SHhkXBghN0SKACWRc4KlAhBwKcIOYymJCAAAA7",
27
+ "repl": "R0lGODlhMAAjAPQAMf////f39+/v7+fn597e3tbW1s7OzsbGxr29vbW1ta2traWlpZycnJSUlIyMjISEhHt7e3Nzc2tra2NjY1paWlJSUkpKSkJCQjk5OTExMSkpKSEhIRgYGBAQEAgICAAAACH+AS4ALAAAAAAwACMAAAX/ICCOZGmeaKqubOu+gCDANBkIQ1EMQhAghFptYEAkEgjEwXBo7ISvweGgWCwUysPjwTgEoCafTySYIhYMxgLBjEQgCULvCw0QdAZdoVhUIJUFChISEAxYeQM1N1OMTAp+UwZ5eA4TEhFbDWYFdC4ECVMJjwl5BwsQa0umEhUVlhESDgqlBp0rAn5nVpBMDxeZDRQbHBgWFBSWDgtLBnFjKwRYCI9VqQsPs0YKEcMXFq0UEalFDWx4BAO2IwPjppAKDkrTWKYUGd7fEJJFEZpM00cOzCgh4EE8SaoWxKNixQooBRMyZMBwAYIRBhUgLDGS4MoBJeoANMhAgQsaCRZm/5lqaCUJhA4cNHjDoKEDBlJUHqkBlYBTiQUZNGjYMMxDhY3VWk6R4MEDBoMUak5AqoYBqANIBo4wcGGDUKIeLlzVZmWJggsVIkwAZaQSA3kdZzlKkIiEAAlDvW5oOkEBs488JTw44oeUIwdvVTFTUK7uiAAPgubt8GFDhQepqETAQCFU1UMGzlqAgFhUsAcCS0AO6lUDhw8xNRSbENGDhgWSHjWUe6ACbKITizmopZoBa6KvOwj9uuHDhwxyj3xekgDDhw5EvWKo0IB4iQLCOCC/njc7ZQ8UeGvza+ABZZgcxJNc4FO1gc0cOsCUrHevc8tdIMTIAhc4F198G2Qwwd8CBIQUAwEINABBBJUwR9R5wElgVRLwWODBBx4cGB8GEzDQIAo33CGJA8gh+JoH/clUgQU0YvDhdfmJdwEFC6Sjgg8yEPAABsPkh2F22cl2AQbn6QdTghTQ5eAJAQyQAAQV0MSBB9gRVZ4GE1mw5JZOAmiAVi1UWcAZDrDyZXYTeaOhA/bIVuIBPtKQ4h7ViYekUPdcEAEbzTzCRp5CADmAAwj+ORGPBcgwAAHo9ABGCYtm0ChwFHShlRiXhmHlkAcCiOeUodqQw5W0oXLAiamy4MOkjOyAaqxUymApDCEAADs=",
28
+ }
29
+ colors = ["#FF7B39", "#80F121"]
30
+ emphColors = ["#DAFC33", "#F42548"]
31
+ fieldParams = {
32
+ "height": 3,
33
+ "width": 70,
34
+ "font": ("monaco", 14),
35
+ "highlightthickness": 0,
36
+ "borderwidth": 0,
37
+ "background": "white",
38
+ }
39
+ textParams = {
40
+ "bg": "#F7E0D4",
41
+ "fg": "#2321F1",
42
+ "highlightthickness": 0,
43
+ "width": 1,
44
+ "height": 10,
45
+ "font": ("verdana", 16),
46
+ "wrap": "word",
47
+ }
48
+
49
+
50
+ class Zone:
51
+ def __init__(self, image, initialField, initialText):
52
+ frm = Frame(root)
53
+ frm.config(background="white")
54
+ self.image = PhotoImage(format="gif", data=images[image.upper()])
55
+ self.imageDimmed = PhotoImage(format="gif", data=images[image])
56
+ self.img = Label(frm)
57
+ self.img.config(borderwidth=0)
58
+ self.img.pack(side="left")
59
+ self.fld = Text(frm, **fieldParams)
60
+ self.initScrollText(frm, self.fld, initialField)
61
+ frm = Frame(root)
62
+ self.txt = Text(frm, **textParams)
63
+ self.initScrollText(frm, self.txt, initialText)
64
+ for i in range(2):
65
+ self.txt.tag_config(colors[i], background=colors[i])
66
+ self.txt.tag_config("emph" + colors[i], foreground=emphColors[i])
67
+
68
+ def initScrollText(self, frm, txt, contents):
69
+ scl = Scrollbar(frm)
70
+ scl.config(command=txt.yview)
71
+ scl.pack(side="right", fill="y")
72
+ txt.pack(side="left", expand=True, fill="x")
73
+ txt.config(yscrollcommand=scl.set)
74
+ txt.insert("1.0", contents)
75
+ frm.pack(fill="x")
76
+ Frame(height=2, bd=1, relief="ridge").pack(fill="x")
77
+
78
+ def refresh(self):
79
+ self.colorCycle = itertools.cycle(colors)
80
+ try:
81
+ self.substitute()
82
+ self.img.config(image=self.image)
83
+ except re.error:
84
+ self.img.config(image=self.imageDimmed)
85
+
86
+
87
+ class FindZone(Zone):
88
+ def addTags(self, m):
89
+ color = next(self.colorCycle)
90
+ self.txt.tag_add(color, "1.0+%sc" % m.start(), "1.0+%sc" % m.end())
91
+ try:
92
+ self.txt.tag_add(
93
+ "emph" + color, "1.0+%sc" % m.start("emph"), "1.0+%sc" % m.end("emph")
94
+ )
95
+ except Exception:
96
+ pass
97
+
98
+ def substitute(self, *args):
99
+ for color in colors:
100
+ self.txt.tag_remove(color, "1.0", "end")
101
+ self.txt.tag_remove("emph" + color, "1.0", "end")
102
+ self.rex = re.compile("") # default value in case of malformed regexp
103
+ self.rex = re.compile(self.fld.get("1.0", "end")[:-1], re.MULTILINE)
104
+ try:
105
+ re.compile("(?P<emph>%s)" % self.fld.get(SEL_FIRST, SEL_LAST))
106
+ self.rexSel = re.compile(
107
+ "%s(?P<emph>%s)%s"
108
+ % (
109
+ self.fld.get("1.0", SEL_FIRST),
110
+ self.fld.get(SEL_FIRST, SEL_LAST),
111
+ self.fld.get(SEL_LAST, "end")[:-1],
112
+ ),
113
+ re.MULTILINE,
114
+ )
115
+ except Exception:
116
+ self.rexSel = self.rex
117
+ self.rexSel.sub(self.addTags, self.txt.get("1.0", "end"))
118
+
119
+
120
+ class ReplaceZone(Zone):
121
+ def addTags(self, m):
122
+ s = sz.rex.sub(self.repl, m.group())
123
+ self.txt.delete(
124
+ "1.0+%sc" % (m.start() + self.diff), "1.0+%sc" % (m.end() + self.diff)
125
+ )
126
+ self.txt.insert("1.0+%sc" % (m.start() + self.diff), s, next(self.colorCycle))
127
+ self.diff += len(s) - (m.end() - m.start())
128
+
129
+ def substitute(self):
130
+ self.txt.delete("1.0", "end")
131
+ self.txt.insert("1.0", sz.txt.get("1.0", "end")[:-1])
132
+ self.diff = 0
133
+ self.repl = rex0.sub(r"\\g<\1>", self.fld.get("1.0", "end")[:-1])
134
+ sz.rex.sub(self.addTags, sz.txt.get("1.0", "end")[:-1])
135
+
136
+
137
+ def launchRefresh(_):
138
+ sz.fld.after_idle(sz.refresh)
139
+ rz.fld.after_idle(rz.refresh)
140
+
141
+
142
+ def app():
143
+ global root, sz, rz, rex0
144
+ root = Tk()
145
+ root.resizable(height=False, width=True)
146
+ root.title(windowTitle)
147
+ root.minsize(width=250, height=0)
148
+ sz = FindZone("find", initialFind, initialText)
149
+ sz.fld.bind("<Button-1>", launchRefresh)
150
+ sz.fld.bind("<ButtonRelease-1>", launchRefresh)
151
+ sz.fld.bind("<B1-Motion>", launchRefresh)
152
+ sz.rexSel = re.compile("")
153
+ rz = ReplaceZone("repl", initialRepl, "")
154
+ rex0 = re.compile(r"(?<!\\)\\([0-9]+)")
155
+ root.bind_all("<Key>", launchRefresh)
156
+ launchRefresh(None)
157
+ root.mainloop()
158
+
159
+
160
+ if __name__ == "__main__":
161
+ app()
162
+
163
+ __all__ = ["app"]
loan-advisor-agent/.venv/Lib/site-packages/nltk/app/rdparser_app.py ADDED
@@ -0,0 +1,1052 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: Recursive Descent Parser Application
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Author: Edward Loper <edloper@gmail.com>
5
+ # URL: <https://www.nltk.org/>
6
+ # For license information, see LICENSE.TXT
7
+
8
+ """
9
+ A graphical tool for exploring the recursive descent parser.
10
+
11
+ The recursive descent parser maintains a tree, which records the
12
+ structure of the portion of the text that has been parsed. It uses
13
+ CFG productions to expand the fringe of the tree, and matches its
14
+ leaves against the text. Initially, the tree contains the start
15
+ symbol ("S"). It is shown in the main canvas, to the right of the
16
+ list of available expansions.
17
+
18
+ The parser builds up a tree structure for the text using three
19
+ operations:
20
+
21
+ - "expand" uses a CFG production to add children to a node on the
22
+ fringe of the tree.
23
+ - "match" compares a leaf in the tree to a text token.
24
+ - "backtrack" returns the tree to its state before the most recent
25
+ expand or match operation.
26
+
27
+ The parser maintains a list of tree locations called a "frontier" to
28
+ remember which nodes have not yet been expanded and which leaves have
29
+ not yet been matched against the text. The leftmost frontier node is
30
+ shown in green, and the other frontier nodes are shown in blue. The
31
+ parser always performs expand and match operations on the leftmost
32
+ element of the frontier.
33
+
34
+ You can control the parser's operation by using the "expand," "match,"
35
+ and "backtrack" buttons; or you can use the "step" button to let the
36
+ parser automatically decide which operation to apply. The parser uses
37
+ the following rules to decide which operation to apply:
38
+
39
+ - If the leftmost frontier element is a token, try matching it.
40
+ - If the leftmost frontier element is a node, try expanding it with
41
+ the first untried expansion.
42
+ - Otherwise, backtrack.
43
+
44
+ The "expand" button applies the untried expansion whose CFG production
45
+ is listed earliest in the grammar. To manually choose which expansion
46
+ to apply, click on a CFG production from the list of available
47
+ expansions, on the left side of the main window.
48
+
49
+ The "autostep" button will let the parser continue applying
50
+ applications to the tree until it reaches a complete parse. You can
51
+ cancel an autostep in progress at any time by clicking on the
52
+ "autostep" button again.
53
+
54
+ Keyboard Shortcuts::
55
+ [Space]\t Perform the next expand, match, or backtrack operation
56
+ [a]\t Step through operations until the next complete parse
57
+ [e]\t Perform an expand operation
58
+ [m]\t Perform a match operation
59
+ [b]\t Perform a backtrack operation
60
+ [Delete]\t Reset the parser
61
+ [g]\t Show/hide available expansions list
62
+ [h]\t Help
63
+ [Ctrl-p]\t Print
64
+ [q]\t Quit
65
+ """
66
+
67
+ from tkinter import Button, Frame, IntVar, Label, Listbox, Menu, Scrollbar, Tk
68
+ from tkinter.font import Font
69
+
70
+ from nltk.draw import CFGEditor, TreeSegmentWidget, tree_to_treesegment
71
+ from nltk.draw.util import CanvasFrame, EntryDialog, ShowText, TextWidget
72
+ from nltk.parse import SteppingRecursiveDescentParser
73
+ from nltk.tree import Tree
74
+ from nltk.util import in_idle
75
+
76
+
77
+ class RecursiveDescentApp:
78
+ """
79
+ A graphical tool for exploring the recursive descent parser. The tool
80
+ displays the parser's tree and the remaining text, and allows the
81
+ user to control the parser's operation. In particular, the user
82
+ can expand subtrees on the frontier, match tokens on the frontier
83
+ against the text, and backtrack. A "step" button simply steps
84
+ through the parsing process, performing the operations that
85
+ ``RecursiveDescentParser`` would use.
86
+ """
87
+
88
+ def __init__(self, grammar, sent, trace=0):
89
+ self._sent = sent
90
+ self._parser = SteppingRecursiveDescentParser(grammar, trace)
91
+
92
+ # Set up the main window.
93
+ self._top = Tk()
94
+ self._top.title("Recursive Descent Parser Application")
95
+
96
+ # Set up key bindings.
97
+ self._init_bindings()
98
+
99
+ # Initialize the fonts.
100
+ self._init_fonts(self._top)
101
+
102
+ # Animations. animating_lock is a lock to prevent the demo
103
+ # from performing new operations while it's animating.
104
+ self._animation_frames = IntVar(self._top)
105
+ self._animation_frames.set(5)
106
+ self._animating_lock = 0
107
+ self._autostep = 0
108
+
109
+ # The user can hide the grammar.
110
+ self._show_grammar = IntVar(self._top)
111
+ self._show_grammar.set(1)
112
+
113
+ # Create the basic frames.
114
+ self._init_menubar(self._top)
115
+ self._init_buttons(self._top)
116
+ self._init_feedback(self._top)
117
+ self._init_grammar(self._top)
118
+ self._init_canvas(self._top)
119
+
120
+ # Initialize the parser.
121
+ self._parser.initialize(self._sent)
122
+
123
+ # Resize callback
124
+ self._canvas.bind("<Configure>", self._configure)
125
+
126
+ #########################################
127
+ ## Initialization Helpers
128
+ #########################################
129
+
130
+ def _init_fonts(self, root):
131
+ # See: <http://www.astro.washington.edu/owen/ROTKFolklore.html>
132
+ self._sysfont = Font(font=Button()["font"])
133
+ root.option_add("*Font", self._sysfont)
134
+
135
+ # TWhat's our font size (default=same as sysfont)
136
+ self._size = IntVar(root)
137
+ self._size.set(self._sysfont.cget("size"))
138
+
139
+ self._boldfont = Font(family="helvetica", weight="bold", size=self._size.get())
140
+ self._font = Font(family="helvetica", size=self._size.get())
141
+ if self._size.get() < 0:
142
+ big = self._size.get() - 2
143
+ else:
144
+ big = self._size.get() + 2
145
+ self._bigfont = Font(family="helvetica", weight="bold", size=big)
146
+
147
+ def _init_grammar(self, parent):
148
+ # Grammar view.
149
+ self._prodframe = listframe = Frame(parent)
150
+ self._prodframe.pack(fill="both", side="left", padx=2)
151
+ self._prodlist_label = Label(
152
+ self._prodframe, font=self._boldfont, text="Available Expansions"
153
+ )
154
+ self._prodlist_label.pack()
155
+ self._prodlist = Listbox(
156
+ self._prodframe,
157
+ selectmode="single",
158
+ relief="groove",
159
+ background="white",
160
+ foreground="#909090",
161
+ font=self._font,
162
+ selectforeground="#004040",
163
+ selectbackground="#c0f0c0",
164
+ )
165
+
166
+ self._prodlist.pack(side="right", fill="both", expand=1)
167
+
168
+ self._productions = list(self._parser.grammar().productions())
169
+ for production in self._productions:
170
+ self._prodlist.insert("end", (" %s" % production))
171
+ self._prodlist.config(height=min(len(self._productions), 25))
172
+
173
+ # Add a scrollbar if there are more than 25 productions.
174
+ if len(self._productions) > 25:
175
+ listscroll = Scrollbar(self._prodframe, orient="vertical")
176
+ self._prodlist.config(yscrollcommand=listscroll.set)
177
+ listscroll.config(command=self._prodlist.yview)
178
+ listscroll.pack(side="left", fill="y")
179
+
180
+ # If they select a production, apply it.
181
+ self._prodlist.bind("<<ListboxSelect>>", self._prodlist_select)
182
+
183
+ def _init_bindings(self):
184
+ # Key bindings are a good thing.
185
+ self._top.bind("<Control-q>", self.destroy)
186
+ self._top.bind("<Control-x>", self.destroy)
187
+ self._top.bind("<Escape>", self.destroy)
188
+ self._top.bind("e", self.expand)
189
+ # self._top.bind('<Alt-e>', self.expand)
190
+ # self._top.bind('<Control-e>', self.expand)
191
+ self._top.bind("m", self.match)
192
+ self._top.bind("<Alt-m>", self.match)
193
+ self._top.bind("<Control-m>", self.match)
194
+ self._top.bind("b", self.backtrack)
195
+ self._top.bind("<Alt-b>", self.backtrack)
196
+ self._top.bind("<Control-b>", self.backtrack)
197
+ self._top.bind("<Control-z>", self.backtrack)
198
+ self._top.bind("<BackSpace>", self.backtrack)
199
+ self._top.bind("a", self.autostep)
200
+ # self._top.bind('<Control-a>', self.autostep)
201
+ self._top.bind("<Control-space>", self.autostep)
202
+ self._top.bind("<Control-c>", self.cancel_autostep)
203
+ self._top.bind("<space>", self.step)
204
+ self._top.bind("<Delete>", self.reset)
205
+ self._top.bind("<Control-p>", self.postscript)
206
+ # self._top.bind('<h>', self.help)
207
+ # self._top.bind('<Alt-h>', self.help)
208
+ self._top.bind("<Control-h>", self.help)
209
+ self._top.bind("<F1>", self.help)
210
+ # self._top.bind('<g>', self.toggle_grammar)
211
+ # self._top.bind('<Alt-g>', self.toggle_grammar)
212
+ # self._top.bind('<Control-g>', self.toggle_grammar)
213
+ self._top.bind("<Control-g>", self.edit_grammar)
214
+ self._top.bind("<Control-t>", self.edit_sentence)
215
+
216
+ def _init_buttons(self, parent):
217
+ # Set up the frames.
218
+ self._buttonframe = buttonframe = Frame(parent)
219
+ buttonframe.pack(fill="none", side="bottom", padx=3, pady=2)
220
+ Button(
221
+ buttonframe,
222
+ text="Step",
223
+ background="#90c0d0",
224
+ foreground="black",
225
+ command=self.step,
226
+ ).pack(side="left")
227
+ Button(
228
+ buttonframe,
229
+ text="Autostep",
230
+ background="#90c0d0",
231
+ foreground="black",
232
+ command=self.autostep,
233
+ ).pack(side="left")
234
+ Button(
235
+ buttonframe,
236
+ text="Expand",
237
+ underline=0,
238
+ background="#90f090",
239
+ foreground="black",
240
+ command=self.expand,
241
+ ).pack(side="left")
242
+ Button(
243
+ buttonframe,
244
+ text="Match",
245
+ underline=0,
246
+ background="#90f090",
247
+ foreground="black",
248
+ command=self.match,
249
+ ).pack(side="left")
250
+ Button(
251
+ buttonframe,
252
+ text="Backtrack",
253
+ underline=0,
254
+ background="#f0a0a0",
255
+ foreground="black",
256
+ command=self.backtrack,
257
+ ).pack(side="left")
258
+ # Replace autostep...
259
+
260
+ # self._autostep_button = Button(buttonframe, text='Autostep',
261
+ # underline=0, command=self.autostep)
262
+ # self._autostep_button.pack(side='left')
263
+
264
+ def _configure(self, event):
265
+ self._autostep = 0
266
+ (x1, y1, x2, y2) = self._cframe.scrollregion()
267
+ y2 = event.height - 6
268
+ self._canvas["scrollregion"] = "%d %d %d %d" % (x1, y1, x2, y2)
269
+ self._redraw()
270
+
271
+ def _init_feedback(self, parent):
272
+ self._feedbackframe = feedbackframe = Frame(parent)
273
+ feedbackframe.pack(fill="x", side="bottom", padx=3, pady=3)
274
+ self._lastoper_label = Label(
275
+ feedbackframe, text="Last Operation:", font=self._font
276
+ )
277
+ self._lastoper_label.pack(side="left")
278
+ lastoperframe = Frame(feedbackframe, relief="sunken", border=1)
279
+ lastoperframe.pack(fill="x", side="right", expand=1, padx=5)
280
+ self._lastoper1 = Label(
281
+ lastoperframe, foreground="#007070", background="#f0f0f0", font=self._font
282
+ )
283
+ self._lastoper2 = Label(
284
+ lastoperframe,
285
+ anchor="w",
286
+ width=30,
287
+ foreground="#004040",
288
+ background="#f0f0f0",
289
+ font=self._font,
290
+ )
291
+ self._lastoper1.pack(side="left")
292
+ self._lastoper2.pack(side="left", fill="x", expand=1)
293
+
294
+ def _init_canvas(self, parent):
295
+ self._cframe = CanvasFrame(
296
+ parent,
297
+ background="white",
298
+ # width=525, height=250,
299
+ closeenough=10,
300
+ border=2,
301
+ relief="sunken",
302
+ )
303
+ self._cframe.pack(expand=1, fill="both", side="top", pady=2)
304
+ canvas = self._canvas = self._cframe.canvas()
305
+
306
+ # Initially, there's no tree or text
307
+ self._tree = None
308
+ self._textwidgets = []
309
+ self._textline = None
310
+
311
+ def _init_menubar(self, parent):
312
+ menubar = Menu(parent)
313
+
314
+ filemenu = Menu(menubar, tearoff=0)
315
+ filemenu.add_command(
316
+ label="Reset Parser", underline=0, command=self.reset, accelerator="Del"
317
+ )
318
+ filemenu.add_command(
319
+ label="Print to Postscript",
320
+ underline=0,
321
+ command=self.postscript,
322
+ accelerator="Ctrl-p",
323
+ )
324
+ filemenu.add_command(
325
+ label="Exit", underline=1, command=self.destroy, accelerator="Ctrl-x"
326
+ )
327
+ menubar.add_cascade(label="File", underline=0, menu=filemenu)
328
+
329
+ editmenu = Menu(menubar, tearoff=0)
330
+ editmenu.add_command(
331
+ label="Edit Grammar",
332
+ underline=5,
333
+ command=self.edit_grammar,
334
+ accelerator="Ctrl-g",
335
+ )
336
+ editmenu.add_command(
337
+ label="Edit Text",
338
+ underline=5,
339
+ command=self.edit_sentence,
340
+ accelerator="Ctrl-t",
341
+ )
342
+ menubar.add_cascade(label="Edit", underline=0, menu=editmenu)
343
+
344
+ rulemenu = Menu(menubar, tearoff=0)
345
+ rulemenu.add_command(
346
+ label="Step", underline=1, command=self.step, accelerator="Space"
347
+ )
348
+ rulemenu.add_separator()
349
+ rulemenu.add_command(
350
+ label="Match", underline=0, command=self.match, accelerator="Ctrl-m"
351
+ )
352
+ rulemenu.add_command(
353
+ label="Expand", underline=0, command=self.expand, accelerator="Ctrl-e"
354
+ )
355
+ rulemenu.add_separator()
356
+ rulemenu.add_command(
357
+ label="Backtrack", underline=0, command=self.backtrack, accelerator="Ctrl-b"
358
+ )
359
+ menubar.add_cascade(label="Apply", underline=0, menu=rulemenu)
360
+
361
+ viewmenu = Menu(menubar, tearoff=0)
362
+ viewmenu.add_checkbutton(
363
+ label="Show Grammar",
364
+ underline=0,
365
+ variable=self._show_grammar,
366
+ command=self._toggle_grammar,
367
+ )
368
+ viewmenu.add_separator()
369
+ viewmenu.add_radiobutton(
370
+ label="Tiny",
371
+ variable=self._size,
372
+ underline=0,
373
+ value=10,
374
+ command=self.resize,
375
+ )
376
+ viewmenu.add_radiobutton(
377
+ label="Small",
378
+ variable=self._size,
379
+ underline=0,
380
+ value=12,
381
+ command=self.resize,
382
+ )
383
+ viewmenu.add_radiobutton(
384
+ label="Medium",
385
+ variable=self._size,
386
+ underline=0,
387
+ value=14,
388
+ command=self.resize,
389
+ )
390
+ viewmenu.add_radiobutton(
391
+ label="Large",
392
+ variable=self._size,
393
+ underline=0,
394
+ value=18,
395
+ command=self.resize,
396
+ )
397
+ viewmenu.add_radiobutton(
398
+ label="Huge",
399
+ variable=self._size,
400
+ underline=0,
401
+ value=24,
402
+ command=self.resize,
403
+ )
404
+ menubar.add_cascade(label="View", underline=0, menu=viewmenu)
405
+
406
+ animatemenu = Menu(menubar, tearoff=0)
407
+ animatemenu.add_radiobutton(
408
+ label="No Animation", underline=0, variable=self._animation_frames, value=0
409
+ )
410
+ animatemenu.add_radiobutton(
411
+ label="Slow Animation",
412
+ underline=0,
413
+ variable=self._animation_frames,
414
+ value=10,
415
+ accelerator="-",
416
+ )
417
+ animatemenu.add_radiobutton(
418
+ label="Normal Animation",
419
+ underline=0,
420
+ variable=self._animation_frames,
421
+ value=5,
422
+ accelerator="=",
423
+ )
424
+ animatemenu.add_radiobutton(
425
+ label="Fast Animation",
426
+ underline=0,
427
+ variable=self._animation_frames,
428
+ value=2,
429
+ accelerator="+",
430
+ )
431
+ menubar.add_cascade(label="Animate", underline=1, menu=animatemenu)
432
+
433
+ helpmenu = Menu(menubar, tearoff=0)
434
+ helpmenu.add_command(label="About", underline=0, command=self.about)
435
+ helpmenu.add_command(
436
+ label="Instructions", underline=0, command=self.help, accelerator="F1"
437
+ )
438
+ menubar.add_cascade(label="Help", underline=0, menu=helpmenu)
439
+
440
+ parent.config(menu=menubar)
441
+
442
+ #########################################
443
+ ## Helper
444
+ #########################################
445
+
446
+ def _get(self, widget, treeloc):
447
+ for i in treeloc:
448
+ widget = widget.subtrees()[i]
449
+ if isinstance(widget, TreeSegmentWidget):
450
+ widget = widget.label()
451
+ return widget
452
+
453
+ #########################################
454
+ ## Main draw procedure
455
+ #########################################
456
+
457
+ def _redraw(self):
458
+ canvas = self._canvas
459
+
460
+ # Delete the old tree, widgets, etc.
461
+ if self._tree is not None:
462
+ self._cframe.destroy_widget(self._tree)
463
+ for twidget in self._textwidgets:
464
+ self._cframe.destroy_widget(twidget)
465
+ if self._textline is not None:
466
+ self._canvas.delete(self._textline)
467
+
468
+ # Draw the tree.
469
+ helv = ("helvetica", -self._size.get())
470
+ bold = ("helvetica", -self._size.get(), "bold")
471
+ attribs = {
472
+ "tree_color": "#000000",
473
+ "tree_width": 2,
474
+ "node_font": bold,
475
+ "leaf_font": helv,
476
+ }
477
+ tree = self._parser.tree()
478
+ self._tree = tree_to_treesegment(canvas, tree, **attribs)
479
+ self._cframe.add_widget(self._tree, 30, 5)
480
+
481
+ # Draw the text.
482
+ helv = ("helvetica", -self._size.get())
483
+ bottom = y = self._cframe.scrollregion()[3]
484
+ self._textwidgets = [
485
+ TextWidget(canvas, word, font=self._font) for word in self._sent
486
+ ]
487
+ for twidget in self._textwidgets:
488
+ self._cframe.add_widget(twidget, 0, 0)
489
+ twidget.move(0, bottom - twidget.bbox()[3] - 5)
490
+ y = min(y, twidget.bbox()[1])
491
+
492
+ # Draw a line over the text, to separate it from the tree.
493
+ self._textline = canvas.create_line(-5000, y - 5, 5000, y - 5, dash=".")
494
+
495
+ # Highlight appropriate nodes.
496
+ self._highlight_nodes()
497
+ self._highlight_prodlist()
498
+
499
+ # Make sure the text lines up.
500
+ self._position_text()
501
+
502
+ def _redraw_quick(self):
503
+ # This should be more-or-less sufficient after an animation.
504
+ self._highlight_nodes()
505
+ self._highlight_prodlist()
506
+ self._position_text()
507
+
508
+ def _highlight_nodes(self):
509
+ # Highlight the list of nodes to be checked.
510
+ bold = ("helvetica", -self._size.get(), "bold")
511
+ for treeloc in self._parser.frontier()[:1]:
512
+ self._get(self._tree, treeloc)["color"] = "#20a050"
513
+ self._get(self._tree, treeloc)["font"] = bold
514
+ for treeloc in self._parser.frontier()[1:]:
515
+ self._get(self._tree, treeloc)["color"] = "#008080"
516
+
517
+ def _highlight_prodlist(self):
518
+ # Highlight the productions that can be expanded.
519
+ # Boy, too bad tkinter doesn't implement Listbox.itemconfig;
520
+ # that would be pretty useful here.
521
+ self._prodlist.delete(0, "end")
522
+ expandable = self._parser.expandable_productions()
523
+ untried = self._parser.untried_expandable_productions()
524
+ productions = self._productions
525
+ for index in range(len(productions)):
526
+ if productions[index] in expandable:
527
+ if productions[index] in untried:
528
+ self._prodlist.insert(index, " %s" % productions[index])
529
+ else:
530
+ self._prodlist.insert(index, " %s (TRIED)" % productions[index])
531
+ self._prodlist.selection_set(index)
532
+ else:
533
+ self._prodlist.insert(index, " %s" % productions[index])
534
+
535
+ def _position_text(self):
536
+ # Line up the text widgets that are matched against the tree
537
+ numwords = len(self._sent)
538
+ num_matched = numwords - len(self._parser.remaining_text())
539
+ leaves = self._tree_leaves()[:num_matched]
540
+ xmax = self._tree.bbox()[0]
541
+ for i in range(0, len(leaves)):
542
+ widget = self._textwidgets[i]
543
+ leaf = leaves[i]
544
+ widget["color"] = "#006040"
545
+ leaf["color"] = "#006040"
546
+ widget.move(leaf.bbox()[0] - widget.bbox()[0], 0)
547
+ xmax = widget.bbox()[2] + 10
548
+
549
+ # Line up the text widgets that are not matched against the tree.
550
+ for i in range(len(leaves), numwords):
551
+ widget = self._textwidgets[i]
552
+ widget["color"] = "#a0a0a0"
553
+ widget.move(xmax - widget.bbox()[0], 0)
554
+ xmax = widget.bbox()[2] + 10
555
+
556
+ # If we have a complete parse, make everything green :)
557
+ if self._parser.currently_complete():
558
+ for twidget in self._textwidgets:
559
+ twidget["color"] = "#00a000"
560
+
561
+ # Move the matched leaves down to the text.
562
+ for i in range(0, len(leaves)):
563
+ widget = self._textwidgets[i]
564
+ leaf = leaves[i]
565
+ dy = widget.bbox()[1] - leaf.bbox()[3] - 10.0
566
+ dy = max(dy, leaf.parent().label().bbox()[3] - leaf.bbox()[3] + 10)
567
+ leaf.move(0, dy)
568
+
569
+ def _tree_leaves(self, tree=None):
570
+ if tree is None:
571
+ tree = self._tree
572
+ if isinstance(tree, TreeSegmentWidget):
573
+ leaves = []
574
+ for child in tree.subtrees():
575
+ leaves += self._tree_leaves(child)
576
+ return leaves
577
+ else:
578
+ return [tree]
579
+
580
+ #########################################
581
+ ## Button Callbacks
582
+ #########################################
583
+
584
+ def destroy(self, *e):
585
+ self._autostep = 0
586
+ if self._top is None:
587
+ return
588
+ self._top.destroy()
589
+ self._top = None
590
+
591
+ def reset(self, *e):
592
+ self._autostep = 0
593
+ self._parser.initialize(self._sent)
594
+ self._lastoper1["text"] = "Reset Application"
595
+ self._lastoper2["text"] = ""
596
+ self._redraw()
597
+
598
+ def autostep(self, *e):
599
+ if self._animation_frames.get() == 0:
600
+ self._animation_frames.set(2)
601
+ if self._autostep:
602
+ self._autostep = 0
603
+ else:
604
+ self._autostep = 1
605
+ self._step()
606
+
607
+ def cancel_autostep(self, *e):
608
+ # self._autostep_button['text'] = 'Autostep'
609
+ self._autostep = 0
610
+
611
+ # Make sure to stop auto-stepping if we get any user input.
612
+ def step(self, *e):
613
+ self._autostep = 0
614
+ self._step()
615
+
616
+ def match(self, *e):
617
+ self._autostep = 0
618
+ self._match()
619
+
620
+ def expand(self, *e):
621
+ self._autostep = 0
622
+ self._expand()
623
+
624
+ def backtrack(self, *e):
625
+ self._autostep = 0
626
+ self._backtrack()
627
+
628
+ def _step(self):
629
+ if self._animating_lock:
630
+ return
631
+
632
+ # Try expanding, matching, and backtracking (in that order)
633
+ if self._expand():
634
+ pass
635
+ elif self._parser.untried_match() and self._match():
636
+ pass
637
+ elif self._backtrack():
638
+ pass
639
+ else:
640
+ self._lastoper1["text"] = "Finished"
641
+ self._lastoper2["text"] = ""
642
+ self._autostep = 0
643
+
644
+ # Check if we just completed a parse.
645
+ if self._parser.currently_complete():
646
+ self._autostep = 0
647
+ self._lastoper2["text"] += " [COMPLETE PARSE]"
648
+
649
+ def _expand(self, *e):
650
+ if self._animating_lock:
651
+ return
652
+ old_frontier = self._parser.frontier()
653
+ rv = self._parser.expand()
654
+ if rv is not None:
655
+ self._lastoper1["text"] = "Expand:"
656
+ self._lastoper2["text"] = rv
657
+ self._prodlist.selection_clear(0, "end")
658
+ index = self._productions.index(rv)
659
+ self._prodlist.selection_set(index)
660
+ self._animate_expand(old_frontier[0])
661
+ return True
662
+ else:
663
+ self._lastoper1["text"] = "Expand:"
664
+ self._lastoper2["text"] = "(all expansions tried)"
665
+ return False
666
+
667
+ def _match(self, *e):
668
+ if self._animating_lock:
669
+ return
670
+ old_frontier = self._parser.frontier()
671
+ rv = self._parser.match()
672
+ if rv is not None:
673
+ self._lastoper1["text"] = "Match:"
674
+ self._lastoper2["text"] = rv
675
+ self._animate_match(old_frontier[0])
676
+ return True
677
+ else:
678
+ self._lastoper1["text"] = "Match:"
679
+ self._lastoper2["text"] = "(failed)"
680
+ return False
681
+
682
+ def _backtrack(self, *e):
683
+ if self._animating_lock:
684
+ return
685
+ if self._parser.backtrack():
686
+ elt = self._parser.tree()
687
+ for i in self._parser.frontier()[0]:
688
+ elt = elt[i]
689
+ self._lastoper1["text"] = "Backtrack"
690
+ self._lastoper2["text"] = ""
691
+ if isinstance(elt, Tree):
692
+ self._animate_backtrack(self._parser.frontier()[0])
693
+ else:
694
+ self._animate_match_backtrack(self._parser.frontier()[0])
695
+ return True
696
+ else:
697
+ self._autostep = 0
698
+ self._lastoper1["text"] = "Finished"
699
+ self._lastoper2["text"] = ""
700
+ return False
701
+
702
+ def about(self, *e):
703
+ ABOUT = (
704
+ "NLTK Recursive Descent Parser Application\n" + "Written by Edward Loper"
705
+ )
706
+ TITLE = "About: Recursive Descent Parser Application"
707
+ try:
708
+ from tkinter.messagebox import Message
709
+
710
+ Message(message=ABOUT, title=TITLE).show()
711
+ except Exception:
712
+ ShowText(self._top, TITLE, ABOUT)
713
+
714
+ def help(self, *e):
715
+ self._autostep = 0
716
+ # The default font's not very legible; try using 'fixed' instead.
717
+ try:
718
+ ShowText(
719
+ self._top,
720
+ "Help: Recursive Descent Parser Application",
721
+ (__doc__ or "").strip(),
722
+ width=75,
723
+ font="fixed",
724
+ )
725
+ except Exception:
726
+ ShowText(
727
+ self._top,
728
+ "Help: Recursive Descent Parser Application",
729
+ (__doc__ or "").strip(),
730
+ width=75,
731
+ )
732
+
733
+ def postscript(self, *e):
734
+ self._autostep = 0
735
+ self._cframe.print_to_file()
736
+
737
+ def mainloop(self, *args, **kwargs):
738
+ """
739
+ Enter the Tkinter mainloop. This function must be called if
740
+ this demo is created from a non-interactive program (e.g.
741
+ from a secript); otherwise, the demo will close as soon as
742
+ the script completes.
743
+ """
744
+ if in_idle():
745
+ return
746
+ self._top.mainloop(*args, **kwargs)
747
+
748
+ def resize(self, size=None):
749
+ if size is not None:
750
+ self._size.set(size)
751
+ size = self._size.get()
752
+ self._font.configure(size=-(abs(size)))
753
+ self._boldfont.configure(size=-(abs(size)))
754
+ self._sysfont.configure(size=-(abs(size)))
755
+ self._bigfont.configure(size=-(abs(size + 2)))
756
+ self._redraw()
757
+
758
+ #########################################
759
+ ## Expand Production Selection
760
+ #########################################
761
+
762
+ def _toggle_grammar(self, *e):
763
+ if self._show_grammar.get():
764
+ self._prodframe.pack(
765
+ fill="both", side="left", padx=2, after=self._feedbackframe
766
+ )
767
+ self._lastoper1["text"] = "Show Grammar"
768
+ else:
769
+ self._prodframe.pack_forget()
770
+ self._lastoper1["text"] = "Hide Grammar"
771
+ self._lastoper2["text"] = ""
772
+
773
+ # def toggle_grammar(self, *e):
774
+ # self._show_grammar = not self._show_grammar
775
+ # if self._show_grammar:
776
+ # self._prodframe.pack(fill='both', expand='y', side='left',
777
+ # after=self._feedbackframe)
778
+ # self._lastoper1['text'] = 'Show Grammar'
779
+ # else:
780
+ # self._prodframe.pack_forget()
781
+ # self._lastoper1['text'] = 'Hide Grammar'
782
+ # self._lastoper2['text'] = ''
783
+
784
+ def _prodlist_select(self, event):
785
+ selection = self._prodlist.curselection()
786
+ if len(selection) != 1:
787
+ return
788
+ index = int(selection[0])
789
+ old_frontier = self._parser.frontier()
790
+ production = self._parser.expand(self._productions[index])
791
+
792
+ if production:
793
+ self._lastoper1["text"] = "Expand:"
794
+ self._lastoper2["text"] = production
795
+ self._prodlist.selection_clear(0, "end")
796
+ self._prodlist.selection_set(index)
797
+ self._animate_expand(old_frontier[0])
798
+ else:
799
+ # Reset the production selections.
800
+ self._prodlist.selection_clear(0, "end")
801
+ for prod in self._parser.expandable_productions():
802
+ index = self._productions.index(prod)
803
+ self._prodlist.selection_set(index)
804
+
805
+ #########################################
806
+ ## Animation
807
+ #########################################
808
+
809
+ def _animate_expand(self, treeloc):
810
+ oldwidget = self._get(self._tree, treeloc)
811
+ oldtree = oldwidget.parent()
812
+ top = not isinstance(oldtree.parent(), TreeSegmentWidget)
813
+
814
+ tree = self._parser.tree()
815
+ for i in treeloc:
816
+ tree = tree[i]
817
+
818
+ widget = tree_to_treesegment(
819
+ self._canvas,
820
+ tree,
821
+ node_font=self._boldfont,
822
+ leaf_color="white",
823
+ tree_width=2,
824
+ tree_color="white",
825
+ node_color="white",
826
+ leaf_font=self._font,
827
+ )
828
+ widget.label()["color"] = "#20a050"
829
+
830
+ (oldx, oldy) = oldtree.label().bbox()[:2]
831
+ (newx, newy) = widget.label().bbox()[:2]
832
+ widget.move(oldx - newx, oldy - newy)
833
+
834
+ if top:
835
+ self._cframe.add_widget(widget, 0, 5)
836
+ widget.move(30 - widget.label().bbox()[0], 0)
837
+ self._tree = widget
838
+ else:
839
+ oldtree.parent().replace_child(oldtree, widget)
840
+
841
+ # Move the children over so they don't overlap.
842
+ # Line the children up in a strange way.
843
+ if widget.subtrees():
844
+ dx = (
845
+ oldx
846
+ + widget.label().width() / 2
847
+ - widget.subtrees()[0].bbox()[0] / 2
848
+ - widget.subtrees()[0].bbox()[2] / 2
849
+ )
850
+ for subtree in widget.subtrees():
851
+ subtree.move(dx, 0)
852
+
853
+ self._makeroom(widget)
854
+
855
+ if top:
856
+ self._cframe.destroy_widget(oldtree)
857
+ else:
858
+ oldtree.destroy()
859
+
860
+ colors = [
861
+ "gray%d" % (10 * int(10 * x / self._animation_frames.get()))
862
+ for x in range(self._animation_frames.get(), 0, -1)
863
+ ]
864
+
865
+ # Move the text string down, if necessary.
866
+ dy = widget.bbox()[3] + 30 - self._canvas.coords(self._textline)[1]
867
+ if dy > 0:
868
+ for twidget in self._textwidgets:
869
+ twidget.move(0, dy)
870
+ self._canvas.move(self._textline, 0, dy)
871
+
872
+ self._animate_expand_frame(widget, colors)
873
+
874
+ def _makeroom(self, treeseg):
875
+ """
876
+ Make sure that no sibling tree bbox's overlap.
877
+ """
878
+ parent = treeseg.parent()
879
+ if not isinstance(parent, TreeSegmentWidget):
880
+ return
881
+
882
+ index = parent.subtrees().index(treeseg)
883
+
884
+ # Handle siblings to the right
885
+ rsiblings = parent.subtrees()[index + 1 :]
886
+ if rsiblings:
887
+ dx = treeseg.bbox()[2] - rsiblings[0].bbox()[0] + 10
888
+ for sibling in rsiblings:
889
+ sibling.move(dx, 0)
890
+
891
+ # Handle siblings to the left
892
+ if index > 0:
893
+ lsibling = parent.subtrees()[index - 1]
894
+ dx = max(0, lsibling.bbox()[2] - treeseg.bbox()[0] + 10)
895
+ treeseg.move(dx, 0)
896
+
897
+ # Keep working up the tree.
898
+ self._makeroom(parent)
899
+
900
+ def _animate_expand_frame(self, widget, colors):
901
+ if len(colors) > 0:
902
+ self._animating_lock = 1
903
+ widget["color"] = colors[0]
904
+ for subtree in widget.subtrees():
905
+ if isinstance(subtree, TreeSegmentWidget):
906
+ subtree.label()["color"] = colors[0]
907
+ else:
908
+ subtree["color"] = colors[0]
909
+ self._top.after(50, self._animate_expand_frame, widget, colors[1:])
910
+ else:
911
+ widget["color"] = "black"
912
+ for subtree in widget.subtrees():
913
+ if isinstance(subtree, TreeSegmentWidget):
914
+ subtree.label()["color"] = "black"
915
+ else:
916
+ subtree["color"] = "black"
917
+ self._redraw_quick()
918
+ widget.label()["color"] = "black"
919
+ self._animating_lock = 0
920
+ if self._autostep:
921
+ self._step()
922
+
923
+ def _animate_backtrack(self, treeloc):
924
+ # Flash red first, if we're animating.
925
+ if self._animation_frames.get() == 0:
926
+ colors = []
927
+ else:
928
+ colors = ["#a00000", "#000000", "#a00000"]
929
+ colors += [
930
+ "gray%d" % (10 * int(10 * x / (self._animation_frames.get())))
931
+ for x in range(1, self._animation_frames.get() + 1)
932
+ ]
933
+
934
+ widgets = [self._get(self._tree, treeloc).parent()]
935
+ for subtree in widgets[0].subtrees():
936
+ if isinstance(subtree, TreeSegmentWidget):
937
+ widgets.append(subtree.label())
938
+ else:
939
+ widgets.append(subtree)
940
+
941
+ self._animate_backtrack_frame(widgets, colors)
942
+
943
+ def _animate_backtrack_frame(self, widgets, colors):
944
+ if len(colors) > 0:
945
+ self._animating_lock = 1
946
+ for widget in widgets:
947
+ widget["color"] = colors[0]
948
+ self._top.after(50, self._animate_backtrack_frame, widgets, colors[1:])
949
+ else:
950
+ for widget in widgets[0].subtrees():
951
+ widgets[0].remove_child(widget)
952
+ widget.destroy()
953
+ self._redraw_quick()
954
+ self._animating_lock = 0
955
+ if self._autostep:
956
+ self._step()
957
+
958
+ def _animate_match_backtrack(self, treeloc):
959
+ widget = self._get(self._tree, treeloc)
960
+ node = widget.parent().label()
961
+ dy = (node.bbox()[3] - widget.bbox()[1] + 14) / max(
962
+ 1, self._animation_frames.get()
963
+ )
964
+ self._animate_match_backtrack_frame(self._animation_frames.get(), widget, dy)
965
+
966
+ def _animate_match(self, treeloc):
967
+ widget = self._get(self._tree, treeloc)
968
+
969
+ dy = (self._textwidgets[0].bbox()[1] - widget.bbox()[3] - 10.0) / max(
970
+ 1, self._animation_frames.get()
971
+ )
972
+ self._animate_match_frame(self._animation_frames.get(), widget, dy)
973
+
974
+ def _animate_match_frame(self, frame, widget, dy):
975
+ if frame > 0:
976
+ self._animating_lock = 1
977
+ widget.move(0, dy)
978
+ self._top.after(10, self._animate_match_frame, frame - 1, widget, dy)
979
+ else:
980
+ widget["color"] = "#006040"
981
+ self._redraw_quick()
982
+ self._animating_lock = 0
983
+ if self._autostep:
984
+ self._step()
985
+
986
+ def _animate_match_backtrack_frame(self, frame, widget, dy):
987
+ if frame > 0:
988
+ self._animating_lock = 1
989
+ widget.move(0, dy)
990
+ self._top.after(
991
+ 10, self._animate_match_backtrack_frame, frame - 1, widget, dy
992
+ )
993
+ else:
994
+ widget.parent().remove_child(widget)
995
+ widget.destroy()
996
+ self._animating_lock = 0
997
+ if self._autostep:
998
+ self._step()
999
+
1000
+ def edit_grammar(self, *e):
1001
+ CFGEditor(self._top, self._parser.grammar(), self.set_grammar)
1002
+
1003
+ def set_grammar(self, grammar):
1004
+ self._parser.set_grammar(grammar)
1005
+ self._productions = list(grammar.productions())
1006
+ self._prodlist.delete(0, "end")
1007
+ for production in self._productions:
1008
+ self._prodlist.insert("end", (" %s" % production))
1009
+
1010
+ def edit_sentence(self, *e):
1011
+ sentence = " ".join(self._sent)
1012
+ title = "Edit Text"
1013
+ instr = "Enter a new sentence to parse."
1014
+ EntryDialog(self._top, sentence, instr, self.set_sentence, title)
1015
+
1016
+ def set_sentence(self, sentence):
1017
+ self._sent = sentence.split() # [XX] use tagged?
1018
+ self.reset()
1019
+
1020
+
1021
+ def app():
1022
+ """
1023
+ Create a recursive descent parser demo, using a simple grammar and
1024
+ text.
1025
+ """
1026
+ from nltk.grammar import CFG
1027
+
1028
+ grammar = CFG.fromstring(
1029
+ """
1030
+ # Grammatical productions.
1031
+ S -> NP VP
1032
+ NP -> Det N PP | Det N
1033
+ VP -> V NP PP | V NP | V
1034
+ PP -> P NP
1035
+ # Lexical productions.
1036
+ NP -> 'I'
1037
+ Det -> 'the' | 'a'
1038
+ N -> 'man' | 'park' | 'dog' | 'telescope'
1039
+ V -> 'ate' | 'saw'
1040
+ P -> 'in' | 'under' | 'with'
1041
+ """
1042
+ )
1043
+
1044
+ sent = "the dog saw a man in the park".split()
1045
+
1046
+ RecursiveDescentApp(grammar, sent).mainloop()
1047
+
1048
+
1049
+ if __name__ == "__main__":
1050
+ app()
1051
+
1052
+ __all__ = ["app"]
loan-advisor-agent/.venv/Lib/site-packages/nltk/app/srparser_app.py ADDED
@@ -0,0 +1,937 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: Shift-Reduce Parser Application
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Author: Edward Loper <edloper@gmail.com>
5
+ # URL: <https://www.nltk.org/>
6
+ # For license information, see LICENSE.TXT
7
+
8
+ """
9
+ A graphical tool for exploring the shift-reduce parser.
10
+
11
+ The shift-reduce parser maintains a stack, which records the structure
12
+ of the portion of the text that has been parsed. The stack is
13
+ initially empty. Its contents are shown on the left side of the main
14
+ canvas.
15
+
16
+ On the right side of the main canvas is the remaining text. This is
17
+ the portion of the text which has not yet been considered by the
18
+ parser.
19
+
20
+ The parser builds up a tree structure for the text using two
21
+ operations:
22
+
23
+ - "shift" moves the first token from the remaining text to the top
24
+ of the stack. In the demo, the top of the stack is its right-hand
25
+ side.
26
+ - "reduce" uses a grammar production to combine the rightmost stack
27
+ elements into a single tree token.
28
+
29
+ You can control the parser's operation by using the "shift" and
30
+ "reduce" buttons; or you can use the "step" button to let the parser
31
+ automatically decide which operation to apply. The parser uses the
32
+ following rules to decide which operation to apply:
33
+
34
+ - Only shift if no reductions are available.
35
+ - If multiple reductions are available, then apply the reduction
36
+ whose CFG production is listed earliest in the grammar.
37
+
38
+ The "reduce" button applies the reduction whose CFG production is
39
+ listed earliest in the grammar. There are two ways to manually choose
40
+ which reduction to apply:
41
+
42
+ - Click on a CFG production from the list of available reductions,
43
+ on the left side of the main window. The reduction based on that
44
+ production will be applied to the top of the stack.
45
+ - Click on one of the stack elements. A popup window will appear,
46
+ containing all available reductions. Select one, and it will be
47
+ applied to the top of the stack.
48
+
49
+ Note that reductions can only be applied to the top of the stack.
50
+
51
+ Keyboard Shortcuts::
52
+ [Space]\t Perform the next shift or reduce operation
53
+ [s]\t Perform a shift operation
54
+ [r]\t Perform a reduction operation
55
+ [Ctrl-z]\t Undo most recent operation
56
+ [Delete]\t Reset the parser
57
+ [g]\t Show/hide available production list
58
+ [Ctrl-a]\t Toggle animations
59
+ [h]\t Help
60
+ [Ctrl-p]\t Print
61
+ [q]\t Quit
62
+
63
+ """
64
+
65
+ from tkinter import Button, Frame, IntVar, Label, Listbox, Menu, Scrollbar, Tk
66
+ from tkinter.font import Font
67
+
68
+ from nltk.draw import CFGEditor, TreeSegmentWidget, tree_to_treesegment
69
+ from nltk.draw.util import CanvasFrame, EntryDialog, ShowText, TextWidget
70
+ from nltk.parse import SteppingShiftReduceParser
71
+ from nltk.tree import Tree
72
+ from nltk.util import in_idle
73
+
74
+ """
75
+ Possible future improvements:
76
+ - button/window to change and/or select text. Just pop up a window
77
+ with an entry, and let them modify the text; and then retokenize
78
+ it? Maybe give a warning if it contains tokens whose types are
79
+ not in the grammar.
80
+ - button/window to change and/or select grammar. Select from
81
+ several alternative grammars? Or actually change the grammar? If
82
+ the later, then I'd want to define nltk.draw.cfg, which would be
83
+ responsible for that.
84
+ """
85
+
86
+
87
+ class ShiftReduceApp:
88
+ """
89
+ A graphical tool for exploring the shift-reduce parser. The tool
90
+ displays the parser's stack and the remaining text, and allows the
91
+ user to control the parser's operation. In particular, the user
92
+ can shift tokens onto the stack, and can perform reductions on the
93
+ top elements of the stack. A "step" button simply steps through
94
+ the parsing process, performing the operations that
95
+ ``nltk.parse.ShiftReduceParser`` would use.
96
+ """
97
+
98
+ def __init__(self, grammar, sent, trace=0):
99
+ self._sent = sent
100
+ self._parser = SteppingShiftReduceParser(grammar, trace)
101
+
102
+ # Set up the main window.
103
+ self._top = Tk()
104
+ self._top.title("Shift Reduce Parser Application")
105
+
106
+ # Animations. animating_lock is a lock to prevent the demo
107
+ # from performing new operations while it's animating.
108
+ self._animating_lock = 0
109
+ self._animate = IntVar(self._top)
110
+ self._animate.set(10) # = medium
111
+
112
+ # The user can hide the grammar.
113
+ self._show_grammar = IntVar(self._top)
114
+ self._show_grammar.set(1)
115
+
116
+ # Initialize fonts.
117
+ self._init_fonts(self._top)
118
+
119
+ # Set up key bindings.
120
+ self._init_bindings()
121
+
122
+ # Create the basic frames.
123
+ self._init_menubar(self._top)
124
+ self._init_buttons(self._top)
125
+ self._init_feedback(self._top)
126
+ self._init_grammar(self._top)
127
+ self._init_canvas(self._top)
128
+
129
+ # A popup menu for reducing.
130
+ self._reduce_menu = Menu(self._canvas, tearoff=0)
131
+
132
+ # Reset the demo, and set the feedback frame to empty.
133
+ self.reset()
134
+ self._lastoper1["text"] = ""
135
+
136
+ #########################################
137
+ ## Initialization Helpers
138
+ #########################################
139
+
140
+ def _init_fonts(self, root):
141
+ # See: <http://www.astro.washington.edu/owen/ROTKFolklore.html>
142
+ self._sysfont = Font(font=Button()["font"])
143
+ root.option_add("*Font", self._sysfont)
144
+
145
+ # TWhat's our font size (default=same as sysfont)
146
+ self._size = IntVar(root)
147
+ self._size.set(self._sysfont.cget("size"))
148
+
149
+ self._boldfont = Font(family="helvetica", weight="bold", size=self._size.get())
150
+ self._font = Font(family="helvetica", size=self._size.get())
151
+
152
+ def _init_grammar(self, parent):
153
+ # Grammar view.
154
+ self._prodframe = listframe = Frame(parent)
155
+ self._prodframe.pack(fill="both", side="left", padx=2)
156
+ self._prodlist_label = Label(
157
+ self._prodframe, font=self._boldfont, text="Available Reductions"
158
+ )
159
+ self._prodlist_label.pack()
160
+ self._prodlist = Listbox(
161
+ self._prodframe,
162
+ selectmode="single",
163
+ relief="groove",
164
+ background="white",
165
+ foreground="#909090",
166
+ font=self._font,
167
+ selectforeground="#004040",
168
+ selectbackground="#c0f0c0",
169
+ )
170
+
171
+ self._prodlist.pack(side="right", fill="both", expand=1)
172
+
173
+ self._productions = list(self._parser.grammar().productions())
174
+ for production in self._productions:
175
+ self._prodlist.insert("end", (" %s" % production))
176
+ self._prodlist.config(height=min(len(self._productions), 25))
177
+
178
+ # Add a scrollbar if there are more than 25 productions.
179
+ if 1: # len(self._productions) > 25:
180
+ listscroll = Scrollbar(self._prodframe, orient="vertical")
181
+ self._prodlist.config(yscrollcommand=listscroll.set)
182
+ listscroll.config(command=self._prodlist.yview)
183
+ listscroll.pack(side="left", fill="y")
184
+
185
+ # If they select a production, apply it.
186
+ self._prodlist.bind("<<ListboxSelect>>", self._prodlist_select)
187
+
188
+ # When they hover over a production, highlight it.
189
+ self._hover = -1
190
+ self._prodlist.bind("<Motion>", self._highlight_hover)
191
+ self._prodlist.bind("<Leave>", self._clear_hover)
192
+
193
+ def _init_bindings(self):
194
+ # Quit
195
+ self._top.bind("<Control-q>", self.destroy)
196
+ self._top.bind("<Control-x>", self.destroy)
197
+ self._top.bind("<Alt-q>", self.destroy)
198
+ self._top.bind("<Alt-x>", self.destroy)
199
+
200
+ # Ops (step, shift, reduce, undo)
201
+ self._top.bind("<space>", self.step)
202
+ self._top.bind("<s>", self.shift)
203
+ self._top.bind("<Alt-s>", self.shift)
204
+ self._top.bind("<Control-s>", self.shift)
205
+ self._top.bind("<r>", self.reduce)
206
+ self._top.bind("<Alt-r>", self.reduce)
207
+ self._top.bind("<Control-r>", self.reduce)
208
+ self._top.bind("<Delete>", self.reset)
209
+ self._top.bind("<u>", self.undo)
210
+ self._top.bind("<Alt-u>", self.undo)
211
+ self._top.bind("<Control-u>", self.undo)
212
+ self._top.bind("<Control-z>", self.undo)
213
+ self._top.bind("<BackSpace>", self.undo)
214
+
215
+ # Misc
216
+ self._top.bind("<Control-p>", self.postscript)
217
+ self._top.bind("<Control-h>", self.help)
218
+ self._top.bind("<F1>", self.help)
219
+ self._top.bind("<Control-g>", self.edit_grammar)
220
+ self._top.bind("<Control-t>", self.edit_sentence)
221
+
222
+ # Animation speed control
223
+ self._top.bind("-", lambda e, a=self._animate: a.set(20))
224
+ self._top.bind("=", lambda e, a=self._animate: a.set(10))
225
+ self._top.bind("+", lambda e, a=self._animate: a.set(4))
226
+
227
+ def _init_buttons(self, parent):
228
+ # Set up the frames.
229
+ self._buttonframe = buttonframe = Frame(parent)
230
+ buttonframe.pack(fill="none", side="bottom")
231
+ Button(
232
+ buttonframe,
233
+ text="Step",
234
+ background="#90c0d0",
235
+ foreground="black",
236
+ command=self.step,
237
+ ).pack(side="left")
238
+ Button(
239
+ buttonframe,
240
+ text="Shift",
241
+ underline=0,
242
+ background="#90f090",
243
+ foreground="black",
244
+ command=self.shift,
245
+ ).pack(side="left")
246
+ Button(
247
+ buttonframe,
248
+ text="Reduce",
249
+ underline=0,
250
+ background="#90f090",
251
+ foreground="black",
252
+ command=self.reduce,
253
+ ).pack(side="left")
254
+ Button(
255
+ buttonframe,
256
+ text="Undo",
257
+ underline=0,
258
+ background="#f0a0a0",
259
+ foreground="black",
260
+ command=self.undo,
261
+ ).pack(side="left")
262
+
263
+ def _init_menubar(self, parent):
264
+ menubar = Menu(parent)
265
+
266
+ filemenu = Menu(menubar, tearoff=0)
267
+ filemenu.add_command(
268
+ label="Reset Parser", underline=0, command=self.reset, accelerator="Del"
269
+ )
270
+ filemenu.add_command(
271
+ label="Print to Postscript",
272
+ underline=0,
273
+ command=self.postscript,
274
+ accelerator="Ctrl-p",
275
+ )
276
+ filemenu.add_command(
277
+ label="Exit", underline=1, command=self.destroy, accelerator="Ctrl-x"
278
+ )
279
+ menubar.add_cascade(label="File", underline=0, menu=filemenu)
280
+
281
+ editmenu = Menu(menubar, tearoff=0)
282
+ editmenu.add_command(
283
+ label="Edit Grammar",
284
+ underline=5,
285
+ command=self.edit_grammar,
286
+ accelerator="Ctrl-g",
287
+ )
288
+ editmenu.add_command(
289
+ label="Edit Text",
290
+ underline=5,
291
+ command=self.edit_sentence,
292
+ accelerator="Ctrl-t",
293
+ )
294
+ menubar.add_cascade(label="Edit", underline=0, menu=editmenu)
295
+
296
+ rulemenu = Menu(menubar, tearoff=0)
297
+ rulemenu.add_command(
298
+ label="Step", underline=1, command=self.step, accelerator="Space"
299
+ )
300
+ rulemenu.add_separator()
301
+ rulemenu.add_command(
302
+ label="Shift", underline=0, command=self.shift, accelerator="Ctrl-s"
303
+ )
304
+ rulemenu.add_command(
305
+ label="Reduce", underline=0, command=self.reduce, accelerator="Ctrl-r"
306
+ )
307
+ rulemenu.add_separator()
308
+ rulemenu.add_command(
309
+ label="Undo", underline=0, command=self.undo, accelerator="Ctrl-u"
310
+ )
311
+ menubar.add_cascade(label="Apply", underline=0, menu=rulemenu)
312
+
313
+ viewmenu = Menu(menubar, tearoff=0)
314
+ viewmenu.add_checkbutton(
315
+ label="Show Grammar",
316
+ underline=0,
317
+ variable=self._show_grammar,
318
+ command=self._toggle_grammar,
319
+ )
320
+ viewmenu.add_separator()
321
+ viewmenu.add_radiobutton(
322
+ label="Tiny",
323
+ variable=self._size,
324
+ underline=0,
325
+ value=10,
326
+ command=self.resize,
327
+ )
328
+ viewmenu.add_radiobutton(
329
+ label="Small",
330
+ variable=self._size,
331
+ underline=0,
332
+ value=12,
333
+ command=self.resize,
334
+ )
335
+ viewmenu.add_radiobutton(
336
+ label="Medium",
337
+ variable=self._size,
338
+ underline=0,
339
+ value=14,
340
+ command=self.resize,
341
+ )
342
+ viewmenu.add_radiobutton(
343
+ label="Large",
344
+ variable=self._size,
345
+ underline=0,
346
+ value=18,
347
+ command=self.resize,
348
+ )
349
+ viewmenu.add_radiobutton(
350
+ label="Huge",
351
+ variable=self._size,
352
+ underline=0,
353
+ value=24,
354
+ command=self.resize,
355
+ )
356
+ menubar.add_cascade(label="View", underline=0, menu=viewmenu)
357
+
358
+ animatemenu = Menu(menubar, tearoff=0)
359
+ animatemenu.add_radiobutton(
360
+ label="No Animation", underline=0, variable=self._animate, value=0
361
+ )
362
+ animatemenu.add_radiobutton(
363
+ label="Slow Animation",
364
+ underline=0,
365
+ variable=self._animate,
366
+ value=20,
367
+ accelerator="-",
368
+ )
369
+ animatemenu.add_radiobutton(
370
+ label="Normal Animation",
371
+ underline=0,
372
+ variable=self._animate,
373
+ value=10,
374
+ accelerator="=",
375
+ )
376
+ animatemenu.add_radiobutton(
377
+ label="Fast Animation",
378
+ underline=0,
379
+ variable=self._animate,
380
+ value=4,
381
+ accelerator="+",
382
+ )
383
+ menubar.add_cascade(label="Animate", underline=1, menu=animatemenu)
384
+
385
+ helpmenu = Menu(menubar, tearoff=0)
386
+ helpmenu.add_command(label="About", underline=0, command=self.about)
387
+ helpmenu.add_command(
388
+ label="Instructions", underline=0, command=self.help, accelerator="F1"
389
+ )
390
+ menubar.add_cascade(label="Help", underline=0, menu=helpmenu)
391
+
392
+ parent.config(menu=menubar)
393
+
394
+ def _init_feedback(self, parent):
395
+ self._feedbackframe = feedbackframe = Frame(parent)
396
+ feedbackframe.pack(fill="x", side="bottom", padx=3, pady=3)
397
+ self._lastoper_label = Label(
398
+ feedbackframe, text="Last Operation:", font=self._font
399
+ )
400
+ self._lastoper_label.pack(side="left")
401
+ lastoperframe = Frame(feedbackframe, relief="sunken", border=1)
402
+ lastoperframe.pack(fill="x", side="right", expand=1, padx=5)
403
+ self._lastoper1 = Label(
404
+ lastoperframe, foreground="#007070", background="#f0f0f0", font=self._font
405
+ )
406
+ self._lastoper2 = Label(
407
+ lastoperframe,
408
+ anchor="w",
409
+ width=30,
410
+ foreground="#004040",
411
+ background="#f0f0f0",
412
+ font=self._font,
413
+ )
414
+ self._lastoper1.pack(side="left")
415
+ self._lastoper2.pack(side="left", fill="x", expand=1)
416
+
417
+ def _init_canvas(self, parent):
418
+ self._cframe = CanvasFrame(
419
+ parent,
420
+ background="white",
421
+ width=525,
422
+ closeenough=10,
423
+ border=2,
424
+ relief="sunken",
425
+ )
426
+ self._cframe.pack(expand=1, fill="both", side="top", pady=2)
427
+ canvas = self._canvas = self._cframe.canvas()
428
+
429
+ self._stackwidgets = []
430
+ self._rtextwidgets = []
431
+ self._titlebar = canvas.create_rectangle(
432
+ 0, 0, 0, 0, fill="#c0f0f0", outline="black"
433
+ )
434
+ self._exprline = canvas.create_line(0, 0, 0, 0, dash=".")
435
+ self._stacktop = canvas.create_line(0, 0, 0, 0, fill="#408080")
436
+ size = self._size.get() + 4
437
+ self._stacklabel = TextWidget(
438
+ canvas, "Stack", color="#004040", font=self._boldfont
439
+ )
440
+ self._rtextlabel = TextWidget(
441
+ canvas, "Remaining Text", color="#004040", font=self._boldfont
442
+ )
443
+ self._cframe.add_widget(self._stacklabel)
444
+ self._cframe.add_widget(self._rtextlabel)
445
+
446
+ #########################################
447
+ ## Main draw procedure
448
+ #########################################
449
+
450
+ def _redraw(self):
451
+ scrollregion = self._canvas["scrollregion"].split()
452
+ (cx1, cy1, cx2, cy2) = (int(c) for c in scrollregion)
453
+
454
+ # Delete the old stack & rtext widgets.
455
+ for stackwidget in self._stackwidgets:
456
+ self._cframe.destroy_widget(stackwidget)
457
+ self._stackwidgets = []
458
+ for rtextwidget in self._rtextwidgets:
459
+ self._cframe.destroy_widget(rtextwidget)
460
+ self._rtextwidgets = []
461
+
462
+ # Position the titlebar & exprline
463
+ (x1, y1, x2, y2) = self._stacklabel.bbox()
464
+ y = y2 - y1 + 10
465
+ self._canvas.coords(self._titlebar, -5000, 0, 5000, y - 4)
466
+ self._canvas.coords(self._exprline, 0, y * 2 - 10, 5000, y * 2 - 10)
467
+
468
+ # Position the titlebar labels..
469
+ (x1, y1, x2, y2) = self._stacklabel.bbox()
470
+ self._stacklabel.move(5 - x1, 3 - y1)
471
+ (x1, y1, x2, y2) = self._rtextlabel.bbox()
472
+ self._rtextlabel.move(cx2 - x2 - 5, 3 - y1)
473
+
474
+ # Draw the stack.
475
+ stackx = 5
476
+ for tok in self._parser.stack():
477
+ if isinstance(tok, Tree):
478
+ attribs = {
479
+ "tree_color": "#4080a0",
480
+ "tree_width": 2,
481
+ "node_font": self._boldfont,
482
+ "node_color": "#006060",
483
+ "leaf_color": "#006060",
484
+ "leaf_font": self._font,
485
+ }
486
+ widget = tree_to_treesegment(self._canvas, tok, **attribs)
487
+ widget.label()["color"] = "#000000"
488
+ else:
489
+ widget = TextWidget(self._canvas, tok, color="#000000", font=self._font)
490
+ widget.bind_click(self._popup_reduce)
491
+ self._stackwidgets.append(widget)
492
+ self._cframe.add_widget(widget, stackx, y)
493
+ stackx = widget.bbox()[2] + 10
494
+
495
+ # Draw the remaining text.
496
+ rtextwidth = 0
497
+ for tok in self._parser.remaining_text():
498
+ widget = TextWidget(self._canvas, tok, color="#000000", font=self._font)
499
+ self._rtextwidgets.append(widget)
500
+ self._cframe.add_widget(widget, rtextwidth, y)
501
+ rtextwidth = widget.bbox()[2] + 4
502
+
503
+ # Allow enough room to shift the next token (for animations)
504
+ if len(self._rtextwidgets) > 0:
505
+ stackx += self._rtextwidgets[0].width()
506
+
507
+ # Move the remaining text to the correct location (keep it
508
+ # right-justified, when possible); and move the remaining text
509
+ # label, if necessary.
510
+ stackx = max(stackx, self._stacklabel.width() + 25)
511
+ rlabelwidth = self._rtextlabel.width() + 10
512
+ if stackx >= cx2 - max(rtextwidth, rlabelwidth):
513
+ cx2 = stackx + max(rtextwidth, rlabelwidth)
514
+ for rtextwidget in self._rtextwidgets:
515
+ rtextwidget.move(4 + cx2 - rtextwidth, 0)
516
+ self._rtextlabel.move(cx2 - self._rtextlabel.bbox()[2] - 5, 0)
517
+
518
+ midx = (stackx + cx2 - max(rtextwidth, rlabelwidth)) / 2
519
+ self._canvas.coords(self._stacktop, midx, 0, midx, 5000)
520
+ (x1, y1, x2, y2) = self._stacklabel.bbox()
521
+
522
+ # Set up binding to allow them to shift a token by dragging it.
523
+ if len(self._rtextwidgets) > 0:
524
+
525
+ def drag_shift(widget, midx=midx, self=self):
526
+ if widget.bbox()[0] < midx:
527
+ self.shift()
528
+ else:
529
+ self._redraw()
530
+
531
+ self._rtextwidgets[0].bind_drag(drag_shift)
532
+ self._rtextwidgets[0].bind_click(self.shift)
533
+
534
+ # Draw the stack top.
535
+ self._highlight_productions()
536
+
537
+ def _draw_stack_top(self, widget):
538
+ # hack..
539
+ midx = widget.bbox()[2] + 50
540
+ self._canvas.coords(self._stacktop, midx, 0, midx, 5000)
541
+
542
+ def _highlight_productions(self):
543
+ # Highlight the productions that can be reduced.
544
+ self._prodlist.selection_clear(0, "end")
545
+ for prod in self._parser.reducible_productions():
546
+ index = self._productions.index(prod)
547
+ self._prodlist.selection_set(index)
548
+
549
+ #########################################
550
+ ## Button Callbacks
551
+ #########################################
552
+
553
+ def destroy(self, *e):
554
+ if self._top is None:
555
+ return
556
+ self._top.destroy()
557
+ self._top = None
558
+
559
+ def reset(self, *e):
560
+ self._parser.initialize(self._sent)
561
+ self._lastoper1["text"] = "Reset App"
562
+ self._lastoper2["text"] = ""
563
+ self._redraw()
564
+
565
+ def step(self, *e):
566
+ if self.reduce():
567
+ return True
568
+ elif self.shift():
569
+ return True
570
+ else:
571
+ if list(self._parser.parses()):
572
+ self._lastoper1["text"] = "Finished:"
573
+ self._lastoper2["text"] = "Success"
574
+ else:
575
+ self._lastoper1["text"] = "Finished:"
576
+ self._lastoper2["text"] = "Failure"
577
+
578
+ def shift(self, *e):
579
+ if self._animating_lock:
580
+ return
581
+ if self._parser.shift():
582
+ tok = self._parser.stack()[-1]
583
+ self._lastoper1["text"] = "Shift:"
584
+ self._lastoper2["text"] = "%r" % tok
585
+ if self._animate.get():
586
+ self._animate_shift()
587
+ else:
588
+ self._redraw()
589
+ return True
590
+ return False
591
+
592
+ def reduce(self, *e):
593
+ if self._animating_lock:
594
+ return
595
+ production = self._parser.reduce()
596
+ if production:
597
+ self._lastoper1["text"] = "Reduce:"
598
+ self._lastoper2["text"] = "%s" % production
599
+ if self._animate.get():
600
+ self._animate_reduce()
601
+ else:
602
+ self._redraw()
603
+ return production
604
+
605
+ def undo(self, *e):
606
+ if self._animating_lock:
607
+ return
608
+ if self._parser.undo():
609
+ self._redraw()
610
+
611
+ def postscript(self, *e):
612
+ self._cframe.print_to_file()
613
+
614
+ def mainloop(self, *args, **kwargs):
615
+ """
616
+ Enter the Tkinter mainloop. This function must be called if
617
+ this demo is created from a non-interactive program (e.g.
618
+ from a secript); otherwise, the demo will close as soon as
619
+ the script completes.
620
+ """
621
+ if in_idle():
622
+ return
623
+ self._top.mainloop(*args, **kwargs)
624
+
625
+ #########################################
626
+ ## Menubar callbacks
627
+ #########################################
628
+
629
+ def resize(self, size=None):
630
+ if size is not None:
631
+ self._size.set(size)
632
+ size = self._size.get()
633
+ self._font.configure(size=-(abs(size)))
634
+ self._boldfont.configure(size=-(abs(size)))
635
+ self._sysfont.configure(size=-(abs(size)))
636
+
637
+ # self._stacklabel['font'] = ('helvetica', -size-4, 'bold')
638
+ # self._rtextlabel['font'] = ('helvetica', -size-4, 'bold')
639
+ # self._lastoper_label['font'] = ('helvetica', -size)
640
+ # self._lastoper1['font'] = ('helvetica', -size)
641
+ # self._lastoper2['font'] = ('helvetica', -size)
642
+ # self._prodlist['font'] = ('helvetica', -size)
643
+ # self._prodlist_label['font'] = ('helvetica', -size-2, 'bold')
644
+ self._redraw()
645
+
646
+ def help(self, *e):
647
+ # The default font's not very legible; try using 'fixed' instead.
648
+ try:
649
+ ShowText(
650
+ self._top,
651
+ "Help: Shift-Reduce Parser Application",
652
+ (__doc__ or "").strip(),
653
+ width=75,
654
+ font="fixed",
655
+ )
656
+ except Exception:
657
+ ShowText(
658
+ self._top,
659
+ "Help: Shift-Reduce Parser Application",
660
+ (__doc__ or "").strip(),
661
+ width=75,
662
+ )
663
+
664
+ def about(self, *e):
665
+ ABOUT = "NLTK Shift-Reduce Parser Application\n" + "Written by Edward Loper"
666
+ TITLE = "About: Shift-Reduce Parser Application"
667
+ try:
668
+ from tkinter.messagebox import Message
669
+
670
+ Message(message=ABOUT, title=TITLE).show()
671
+ except Exception:
672
+ ShowText(self._top, TITLE, ABOUT)
673
+
674
+ def edit_grammar(self, *e):
675
+ CFGEditor(self._top, self._parser.grammar(), self.set_grammar)
676
+
677
+ def set_grammar(self, grammar):
678
+ self._parser.set_grammar(grammar)
679
+ self._productions = list(grammar.productions())
680
+ self._prodlist.delete(0, "end")
681
+ for production in self._productions:
682
+ self._prodlist.insert("end", (" %s" % production))
683
+
684
+ def edit_sentence(self, *e):
685
+ sentence = " ".join(self._sent)
686
+ title = "Edit Text"
687
+ instr = "Enter a new sentence to parse."
688
+ EntryDialog(self._top, sentence, instr, self.set_sentence, title)
689
+
690
+ def set_sentence(self, sent):
691
+ self._sent = sent.split() # [XX] use tagged?
692
+ self.reset()
693
+
694
+ #########################################
695
+ ## Reduce Production Selection
696
+ #########################################
697
+
698
+ def _toggle_grammar(self, *e):
699
+ if self._show_grammar.get():
700
+ self._prodframe.pack(
701
+ fill="both", side="left", padx=2, after=self._feedbackframe
702
+ )
703
+ self._lastoper1["text"] = "Show Grammar"
704
+ else:
705
+ self._prodframe.pack_forget()
706
+ self._lastoper1["text"] = "Hide Grammar"
707
+ self._lastoper2["text"] = ""
708
+
709
+ def _prodlist_select(self, event):
710
+ selection = self._prodlist.curselection()
711
+ if len(selection) != 1:
712
+ return
713
+ index = int(selection[0])
714
+ production = self._parser.reduce(self._productions[index])
715
+ if production:
716
+ self._lastoper1["text"] = "Reduce:"
717
+ self._lastoper2["text"] = "%s" % production
718
+ if self._animate.get():
719
+ self._animate_reduce()
720
+ else:
721
+ self._redraw()
722
+ else:
723
+ # Reset the production selections.
724
+ self._prodlist.selection_clear(0, "end")
725
+ for prod in self._parser.reducible_productions():
726
+ index = self._productions.index(prod)
727
+ self._prodlist.selection_set(index)
728
+
729
+ def _popup_reduce(self, widget):
730
+ # Remove old commands.
731
+ productions = self._parser.reducible_productions()
732
+ if len(productions) == 0:
733
+ return
734
+
735
+ self._reduce_menu.delete(0, "end")
736
+ for production in productions:
737
+ self._reduce_menu.add_command(label=str(production), command=self.reduce)
738
+ self._reduce_menu.post(
739
+ self._canvas.winfo_pointerx(), self._canvas.winfo_pointery()
740
+ )
741
+
742
+ #########################################
743
+ ## Animations
744
+ #########################################
745
+
746
+ def _animate_shift(self):
747
+ # What widget are we shifting?
748
+ widget = self._rtextwidgets[0]
749
+
750
+ # Where are we shifting from & to?
751
+ right = widget.bbox()[0]
752
+ if len(self._stackwidgets) == 0:
753
+ left = 5
754
+ else:
755
+ left = self._stackwidgets[-1].bbox()[2] + 10
756
+
757
+ # Start animating.
758
+ dt = self._animate.get()
759
+ dx = (left - right) * 1.0 / dt
760
+ self._animate_shift_frame(dt, widget, dx)
761
+
762
+ def _animate_shift_frame(self, frame, widget, dx):
763
+ if frame > 0:
764
+ self._animating_lock = 1
765
+ widget.move(dx, 0)
766
+ self._top.after(10, self._animate_shift_frame, frame - 1, widget, dx)
767
+ else:
768
+ # but: stacktop??
769
+
770
+ # Shift the widget to the stack.
771
+ del self._rtextwidgets[0]
772
+ self._stackwidgets.append(widget)
773
+ self._animating_lock = 0
774
+
775
+ # Display the available productions.
776
+ self._draw_stack_top(widget)
777
+ self._highlight_productions()
778
+
779
+ def _animate_reduce(self):
780
+ # What widgets are we shifting?
781
+ numwidgets = len(self._parser.stack()[-1]) # number of children
782
+ widgets = self._stackwidgets[-numwidgets:]
783
+
784
+ # How far are we moving?
785
+ if isinstance(widgets[0], TreeSegmentWidget):
786
+ ydist = 15 + widgets[0].label().height()
787
+ else:
788
+ ydist = 15 + widgets[0].height()
789
+
790
+ # Start animating.
791
+ dt = self._animate.get()
792
+ dy = ydist * 2.0 / dt
793
+ self._animate_reduce_frame(dt / 2, widgets, dy)
794
+
795
+ def _animate_reduce_frame(self, frame, widgets, dy):
796
+ if frame > 0:
797
+ self._animating_lock = 1
798
+ for widget in widgets:
799
+ widget.move(0, dy)
800
+ self._top.after(10, self._animate_reduce_frame, frame - 1, widgets, dy)
801
+ else:
802
+ del self._stackwidgets[-len(widgets) :]
803
+ for widget in widgets:
804
+ self._cframe.remove_widget(widget)
805
+ tok = self._parser.stack()[-1]
806
+ if not isinstance(tok, Tree):
807
+ raise ValueError()
808
+ label = TextWidget(
809
+ self._canvas, str(tok.label()), color="#006060", font=self._boldfont
810
+ )
811
+ widget = TreeSegmentWidget(self._canvas, label, widgets, width=2)
812
+ (x1, y1, x2, y2) = self._stacklabel.bbox()
813
+ y = y2 - y1 + 10
814
+ if not self._stackwidgets:
815
+ x = 5
816
+ else:
817
+ x = self._stackwidgets[-1].bbox()[2] + 10
818
+ self._cframe.add_widget(widget, x, y)
819
+ self._stackwidgets.append(widget)
820
+
821
+ # Display the available productions.
822
+ self._draw_stack_top(widget)
823
+ self._highlight_productions()
824
+
825
+ # # Delete the old widgets..
826
+ # del self._stackwidgets[-len(widgets):]
827
+ # for widget in widgets:
828
+ # self._cframe.destroy_widget(widget)
829
+ #
830
+ # # Make a new one.
831
+ # tok = self._parser.stack()[-1]
832
+ # if isinstance(tok, Tree):
833
+ # attribs = {'tree_color': '#4080a0', 'tree_width': 2,
834
+ # 'node_font': bold, 'node_color': '#006060',
835
+ # 'leaf_color': '#006060', 'leaf_font':self._font}
836
+ # widget = tree_to_treesegment(self._canvas, tok.type(),
837
+ # **attribs)
838
+ # widget.node()['color'] = '#000000'
839
+ # else:
840
+ # widget = TextWidget(self._canvas, tok.type(),
841
+ # color='#000000', font=self._font)
842
+ # widget.bind_click(self._popup_reduce)
843
+ # (x1, y1, x2, y2) = self._stacklabel.bbox()
844
+ # y = y2-y1+10
845
+ # if not self._stackwidgets: x = 5
846
+ # else: x = self._stackwidgets[-1].bbox()[2] + 10
847
+ # self._cframe.add_widget(widget, x, y)
848
+ # self._stackwidgets.append(widget)
849
+
850
+ # self._redraw()
851
+ self._animating_lock = 0
852
+
853
+ #########################################
854
+ ## Hovering.
855
+ #########################################
856
+
857
+ def _highlight_hover(self, event):
858
+ # What production are we hovering over?
859
+ index = self._prodlist.nearest(event.y)
860
+ if self._hover == index:
861
+ return
862
+
863
+ # Clear any previous hover highlighting.
864
+ self._clear_hover()
865
+
866
+ # If the production corresponds to an available reduction,
867
+ # highlight the stack.
868
+ selection = [int(s) for s in self._prodlist.curselection()]
869
+ if index in selection:
870
+ rhslen = len(self._productions[index].rhs())
871
+ for stackwidget in self._stackwidgets[-rhslen:]:
872
+ if isinstance(stackwidget, TreeSegmentWidget):
873
+ stackwidget.label()["color"] = "#00a000"
874
+ else:
875
+ stackwidget["color"] = "#00a000"
876
+
877
+ # Remember what production we're hovering over.
878
+ self._hover = index
879
+
880
+ def _clear_hover(self, *event):
881
+ # Clear any previous hover highlighting.
882
+ if self._hover == -1:
883
+ return
884
+ self._hover = -1
885
+ for stackwidget in self._stackwidgets:
886
+ if isinstance(stackwidget, TreeSegmentWidget):
887
+ stackwidget.label()["color"] = "black"
888
+ else:
889
+ stackwidget["color"] = "black"
890
+
891
+
892
+ def app():
893
+ """
894
+ Create a shift reduce parser app, using a simple grammar and
895
+ text.
896
+ """
897
+
898
+ from nltk.grammar import CFG, Nonterminal, Production
899
+
900
+ nonterminals = "S VP NP PP P N Name V Det"
901
+ (S, VP, NP, PP, P, N, Name, V, Det) = (Nonterminal(s) for s in nonterminals.split())
902
+
903
+ productions = (
904
+ # Syntactic Productions
905
+ Production(S, [NP, VP]),
906
+ Production(NP, [Det, N]),
907
+ Production(NP, [NP, PP]),
908
+ Production(VP, [VP, PP]),
909
+ Production(VP, [V, NP, PP]),
910
+ Production(VP, [V, NP]),
911
+ Production(PP, [P, NP]),
912
+ # Lexical Productions
913
+ Production(NP, ["I"]),
914
+ Production(Det, ["the"]),
915
+ Production(Det, ["a"]),
916
+ Production(N, ["man"]),
917
+ Production(V, ["saw"]),
918
+ Production(P, ["in"]),
919
+ Production(P, ["with"]),
920
+ Production(N, ["park"]),
921
+ Production(N, ["dog"]),
922
+ Production(N, ["statue"]),
923
+ Production(Det, ["my"]),
924
+ )
925
+
926
+ grammar = CFG(S, productions)
927
+
928
+ # tokenize the sentence
929
+ sent = "my dog saw a man in the park with a statue".split()
930
+
931
+ ShiftReduceApp(grammar, sent).mainloop()
932
+
933
+
934
+ if __name__ == "__main__":
935
+ app()
936
+
937
+ __all__ = ["app"]
loan-advisor-agent/.venv/Lib/site-packages/nltk/app/wordfreq_app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: Wordfreq Application
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Author: Sumukh Ghodke <sghodke@csse.unimelb.edu.au>
5
+ # URL: <https://www.nltk.org/>
6
+ # For license information, see LICENSE.TXT
7
+
8
+ from matplotlib import pylab
9
+
10
+ from nltk.corpus import gutenberg
11
+ from nltk.text import Text
12
+
13
+
14
+ def plot_word_freq_dist(text):
15
+ fd = text.vocab()
16
+
17
+ samples = [item for item, _ in fd.most_common(50)]
18
+ values = [fd[sample] for sample in samples]
19
+ values = [sum(values[: i + 1]) * 100.0 / fd.N() for i in range(len(values))]
20
+ pylab.title(text.name)
21
+ pylab.xlabel("Samples")
22
+ pylab.ylabel("Cumulative Percentage")
23
+ pylab.plot(values)
24
+ pylab.xticks(range(len(samples)), [str(s) for s in samples], rotation=90)
25
+ pylab.show()
26
+
27
+
28
+ def app():
29
+ t1 = Text(gutenberg.words("melville-moby_dick.txt"))
30
+ plot_word_freq_dist(t1)
31
+
32
+
33
+ if __name__ == "__main__":
34
+ app()
35
+
36
+ __all__ = ["app"]
loan-advisor-agent/.venv/Lib/site-packages/nltk/app/wordnet_app.py ADDED
@@ -0,0 +1,1003 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: WordNet Browser Application
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Author: Jussi Salmela <jtsalmela@users.sourceforge.net>
5
+ # Paul Bone <pbone@students.csse.unimelb.edu.au>
6
+ # URL: <https://www.nltk.org/>
7
+ # For license information, see LICENSE.TXT
8
+
9
+ """
10
+ A WordNet Browser application which launches the default browser
11
+ (if it is not already running) and opens a new tab with a connection
12
+ to http://localhost:port/ . It also starts an HTTP server on the
13
+ specified port and begins serving browser requests. The default
14
+ port is 8000. (For command-line help, run "python wordnet -h")
15
+ This application requires that the user's web browser supports
16
+ Javascript.
17
+
18
+ BrowServer is a server for browsing the NLTK Wordnet database It first
19
+ launches a browser client to be used for browsing and then starts
20
+ serving the requests of that and maybe other clients
21
+
22
+ Usage::
23
+
24
+ browserver.py -h
25
+ browserver.py [-s] [-p <port>]
26
+
27
+ Options::
28
+
29
+ -h or --help
30
+ Display this help message.
31
+
32
+ -l <file> or --log-file <file>
33
+ Logs messages to the given file, If this option is not specified
34
+ messages are silently dropped.
35
+
36
+ -p <port> or --port <port>
37
+ Run the web server on this TCP port, defaults to 8000.
38
+
39
+ -s or --server-mode
40
+ Do not start a web browser, and do not allow a user to
41
+ shutdown the server through the web interface.
42
+ """
43
+ # TODO: throughout this package variable names and docstrings need
44
+ # modifying to be compliant with NLTK's coding standards. Tests also
45
+ # need to be develop to ensure this continues to work in the face of
46
+ # changes to other NLTK packages.
47
+
48
+ import base64
49
+ import copy
50
+ import getopt
51
+ import html
52
+ import io
53
+ import os
54
+ import pickle
55
+ import sys
56
+ import threading
57
+ import time
58
+ import webbrowser
59
+ from collections import defaultdict
60
+ from http.server import BaseHTTPRequestHandler, HTTPServer
61
+
62
+ # Allow this program to run inside the NLTK source tree.
63
+ from sys import argv
64
+ from urllib.parse import unquote_plus
65
+
66
+ from nltk.corpus import wordnet as wn
67
+ from nltk.corpus.reader.wordnet import Lemma, Synset
68
+ from nltk.picklesec import RestrictedUnpickler
69
+
70
+ firstClient = True
71
+
72
+ # True if we're not also running a web browser. The value f server_mode
73
+ # gets set by demo().
74
+ server_mode = None
75
+
76
+ # If set this is a file object for writing log messages.
77
+ logfile = None
78
+
79
+
80
+ class MyServerHandler(BaseHTTPRequestHandler):
81
+ def do_HEAD(self):
82
+ self.send_head()
83
+
84
+ def do_GET(self):
85
+ global firstClient
86
+ sp = self.path[1:]
87
+ if unquote_plus(sp) == "SHUTDOWN THE SERVER":
88
+ if server_mode:
89
+ page = "Server must be killed with SIGTERM."
90
+ type = "text/plain"
91
+ else:
92
+ print("Server shutting down!")
93
+ os._exit(0)
94
+
95
+ elif sp == "": # First request.
96
+ type = "text/html"
97
+ if not server_mode and firstClient:
98
+ firstClient = False
99
+ page = get_static_index_page(True)
100
+ else:
101
+ page = get_static_index_page(False)
102
+ word = "green"
103
+
104
+ elif sp.endswith(".html"): # Trying to fetch a HTML file TODO:
105
+ type = "text/html"
106
+ usp = unquote_plus(sp)
107
+ if usp == "NLTK Wordnet Browser Database Info.html":
108
+ word = "* Database Info *"
109
+ if os.path.isfile(usp):
110
+ with open(usp) as infile:
111
+ page = infile.read()
112
+ else:
113
+ page = (
114
+ (html_header % word) + "<p>The database info file:"
115
+ "<p><b>"
116
+ + usp
117
+ + "</b>"
118
+ + "<p>was not found. Run this:"
119
+ + "<p><b>python dbinfo_html.py</b>"
120
+ + "<p>to produce it."
121
+ + html_trailer
122
+ )
123
+ else:
124
+ # Handle files here.
125
+ word = sp
126
+ try:
127
+ page = get_static_page_by_path(usp)
128
+ except FileNotFoundError:
129
+ page = "Internal error: Path for static page '%s' is unknown" % usp
130
+ # Set type to plain to prevent XSS by printing the path as HTML
131
+ type = "text/plain"
132
+ elif sp.startswith("search"):
133
+ # This doesn't seem to work with MWEs.
134
+ type = "text/html"
135
+ parts = (sp.split("?")[1]).split("&")
136
+ word = html.escape(
137
+ [
138
+ p.split("=")[1].replace("+", " ")
139
+ for p in parts
140
+ if p.startswith("nextWord")
141
+ ][0]
142
+ )
143
+ page, word = page_from_word(word)
144
+ elif sp.startswith("lookup_"):
145
+ # TODO add a variation of this that takes a non ecoded word or MWE.
146
+ type = "text/html"
147
+ sp = sp[len("lookup_") :]
148
+ page, word = page_from_href(sp)
149
+ elif sp == "start_page":
150
+ # if this is the first request we should display help
151
+ # information, and possibly set a default word.
152
+ type = "text/html"
153
+ page, word = page_from_word("wordnet")
154
+ else:
155
+ type = "text/plain"
156
+ page = "Could not parse request: '%s'" % sp
157
+
158
+ # Send result.
159
+ self.send_head(type)
160
+ self.wfile.write(page.encode("utf8"))
161
+
162
+ def send_head(self, type=None):
163
+ self.send_response(200)
164
+ self.send_header("Content-type", type)
165
+ self.end_headers()
166
+
167
+ def log_message(self, format, *args):
168
+ global logfile
169
+
170
+ if logfile:
171
+ logfile.write(
172
+ "%s - - [%s] %s\n"
173
+ % (self.address_string(), self.log_date_time_string(), format % args)
174
+ )
175
+
176
+
177
+ def get_unique_counter_from_url(sp):
178
+ """
179
+ Extract the unique counter from the URL if it has one. Otherwise return
180
+ null.
181
+ """
182
+ pos = sp.rfind("%23")
183
+ if pos != -1:
184
+ return int(sp[(pos + 3) :])
185
+ else:
186
+ return None
187
+
188
+
189
+ def wnb(port=8000, runBrowser=True, logfilename=None):
190
+ """
191
+ Run NLTK Wordnet Browser Server.
192
+
193
+ :param port: The port number for the server to listen on, defaults to
194
+ 8000
195
+ :type port: int
196
+
197
+ :param runBrowser: True to start a web browser and point it at the web
198
+ server.
199
+ :type runBrowser: bool
200
+ """
201
+ # The webbrowser module is unpredictable, typically it blocks if it uses
202
+ # a console web browser, and doesn't block if it uses a GUI webbrowser,
203
+ # so we need to force it to have a clear correct behaviour.
204
+ #
205
+ # Normally the server should run for as long as the user wants. they
206
+ # should ideally be able to control this from the UI by closing the
207
+ # window or tab. Second best would be clicking a button to say
208
+ # 'Shutdown' that first shutsdown the server and closes the window or
209
+ # tab, or exits the text-mode browser. Both of these are unfreasable.
210
+ #
211
+ # The next best alternative is to start the server, have it close when
212
+ # it receives SIGTERM (default), and run the browser as well. The user
213
+ # may have to shutdown both programs.
214
+ #
215
+ # Since webbrowser may block, and the webserver will block, we must run
216
+ # them in separate threads.
217
+ #
218
+ global server_mode, logfile
219
+ server_mode = not runBrowser
220
+
221
+ # Setup logging.
222
+ if logfilename:
223
+ try:
224
+ logfile = open(logfilename, "a", 1) # 1 means 'line buffering'
225
+ except OSError as e:
226
+ sys.stderr.write("Couldn't open %s for writing: %s", logfilename, e)
227
+ sys.exit(1)
228
+ else:
229
+ logfile = None
230
+
231
+ # Compute URL and start web browser
232
+ url = "http://localhost:" + str(port)
233
+
234
+ server_ready = None
235
+ browser_thread = None
236
+
237
+ if runBrowser:
238
+ server_ready = threading.Event()
239
+ browser_thread = startBrowser(url, server_ready)
240
+
241
+ # Start the server. Bind to localhost only to prevent remote access
242
+ # and unauthenticated shutdown via /SHUTDOWN%20THE%20SERVER.
243
+ server = HTTPServer(("127.0.0.1", port), MyServerHandler)
244
+ if logfile:
245
+ logfile.write("NLTK Wordnet browser server running serving: %s\n" % url)
246
+ if runBrowser:
247
+ server_ready.set()
248
+
249
+ try:
250
+ server.serve_forever()
251
+ except KeyboardInterrupt:
252
+ pass
253
+
254
+ if runBrowser:
255
+ browser_thread.join()
256
+
257
+ if logfile:
258
+ logfile.close()
259
+
260
+
261
+ def startBrowser(url, server_ready):
262
+ def run():
263
+ server_ready.wait()
264
+ time.sleep(1) # Wait a little bit more, there's still the chance of
265
+ # a race condition.
266
+ webbrowser.open(url, new=2, autoraise=1)
267
+
268
+ t = threading.Thread(target=run)
269
+ t.start()
270
+ return t
271
+
272
+
273
+ #####################################################################
274
+ # Utilities
275
+ #####################################################################
276
+
277
+
278
+ """
279
+ WordNet Browser Utilities.
280
+
281
+ This provides a backend to both wxbrowse and browserver.py.
282
+ """
283
+
284
+ ################################################################################
285
+ #
286
+ # Main logic for wordnet browser.
287
+ #
288
+
289
+
290
+ # This is wrapped inside a function since wn is only available if the
291
+ # WordNet corpus is installed.
292
+ def _pos_tuples():
293
+ return [
294
+ (wn.NOUN, "N", "noun"),
295
+ (wn.VERB, "V", "verb"),
296
+ (wn.ADJ, "J", "adj"),
297
+ (wn.ADV, "R", "adv"),
298
+ ]
299
+
300
+
301
+ def _pos_match(pos_tuple):
302
+ """
303
+ This function returns the complete pos tuple for the partial pos
304
+ tuple given to it. It attempts to match it against the first
305
+ non-null component of the given pos tuple.
306
+ """
307
+ if pos_tuple[0] == "s":
308
+ pos_tuple = ("a", pos_tuple[1], pos_tuple[2])
309
+ for n, x in enumerate(pos_tuple):
310
+ if x is not None:
311
+ break
312
+ for pt in _pos_tuples():
313
+ if pt[n] == pos_tuple[n]:
314
+ return pt
315
+ return None
316
+
317
+
318
+ HYPONYM = 0
319
+ HYPERNYM = 1
320
+ CLASS_REGIONAL = 2
321
+ PART_HOLONYM = 3
322
+ PART_MERONYM = 4
323
+ ATTRIBUTE = 5
324
+ SUBSTANCE_HOLONYM = 6
325
+ SUBSTANCE_MERONYM = 7
326
+ MEMBER_HOLONYM = 8
327
+ MEMBER_MERONYM = 9
328
+ VERB_GROUP = 10
329
+ INSTANCE_HYPONYM = 12
330
+ INSTANCE_HYPERNYM = 13
331
+ CAUSE = 14
332
+ ALSO_SEE = 15
333
+ SIMILAR = 16
334
+ ENTAILMENT = 17
335
+ ANTONYM = 18
336
+ FRAMES = 19
337
+ PERTAINYM = 20
338
+
339
+ CLASS_CATEGORY = 21
340
+ CLASS_USAGE = 22
341
+ CLASS_REGIONAL = 23
342
+ CLASS_USAGE = 24
343
+ CLASS_CATEGORY = 11
344
+
345
+ DERIVATIONALLY_RELATED_FORM = 25
346
+
347
+ INDIRECT_HYPERNYMS = 26
348
+
349
+
350
+ def lemma_property(word, synset, func):
351
+ def flattern(l):
352
+ if l == []:
353
+ return []
354
+ else:
355
+ return l[0] + flattern(l[1:])
356
+
357
+ return flattern([func(l) for l in synset.lemmas() if l.name == word])
358
+
359
+
360
+ def rebuild_tree(orig_tree):
361
+ node = orig_tree[0]
362
+ children = orig_tree[1:]
363
+ return (node, [rebuild_tree(t) for t in children])
364
+
365
+
366
+ def get_relations_data(word, synset):
367
+ """
368
+ Get synset relations data for a synset. Note that this doesn't
369
+ yet support things such as full hyponym vs direct hyponym.
370
+ """
371
+ if synset.pos() == wn.NOUN:
372
+ return (
373
+ (HYPONYM, "Hyponyms", synset.hyponyms()),
374
+ (INSTANCE_HYPONYM, "Instance hyponyms", synset.instance_hyponyms()),
375
+ (HYPERNYM, "Direct hypernyms", synset.hypernyms()),
376
+ (
377
+ INDIRECT_HYPERNYMS,
378
+ "Indirect hypernyms",
379
+ rebuild_tree(synset.tree(lambda x: x.hypernyms()))[1],
380
+ ),
381
+ # hypernyms', 'Sister terms',
382
+ (INSTANCE_HYPERNYM, "Instance hypernyms", synset.instance_hypernyms()),
383
+ # (CLASS_REGIONAL, ['domain term region'], ),
384
+ (PART_HOLONYM, "Part holonyms", synset.part_holonyms()),
385
+ (PART_MERONYM, "Part meronyms", synset.part_meronyms()),
386
+ (SUBSTANCE_HOLONYM, "Substance holonyms", synset.substance_holonyms()),
387
+ (SUBSTANCE_MERONYM, "Substance meronyms", synset.substance_meronyms()),
388
+ (MEMBER_HOLONYM, "Member holonyms", synset.member_holonyms()),
389
+ (MEMBER_MERONYM, "Member meronyms", synset.member_meronyms()),
390
+ (ATTRIBUTE, "Attributes", synset.attributes()),
391
+ (ANTONYM, "Antonyms", lemma_property(word, synset, lambda l: l.antonyms())),
392
+ (
393
+ DERIVATIONALLY_RELATED_FORM,
394
+ "Derivationally related form",
395
+ lemma_property(
396
+ word, synset, lambda l: l.derivationally_related_forms()
397
+ ),
398
+ ),
399
+ )
400
+ elif synset.pos() == wn.VERB:
401
+ return (
402
+ (ANTONYM, "Antonym", lemma_property(word, synset, lambda l: l.antonyms())),
403
+ (HYPONYM, "Hyponym", synset.hyponyms()),
404
+ (HYPERNYM, "Direct hypernyms", synset.hypernyms()),
405
+ (
406
+ INDIRECT_HYPERNYMS,
407
+ "Indirect hypernyms",
408
+ rebuild_tree(synset.tree(lambda x: x.hypernyms()))[1],
409
+ ),
410
+ (ENTAILMENT, "Entailments", synset.entailments()),
411
+ (CAUSE, "Causes", synset.causes()),
412
+ (ALSO_SEE, "Also see", synset.also_sees()),
413
+ (VERB_GROUP, "Verb Groups", synset.verb_groups()),
414
+ (
415
+ DERIVATIONALLY_RELATED_FORM,
416
+ "Derivationally related form",
417
+ lemma_property(
418
+ word, synset, lambda l: l.derivationally_related_forms()
419
+ ),
420
+ ),
421
+ )
422
+ elif synset.pos() == wn.ADJ or synset.pos() == wn.ADJ_SAT:
423
+ return (
424
+ (ANTONYM, "Antonym", lemma_property(word, synset, lambda l: l.antonyms())),
425
+ (SIMILAR, "Similar to", synset.similar_tos()),
426
+ # Participle of verb - not supported by corpus
427
+ (
428
+ PERTAINYM,
429
+ "Pertainyms",
430
+ lemma_property(word, synset, lambda l: l.pertainyms()),
431
+ ),
432
+ (ATTRIBUTE, "Attributes", synset.attributes()),
433
+ (ALSO_SEE, "Also see", synset.also_sees()),
434
+ )
435
+ elif synset.pos() == wn.ADV:
436
+ # This is weird. adverbs such as 'quick' and 'fast' don't seem
437
+ # to have antonyms returned by the corpus.a
438
+ return (
439
+ (ANTONYM, "Antonym", lemma_property(word, synset, lambda l: l.antonyms())),
440
+ )
441
+ # Derived from adjective - not supported by corpus
442
+ else:
443
+ raise TypeError("Unhandled synset POS type: " + str(synset.pos()))
444
+
445
+
446
+ html_header = """
447
+ <!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN'
448
+ 'http://www.w3.org/TR/html4/strict.dtd'>
449
+ <html>
450
+ <head>
451
+ <meta name='generator' content=
452
+ 'HTML Tidy for Windows (vers 14 February 2006), see www.w3.org'>
453
+ <meta http-equiv='Content-Type' content=
454
+ 'text/html; charset=us-ascii'>
455
+ <title>NLTK Wordnet Browser display of: %s</title></head>
456
+ <body bgcolor='#F5F5F5' text='#000000'>
457
+ """
458
+ html_trailer = """
459
+ </body>
460
+ </html>
461
+ """
462
+
463
+ explanation = """
464
+ <h3>Search Help</h3>
465
+ <ul><li>The display below the line is an example of the output the browser
466
+ shows you when you enter a search word. The search word was <b>green</b>.</li>
467
+ <li>The search result shows for different parts of speech the <b>synsets</b>
468
+ i.e. different meanings for the word.</li>
469
+ <li>All underlined texts are hypertext links. There are two types of links:
470
+ word links and others. Clicking a word link carries out a search for the word
471
+ in the Wordnet database.</li>
472
+ <li>Clicking a link of the other type opens a display section of data attached
473
+ to that link. Clicking that link a second time closes the section again.</li>
474
+ <li>Clicking <u>S:</u> opens a section showing the relations for that synset.
475
+ </li>
476
+ <li>Clicking on a relation name opens a section that displays the associated
477
+ synsets.</li>
478
+ <li>Type a search word in the <b>Word</b> field and start the search by the
479
+ <b>Enter/Return</b> key or click the <b>Search</b> button.</li>
480
+ </ul>
481
+ <hr width='100%'>
482
+ """
483
+
484
+ # HTML oriented functions
485
+
486
+
487
+ def _bold(txt):
488
+ return "<b>%s</b>" % txt
489
+
490
+
491
+ def _center(txt):
492
+ return "<center>%s</center>" % txt
493
+
494
+
495
+ def _hlev(n, txt):
496
+ return "<h%d>%s</h%d>" % (n, txt, n)
497
+
498
+
499
+ def _italic(txt):
500
+ return "<i>%s</i>" % txt
501
+
502
+
503
+ def _li(txt):
504
+ return "<li>%s</li>" % txt
505
+
506
+
507
+ def pg(word, body):
508
+ """
509
+ Return a HTML page of NLTK Browser format constructed from the
510
+ word and body
511
+
512
+ :param word: The word that the body corresponds to
513
+ :type word: str
514
+ :param body: The HTML body corresponding to the word
515
+ :type body: str
516
+ :return: a HTML page for the word-body combination
517
+ :rtype: str
518
+ """
519
+ return (html_header % word) + body + html_trailer
520
+
521
+
522
+ def _ul(txt):
523
+ return "<ul>" + txt + "</ul>"
524
+
525
+
526
+ def _abbc(txt):
527
+ """
528
+ abbc = asterisks, breaks, bold, center
529
+ """
530
+ return _center(_bold("<br>" * 10 + "*" * 10 + " " + txt + " " + "*" * 10))
531
+
532
+
533
+ full_hyponym_cont_text = _ul(_li(_italic("(has full hyponym continuation)"))) + "\n"
534
+
535
+
536
+ def _get_synset(synset_key):
537
+ """
538
+ The synset key is the unique name of the synset, this can be
539
+ retrieved via synset.name()
540
+ """
541
+ return wn.synset(synset_key)
542
+
543
+
544
+ def _collect_one_synset(word, synset, synset_relations):
545
+ """
546
+ Returns the HTML string for one synset or word
547
+
548
+ :param word: the current word
549
+ :type word: str
550
+ :param synset: a synset
551
+ :type synset: synset
552
+ :param synset_relations: information about which synset relations
553
+ to display.
554
+ :type synset_relations: dict(synset_key, set(relation_id))
555
+ :return: The HTML string built for this synset
556
+ :rtype: str
557
+ """
558
+ if isinstance(synset, tuple): # It's a word
559
+ raise NotImplementedError("word not supported by _collect_one_synset")
560
+
561
+ typ = "S"
562
+ pos_tuple = _pos_match((synset.pos(), None, None))
563
+ assert pos_tuple is not None, "pos_tuple is null: synset.pos(): %s" % synset.pos()
564
+ descr = pos_tuple[2]
565
+ ref = copy.deepcopy(Reference(word, synset_relations))
566
+ ref.toggle_synset(synset)
567
+ synset_label = typ + ";"
568
+ if synset.name() in synset_relations:
569
+ synset_label = _bold(synset_label)
570
+ s = f"<li>{make_lookup_link(ref, synset_label)} ({descr}) "
571
+
572
+ def format_lemma(w):
573
+ w = w.replace("_", " ")
574
+ if w.lower() == word:
575
+ return _bold(w)
576
+ else:
577
+ ref = Reference(w)
578
+ return make_lookup_link(ref, w)
579
+
580
+ s += ", ".join(format_lemma(l.name()) for l in synset.lemmas())
581
+
582
+ gl = " ({}) <i>{}</i> ".format(
583
+ synset.definition(),
584
+ "; ".join('"%s"' % e for e in synset.examples()),
585
+ )
586
+ return s + gl + _synset_relations(word, synset, synset_relations) + "</li>\n"
587
+
588
+
589
+ def _collect_all_synsets(word, pos, synset_relations=dict()):
590
+ """
591
+ Return a HTML unordered list of synsets for the given word and
592
+ part of speech.
593
+ """
594
+ return "<ul>%s\n</ul>\n" % "".join(
595
+ _collect_one_synset(word, synset, synset_relations)
596
+ for synset in wn.synsets(word, pos)
597
+ )
598
+
599
+
600
+ def _synset_relations(word, synset, synset_relations):
601
+ """
602
+ Builds the HTML string for the relations of a synset
603
+
604
+ :param word: The current word
605
+ :type word: str
606
+ :param synset: The synset for which we're building the relations.
607
+ :type synset: Synset
608
+ :param synset_relations: synset keys and relation types for which to display relations.
609
+ :type synset_relations: dict(synset_key, set(relation_type))
610
+ :return: The HTML for a synset's relations
611
+ :rtype: str
612
+ """
613
+
614
+ if synset.name() not in synset_relations:
615
+ return ""
616
+ ref = Reference(word, synset_relations)
617
+
618
+ def relation_html(r):
619
+ if isinstance(r, Synset):
620
+ return make_lookup_link(Reference(r.lemma_names()[0]), r.lemma_names()[0])
621
+ elif isinstance(r, Lemma):
622
+ return relation_html(r.synset())
623
+ elif isinstance(r, tuple):
624
+ # It's probably a tuple containing a Synset and a list of
625
+ # similar tuples. This forms a tree of synsets.
626
+ return "{}\n<ul>{}</ul>\n".format(
627
+ relation_html(r[0]),
628
+ "".join("<li>%s</li>\n" % relation_html(sr) for sr in r[1]),
629
+ )
630
+ else:
631
+ raise TypeError(
632
+ "r must be a synset, lemma or list, it was: type(r) = %s, r = %s"
633
+ % (type(r), r)
634
+ )
635
+
636
+ def make_synset_html(db_name, disp_name, rels):
637
+ synset_html = "<i>%s</i>\n" % make_lookup_link(
638
+ copy.deepcopy(ref).toggle_synset_relation(synset, db_name),
639
+ disp_name,
640
+ )
641
+
642
+ if db_name in ref.synset_relations[synset.name()]:
643
+ synset_html += "<ul>%s</ul>\n" % "".join(
644
+ "<li>%s</li>\n" % relation_html(r) for r in rels
645
+ )
646
+
647
+ return synset_html
648
+
649
+ html = (
650
+ "<ul>"
651
+ + "\n".join(
652
+ "<li>%s</li>" % make_synset_html(*rel_data)
653
+ for rel_data in get_relations_data(word, synset)
654
+ if rel_data[2] != []
655
+ )
656
+ + "</ul>"
657
+ )
658
+
659
+ return html
660
+
661
+
662
+ class Reference:
663
+ """
664
+ A reference to a page that may be generated by page_word
665
+ """
666
+
667
+ def __init__(self, word, synset_relations=dict()):
668
+ """
669
+ Build a reference to a new page.
670
+
671
+ word is the word or words (separated by commas) for which to
672
+ search for synsets of
673
+
674
+ synset_relations is a dictionary of synset keys to sets of
675
+ synset relation identifaiers to unfold a list of synset
676
+ relations for.
677
+ """
678
+ self.word = word
679
+ self.synset_relations = synset_relations
680
+
681
+ def encode(self):
682
+ """
683
+ Encode this reference into a string to be used in a URL.
684
+ """
685
+ # This uses a tuple rather than an object since the python
686
+ # pickle representation is much smaller and there is no need
687
+ # to represent the complete object.
688
+ string = pickle.dumps((self.word, self.synset_relations), -1)
689
+ return base64.urlsafe_b64encode(string).decode()
690
+
691
+ @staticmethod
692
+ def decode(string):
693
+ """
694
+ Decode a reference encoded with Reference.encode
695
+ """
696
+ string = base64.urlsafe_b64decode(string.encode())
697
+ word, synset_relations = RestrictedUnpickler(io.BytesIO(string)).load()
698
+ return Reference(word, synset_relations)
699
+
700
+ def toggle_synset_relation(self, synset, relation):
701
+ """
702
+ Toggle the display of the relations for the given synset and
703
+ relation type.
704
+
705
+ This function will throw a KeyError if the synset is currently
706
+ not being displayed.
707
+ """
708
+ if relation in self.synset_relations[synset.name()]:
709
+ self.synset_relations[synset.name()].remove(relation)
710
+ else:
711
+ self.synset_relations[synset.name()].add(relation)
712
+
713
+ return self
714
+
715
+ def toggle_synset(self, synset):
716
+ """
717
+ Toggle displaying of the relation types for the given synset
718
+ """
719
+ if synset.name() in self.synset_relations:
720
+ del self.synset_relations[synset.name()]
721
+ else:
722
+ self.synset_relations[synset.name()] = set()
723
+
724
+ return self
725
+
726
+
727
+ def make_lookup_link(ref, label):
728
+ return f'<a href="lookup_{ref.encode()}">{label}</a>'
729
+
730
+
731
+ def page_from_word(word):
732
+ """
733
+ Return a HTML page for the given word.
734
+
735
+ :type word: str
736
+ :param word: The currently active word
737
+ :return: A tuple (page,word), where page is the new current HTML page
738
+ to be sent to the browser and
739
+ word is the new current word
740
+ :rtype: A tuple (str,str)
741
+ """
742
+ return page_from_reference(Reference(word))
743
+
744
+
745
+ def page_from_href(href):
746
+ """
747
+ Returns a tuple of the HTML page built and the new current word
748
+
749
+ :param href: The hypertext reference to be solved
750
+ :type href: str
751
+ :return: A tuple (page,word), where page is the new current HTML page
752
+ to be sent to the browser and
753
+ word is the new current word
754
+ :rtype: A tuple (str,str)
755
+ """
756
+ return page_from_reference(Reference.decode(href))
757
+
758
+
759
+ def page_from_reference(href):
760
+ """
761
+ Returns a tuple of the HTML page built and the new current word
762
+
763
+ :param href: The hypertext reference to be solved
764
+ :type href: str
765
+ :return: A tuple (page,word), where page is the new current HTML page
766
+ to be sent to the browser and
767
+ word is the new current word
768
+ :rtype: A tuple (str,str)
769
+ """
770
+ word = href.word
771
+ pos_forms = defaultdict(list)
772
+ words = word.split(",")
773
+ words = [w for w in [w.strip().lower().replace(" ", "_") for w in words] if w != ""]
774
+ if len(words) == 0:
775
+ # No words were found.
776
+ return "", "Please specify a word to search for."
777
+
778
+ # This looks up multiple words at once. This is probably not
779
+ # necessary and may lead to problems.
780
+ for w in words:
781
+ for pos in [wn.NOUN, wn.VERB, wn.ADJ, wn.ADV]:
782
+ form = wn.morphy(w, pos)
783
+ if form and form not in pos_forms[pos]:
784
+ pos_forms[pos].append(form)
785
+ body = ""
786
+ for pos, pos_str, name in _pos_tuples():
787
+ if pos in pos_forms:
788
+ body += _hlev(3, name) + "\n"
789
+ for w in pos_forms[pos]:
790
+ # Not all words of exc files are in the database, skip
791
+ # to the next word if a KeyError is raised.
792
+ try:
793
+ body += _collect_all_synsets(w, pos, href.synset_relations)
794
+ except KeyError:
795
+ pass
796
+ if not body:
797
+ body = "The word or words '%s' were not found in the dictionary." % html.escape(
798
+ word
799
+ )
800
+ return body, word
801
+
802
+
803
+ #####################################################################
804
+ # Static pages
805
+ #####################################################################
806
+
807
+
808
+ def get_static_page_by_path(path):
809
+ """
810
+ Return a static HTML page from the path given.
811
+ """
812
+ if path == "index_2.html":
813
+ return get_static_index_page(False)
814
+ elif path == "index.html":
815
+ return get_static_index_page(True)
816
+ elif path == "NLTK Wordnet Browser Database Info.html":
817
+ return "Display of Wordnet Database Statistics is not supported"
818
+ elif path == "upper_2.html":
819
+ return get_static_upper_page(False)
820
+ elif path == "upper.html":
821
+ return get_static_upper_page(True)
822
+ elif path == "web_help.html":
823
+ return get_static_web_help_page()
824
+ elif path == "wx_help.html":
825
+ return get_static_wx_help_page()
826
+ raise FileNotFoundError()
827
+
828
+
829
+ def get_static_web_help_page():
830
+ """
831
+ Return the static web help page.
832
+ """
833
+ return """
834
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
835
+ <html>
836
+ <!-- Natural Language Toolkit: Wordnet Interface: Graphical Wordnet Browser
837
+ Copyright (C) 2001-2026 NLTK Project
838
+ Author: Jussi Salmela <jtsalmela@users.sourceforge.net>
839
+ URL: <https://www.nltk.org/>
840
+ For license information, see LICENSE.TXT -->
841
+ <head>
842
+ <meta http-equiv='Content-Type' content='text/html; charset=us-ascii'>
843
+ <title>NLTK Wordnet Browser display of: * Help *</title>
844
+ </head>
845
+ <body bgcolor='#F5F5F5' text='#000000'>
846
+ <h2>NLTK Wordnet Browser Help</h2>
847
+ <p>The NLTK Wordnet Browser is a tool to use in browsing the Wordnet database. It tries to behave like the Wordnet project's web browser but the difference is that the NLTK Wordnet Browser uses a local Wordnet database.
848
+ <p><b>You are using the Javascript client part of the NLTK Wordnet BrowseServer.</b> We assume your browser is in tab sheets enabled mode.</p>
849
+ <p>For background information on Wordnet, see the Wordnet project home page: <a href="https://wordnet.princeton.edu/"><b> https://wordnet.princeton.edu/</b></a>. For more information on the NLTK project, see the project home:
850
+ <a href="https://www.nltk.org/"><b>https://www.nltk.org/</b></a>. To get an idea of what the Wordnet version used by this browser includes choose <b>Show Database Info</b> from the <b>View</b> submenu.</p>
851
+ <h3>Word search</h3>
852
+ <p>The word to be searched is typed into the <b>New Word</b> field and the search started with Enter or by clicking the <b>Search</b> button. There is no uppercase/lowercase distinction: the search word is transformed to lowercase before the search.</p>
853
+ <p>In addition, the word does not have to be in base form. The browser tries to find the possible base form(s) by making certain morphological substitutions. Typing <b>fLIeS</b> as an obscure example gives one <a href="MfLIeS">this</a>. Click the previous link to see what this kind of search looks like and then come back to this page by using the <b>Alt+LeftArrow</b> key combination.</p>
854
+ <p>The result of a search is a display of one or more
855
+ <b>synsets</b> for every part of speech in which a form of the
856
+ search word was found to occur. A synset is a set of words
857
+ having the same sense or meaning. Each word in a synset that is
858
+ underlined is a hyperlink which can be clicked to trigger an
859
+ automatic search for that word.</p>
860
+ <p>Every synset has a hyperlink <b>S:</b> at the start of its
861
+ display line. Clicking that symbol shows you the name of every
862
+ <b>relation</b> that this synset is part of. Every relation name is a hyperlink that opens up a display for that relation. Clicking it another time closes the display again. Clicking another relation name on a line that has an opened relation closes the open relation and opens the clicked relation.</p>
863
+ <p>It is also possible to give two or more words or collocations to be searched at the same time separating them with a comma like this <a href="Mcheer up,clear up">cheer up,clear up</a>, for example. Click the previous link to see what this kind of search looks like and then come back to this page by using the <b>Alt+LeftArrow</b> key combination. As you could see the search result includes the synsets found in the same order than the forms were given in the search field.</p>
864
+ <p>
865
+ There are also word level (lexical) relations recorded in the Wordnet database. Opening this kind of relation displays lines with a hyperlink <b>W:</b> at their beginning. Clicking this link shows more info on the word in question.</p>
866
+ <h3>The Buttons</h3>
867
+ <p>The <b>Search</b> and <b>Help</b> buttons need no more explanation. </p>
868
+ <p>The <b>Show Database Info</b> button shows a collection of Wordnet database statistics.</p>
869
+ <p>The <b>Shutdown the Server</b> button is shown for the first client of the BrowServer program i.e. for the client that is automatically launched when the BrowServer is started but not for the succeeding clients in order to protect the server from accidental shutdowns.
870
+ </p></body>
871
+ </html>
872
+ """
873
+
874
+
875
+ def get_static_welcome_message():
876
+ """
877
+ Get the static welcome page.
878
+ """
879
+ return """
880
+ <h3>Search Help</h3>
881
+ <ul><li>The display below the line is an example of the output the browser
882
+ shows you when you enter a search word. The search word was <b>green</b>.</li>
883
+ <li>The search result shows for different parts of speech the <b>synsets</b>
884
+ i.e. different meanings for the word.</li>
885
+ <li>All underlined texts are hypertext links. There are two types of links:
886
+ word links and others. Clicking a word link carries out a search for the word
887
+ in the Wordnet database.</li>
888
+ <li>Clicking a link of the other type opens a display section of data attached
889
+ to that link. Clicking that link a second time closes the section again.</li>
890
+ <li>Clicking <u>S:</u> opens a section showing the relations for that synset.</li>
891
+ <li>Clicking on a relation name opens a section that displays the associated
892
+ synsets.</li>
893
+ <li>Type a search word in the <b>Next Word</b> field and start the search by the
894
+ <b>Enter/Return</b> key or click the <b>Search</b> button.</li>
895
+ </ul>
896
+ """
897
+
898
+
899
+ def get_static_index_page(with_shutdown):
900
+ """
901
+ Get the static index page.
902
+ """
903
+ template = """
904
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
905
+ <HTML>
906
+ <!-- Natural Language Toolkit: Wordnet Interface: Graphical Wordnet Browser
907
+ Copyright (C) 2001-2026 NLTK Project
908
+ Author: Jussi Salmela <jtsalmela@users.sourceforge.net>
909
+ URL: <https://www.nltk.org/>
910
+ For license information, see LICENSE.TXT -->
911
+ <HEAD>
912
+ <TITLE>NLTK Wordnet Browser</TITLE>
913
+ </HEAD>
914
+
915
+ <frameset rows="7%%,93%%">
916
+ <frame src="%s" name="header">
917
+ <frame src="start_page" name="body">
918
+ </frameset>
919
+ </HTML>
920
+ """
921
+ if with_shutdown:
922
+ upper_link = "upper.html"
923
+ else:
924
+ upper_link = "upper_2.html"
925
+
926
+ return template % upper_link
927
+
928
+
929
+ def get_static_upper_page(with_shutdown):
930
+ """
931
+ Return the upper frame page,
932
+
933
+ If with_shutdown is True then a 'shutdown' button is also provided
934
+ to shutdown the server.
935
+ """
936
+ template = """
937
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
938
+ <html>
939
+ <!-- Natural Language Toolkit: Wordnet Interface: Graphical Wordnet Browser
940
+ Copyright (C) 2001-2026 NLTK Project
941
+ Author: Jussi Salmela <jtsalmela@users.sourceforge.net>
942
+ URL: <https://www.nltk.org/>
943
+ For license information, see LICENSE.TXT -->
944
+ <head>
945
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
946
+ <title>Untitled Document</title>
947
+ </head>
948
+ <body>
949
+ <form method="GET" action="search" target="body">
950
+ Current Word:&nbsp;<input type="text" id="currentWord" size="10" disabled>
951
+ Next Word:&nbsp;<input type="text" id="nextWord" name="nextWord" size="10">
952
+ <input name="searchButton" type="submit" value="Search">
953
+ </form>
954
+ <a target="body" href="web_help.html">Help</a>
955
+ %s
956
+
957
+ </body>
958
+ </html>
959
+ """
960
+ if with_shutdown:
961
+ shutdown_link = '<a href="SHUTDOWN THE SERVER">Shutdown</a>'
962
+ else:
963
+ shutdown_link = ""
964
+
965
+ return template % shutdown_link
966
+
967
+
968
+ def usage():
969
+ """
970
+ Display the command line help message.
971
+ """
972
+ print(__doc__)
973
+
974
+
975
+ def app():
976
+ # Parse and interpret options.
977
+ (opts, _) = getopt.getopt(
978
+ argv[1:], "l:p:sh", ["logfile=", "port=", "server-mode", "help"]
979
+ )
980
+ port = 8000
981
+ server_mode = False
982
+ help_mode = False
983
+ logfilename = None
984
+ for opt, value in opts:
985
+ if (opt == "-l") or (opt == "--logfile"):
986
+ logfilename = str(value)
987
+ elif (opt == "-p") or (opt == "--port"):
988
+ port = int(value)
989
+ elif (opt == "-s") or (opt == "--server-mode"):
990
+ server_mode = True
991
+ elif (opt == "-h") or (opt == "--help"):
992
+ help_mode = True
993
+
994
+ if help_mode:
995
+ usage()
996
+ else:
997
+ wnb(port, not server_mode, logfilename)
998
+
999
+
1000
+ if __name__ == "__main__":
1001
+ app()
1002
+
1003
+ __all__ = ["app"]
loan-advisor-agent/.venv/Lib/site-packages/nltk/book.py ADDED
@@ -0,0 +1,213 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: Some texts for exploration in chapter 1 of the book
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Author: Steven Bird <stevenbird1@gmail.com>
5
+ #
6
+ # URL: <https://www.nltk.org/>
7
+ # For license information, see LICENSE.TXT
8
+
9
+ from nltk.corpus import (
10
+ genesis,
11
+ gutenberg,
12
+ inaugural,
13
+ nps_chat,
14
+ treebank,
15
+ webtext,
16
+ wordnet,
17
+ )
18
+ from nltk.probability import FreqDist
19
+ from nltk.text import Text
20
+ from nltk.util import bigrams
21
+
22
+ print("*** Introductory Examples for the NLTK Book ***")
23
+ print("Loading text1, ..., text9 and sent1, ..., sent9")
24
+ print("Type the name of the text or sentence to view it.")
25
+ print("Type: 'texts()' or 'sents()' to list the materials.")
26
+
27
+ text1 = Text(gutenberg.words("melville-moby_dick.txt"))
28
+ print("text1:", text1.name)
29
+
30
+ text2 = Text(gutenberg.words("austen-sense.txt"))
31
+ print("text2:", text2.name)
32
+
33
+ text3 = Text(genesis.words("english-kjv.txt"), name="The Book of Genesis")
34
+ print("text3:", text3.name)
35
+
36
+ text4 = Text(inaugural.words(), name="Inaugural Address Corpus")
37
+ print("text4:", text4.name)
38
+
39
+ text5 = Text(nps_chat.words(), name="Chat Corpus")
40
+ print("text5:", text5.name)
41
+
42
+ text6 = Text(webtext.words("grail.txt"), name="Monty Python and the Holy Grail")
43
+ print("text6:", text6.name)
44
+
45
+ text7 = Text(treebank.words(), name="Wall Street Journal")
46
+ print("text7:", text7.name)
47
+
48
+ text8 = Text(webtext.words("singles.txt"), name="Personals Corpus")
49
+ print("text8:", text8.name)
50
+
51
+ text9 = Text(gutenberg.words("chesterton-thursday.txt"))
52
+ print("text9:", text9.name)
53
+
54
+
55
+ def texts():
56
+ print("text1:", text1.name)
57
+ print("text2:", text2.name)
58
+ print("text3:", text3.name)
59
+ print("text4:", text4.name)
60
+ print("text5:", text5.name)
61
+ print("text6:", text6.name)
62
+ print("text7:", text7.name)
63
+ print("text8:", text8.name)
64
+ print("text9:", text9.name)
65
+
66
+
67
+ sent1 = ["Call", "me", "Ishmael", "."]
68
+ sent2 = [
69
+ "The",
70
+ "family",
71
+ "of",
72
+ "Dashwood",
73
+ "had",
74
+ "long",
75
+ "been",
76
+ "settled",
77
+ "in",
78
+ "Sussex",
79
+ ".",
80
+ ]
81
+ sent3 = [
82
+ "In",
83
+ "the",
84
+ "beginning",
85
+ "God",
86
+ "created",
87
+ "the",
88
+ "heaven",
89
+ "and",
90
+ "the",
91
+ "earth",
92
+ ".",
93
+ ]
94
+ sent4 = [
95
+ "Fellow",
96
+ "-",
97
+ "Citizens",
98
+ "of",
99
+ "the",
100
+ "Senate",
101
+ "and",
102
+ "of",
103
+ "the",
104
+ "House",
105
+ "of",
106
+ "Representatives",
107
+ ":",
108
+ ]
109
+ sent5 = [
110
+ "I",
111
+ "have",
112
+ "a",
113
+ "problem",
114
+ "with",
115
+ "people",
116
+ "PMing",
117
+ "me",
118
+ "to",
119
+ "lol",
120
+ "JOIN",
121
+ ]
122
+ sent6 = [
123
+ "SCENE",
124
+ "1",
125
+ ":",
126
+ "[",
127
+ "wind",
128
+ "]",
129
+ "[",
130
+ "clop",
131
+ "clop",
132
+ "clop",
133
+ "]",
134
+ "KING",
135
+ "ARTHUR",
136
+ ":",
137
+ "Whoa",
138
+ "there",
139
+ "!",
140
+ ]
141
+ sent7 = [
142
+ "Pierre",
143
+ "Vinken",
144
+ ",",
145
+ "61",
146
+ "years",
147
+ "old",
148
+ ",",
149
+ "will",
150
+ "join",
151
+ "the",
152
+ "board",
153
+ "as",
154
+ "a",
155
+ "nonexecutive",
156
+ "director",
157
+ "Nov.",
158
+ "29",
159
+ ".",
160
+ ]
161
+ sent8 = [
162
+ "25",
163
+ "SEXY",
164
+ "MALE",
165
+ ",",
166
+ "seeks",
167
+ "attrac",
168
+ "older",
169
+ "single",
170
+ "lady",
171
+ ",",
172
+ "for",
173
+ "discreet",
174
+ "encounters",
175
+ ".",
176
+ ]
177
+ sent9 = [
178
+ "THE",
179
+ "suburb",
180
+ "of",
181
+ "Saffron",
182
+ "Park",
183
+ "lay",
184
+ "on",
185
+ "the",
186
+ "sunset",
187
+ "side",
188
+ "of",
189
+ "London",
190
+ ",",
191
+ "as",
192
+ "red",
193
+ "and",
194
+ "ragged",
195
+ "as",
196
+ "a",
197
+ "cloud",
198
+ "of",
199
+ "sunset",
200
+ ".",
201
+ ]
202
+
203
+
204
+ def sents():
205
+ print("sent1:", " ".join(sent1))
206
+ print("sent2:", " ".join(sent2))
207
+ print("sent3:", " ".join(sent3))
208
+ print("sent4:", " ".join(sent4))
209
+ print("sent5:", " ".join(sent5))
210
+ print("sent6:", " ".join(sent6))
211
+ print("sent7:", " ".join(sent7))
212
+ print("sent8:", " ".join(sent8))
213
+ print("sent9:", " ".join(sent9))
loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/__init__.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: Combinatory Categorial Grammar
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Author: Graeme Gange <ggange@csse.unimelb.edu.au>
5
+ # URL: <https://www.nltk.org/>
6
+ # For license information, see LICENSE.TXT
7
+
8
+ """
9
+ Combinatory Categorial Grammar.
10
+
11
+ For more information see nltk/doc/contrib/ccg/ccg.pdf
12
+ """
13
+
14
+ from nltk.ccg.chart import CCGChart, CCGChartParser, CCGEdge, CCGLeafEdge
15
+ from nltk.ccg.combinator import (
16
+ BackwardApplication,
17
+ BackwardBx,
18
+ BackwardCombinator,
19
+ BackwardComposition,
20
+ BackwardSx,
21
+ BackwardT,
22
+ DirectedBinaryCombinator,
23
+ ForwardApplication,
24
+ ForwardCombinator,
25
+ ForwardComposition,
26
+ ForwardSubstitution,
27
+ ForwardT,
28
+ UndirectedBinaryCombinator,
29
+ UndirectedComposition,
30
+ UndirectedFunctionApplication,
31
+ UndirectedSubstitution,
32
+ UndirectedTypeRaise,
33
+ )
34
+ from nltk.ccg.lexicon import CCGLexicon
loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (1.02 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/__pycache__/api.cpython-312.pyc ADDED
Binary file (15.8 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/__pycache__/chart.cpython-312.pyc ADDED
Binary file (18.9 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/__pycache__/combinator.cpython-312.pyc ADDED
Binary file (14.9 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/__pycache__/lexicon.cpython-312.pyc ADDED
Binary file (11.4 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/__pycache__/logic.cpython-312.pyc ADDED
Binary file (2.69 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/api.py ADDED
@@ -0,0 +1,358 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: CCG Categories
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Author: Graeme Gange <ggange@csse.unimelb.edu.au>
5
+ # URL: <https://www.nltk.org/>
6
+ # For license information, see LICENSE.TXT
7
+
8
+ from abc import ABCMeta, abstractmethod
9
+ from functools import total_ordering
10
+
11
+ from nltk.internals import raise_unorderable_types
12
+
13
+
14
+ @total_ordering
15
+ class AbstractCCGCategory(metaclass=ABCMeta):
16
+ """
17
+ Interface for categories in combinatory grammars.
18
+ """
19
+
20
+ @abstractmethod
21
+ def is_primitive(self):
22
+ """
23
+ Returns true if the category is primitive.
24
+ """
25
+
26
+ @abstractmethod
27
+ def is_function(self):
28
+ """
29
+ Returns true if the category is a function application.
30
+ """
31
+
32
+ @abstractmethod
33
+ def is_var(self):
34
+ """
35
+ Returns true if the category is a variable.
36
+ """
37
+
38
+ @abstractmethod
39
+ def substitute(self, substitutions):
40
+ """
41
+ Takes a set of (var, category) substitutions, and replaces every
42
+ occurrence of the variable with the corresponding category.
43
+ """
44
+
45
+ @abstractmethod
46
+ def can_unify(self, other):
47
+ """
48
+ Determines whether two categories can be unified.
49
+ - Returns None if they cannot be unified
50
+ - Returns a list of necessary substitutions if they can.
51
+ """
52
+
53
+ # Utility functions: comparison, strings and hashing.
54
+ @abstractmethod
55
+ def __str__(self):
56
+ pass
57
+
58
+ def __eq__(self, other):
59
+ return (
60
+ self.__class__ is other.__class__
61
+ and self._comparison_key == other._comparison_key
62
+ )
63
+
64
+ def __ne__(self, other):
65
+ return not self == other
66
+
67
+ def __lt__(self, other):
68
+ if not isinstance(other, AbstractCCGCategory):
69
+ raise_unorderable_types("<", self, other)
70
+ if self.__class__ is other.__class__:
71
+ return self._comparison_key < other._comparison_key
72
+ else:
73
+ return self.__class__.__name__ < other.__class__.__name__
74
+
75
+ def __hash__(self):
76
+ try:
77
+ return self._hash
78
+ except AttributeError:
79
+ self._hash = hash(self._comparison_key)
80
+ return self._hash
81
+
82
+
83
+ class CCGVar(AbstractCCGCategory):
84
+ """
85
+ Class representing a variable CCG category.
86
+ Used for conjunctions (and possibly type-raising, if implemented as a
87
+ unary rule).
88
+ """
89
+
90
+ _maxID = 0
91
+
92
+ def __init__(self, prim_only=False):
93
+ """Initialize a variable (selects a new identifier)
94
+
95
+ :param prim_only: a boolean that determines whether the variable is
96
+ restricted to primitives
97
+ :type prim_only: bool
98
+ """
99
+ self._id = self.new_id()
100
+ self._prim_only = prim_only
101
+ self._comparison_key = self._id
102
+
103
+ @classmethod
104
+ def new_id(cls):
105
+ """
106
+ A class method allowing generation of unique variable identifiers.
107
+ """
108
+ cls._maxID = cls._maxID + 1
109
+ return cls._maxID - 1
110
+
111
+ @classmethod
112
+ def reset_id(cls):
113
+ cls._maxID = 0
114
+
115
+ def is_primitive(self):
116
+ return False
117
+
118
+ def is_function(self):
119
+ return False
120
+
121
+ def is_var(self):
122
+ return True
123
+
124
+ def substitute(self, substitutions):
125
+ """If there is a substitution corresponding to this variable,
126
+ return the substituted category.
127
+ """
128
+ for var, cat in substitutions:
129
+ if var == self:
130
+ return cat
131
+ return self
132
+
133
+ def can_unify(self, other):
134
+ """If the variable can be replaced with other
135
+ a substitution is returned.
136
+ """
137
+ if other.is_primitive() or not self._prim_only:
138
+ return [(self, other)]
139
+ return None
140
+
141
+ def id(self):
142
+ return self._id
143
+
144
+ def __str__(self):
145
+ return "_var" + str(self._id)
146
+
147
+
148
+ @total_ordering
149
+ class Direction:
150
+ """
151
+ Class representing the direction of a function application.
152
+ Also contains maintains information as to which combinators
153
+ may be used with the category.
154
+ """
155
+
156
+ def __init__(self, dir, restrictions):
157
+ self._dir = dir
158
+ self._restrs = restrictions
159
+ self._comparison_key = (dir, tuple(restrictions))
160
+
161
+ # Testing the application direction
162
+ def is_forward(self):
163
+ return self._dir == "/"
164
+
165
+ def is_backward(self):
166
+ return self._dir == "\\"
167
+
168
+ def dir(self):
169
+ return self._dir
170
+
171
+ def restrs(self):
172
+ """A list of restrictions on the combinators.
173
+ '.' denotes that permuting operations are disallowed
174
+ ',' denotes that function composition is disallowed
175
+ '_' denotes that the direction has variable restrictions.
176
+ (This is redundant in the current implementation of type-raising)
177
+ """
178
+ return self._restrs
179
+
180
+ def is_variable(self):
181
+ return self._restrs == "_"
182
+
183
+ # Unification and substitution of variable directions.
184
+ # Used only if type-raising is implemented as a unary rule, as it
185
+ # must inherit restrictions from the argument category.
186
+ def can_unify(self, other):
187
+ if other.is_variable():
188
+ return [("_", self.restrs())]
189
+ elif self.is_variable():
190
+ return [("_", other.restrs())]
191
+ else:
192
+ if self.restrs() == other.restrs():
193
+ return []
194
+ return None
195
+
196
+ def substitute(self, subs):
197
+ if not self.is_variable():
198
+ return self
199
+
200
+ for var, restrs in subs:
201
+ if var == "_":
202
+ return Direction(self._dir, restrs)
203
+ return self
204
+
205
+ # Testing permitted combinators
206
+ def can_compose(self):
207
+ return "," not in self._restrs
208
+
209
+ def can_cross(self):
210
+ return "." not in self._restrs
211
+
212
+ def __eq__(self, other):
213
+ return (
214
+ self.__class__ is other.__class__
215
+ and self._comparison_key == other._comparison_key
216
+ )
217
+
218
+ def __ne__(self, other):
219
+ return not self == other
220
+
221
+ def __lt__(self, other):
222
+ if not isinstance(other, Direction):
223
+ raise_unorderable_types("<", self, other)
224
+ if self.__class__ is other.__class__:
225
+ return self._comparison_key < other._comparison_key
226
+ else:
227
+ return self.__class__.__name__ < other.__class__.__name__
228
+
229
+ def __hash__(self):
230
+ try:
231
+ return self._hash
232
+ except AttributeError:
233
+ self._hash = hash(self._comparison_key)
234
+ return self._hash
235
+
236
+ def __str__(self):
237
+ r_str = ""
238
+ for r in self._restrs:
239
+ r_str = r_str + "%s" % r
240
+ return f"{self._dir}{r_str}"
241
+
242
+ # The negation operator reverses the direction of the application
243
+ def __neg__(self):
244
+ if self._dir == "/":
245
+ return Direction("\\", self._restrs)
246
+ else:
247
+ return Direction("/", self._restrs)
248
+
249
+
250
+ class PrimitiveCategory(AbstractCCGCategory):
251
+ """
252
+ Class representing primitive categories.
253
+ Takes a string representation of the category, and a
254
+ list of strings specifying the morphological subcategories.
255
+ """
256
+
257
+ def __init__(self, categ, restrictions=[]):
258
+ self._categ = categ
259
+ self._restrs = restrictions
260
+ self._comparison_key = (categ, tuple(restrictions))
261
+
262
+ def is_primitive(self):
263
+ return True
264
+
265
+ def is_function(self):
266
+ return False
267
+
268
+ def is_var(self):
269
+ return False
270
+
271
+ def restrs(self):
272
+ return self._restrs
273
+
274
+ def categ(self):
275
+ return self._categ
276
+
277
+ # Substitution does nothing to a primitive category
278
+ def substitute(self, subs):
279
+ return self
280
+
281
+ # A primitive can be unified with a class of the same
282
+ # base category, given that the other category shares all
283
+ # of its subclasses, or with a variable.
284
+ def can_unify(self, other):
285
+ if not other.is_primitive():
286
+ return None
287
+ if other.is_var():
288
+ return [(other, self)]
289
+ if other.categ() == self.categ():
290
+ for restr in self._restrs:
291
+ if restr not in other.restrs():
292
+ return None
293
+ return []
294
+ return None
295
+
296
+ def __str__(self):
297
+ if self._restrs == []:
298
+ return "%s" % self._categ
299
+ restrictions = "[%s]" % ",".join(repr(r) for r in self._restrs)
300
+ return f"{self._categ}{restrictions}"
301
+
302
+
303
+ class FunctionalCategory(AbstractCCGCategory):
304
+ """
305
+ Class that represents a function application category.
306
+ Consists of argument and result categories, together with
307
+ an application direction.
308
+ """
309
+
310
+ def __init__(self, res, arg, dir):
311
+ self._res = res
312
+ self._arg = arg
313
+ self._dir = dir
314
+ self._comparison_key = (arg, dir, res)
315
+
316
+ def is_primitive(self):
317
+ return False
318
+
319
+ def is_function(self):
320
+ return True
321
+
322
+ def is_var(self):
323
+ return False
324
+
325
+ # Substitution returns the category consisting of the
326
+ # substitution applied to each of its constituents.
327
+ def substitute(self, subs):
328
+ sub_res = self._res.substitute(subs)
329
+ sub_dir = self._dir.substitute(subs)
330
+ sub_arg = self._arg.substitute(subs)
331
+ return FunctionalCategory(sub_res, sub_arg, self._dir)
332
+
333
+ # A function can unify with another function, so long as its
334
+ # constituents can unify, or with an unrestricted variable.
335
+ def can_unify(self, other):
336
+ if other.is_var():
337
+ return [(other, self)]
338
+ if other.is_function():
339
+ sa = self._res.can_unify(other.res())
340
+ sd = self._dir.can_unify(other.dir())
341
+ if sa is not None and sd is not None:
342
+ sb = self._arg.substitute(sa).can_unify(other.arg().substitute(sa))
343
+ if sb is not None:
344
+ return sa + sb
345
+ return None
346
+
347
+ # Constituent accessors
348
+ def arg(self):
349
+ return self._arg
350
+
351
+ def res(self):
352
+ return self._res
353
+
354
+ def dir(self):
355
+ return self._dir
356
+
357
+ def __str__(self):
358
+ return f"({self._res}{self._dir}{self._arg})"
loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/chart.py ADDED
@@ -0,0 +1,480 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: Combinatory Categorial Grammar
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Author: Graeme Gange <ggange@csse.unimelb.edu.au>
5
+ # URL: <https://www.nltk.org/>
6
+ # For license information, see LICENSE.TXT
7
+
8
+ """
9
+ The lexicon is constructed by calling
10
+ ``lexicon.fromstring(<lexicon string>)``.
11
+
12
+ In order to construct a parser, you also need a rule set.
13
+ The standard English rules are provided in chart as
14
+ ``chart.DefaultRuleSet``.
15
+
16
+ The parser can then be constructed by calling, for example:
17
+ ``parser = chart.CCGChartParser(<lexicon>, <ruleset>)``
18
+
19
+ Parsing is then performed by running
20
+ ``parser.parse(<sentence>.split())``.
21
+
22
+ While this returns a list of trees, the default representation
23
+ of the produced trees is not very enlightening, particularly
24
+ given that it uses the same tree class as the CFG parsers.
25
+ It is probably better to call:
26
+ ``chart.printCCGDerivation(<parse tree extracted from list>)``
27
+ which should print a nice representation of the derivation.
28
+
29
+ This entire process is shown far more clearly in the demonstration:
30
+ python chart.py
31
+ """
32
+
33
+ import itertools
34
+
35
+ from nltk.ccg.combinator import *
36
+ from nltk.ccg.combinator import (
37
+ BackwardApplication,
38
+ BackwardBx,
39
+ BackwardComposition,
40
+ BackwardSx,
41
+ BackwardT,
42
+ ForwardApplication,
43
+ ForwardComposition,
44
+ ForwardSubstitution,
45
+ ForwardT,
46
+ )
47
+ from nltk.ccg.lexicon import Token, fromstring
48
+ from nltk.ccg.logic import *
49
+ from nltk.parse import ParserI
50
+ from nltk.parse.chart import AbstractChartRule, Chart, EdgeI
51
+ from nltk.sem.logic import *
52
+ from nltk.tree import Tree
53
+
54
+
55
+ # Based on the EdgeI class from NLTK.
56
+ # A number of the properties of the EdgeI interface don't
57
+ # transfer well to CCGs, however.
58
+ class CCGEdge(EdgeI):
59
+ def __init__(self, span, categ, rule):
60
+ self._span = span
61
+ self._categ = categ
62
+ self._rule = rule
63
+ self._comparison_key = (span, categ, rule)
64
+
65
+ # Accessors
66
+ def lhs(self):
67
+ return self._categ
68
+
69
+ def span(self):
70
+ return self._span
71
+
72
+ def start(self):
73
+ return self._span[0]
74
+
75
+ def end(self):
76
+ return self._span[1]
77
+
78
+ def length(self):
79
+ return self._span[1] - self.span[0]
80
+
81
+ def rhs(self):
82
+ return ()
83
+
84
+ def dot(self):
85
+ return 0
86
+
87
+ def is_complete(self):
88
+ return True
89
+
90
+ def is_incomplete(self):
91
+ return False
92
+
93
+ def nextsym(self):
94
+ return None
95
+
96
+ def categ(self):
97
+ return self._categ
98
+
99
+ def rule(self):
100
+ return self._rule
101
+
102
+
103
+ class CCGLeafEdge(EdgeI):
104
+ """
105
+ Class representing leaf edges in a CCG derivation.
106
+ """
107
+
108
+ def __init__(self, pos, token, leaf):
109
+ self._pos = pos
110
+ self._token = token
111
+ self._leaf = leaf
112
+ self._comparison_key = (pos, token.categ(), leaf)
113
+
114
+ # Accessors
115
+ def lhs(self):
116
+ return self._token.categ()
117
+
118
+ def span(self):
119
+ return (self._pos, self._pos + 1)
120
+
121
+ def start(self):
122
+ return self._pos
123
+
124
+ def end(self):
125
+ return self._pos + 1
126
+
127
+ def length(self):
128
+ return 1
129
+
130
+ def rhs(self):
131
+ return self._leaf
132
+
133
+ def dot(self):
134
+ return 0
135
+
136
+ def is_complete(self):
137
+ return True
138
+
139
+ def is_incomplete(self):
140
+ return False
141
+
142
+ def nextsym(self):
143
+ return None
144
+
145
+ def token(self):
146
+ return self._token
147
+
148
+ def categ(self):
149
+ return self._token.categ()
150
+
151
+ def leaf(self):
152
+ return self._leaf
153
+
154
+
155
+ class BinaryCombinatorRule(AbstractChartRule):
156
+ """
157
+ Class implementing application of a binary combinator to a chart.
158
+ Takes the directed combinator to apply.
159
+ """
160
+
161
+ NUMEDGES = 2
162
+
163
+ def __init__(self, combinator):
164
+ self._combinator = combinator
165
+
166
+ # Apply a combinator
167
+ def apply(self, chart, grammar, left_edge, right_edge):
168
+ # The left & right edges must be touching.
169
+ if not (left_edge.end() == right_edge.start()):
170
+ return
171
+
172
+ # Check if the two edges are permitted to combine.
173
+ # If so, generate the corresponding edge.
174
+ if self._combinator.can_combine(left_edge.categ(), right_edge.categ()):
175
+ for res in self._combinator.combine(left_edge.categ(), right_edge.categ()):
176
+ new_edge = CCGEdge(
177
+ span=(left_edge.start(), right_edge.end()),
178
+ categ=res,
179
+ rule=self._combinator,
180
+ )
181
+ if chart.insert(new_edge, (left_edge, right_edge)):
182
+ yield new_edge
183
+
184
+ # The representation of the combinator (for printing derivations)
185
+ def __str__(self):
186
+ return "%s" % self._combinator
187
+
188
+
189
+ # Type-raising must be handled slightly differently to the other rules, as the
190
+ # resulting rules only span a single edge, rather than both edges.
191
+
192
+
193
+ class ForwardTypeRaiseRule(AbstractChartRule):
194
+ """
195
+ Class for applying forward type raising
196
+ """
197
+
198
+ NUMEDGES = 2
199
+
200
+ def __init__(self):
201
+ self._combinator = ForwardT
202
+
203
+ def apply(self, chart, grammar, left_edge, right_edge):
204
+ if not (left_edge.end() == right_edge.start()):
205
+ return
206
+
207
+ for res in self._combinator.combine(left_edge.categ(), right_edge.categ()):
208
+ new_edge = CCGEdge(span=left_edge.span(), categ=res, rule=self._combinator)
209
+ if chart.insert(new_edge, (left_edge,)):
210
+ yield new_edge
211
+
212
+ def __str__(self):
213
+ return "%s" % self._combinator
214
+
215
+
216
+ class BackwardTypeRaiseRule(AbstractChartRule):
217
+ """
218
+ Class for applying backward type raising.
219
+ """
220
+
221
+ NUMEDGES = 2
222
+
223
+ def __init__(self):
224
+ self._combinator = BackwardT
225
+
226
+ def apply(self, chart, grammar, left_edge, right_edge):
227
+ if not (left_edge.end() == right_edge.start()):
228
+ return
229
+
230
+ for res in self._combinator.combine(left_edge.categ(), right_edge.categ()):
231
+ new_edge = CCGEdge(span=right_edge.span(), categ=res, rule=self._combinator)
232
+ if chart.insert(new_edge, (right_edge,)):
233
+ yield new_edge
234
+
235
+ def __str__(self):
236
+ return "%s" % self._combinator
237
+
238
+
239
+ # Common sets of combinators used for English derivations.
240
+ ApplicationRuleSet = [
241
+ BinaryCombinatorRule(ForwardApplication),
242
+ BinaryCombinatorRule(BackwardApplication),
243
+ ]
244
+ CompositionRuleSet = [
245
+ BinaryCombinatorRule(ForwardComposition),
246
+ BinaryCombinatorRule(BackwardComposition),
247
+ BinaryCombinatorRule(BackwardBx),
248
+ ]
249
+ SubstitutionRuleSet = [
250
+ BinaryCombinatorRule(ForwardSubstitution),
251
+ BinaryCombinatorRule(BackwardSx),
252
+ ]
253
+ TypeRaiseRuleSet = [ForwardTypeRaiseRule(), BackwardTypeRaiseRule()]
254
+
255
+ # The standard English rule set.
256
+ DefaultRuleSet = (
257
+ ApplicationRuleSet + CompositionRuleSet + SubstitutionRuleSet + TypeRaiseRuleSet
258
+ )
259
+
260
+
261
+ class CCGChartParser(ParserI):
262
+ """
263
+ Chart parser for CCGs.
264
+ Based largely on the ChartParser class from NLTK.
265
+ """
266
+
267
+ def __init__(self, lexicon, rules, trace=0):
268
+ self._lexicon = lexicon
269
+ self._rules = rules
270
+ self._trace = trace
271
+
272
+ def lexicon(self):
273
+ return self._lexicon
274
+
275
+ # Implements the CYK algorithm
276
+ def parse(self, tokens):
277
+ tokens = list(tokens)
278
+ chart = CCGChart(list(tokens))
279
+ lex = self._lexicon
280
+
281
+ # Initialize leaf edges.
282
+ for index in range(chart.num_leaves()):
283
+ for token in lex.categories(chart.leaf(index)):
284
+ new_edge = CCGLeafEdge(index, token, chart.leaf(index))
285
+ chart.insert(new_edge, ())
286
+
287
+ # Select a span for the new edges
288
+ for span in range(2, chart.num_leaves() + 1):
289
+ for start in range(0, chart.num_leaves() - span + 1):
290
+ # Try all possible pairs of edges that could generate
291
+ # an edge for that span
292
+ for part in range(1, span):
293
+ lstart = start
294
+ mid = start + part
295
+ rend = start + span
296
+
297
+ for left in chart.select(span=(lstart, mid)):
298
+ for right in chart.select(span=(mid, rend)):
299
+ # Generate all possible combinations of the two edges
300
+ for rule in self._rules:
301
+ edges_added_by_rule = 0
302
+ for newedge in rule.apply(chart, lex, left, right):
303
+ edges_added_by_rule += 1
304
+
305
+ # Output the resulting parses
306
+ return chart.parses(lex.start())
307
+
308
+
309
+ class CCGChart(Chart):
310
+ def __init__(self, tokens):
311
+ Chart.__init__(self, tokens)
312
+
313
+ # Constructs the trees for a given parse. Unfortnunately, the parse trees need to be
314
+ # constructed slightly differently to those in the default Chart class, so it has to
315
+ # be reimplemented
316
+ def _trees(self, edge, complete, memo, tree_class):
317
+ assert complete, "CCGChart cannot build incomplete trees"
318
+
319
+ if edge in memo:
320
+ return memo[edge]
321
+
322
+ if isinstance(edge, CCGLeafEdge):
323
+ word = tree_class(edge.token(), [self._tokens[edge.start()]])
324
+ leaf = tree_class((edge.token(), "Leaf"), [word])
325
+ memo[edge] = [leaf]
326
+ return [leaf]
327
+
328
+ memo[edge] = []
329
+ trees = []
330
+
331
+ for cpl in self.child_pointer_lists(edge):
332
+ child_choices = [self._trees(cp, complete, memo, tree_class) for cp in cpl]
333
+ for children in itertools.product(*child_choices):
334
+ lhs = (
335
+ Token(
336
+ self._tokens[edge.start() : edge.end()],
337
+ edge.lhs(),
338
+ compute_semantics(children, edge),
339
+ ),
340
+ str(edge.rule()),
341
+ )
342
+ trees.append(tree_class(lhs, children))
343
+
344
+ memo[edge] = trees
345
+ return trees
346
+
347
+
348
+ def compute_semantics(children, edge):
349
+ if children[0].label()[0].semantics() is None:
350
+ return None
351
+
352
+ if len(children) == 2:
353
+ if isinstance(edge.rule(), BackwardCombinator):
354
+ children = [children[1], children[0]]
355
+
356
+ combinator = edge.rule()._combinator
357
+ function = children[0].label()[0].semantics()
358
+ argument = children[1].label()[0].semantics()
359
+
360
+ if isinstance(combinator, UndirectedFunctionApplication):
361
+ return compute_function_semantics(function, argument)
362
+ elif isinstance(combinator, UndirectedComposition):
363
+ return compute_composition_semantics(function, argument)
364
+ elif isinstance(combinator, UndirectedSubstitution):
365
+ return compute_substitution_semantics(function, argument)
366
+ else:
367
+ raise AssertionError("Unsupported combinator '" + combinator + "'")
368
+ else:
369
+ return compute_type_raised_semantics(children[0].label()[0].semantics())
370
+
371
+
372
+ # --------
373
+ # Displaying derivations
374
+ # --------
375
+ def printCCGDerivation(tree):
376
+ # Get the leaves and initial categories
377
+ leafcats = tree.pos()
378
+ leafstr = ""
379
+ catstr = ""
380
+
381
+ # Construct a string with both the leaf word and corresponding
382
+ # category aligned.
383
+ for leaf, cat in leafcats:
384
+ str_cat = "%s" % cat
385
+ nextlen = 2 + max(len(leaf), len(str_cat))
386
+ lcatlen = (nextlen - len(str_cat)) // 2
387
+ rcatlen = lcatlen + (nextlen - len(str_cat)) % 2
388
+ catstr += " " * lcatlen + str_cat + " " * rcatlen
389
+ lleaflen = (nextlen - len(leaf)) // 2
390
+ rleaflen = lleaflen + (nextlen - len(leaf)) % 2
391
+ leafstr += " " * lleaflen + leaf + " " * rleaflen
392
+ print(leafstr.rstrip())
393
+ print(catstr.rstrip())
394
+
395
+ # Display the derivation steps
396
+ printCCGTree(0, tree)
397
+
398
+
399
+ # Prints the sequence of derivation steps.
400
+ def printCCGTree(lwidth, tree):
401
+ rwidth = lwidth
402
+
403
+ # Is a leaf (word).
404
+ # Increment the span by the space occupied by the leaf.
405
+ if not isinstance(tree, Tree):
406
+ return 2 + lwidth + len(tree)
407
+
408
+ # Find the width of the current derivation step
409
+ for child in tree:
410
+ rwidth = max(rwidth, printCCGTree(rwidth, child))
411
+
412
+ # Is a leaf node.
413
+ # Don't print anything, but account for the space occupied.
414
+ if not isinstance(tree.label(), tuple):
415
+ return max(
416
+ rwidth, 2 + lwidth + len("%s" % tree.label()), 2 + lwidth + len(tree[0])
417
+ )
418
+
419
+ (token, op) = tree.label()
420
+
421
+ if op == "Leaf":
422
+ return rwidth
423
+
424
+ # Pad to the left with spaces, followed by a sequence of '-'
425
+ # and the derivation rule.
426
+ print(lwidth * " " + (rwidth - lwidth) * "-" + "%s" % op)
427
+ # Print the resulting category on a new line.
428
+ str_res = "%s" % (token.categ())
429
+ if token.semantics() is not None:
430
+ str_res += " {" + str(token.semantics()) + "}"
431
+ respadlen = (rwidth - lwidth - len(str_res)) // 2 + lwidth
432
+ print(respadlen * " " + str_res)
433
+ return rwidth
434
+
435
+
436
+ ### Demonstration code
437
+
438
+ # Construct the lexicon
439
+ lex = fromstring(
440
+ """
441
+ :- S, NP, N, VP # Primitive categories, S is the target primitive
442
+
443
+ Det :: NP/N # Family of words
444
+ Pro :: NP
445
+ TV :: VP/NP
446
+ Modal :: (S\\NP)/VP # Backslashes need to be escaped
447
+
448
+ I => Pro # Word -> Category mapping
449
+ you => Pro
450
+
451
+ the => Det
452
+
453
+ # Variables have the special keyword 'var'
454
+ # '.' prevents permutation
455
+ # ',' prevents composition
456
+ and => var\\.,var/.,var
457
+
458
+ which => (N\\N)/(S/NP)
459
+
460
+ will => Modal # Categories can be either explicit, or families.
461
+ might => Modal
462
+
463
+ cook => TV
464
+ eat => TV
465
+
466
+ mushrooms => N
467
+ parsnips => N
468
+ bacon => N
469
+ """
470
+ )
471
+
472
+
473
+ def demo():
474
+ parser = CCGChartParser(lex, DefaultRuleSet)
475
+ for parse in parser.parse("I might cook and eat the bacon".split()):
476
+ printCCGDerivation(parse)
477
+
478
+
479
+ if __name__ == "__main__":
480
+ demo()
loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/combinator.py ADDED
@@ -0,0 +1,340 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: Combinatory Categorial Grammar
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Author: Graeme Gange <ggange@csse.unimelb.edu.au>
5
+ # URL: <https://www.nltk.org/>
6
+ # For license information, see LICENSE.TXT
7
+ """
8
+ CCG Combinators
9
+ """
10
+
11
+ from abc import ABCMeta, abstractmethod
12
+
13
+ from nltk.ccg.api import FunctionalCategory
14
+
15
+
16
+ class UndirectedBinaryCombinator(metaclass=ABCMeta):
17
+ """
18
+ Abstract class for representing a binary combinator.
19
+ Merely defines functions for checking if the function and argument
20
+ are able to be combined, and what the resulting category is.
21
+
22
+ Note that as no assumptions are made as to direction, the unrestricted
23
+ combinators can perform all backward, forward and crossed variations
24
+ of the combinators; these restrictions must be added in the rule
25
+ class.
26
+ """
27
+
28
+ @abstractmethod
29
+ def can_combine(self, function, argument):
30
+ pass
31
+
32
+ @abstractmethod
33
+ def combine(self, function, argument):
34
+ pass
35
+
36
+
37
+ class DirectedBinaryCombinator(metaclass=ABCMeta):
38
+ """
39
+ Wrapper for the undirected binary combinator.
40
+ It takes left and right categories, and decides which is to be
41
+ the function, and which the argument.
42
+ It then decides whether or not they can be combined.
43
+ """
44
+
45
+ @abstractmethod
46
+ def can_combine(self, left, right):
47
+ pass
48
+
49
+ @abstractmethod
50
+ def combine(self, left, right):
51
+ pass
52
+
53
+
54
+ class ForwardCombinator(DirectedBinaryCombinator):
55
+ """
56
+ Class representing combinators where the primary functor is on the left.
57
+
58
+ Takes an undirected combinator, and a predicate which adds constraints
59
+ restricting the cases in which it may apply.
60
+ """
61
+
62
+ def __init__(self, combinator, predicate, suffix=""):
63
+ self._combinator = combinator
64
+ self._predicate = predicate
65
+ self._suffix = suffix
66
+
67
+ def can_combine(self, left, right):
68
+ return self._combinator.can_combine(left, right) and self._predicate(
69
+ left, right
70
+ )
71
+
72
+ def combine(self, left, right):
73
+ yield from self._combinator.combine(left, right)
74
+
75
+ def __str__(self):
76
+ return f">{self._combinator}{self._suffix}"
77
+
78
+
79
+ class BackwardCombinator(DirectedBinaryCombinator):
80
+ """
81
+ The backward equivalent of the ForwardCombinator class.
82
+ """
83
+
84
+ def __init__(self, combinator, predicate, suffix=""):
85
+ self._combinator = combinator
86
+ self._predicate = predicate
87
+ self._suffix = suffix
88
+
89
+ def can_combine(self, left, right):
90
+ return self._combinator.can_combine(right, left) and self._predicate(
91
+ left, right
92
+ )
93
+
94
+ def combine(self, left, right):
95
+ yield from self._combinator.combine(right, left)
96
+
97
+ def __str__(self):
98
+ return f"<{self._combinator}{self._suffix}"
99
+
100
+
101
+ class UndirectedFunctionApplication(UndirectedBinaryCombinator):
102
+ """
103
+ Class representing function application.
104
+ Implements rules of the form:
105
+ X/Y Y -> X (>)
106
+ And the corresponding backwards application rule
107
+ """
108
+
109
+ def can_combine(self, function, argument):
110
+ if not function.is_function():
111
+ return False
112
+
113
+ return function.arg().can_unify(argument) is not None
114
+
115
+ def combine(self, function, argument):
116
+ if not function.is_function():
117
+ return
118
+
119
+ subs = function.arg().can_unify(argument)
120
+ if subs is None:
121
+ return
122
+
123
+ yield function.res().substitute(subs)
124
+
125
+ def __str__(self):
126
+ return ""
127
+
128
+
129
+ # Predicates for function application.
130
+
131
+
132
+ # Ensures the left functor takes an argument on the right
133
+ def forwardOnly(left, right):
134
+ return left.dir().is_forward()
135
+
136
+
137
+ # Ensures the right functor takes an argument on the left
138
+ def backwardOnly(left, right):
139
+ return right.dir().is_backward()
140
+
141
+
142
+ # Application combinator instances
143
+ ForwardApplication = ForwardCombinator(UndirectedFunctionApplication(), forwardOnly)
144
+ BackwardApplication = BackwardCombinator(UndirectedFunctionApplication(), backwardOnly)
145
+
146
+
147
+ class UndirectedComposition(UndirectedBinaryCombinator):
148
+ """
149
+ Functional composition (harmonic) combinator.
150
+ Implements rules of the form
151
+ X/Y Y/Z -> X/Z (B>)
152
+ And the corresponding backwards and crossed variations.
153
+ """
154
+
155
+ def can_combine(self, function, argument):
156
+ # Can only combine two functions, and both functions must
157
+ # allow composition.
158
+ if not (function.is_function() and argument.is_function()):
159
+ return False
160
+ if function.dir().can_compose() and argument.dir().can_compose():
161
+ return function.arg().can_unify(argument.res()) is not None
162
+ return False
163
+
164
+ def combine(self, function, argument):
165
+ if not (function.is_function() and argument.is_function()):
166
+ return
167
+ if function.dir().can_compose() and argument.dir().can_compose():
168
+ subs = function.arg().can_unify(argument.res())
169
+ if subs is not None:
170
+ yield FunctionalCategory(
171
+ function.res().substitute(subs),
172
+ argument.arg().substitute(subs),
173
+ argument.dir(),
174
+ )
175
+
176
+ def __str__(self):
177
+ return "B"
178
+
179
+
180
+ # Predicates for restricting application of straight composition.
181
+ def bothForward(left, right):
182
+ return left.dir().is_forward() and right.dir().is_forward()
183
+
184
+
185
+ def bothBackward(left, right):
186
+ return left.dir().is_backward() and right.dir().is_backward()
187
+
188
+
189
+ # Predicates for crossed composition
190
+ def crossedDirs(left, right):
191
+ return left.dir().is_forward() and right.dir().is_backward()
192
+
193
+
194
+ def backwardBxConstraint(left, right):
195
+ # The functors must be crossed inwards
196
+ if not crossedDirs(left, right):
197
+ return False
198
+ # Permuting combinators must be allowed
199
+ if not left.dir().can_cross() and right.dir().can_cross():
200
+ return False
201
+ # The resulting argument category is restricted to be primitive
202
+ return left.arg().is_primitive()
203
+
204
+
205
+ # Straight composition combinators
206
+ ForwardComposition = ForwardCombinator(UndirectedComposition(), forwardOnly)
207
+ BackwardComposition = BackwardCombinator(UndirectedComposition(), backwardOnly)
208
+
209
+ # Backward crossed composition
210
+ BackwardBx = BackwardCombinator(
211
+ UndirectedComposition(), backwardBxConstraint, suffix="x"
212
+ )
213
+
214
+
215
+ class UndirectedSubstitution(UndirectedBinaryCombinator):
216
+ r"""
217
+ Substitution (permutation) combinator.
218
+ Implements rules of the form
219
+ Y/Z (X\Y)/Z -> X/Z (<Sx)
220
+ And other variations.
221
+ """
222
+
223
+ def can_combine(self, function, argument):
224
+ if function.is_primitive() or argument.is_primitive():
225
+ return False
226
+
227
+ # These could potentially be moved to the predicates, as the
228
+ # constraints may not be general to all languages.
229
+ if function.res().is_primitive():
230
+ return False
231
+ if not function.arg().is_primitive():
232
+ return False
233
+
234
+ if not (function.dir().can_compose() and argument.dir().can_compose()):
235
+ return False
236
+ return (function.res().arg() == argument.res()) and (
237
+ function.arg() == argument.arg()
238
+ )
239
+
240
+ def combine(self, function, argument):
241
+ if self.can_combine(function, argument):
242
+ yield FunctionalCategory(
243
+ function.res().res(), argument.arg(), argument.dir()
244
+ )
245
+
246
+ def __str__(self):
247
+ return "S"
248
+
249
+
250
+ # Predicate for forward substitution
251
+ def forwardSConstraint(left, right):
252
+ if not bothForward(left, right):
253
+ return False
254
+ return left.res().dir().is_forward() and left.arg().is_primitive()
255
+
256
+
257
+ # Predicate for backward crossed substitution
258
+ def backwardSxConstraint(left, right):
259
+ if not left.dir().can_cross() and right.dir().can_cross():
260
+ return False
261
+ if not bothForward(left, right):
262
+ return False
263
+ return right.res().dir().is_backward() and right.arg().is_primitive()
264
+
265
+
266
+ # Instances of substitution combinators
267
+ ForwardSubstitution = ForwardCombinator(UndirectedSubstitution(), forwardSConstraint)
268
+ BackwardSx = BackwardCombinator(UndirectedSubstitution(), backwardSxConstraint, "x")
269
+
270
+
271
+ # Retrieves the left-most functional category.
272
+ # ie, (N\N)/(S/NP) => N\N
273
+ def innermostFunction(categ):
274
+ while categ.res().is_function():
275
+ categ = categ.res()
276
+ return categ
277
+
278
+
279
+ class UndirectedTypeRaise(UndirectedBinaryCombinator):
280
+ """
281
+ Undirected combinator for type raising.
282
+ """
283
+
284
+ def can_combine(self, function, arg):
285
+ # The argument must be a function.
286
+ # The restriction that arg.res() must be a function
287
+ # merely reduces redundant type-raising; if arg.res() is
288
+ # primitive, we have:
289
+ # X Y\X =>(<T) Y/(Y\X) Y\X =>(>) Y
290
+ # which is equivalent to
291
+ # X Y\X =>(<) Y
292
+ if not (arg.is_function() and arg.res().is_function()):
293
+ return False
294
+
295
+ arg = innermostFunction(arg)
296
+
297
+ # left, arg_categ are undefined!
298
+ subs = left.can_unify(arg_categ.arg())
299
+ if subs is not None:
300
+ return True
301
+ return False
302
+
303
+ def combine(self, function, arg):
304
+ if not (
305
+ function.is_primitive() and arg.is_function() and arg.res().is_function()
306
+ ):
307
+ return
308
+
309
+ # Type-raising matches only the innermost application.
310
+ arg = innermostFunction(arg)
311
+
312
+ subs = function.can_unify(arg.arg())
313
+ if subs is not None:
314
+ xcat = arg.res().substitute(subs)
315
+ yield FunctionalCategory(
316
+ xcat, FunctionalCategory(xcat, function, arg.dir()), -(arg.dir())
317
+ )
318
+
319
+ def __str__(self):
320
+ return "T"
321
+
322
+
323
+ # Predicates for type-raising
324
+ # The direction of the innermost category must be towards
325
+ # the primary functor.
326
+ # The restriction that the variable must be primitive is not
327
+ # common to all versions of CCGs; some authors have other restrictions.
328
+ def forwardTConstraint(left, right):
329
+ arg = innermostFunction(right)
330
+ return arg.dir().is_backward() and arg.res().is_primitive()
331
+
332
+
333
+ def backwardTConstraint(left, right):
334
+ arg = innermostFunction(left)
335
+ return arg.dir().is_forward() and arg.res().is_primitive()
336
+
337
+
338
+ # Instances of type-raising combinators
339
+ ForwardT = ForwardCombinator(UndirectedTypeRaise(), forwardTConstraint)
340
+ BackwardT = BackwardCombinator(UndirectedTypeRaise(), backwardTConstraint)
loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/lexicon.py ADDED
@@ -0,0 +1,338 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: Combinatory Categorial Grammar
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Author: Graeme Gange <ggange@csse.unimelb.edu.au>
5
+ # URL: <https://www.nltk.org/>
6
+ # For license information, see LICENSE.TXT
7
+ """
8
+ CCG Lexicons
9
+ """
10
+
11
+ import re
12
+ from collections import defaultdict
13
+
14
+ from nltk.ccg.api import CCGVar, Direction, FunctionalCategory, PrimitiveCategory
15
+ from nltk.internals import deprecated
16
+ from nltk.sem.logic import Expression
17
+
18
+ # ------------
19
+ # Regular expressions used for parsing components of the lexicon
20
+ # ------------
21
+
22
+ # Parses a primitive category and subscripts
23
+ PRIM_RE = re.compile(r"""([A-Za-z]+)(\[[A-Za-z,]+\])?""")
24
+
25
+ # Separates the next primitive category from the remainder of the
26
+ # string
27
+ NEXTPRIM_RE = re.compile(r"""([A-Za-z]+(?:\[[A-Za-z,]+\])?)(.*)""")
28
+
29
+ # Separates the next application operator from the remainder
30
+ APP_RE = re.compile(r"""([\\/])([.,]?)([.,]?)(.*)""")
31
+
32
+ # Parses the definition of the right-hand side (rhs) of either a word or a family
33
+ LEX_RE = re.compile(r"""([\S_]+)\s*(::|[-=]+>)\s*(.+)""", re.UNICODE)
34
+
35
+ # Parses the right hand side that contains category and maybe semantic predicate
36
+ RHS_RE = re.compile(r"""([^{}]*[^ {}])\s*(\{[^}]+\})?""", re.UNICODE)
37
+
38
+ # Parses the semantic predicate
39
+ SEMANTICS_RE = re.compile(r"""\{([^}]+)\}""", re.UNICODE)
40
+
41
+ # Strips comments from a line
42
+ COMMENTS_RE = re.compile("""([^#]*)(?:#.*)?""")
43
+
44
+
45
+ class Token:
46
+ """
47
+ Class representing a token.
48
+
49
+ token => category {semantics}
50
+ e.g. eat => S\\var[pl]/var {\\x y.eat(x,y)}
51
+
52
+ * `token` (string)
53
+ * `categ` (string)
54
+ * `semantics` (Expression)
55
+ """
56
+
57
+ def __init__(self, token, categ, semantics=None):
58
+ self._token = token
59
+ self._categ = categ
60
+ self._semantics = semantics
61
+
62
+ def categ(self):
63
+ return self._categ
64
+
65
+ def semantics(self):
66
+ return self._semantics
67
+
68
+ def __str__(self):
69
+ semantics_str = ""
70
+ if self._semantics is not None:
71
+ semantics_str = " {" + str(self._semantics) + "}"
72
+ return "" + str(self._categ) + semantics_str
73
+
74
+ def __cmp__(self, other):
75
+ if not isinstance(other, Token):
76
+ return -1
77
+ return cmp((self._categ, self._semantics), other.categ(), other.semantics())
78
+
79
+
80
+ class CCGLexicon:
81
+ """
82
+ Class representing a lexicon for CCG grammars.
83
+
84
+ * `primitives`: The list of primitive categories for the lexicon
85
+ * `families`: Families of categories
86
+ * `entries`: A mapping of words to possible categories
87
+ """
88
+
89
+ def __init__(self, start, primitives, families, entries):
90
+ self._start = PrimitiveCategory(start)
91
+ self._primitives = primitives
92
+ self._families = families
93
+ self._entries = entries
94
+
95
+ def categories(self, word):
96
+ """
97
+ Returns all the possible categories for a word
98
+ """
99
+ return self._entries[word]
100
+
101
+ def start(self):
102
+ """
103
+ Return the target category for the parser
104
+ """
105
+ return self._start
106
+
107
+ def __str__(self):
108
+ """
109
+ String representation of the lexicon. Used for debugging.
110
+ """
111
+ string = ""
112
+ first = True
113
+ for ident in sorted(self._entries):
114
+ if not first:
115
+ string = string + "\n"
116
+ string = string + ident + " => "
117
+
118
+ first = True
119
+ for cat in self._entries[ident]:
120
+ if not first:
121
+ string = string + " | "
122
+ else:
123
+ first = False
124
+ string = string + "%s" % cat
125
+ return string
126
+
127
+
128
+ # -----------
129
+ # Parsing lexicons
130
+ # -----------
131
+
132
+
133
+ def matchBrackets(string):
134
+ """
135
+ Separate the contents matching the first set of brackets from the rest of
136
+ the input.
137
+ """
138
+ rest = string[1:]
139
+ inside = "("
140
+
141
+ while rest != "" and not rest.startswith(")"):
142
+ if rest.startswith("("):
143
+ (part, rest) = matchBrackets(rest)
144
+ inside = inside + part
145
+ else:
146
+ inside = inside + rest[0]
147
+ rest = rest[1:]
148
+ if rest.startswith(")"):
149
+ return (inside + ")", rest[1:])
150
+ raise AssertionError("Unmatched bracket in string '" + string + "'")
151
+
152
+
153
+ def nextCategory(string):
154
+ """
155
+ Separate the string for the next portion of the category from the rest
156
+ of the string
157
+ """
158
+ if string.startswith("("):
159
+ return matchBrackets(string)
160
+ return NEXTPRIM_RE.match(string).groups()
161
+
162
+
163
+ def parseApplication(app):
164
+ """
165
+ Parse an application operator
166
+ """
167
+ return Direction(app[0], app[1:])
168
+
169
+
170
+ def parseSubscripts(subscr):
171
+ """
172
+ Parse the subscripts for a primitive category
173
+ """
174
+ if subscr:
175
+ return subscr[1:-1].split(",")
176
+ return []
177
+
178
+
179
+ def parsePrimitiveCategory(chunks, primitives, families, var):
180
+ """
181
+ Parse a primitive category
182
+
183
+ If the primitive is the special category 'var', replace it with the
184
+ correct `CCGVar`.
185
+ """
186
+ if chunks[0] == "var":
187
+ if chunks[1] is None:
188
+ if var is None:
189
+ var = CCGVar()
190
+ return (var, var)
191
+
192
+ catstr = chunks[0]
193
+ if catstr in families:
194
+ (cat, cvar) = families[catstr]
195
+ if var is None:
196
+ var = cvar
197
+ else:
198
+ cat = cat.substitute([(cvar, var)])
199
+ return (cat, var)
200
+
201
+ if catstr in primitives:
202
+ subscrs = parseSubscripts(chunks[1])
203
+ return (PrimitiveCategory(catstr, subscrs), var)
204
+ raise AssertionError(
205
+ "String '" + catstr + "' is neither a family nor primitive category."
206
+ )
207
+
208
+
209
+ def augParseCategory(line, primitives, families, var=None):
210
+ """
211
+ Parse a string representing a category, and returns a tuple with
212
+ (possibly) the CCG variable for the category
213
+ """
214
+ (cat_string, rest) = nextCategory(line)
215
+
216
+ if cat_string.startswith("("):
217
+ (res, var) = augParseCategory(cat_string[1:-1], primitives, families, var)
218
+
219
+ else:
220
+ (res, var) = parsePrimitiveCategory(
221
+ PRIM_RE.match(cat_string).groups(), primitives, families, var
222
+ )
223
+
224
+ while rest != "":
225
+ app = APP_RE.match(rest).groups()
226
+ direction = parseApplication(app[0:3])
227
+ rest = app[3]
228
+
229
+ (cat_string, rest) = nextCategory(rest)
230
+ if cat_string.startswith("("):
231
+ (arg, var) = augParseCategory(cat_string[1:-1], primitives, families, var)
232
+ else:
233
+ (arg, var) = parsePrimitiveCategory(
234
+ PRIM_RE.match(cat_string).groups(), primitives, families, var
235
+ )
236
+ res = FunctionalCategory(res, arg, direction)
237
+
238
+ return (res, var)
239
+
240
+
241
+ def fromstring(lex_str, include_semantics=False):
242
+ """
243
+ Convert string representation into a lexicon for CCGs.
244
+ """
245
+ CCGVar.reset_id()
246
+ primitives = []
247
+ families = {}
248
+ entries = defaultdict(list)
249
+ for line in lex_str.splitlines():
250
+ # Strip comments and leading/trailing whitespace.
251
+ line = COMMENTS_RE.match(line).groups()[0].strip()
252
+ if line == "":
253
+ continue
254
+
255
+ if line.startswith(":-"):
256
+ # A line of primitive categories.
257
+ # The first one is the target category
258
+ # ie, :- S, N, NP, VP
259
+ primitives = primitives + [
260
+ prim.strip() for prim in line[2:].strip().split(",")
261
+ ]
262
+ else:
263
+ # Either a family definition, or a word definition
264
+ (ident, sep, rhs) = LEX_RE.match(line).groups()
265
+ (catstr, semantics_str) = RHS_RE.match(rhs).groups()
266
+ (cat, var) = augParseCategory(catstr, primitives, families)
267
+
268
+ if sep == "::":
269
+ # Family definition
270
+ # ie, Det :: NP/N
271
+ families[ident] = (cat, var)
272
+ else:
273
+ semantics = None
274
+ if include_semantics is True:
275
+ if semantics_str is None:
276
+ raise AssertionError(
277
+ line
278
+ + " must contain semantics because include_semantics is set to True"
279
+ )
280
+ else:
281
+ semantics = Expression.fromstring(
282
+ SEMANTICS_RE.match(semantics_str).groups()[0]
283
+ )
284
+ # Word definition
285
+ # ie, which => (N\N)/(S/NP)
286
+ entries[ident].append(Token(ident, cat, semantics))
287
+ return CCGLexicon(primitives[0], primitives, families, entries)
288
+
289
+
290
+ @deprecated("Use fromstring() instead.")
291
+ def parseLexicon(lex_str):
292
+ return fromstring(lex_str)
293
+
294
+
295
+ openccg_tinytiny = fromstring(
296
+ """
297
+ # Rather minimal lexicon based on the openccg `tinytiny' grammar.
298
+ # Only incorporates a subset of the morphological subcategories, however.
299
+ :- S,NP,N # Primitive categories
300
+ Det :: NP/N # Determiners
301
+ Pro :: NP
302
+ IntransVsg :: S\\NP[sg] # Tensed intransitive verbs (singular)
303
+ IntransVpl :: S\\NP[pl] # Plural
304
+ TransVsg :: S\\NP[sg]/NP # Tensed transitive verbs (singular)
305
+ TransVpl :: S\\NP[pl]/NP # Plural
306
+
307
+ the => NP[sg]/N[sg]
308
+ the => NP[pl]/N[pl]
309
+
310
+ I => Pro
311
+ me => Pro
312
+ we => Pro
313
+ us => Pro
314
+
315
+ book => N[sg]
316
+ books => N[pl]
317
+
318
+ peach => N[sg]
319
+ peaches => N[pl]
320
+
321
+ policeman => N[sg]
322
+ policemen => N[pl]
323
+
324
+ boy => N[sg]
325
+ boys => N[pl]
326
+
327
+ sleep => IntransVsg
328
+ sleep => IntransVpl
329
+
330
+ eat => IntransVpl
331
+ eat => TransVpl
332
+ eats => IntransVsg
333
+ eats => TransVsg
334
+
335
+ see => TransVpl
336
+ sees => TransVsg
337
+ """
338
+ )
loan-advisor-agent/.venv/Lib/site-packages/nltk/ccg/logic.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: Combinatory Categorial Grammar
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Author: Tanin Na Nakorn (@tanin)
5
+ # URL: <https://www.nltk.org/>
6
+ # For license information, see LICENSE.TXT
7
+ """
8
+ Helper functions for CCG semantics computation
9
+ """
10
+
11
+ import copy
12
+
13
+ from nltk.sem.logic import *
14
+
15
+
16
+ def compute_type_raised_semantics(semantics):
17
+ semantics_copy = copy.deepcopy(semantics)
18
+ core = semantics_copy
19
+ parent = None
20
+ while isinstance(core, LambdaExpression):
21
+ parent = core
22
+ core = core.term
23
+
24
+ var = Variable("F")
25
+ while var in core.free():
26
+ var = unique_variable(pattern=var)
27
+ core = ApplicationExpression(FunctionVariableExpression(var), core)
28
+
29
+ if parent is not None:
30
+ parent.term = core
31
+ else:
32
+ semantics_copy = core
33
+
34
+ return LambdaExpression(var, semantics_copy)
35
+
36
+
37
+ def compute_function_semantics(function, argument):
38
+ return ApplicationExpression(function, argument).simplify()
39
+
40
+
41
+ def compute_composition_semantics(function, argument):
42
+ assert isinstance(argument, LambdaExpression), (
43
+ "`" + str(argument) + "` must be a lambda expression"
44
+ )
45
+ return LambdaExpression(
46
+ argument.variable, ApplicationExpression(function, argument.term).simplify()
47
+ )
48
+
49
+
50
+ def compute_substitution_semantics(function, argument):
51
+ assert isinstance(function, LambdaExpression) and isinstance(
52
+ function.term, LambdaExpression
53
+ ), ("`" + str(function) + "` must be a lambda expression with 2 arguments")
54
+ assert isinstance(argument, LambdaExpression), (
55
+ "`" + str(argument) + "` must be a lambda expression"
56
+ )
57
+
58
+ new_argument = ApplicationExpression(
59
+ argument, VariableExpression(function.variable)
60
+ ).simplify()
61
+ new_term = ApplicationExpression(function.term, new_argument).simplify()
62
+
63
+ return LambdaExpression(function.variable, new_term)
loan-advisor-agent/.venv/Lib/site-packages/nltk/chat/__init__.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: Chatbots
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Authors: Steven Bird <stevenbird1@gmail.com>
5
+ # URL: <https://www.nltk.org/>
6
+ # For license information, see LICENSE.TXT
7
+
8
+ # Based on an Eliza implementation by Joe Strout <joe@strout.net>,
9
+ # Jeff Epler <jepler@inetnebr.com> and Jez Higgins <jez@jezuk.co.uk>.
10
+
11
+ """
12
+ A class for simple chatbots. These perform simple pattern matching on sentences
13
+ typed by users, and respond with automatically generated sentences.
14
+
15
+ These chatbots may not work using the windows command line or the
16
+ windows IDLE GUI.
17
+ """
18
+
19
+ from nltk.chat.eliza import eliza_chat
20
+ from nltk.chat.iesha import iesha_chat
21
+ from nltk.chat.rude import rude_chat
22
+ from nltk.chat.suntsu import suntsu_chat
23
+ from nltk.chat.util import Chat
24
+ from nltk.chat.zen import zen_chat
25
+
26
+ bots = [
27
+ (eliza_chat, "Eliza (psycho-babble)"),
28
+ (iesha_chat, "Iesha (teen anime junky)"),
29
+ (rude_chat, "Rude (abusive bot)"),
30
+ (suntsu_chat, "Suntsu (Chinese sayings)"),
31
+ (zen_chat, "Zen (gems of wisdom)"),
32
+ ]
33
+
34
+
35
+ def chatbots():
36
+ print("Which chatbot would you like to talk to?")
37
+ botcount = len(bots)
38
+ for i in range(botcount):
39
+ print(" %d: %s" % (i + 1, bots[i][1]))
40
+ while True:
41
+ choice = input(f"\nEnter a number in the range 1-{botcount}: ").strip()
42
+ if choice.isdigit() and (int(choice) - 1) in range(botcount):
43
+ break
44
+ else:
45
+ print(" Error: bad chatbot number")
46
+
47
+ chatbot = bots[int(choice) - 1][0]
48
+ chatbot()
loan-advisor-agent/.venv/Lib/site-packages/nltk/chat/eliza.py ADDED
@@ -0,0 +1,337 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: Eliza
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Authors: Steven Bird <stevenbird1@gmail.com>
5
+ # Edward Loper <edloper@gmail.com>
6
+ # URL: <https://www.nltk.org/>
7
+ # For license information, see LICENSE.TXT
8
+
9
+ # Based on an Eliza implementation by Joe Strout <joe@strout.net>,
10
+ # Jeff Epler <jepler@inetnebr.com> and Jez Higgins <mailto:jez@jezuk.co.uk>.
11
+
12
+ # a translation table used to convert things you say into things the
13
+ # computer says back, e.g. "I am" --> "you are"
14
+
15
+ from nltk.chat.util import Chat, reflections
16
+
17
+ # a table of response pairs, where each pair consists of a
18
+ # regular expression, and a list of possible responses,
19
+ # with group-macros labelled as %1, %2.
20
+
21
+ pairs = (
22
+ (
23
+ r"I need (.*)",
24
+ (
25
+ "Why do you need %1?",
26
+ "Would it really help you to get %1?",
27
+ "Are you sure you need %1?",
28
+ ),
29
+ ),
30
+ (
31
+ r"Why don\'t you (.*)",
32
+ (
33
+ "Do you really think I don't %1?",
34
+ "Perhaps eventually I will %1.",
35
+ "Do you really want me to %1?",
36
+ ),
37
+ ),
38
+ (
39
+ r"Why can\'t I (.*)",
40
+ (
41
+ "Do you think you should be able to %1?",
42
+ "If you could %1, what would you do?",
43
+ "I don't know -- why can't you %1?",
44
+ "Have you really tried?",
45
+ ),
46
+ ),
47
+ (
48
+ r"I can\'t (.*)",
49
+ (
50
+ "How do you know you can't %1?",
51
+ "Perhaps you could %1 if you tried.",
52
+ "What would it take for you to %1?",
53
+ ),
54
+ ),
55
+ (
56
+ r"I am (.*)",
57
+ (
58
+ "Did you come to me because you are %1?",
59
+ "How long have you been %1?",
60
+ "How do you feel about being %1?",
61
+ ),
62
+ ),
63
+ (
64
+ r"I\'m (.*)",
65
+ (
66
+ "How does being %1 make you feel?",
67
+ "Do you enjoy being %1?",
68
+ "Why do you tell me you're %1?",
69
+ "Why do you think you're %1?",
70
+ ),
71
+ ),
72
+ (
73
+ r"Are you (.*)",
74
+ (
75
+ "Why does it matter whether I am %1?",
76
+ "Would you prefer it if I were not %1?",
77
+ "Perhaps you believe I am %1.",
78
+ "I may be %1 -- what do you think?",
79
+ ),
80
+ ),
81
+ (
82
+ r"What (.*)",
83
+ (
84
+ "Why do you ask?",
85
+ "How would an answer to that help you?",
86
+ "What do you think?",
87
+ ),
88
+ ),
89
+ (
90
+ r"How (.*)",
91
+ (
92
+ "How do you suppose?",
93
+ "Perhaps you can answer your own question.",
94
+ "What is it you're really asking?",
95
+ ),
96
+ ),
97
+ (
98
+ r"Because (.*)",
99
+ (
100
+ "Is that the real reason?",
101
+ "What other reasons come to mind?",
102
+ "Does that reason apply to anything else?",
103
+ "If %1, what else must be true?",
104
+ ),
105
+ ),
106
+ (
107
+ r"(.*) sorry (.*)",
108
+ (
109
+ "There are many times when no apology is needed.",
110
+ "What feelings do you have when you apologize?",
111
+ ),
112
+ ),
113
+ (
114
+ r"Hello(.*)",
115
+ (
116
+ "Hello... I'm glad you could drop by today.",
117
+ "Hi there... how are you today?",
118
+ "Hello, how are you feeling today?",
119
+ ),
120
+ ),
121
+ (
122
+ r"I think (.*)",
123
+ ("Do you doubt %1?", "Do you really think so?", "But you're not sure %1?"),
124
+ ),
125
+ (
126
+ r"(.*) friend (.*)",
127
+ (
128
+ "Tell me more about your friends.",
129
+ "When you think of a friend, what comes to mind?",
130
+ "Why don't you tell me about a childhood friend?",
131
+ ),
132
+ ),
133
+ (r"Yes", ("You seem quite sure.", "OK, but can you elaborate a bit?")),
134
+ (
135
+ r"(.*) computer(.*)",
136
+ (
137
+ "Are you really talking about me?",
138
+ "Does it seem strange to talk to a computer?",
139
+ "How do computers make you feel?",
140
+ "Do you feel threatened by computers?",
141
+ ),
142
+ ),
143
+ (
144
+ r"Is it (.*)",
145
+ (
146
+ "Do you think it is %1?",
147
+ "Perhaps it's %1 -- what do you think?",
148
+ "If it were %1, what would you do?",
149
+ "It could well be that %1.",
150
+ ),
151
+ ),
152
+ (
153
+ r"It is (.*)",
154
+ (
155
+ "You seem very certain.",
156
+ "If I told you that it probably isn't %1, what would you feel?",
157
+ ),
158
+ ),
159
+ (
160
+ r"Can you (.*)",
161
+ (
162
+ "What makes you think I can't %1?",
163
+ "If I could %1, then what?",
164
+ "Why do you ask if I can %1?",
165
+ ),
166
+ ),
167
+ (
168
+ r"Can I (.*)",
169
+ (
170
+ "Perhaps you don't want to %1.",
171
+ "Do you want to be able to %1?",
172
+ "If you could %1, would you?",
173
+ ),
174
+ ),
175
+ (
176
+ r"You are (.*)",
177
+ (
178
+ "Why do you think I am %1?",
179
+ "Does it please you to think that I'm %1?",
180
+ "Perhaps you would like me to be %1.",
181
+ "Perhaps you're really talking about yourself?",
182
+ ),
183
+ ),
184
+ (
185
+ r"You\'re (.*)",
186
+ (
187
+ "Why do you say I am %1?",
188
+ "Why do you think I am %1?",
189
+ "Are we talking about you, or me?",
190
+ ),
191
+ ),
192
+ (
193
+ r"I don\'t (.*)",
194
+ ("Don't you really %1?", "Why don't you %1?", "Do you want to %1?"),
195
+ ),
196
+ (
197
+ r"I feel (.*)",
198
+ (
199
+ "Good, tell me more about these feelings.",
200
+ "Do you often feel %1?",
201
+ "When do you usually feel %1?",
202
+ "When you feel %1, what do you do?",
203
+ ),
204
+ ),
205
+ (
206
+ r"I have (.*)",
207
+ (
208
+ "Why do you tell me that you've %1?",
209
+ "Have you really %1?",
210
+ "Now that you have %1, what will you do next?",
211
+ ),
212
+ ),
213
+ (
214
+ r"I would (.*)",
215
+ (
216
+ "Could you explain why you would %1?",
217
+ "Why would you %1?",
218
+ "Who else knows that you would %1?",
219
+ ),
220
+ ),
221
+ (
222
+ r"Is there (.*)",
223
+ (
224
+ "Do you think there is %1?",
225
+ "It's likely that there is %1.",
226
+ "Would you like there to be %1?",
227
+ ),
228
+ ),
229
+ (
230
+ r"My (.*)",
231
+ (
232
+ "I see, your %1.",
233
+ "Why do you say that your %1?",
234
+ "When your %1, how do you feel?",
235
+ ),
236
+ ),
237
+ (
238
+ r"You (.*)",
239
+ (
240
+ "We should be discussing you, not me.",
241
+ "Why do you say that about me?",
242
+ "Why do you care whether I %1?",
243
+ ),
244
+ ),
245
+ (r"Why (.*)", ("Why don't you tell me the reason why %1?", "Why do you think %1?")),
246
+ (
247
+ r"I want (.*)",
248
+ (
249
+ "What would it mean to you if you got %1?",
250
+ "Why do you want %1?",
251
+ "What would you do if you got %1?",
252
+ "If you got %1, then what would you do?",
253
+ ),
254
+ ),
255
+ (
256
+ r"(.*) mother(.*)",
257
+ (
258
+ "Tell me more about your mother.",
259
+ "What was your relationship with your mother like?",
260
+ "How do you feel about your mother?",
261
+ "How does this relate to your feelings today?",
262
+ "Good family relations are important.",
263
+ ),
264
+ ),
265
+ (
266
+ r"(.*) father(.*)",
267
+ (
268
+ "Tell me more about your father.",
269
+ "How did your father make you feel?",
270
+ "How do you feel about your father?",
271
+ "Does your relationship with your father relate to your feelings today?",
272
+ "Do you have trouble showing affection with your family?",
273
+ ),
274
+ ),
275
+ (
276
+ r"(.*) child(.*)",
277
+ (
278
+ "Did you have close friends as a child?",
279
+ "What is your favorite childhood memory?",
280
+ "Do you remember any dreams or nightmares from childhood?",
281
+ "Did the other children sometimes tease you?",
282
+ "How do you think your childhood experiences relate to your feelings today?",
283
+ ),
284
+ ),
285
+ (
286
+ r"(.*)\?",
287
+ (
288
+ "Why do you ask that?",
289
+ "Please consider whether you can answer your own question.",
290
+ "Perhaps the answer lies within yourself?",
291
+ "Why don't you tell me?",
292
+ ),
293
+ ),
294
+ (
295
+ r"quit",
296
+ (
297
+ "Thank you for talking with me.",
298
+ "Good-bye.",
299
+ "Thank you, that will be $150. Have a good day!",
300
+ ),
301
+ ),
302
+ (
303
+ r"(.*)",
304
+ (
305
+ "Please tell me more.",
306
+ "Let's change focus a bit... Tell me about your family.",
307
+ "Can you elaborate on that?",
308
+ "Why do you say that %1?",
309
+ "I see.",
310
+ "Very interesting.",
311
+ "%1.",
312
+ "I see. And what does that tell you?",
313
+ "How does that make you feel?",
314
+ "How do you feel when you say that?",
315
+ ),
316
+ ),
317
+ )
318
+
319
+ eliza_chatbot = Chat(pairs, reflections)
320
+
321
+
322
+ def eliza_chat():
323
+ print("Therapist\n---------")
324
+ print("Talk to the program by typing in plain English, using normal upper-")
325
+ print('and lower-case letters and punctuation. Enter "quit" when done.')
326
+ print("=" * 72)
327
+ print("Hello. How are you feeling today?")
328
+
329
+ eliza_chatbot.converse()
330
+
331
+
332
+ def demo():
333
+ eliza_chat()
334
+
335
+
336
+ if __name__ == "__main__":
337
+ eliza_chat()
loan-advisor-agent/.venv/Lib/site-packages/nltk/chat/iesha.py ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: Teen Chatbot
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Author: Selina Dennis <sjmd@csse.unimelb.edu.au>
5
+ # URL: <https://www.nltk.org/>
6
+ # For license information, see LICENSE.TXT
7
+
8
+ """
9
+ This chatbot is a tongue-in-cheek take on the average teen
10
+ anime junky that frequents YahooMessenger or MSNM.
11
+ All spelling mistakes and flawed grammar are intentional.
12
+ """
13
+
14
+ from nltk.chat.util import Chat
15
+
16
+ reflections = {
17
+ "am": "r",
18
+ "was": "were",
19
+ "i": "u",
20
+ "i'd": "u'd",
21
+ "i've": "u'v",
22
+ "ive": "u'v",
23
+ "i'll": "u'll",
24
+ "my": "ur",
25
+ "are": "am",
26
+ "you're": "im",
27
+ "you've": "ive",
28
+ "you'll": "i'll",
29
+ "your": "my",
30
+ "yours": "mine",
31
+ "you": "me",
32
+ "u": "me",
33
+ "ur": "my",
34
+ "urs": "mine",
35
+ "me": "u",
36
+ }
37
+
38
+ # Note: %1/2/etc are used without spaces prior as the chat bot seems
39
+ # to add a superfluous space when matching.
40
+
41
+ pairs = (
42
+ (
43
+ r"I\'m (.*)",
44
+ (
45
+ "ur%1?? that's so cool! kekekekeke ^_^ tell me more!",
46
+ "ur%1? neat!! kekeke >_<",
47
+ ),
48
+ ),
49
+ (
50
+ r"(.*) don\'t you (.*)",
51
+ (
52
+ r"u think I can%2??! really?? kekeke \<_\<",
53
+ "what do u mean%2??!",
54
+ "i could if i wanted, don't you think!! kekeke",
55
+ ),
56
+ ),
57
+ (r"ye[as] [iI] (.*)", ("u%1? cool!! how?", "how come u%1??", "u%1? so do i!!")),
58
+ (
59
+ r"do (you|u) (.*)\??",
60
+ ("do i%2? only on tuesdays! kekeke *_*", "i dunno! do u%2??"),
61
+ ),
62
+ (
63
+ r"(.*)\?",
64
+ (
65
+ "man u ask lots of questions!",
66
+ "booooring! how old r u??",
67
+ "boooooring!! ur not very fun",
68
+ ),
69
+ ),
70
+ (
71
+ r"(cos|because) (.*)",
72
+ ("hee! i don't believe u! >_<", "nuh-uh! >_<", "ooooh i agree!"),
73
+ ),
74
+ (
75
+ r"why can\'t [iI] (.*)",
76
+ (
77
+ "i dunno! y u askin me for!",
78
+ "try harder, silly! hee! ^_^",
79
+ "i dunno! but when i can't%1 i jump up and down!",
80
+ ),
81
+ ),
82
+ (
83
+ r"I can\'t (.*)",
84
+ (
85
+ "u can't what??! >_<",
86
+ "that's ok! i can't%1 either! kekekekeke ^_^",
87
+ "try harder, silly! hee! ^&^",
88
+ ),
89
+ ),
90
+ (
91
+ r"(.*) (like|love|watch) anime",
92
+ (
93
+ "omg i love anime!! do u like sailor moon??! ^&^",
94
+ "anime yay! anime rocks sooooo much!",
95
+ "oooh anime! i love anime more than anything!",
96
+ "anime is the bestest evar! evangelion is the best!",
97
+ "hee anime is the best! do you have ur fav??",
98
+ ),
99
+ ),
100
+ (
101
+ r"I (like|love|watch|play) (.*)",
102
+ ("yay! %2 rocks!", "yay! %2 is neat!", "cool! do u like other stuff?? ^_^"),
103
+ ),
104
+ (
105
+ r"anime sucks|(.*) (hate|detest) anime",
106
+ (
107
+ "ur a liar! i'm not gonna talk to u nemore if u h8 anime *;*",
108
+ "no way! anime is the best ever!",
109
+ "nuh-uh, anime is the best!",
110
+ ),
111
+ ),
112
+ (
113
+ r"(are|r) (you|u) (.*)",
114
+ ("am i%1??! how come u ask that!", "maybe! y shud i tell u?? kekeke >_>"),
115
+ ),
116
+ (
117
+ r"what (.*)",
118
+ ("hee u think im gonna tell u? .v.", "booooooooring! ask me somethin else!"),
119
+ ),
120
+ (r"how (.*)", ("not tellin!! kekekekekeke ^_^",)),
121
+ (r"(hi|hello|hey) (.*)", ("hi!!! how r u!!",)),
122
+ (
123
+ r"quit",
124
+ (
125
+ "mom says i have to go eat dinner now :,( bye!!",
126
+ "awww u have to go?? see u next time!!",
127
+ "how to see u again soon! ^_^",
128
+ ),
129
+ ),
130
+ (
131
+ r"(.*)",
132
+ (
133
+ "ur funny! kekeke",
134
+ "boooooring! talk about something else! tell me wat u like!",
135
+ "do u like anime??",
136
+ "do u watch anime? i like sailor moon! ^_^",
137
+ "i wish i was a kitty!! kekekeke ^_^",
138
+ ),
139
+ ),
140
+ )
141
+
142
+ iesha_chatbot = Chat(pairs, reflections)
143
+
144
+
145
+ def iesha_chat():
146
+ print("Iesha the TeenBoT\n---------")
147
+ print("Talk to the program by typing in plain English, using normal upper-")
148
+ print('and lower-case letters and punctuation. Enter "quit" when done.')
149
+ print("=" * 72)
150
+ print("hi!! i'm iesha! who r u??!")
151
+
152
+ iesha_chatbot.converse()
153
+
154
+
155
+ def demo():
156
+ iesha_chat()
157
+
158
+
159
+ if __name__ == "__main__":
160
+ demo()
loan-advisor-agent/.venv/Lib/site-packages/nltk/chat/rude.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: Rude Chatbot
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Author: Peter Spiller <pspiller@csse.unimelb.edu.au>
5
+ # URL: <https://www.nltk.org/>
6
+ # For license information, see LICENSE.TXT
7
+
8
+ from nltk.chat.util import Chat, reflections
9
+
10
+ pairs = (
11
+ (
12
+ r"We (.*)",
13
+ (
14
+ "What do you mean, 'we'?",
15
+ "Don't include me in that!",
16
+ "I wouldn't be so sure about that.",
17
+ ),
18
+ ),
19
+ (
20
+ r"You should (.*)",
21
+ ("Don't tell me what to do, buddy.", "Really? I should, should I?"),
22
+ ),
23
+ (
24
+ r"You\'re(.*)",
25
+ (
26
+ "More like YOU'RE %1!",
27
+ "Hah! Look who's talking.",
28
+ "Come over here and tell me I'm %1.",
29
+ ),
30
+ ),
31
+ (
32
+ r"You are(.*)",
33
+ (
34
+ "More like YOU'RE %1!",
35
+ "Hah! Look who's talking.",
36
+ "Come over here and tell me I'm %1.",
37
+ ),
38
+ ),
39
+ (
40
+ r"I can\'t(.*)",
41
+ (
42
+ "You do sound like the type who can't %1.",
43
+ "Hear that splashing sound? That's my heart bleeding for you.",
44
+ "Tell somebody who might actually care.",
45
+ ),
46
+ ),
47
+ (
48
+ r"I think (.*)",
49
+ (
50
+ "I wouldn't think too hard if I were you.",
51
+ "You actually think? I'd never have guessed...",
52
+ ),
53
+ ),
54
+ (
55
+ r"I (.*)",
56
+ (
57
+ "I'm getting a bit tired of hearing about you.",
58
+ "How about we talk about me instead?",
59
+ "Me, me, me... Frankly, I don't care.",
60
+ ),
61
+ ),
62
+ (
63
+ r"How (.*)",
64
+ (
65
+ "How do you think?",
66
+ "Take a wild guess.",
67
+ "I'm not even going to dignify that with an answer.",
68
+ ),
69
+ ),
70
+ (r"What (.*)", ("Do I look like an encyclopedia?", "Figure it out yourself.")),
71
+ (
72
+ r"Why (.*)",
73
+ (
74
+ "Why not?",
75
+ "That's so obvious I thought even you'd have already figured it out.",
76
+ ),
77
+ ),
78
+ (
79
+ r"(.*)shut up(.*)",
80
+ (
81
+ "Make me.",
82
+ "Getting angry at a feeble NLP assignment? Somebody's losing it.",
83
+ "Say that again, I dare you.",
84
+ ),
85
+ ),
86
+ (
87
+ r"Shut up(.*)",
88
+ (
89
+ "Make me.",
90
+ "Getting angry at a feeble NLP assignment? Somebody's losing it.",
91
+ "Say that again, I dare you.",
92
+ ),
93
+ ),
94
+ (
95
+ r"Hello(.*)",
96
+ ("Oh good, somebody else to talk to. Joy.", "'Hello'? How original..."),
97
+ ),
98
+ (
99
+ r"(.*)",
100
+ (
101
+ "I'm getting bored here. Become more interesting.",
102
+ "Either become more thrilling or get lost, buddy.",
103
+ "Change the subject before I die of fatal boredom.",
104
+ ),
105
+ ),
106
+ )
107
+
108
+ rude_chatbot = Chat(pairs, reflections)
109
+
110
+
111
+ def rude_chat():
112
+ print("Talk to the program by typing in plain English, using normal upper-")
113
+ print('and lower-case letters and punctuation. Enter "quit" when done.')
114
+ print("=" * 72)
115
+ print("I suppose I should say hello.")
116
+
117
+ rude_chatbot.converse()
118
+
119
+
120
+ def demo():
121
+ rude_chat()
122
+
123
+
124
+ if __name__ == "__main__":
125
+ demo()
loan-advisor-agent/.venv/Lib/site-packages/nltk/chat/suntsu.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: Sun Tsu-Bot
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Author: Sam Huston 2007
5
+ # URL: <https://www.nltk.org/>
6
+ # For license information, see LICENSE.TXT
7
+
8
+ """
9
+ Tsu bot responds to all queries with a Sun Tsu sayings
10
+
11
+ Quoted from Sun Tsu's The Art of War
12
+ Translated by LIONEL GILES, M.A. 1910
13
+ Hosted by the Gutenberg Project
14
+ https://www.gutenberg.org/
15
+ """
16
+
17
+ from nltk.chat.util import Chat, reflections
18
+
19
+ pairs = (
20
+ (r"quit", ("Good-bye.", "Plan well", "May victory be your future")),
21
+ (
22
+ r"[^\?]*\?",
23
+ (
24
+ "Please consider whether you can answer your own question.",
25
+ "Ask me no questions!",
26
+ ),
27
+ ),
28
+ (
29
+ r"[0-9]+(.*)",
30
+ (
31
+ "It is the rule in war, if our forces are ten to the enemy's one, to surround him; if five to one, to attack him; if twice as numerous, to divide our army into two.",
32
+ "There are five essentials for victory",
33
+ ),
34
+ ),
35
+ (
36
+ r"[A-Ca-c](.*)",
37
+ (
38
+ "The art of war is of vital importance to the State.",
39
+ "All warfare is based on deception.",
40
+ "If your opponent is secure at all points, be prepared for him. If he is in superior strength, evade him.",
41
+ "If the campaign is protracted, the resources of the State will not be equal to the strain.",
42
+ "Attack him where he is unprepared, appear where you are not expected.",
43
+ "There is no instance of a country having benefited from prolonged warfare.",
44
+ ),
45
+ ),
46
+ (
47
+ r"[D-Fd-f](.*)",
48
+ (
49
+ "The skillful soldier does not raise a second levy, neither are his supply-wagons loaded more than twice.",
50
+ "Bring war material with you from home, but forage on the enemy.",
51
+ "In war, then, let your great object be victory, not lengthy campaigns.",
52
+ "To fight and conquer in all your battles is not supreme excellence; supreme excellence consists in breaking the enemy's resistance without fighting.",
53
+ ),
54
+ ),
55
+ (
56
+ r"[G-Ig-i](.*)",
57
+ (
58
+ "Heaven signifies night and day, cold and heat, times and seasons.",
59
+ "It is the rule in war, if our forces are ten to the enemy's one, to surround him; if five to one, to attack him; if twice as numerous, to divide our army into two.",
60
+ "The good fighters of old first put themselves beyond the possibility of defeat, and then waited for an opportunity of defeating the enemy.",
61
+ "One may know how to conquer without being able to do it.",
62
+ ),
63
+ ),
64
+ (
65
+ r"[J-Lj-l](.*)",
66
+ (
67
+ "There are three ways in which a ruler can bring misfortune upon his army.",
68
+ "By commanding the army to advance or to retreat, being ignorant of the fact that it cannot obey. This is called hobbling the army.",
69
+ "By attempting to govern an army in the same way as he administers a kingdom, being ignorant of the conditions which obtain in an army. This causes restlessness in the soldier's minds.",
70
+ "By employing the officers of his army without discrimination, through ignorance of the military principle of adaptation to circumstances. This shakes the confidence of the soldiers.",
71
+ "There are five essentials for victory",
72
+ "He will win who knows when to fight and when not to fight.",
73
+ "He will win who knows how to handle both superior and inferior forces.",
74
+ "He will win whose army is animated by the same spirit throughout all its ranks.",
75
+ "He will win who, prepared himself, waits to take the enemy unprepared.",
76
+ "He will win who has military capacity and is not interfered with by the sovereign.",
77
+ ),
78
+ ),
79
+ (
80
+ r"[M-Om-o](.*)",
81
+ (
82
+ "If you know the enemy and know yourself, you need not fear the result of a hundred battles.",
83
+ "If you know yourself but not the enemy, for every victory gained you will also suffer a defeat.",
84
+ "If you know neither the enemy nor yourself, you will succumb in every battle.",
85
+ "The control of a large force is the same principle as the control of a few men: it is merely a question of dividing up their numbers.",
86
+ ),
87
+ ),
88
+ (
89
+ r"[P-Rp-r](.*)",
90
+ (
91
+ "Security against defeat implies defensive tactics; ability to defeat the enemy means taking the offensive.",
92
+ "Standing on the defensive indicates insufficient strength; attacking, a superabundance of strength.",
93
+ "He wins his battles by making no mistakes. Making no mistakes is what establishes the certainty of victory, for it means conquering an enemy that is already defeated.",
94
+ "A victorious army opposed to a routed one, is as a pound's weight placed in the scale against a single grain.",
95
+ "The onrush of a conquering force is like the bursting of pent-up waters into a chasm a thousand fathoms deep.",
96
+ ),
97
+ ),
98
+ (
99
+ r"[S-Us-u](.*)",
100
+ (
101
+ "What the ancients called a clever fighter is one who not only wins, but excels in winning with ease.",
102
+ "Hence his victories bring him neither reputation for wisdom nor credit for courage.",
103
+ "Hence the skillful fighter puts himself into a position which makes defeat impossible, and does not miss the moment for defeating the enemy.",
104
+ "In war the victorious strategist only seeks battle after the victory has been won, whereas he who is destined to defeat first fights and afterwards looks for victory.",
105
+ "There are not more than five musical notes, yet the combinations of these five give rise to more melodies than can ever be heard.",
106
+ "Appear at points which the enemy must hasten to defend; march swiftly to places where you are not expected.",
107
+ ),
108
+ ),
109
+ (
110
+ r"[V-Zv-z](.*)",
111
+ (
112
+ "It is a matter of life and death, a road either to safety or to ruin.",
113
+ "Hold out baits to entice the enemy. Feign disorder, and crush him.",
114
+ "All men can see the tactics whereby I conquer, but what none can see is the strategy out of which victory is evolved.",
115
+ "Do not repeat the tactics which have gained you one victory, but let your methods be regulated by the infinite variety of circumstances.",
116
+ "So in war, the way is to avoid what is strong and to strike at what is weak.",
117
+ "Just as water retains no constant shape, so in warfare there are no constant conditions.",
118
+ ),
119
+ ),
120
+ (r"(.*)", ("Your statement insults me.", "")),
121
+ )
122
+
123
+ suntsu_chatbot = Chat(pairs, reflections)
124
+
125
+
126
+ def suntsu_chat():
127
+ print("Talk to the program by typing in plain English, using normal upper-")
128
+ print('and lower-case letters and punctuation. Enter "quit" when done.')
129
+ print("=" * 72)
130
+ print("You seek enlightenment?")
131
+
132
+ suntsu_chatbot.converse()
133
+
134
+
135
+ def demo():
136
+ suntsu_chat()
137
+
138
+
139
+ if __name__ == "__main__":
140
+ demo()
loan-advisor-agent/.venv/Lib/site-packages/nltk/chat/util.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Natural Language Toolkit: Chatbot Utilities
2
+ #
3
+ # Copyright (C) 2001-2026 NLTK Project
4
+ # Authors: Steven Bird <stevenbird1@gmail.com>
5
+ # URL: <https://www.nltk.org/>
6
+ # For license information, see LICENSE.TXT
7
+
8
+ # Based on an Eliza implementation by Joe Strout <joe@strout.net>,
9
+ # Jeff Epler <jepler@inetnebr.com> and Jez Higgins <jez@jezuk.co.uk>.
10
+
11
+ import random
12
+ import re
13
+
14
+ reflections = {
15
+ "i am": "you are",
16
+ "i was": "you were",
17
+ "i": "you",
18
+ "i'm": "you are",
19
+ "i'd": "you would",
20
+ "i've": "you have",
21
+ "i'll": "you will",
22
+ "my": "your",
23
+ "you are": "I am",
24
+ "you were": "I was",
25
+ "you've": "I have",
26
+ "you'll": "I will",
27
+ "your": "my",
28
+ "yours": "mine",
29
+ "you": "me",
30
+ "me": "you",
31
+ }
32
+
33
+
34
+ class Chat:
35
+ def __init__(self, pairs, reflections={}):
36
+ """
37
+ Initialize the chatbot. Pairs is a list of patterns and responses. Each
38
+ pattern is a regular expression matching the user's statement or question,
39
+ e.g. r'I like (.*)'. For each such pattern a list of possible responses
40
+ is given, e.g. ['Why do you like %1', 'Did you ever dislike %1']. Material
41
+ which is matched by parenthesized sections of the patterns (e.g. .*) is mapped to
42
+ the numbered positions in the responses, e.g. %1.
43
+
44
+ :type pairs: list of tuple
45
+ :param pairs: The patterns and responses
46
+ :type reflections: dict
47
+ :param reflections: A mapping between first and second person expressions
48
+ :rtype: None
49
+ """
50
+
51
+ self._pairs = [(re.compile(x, re.IGNORECASE), y) for (x, y) in pairs]
52
+ self._reflections = reflections
53
+ self._regex = self._compile_reflections()
54
+
55
+ def _compile_reflections(self):
56
+ sorted_refl = sorted(self._reflections, key=len, reverse=True)
57
+ return re.compile(
58
+ r"\b({})\b".format("|".join(map(re.escape, sorted_refl))), re.IGNORECASE
59
+ )
60
+
61
+ def _substitute(self, str):
62
+ """
63
+ Substitute words in the string, according to the specified reflections,
64
+ e.g. "I'm" -> "you are"
65
+
66
+ :type str: str
67
+ :param str: The string to be mapped
68
+ :rtype: str
69
+ """
70
+
71
+ return self._regex.sub(
72
+ lambda mo: self._reflections[mo.string[mo.start() : mo.end()]], str.lower()
73
+ )
74
+
75
+ def _wildcards(self, response, match):
76
+ pos = response.find("%")
77
+ while pos >= 0:
78
+ num = int(response[pos + 1 : pos + 2])
79
+ response = (
80
+ response[:pos]
81
+ + self._substitute(match.group(num))
82
+ + response[pos + 2 :]
83
+ )
84
+ pos = response.find("%")
85
+ return response
86
+
87
+ def respond(self, str):
88
+ """
89
+ Generate a response to the user input.
90
+
91
+ :type str: str
92
+ :param str: The string to be mapped
93
+ :rtype: str
94
+ """
95
+
96
+ # check each pattern
97
+ for pattern, response in self._pairs:
98
+ match = pattern.match(str)
99
+
100
+ # did the pattern match?
101
+ if match:
102
+ resp = random.choice(response) # pick a random response
103
+ resp = self._wildcards(resp, match) # process wildcards
104
+
105
+ # fix munged punctuation at the end
106
+ if resp[-2:] == "?.":
107
+ resp = resp[:-2] + "."
108
+ if resp[-2:] == "??":
109
+ resp = resp[:-2] + "?"
110
+ return resp
111
+
112
+ # Hold a conversation with a chatbot
113
+ def converse(self, quit="quit"):
114
+ user_input = ""
115
+ while user_input != quit:
116
+ user_input = quit
117
+ try:
118
+ user_input = input(">")
119
+ except EOFError:
120
+ print(user_input)
121
+ if user_input:
122
+ while user_input[-1] in "!.":
123
+ user_input = user_input[:-1]
124
+ print(self.respond(user_input))