include dictionary parsing in first hinting
I though this would be to slow, but it's actually OK
This commit is contained in:
parent
f549b4aa1e
commit
aaad8588b6
@ -20,6 +20,7 @@
|
||||
"""A HintManager to draw hints over links."""
|
||||
|
||||
import os
|
||||
import string
|
||||
import math
|
||||
import functools
|
||||
import collections
|
||||
@ -154,8 +155,17 @@ class HintManager(QObject):
|
||||
|
||||
def _get_word_hints(self, words=[]):
|
||||
if not words:
|
||||
with open(os.path.join(standarddir.config(), "hints")) as hintfile:
|
||||
words.extend(hint.rstrip() for hint in hintfile)
|
||||
with open("/usr/share/dict/words") as wordfile:
|
||||
alphabet = set(string.ascii_lowercase)
|
||||
hints = set()
|
||||
lines = (line.rstrip().lower() for line in wordfile)
|
||||
for word in lines:
|
||||
if not set(word) <= alphabet: continue
|
||||
if not len(word) <= 4: continue
|
||||
for i in range(len(word)):
|
||||
hints.discard(word[:i+1])
|
||||
hints.add(word)
|
||||
words.extend(hints)
|
||||
return words
|
||||
|
||||
def _get_text(self):
|
||||
@ -207,7 +217,12 @@ class HintManager(QObject):
|
||||
A list of hint strings, in the same order as the elements.
|
||||
"""
|
||||
if config.get('hints', 'mode') == 'words':
|
||||
try:
|
||||
return self._get_word_hints()[:len(elems)]
|
||||
except IOError:
|
||||
message.error(self._win_id, "Word hints require a dictionary" +
|
||||
" at /usr/share/dict/words.", immediately=True)
|
||||
# falls back on letter hints
|
||||
if config.get('hints', 'mode') == 'number':
|
||||
chars = '0123456789'
|
||||
else:
|
||||
|
@ -1,39 +0,0 @@
|
||||
|
||||
import sys
|
||||
import string
|
||||
|
||||
def filter_hints(words):
|
||||
|
||||
alphabet = set("asdflkjqwerpoiu")
|
||||
hints = set()
|
||||
|
||||
for word in words:
|
||||
|
||||
# hints should be lowercase
|
||||
word = word.lower()
|
||||
|
||||
# hints should be alphabetic
|
||||
if not set(word) <= alphabet:
|
||||
continue
|
||||
|
||||
# hints shouldn't be longer than 5 characters
|
||||
if len(word) > 5:
|
||||
continue
|
||||
|
||||
# hints should not be prefixes of other hints. we prefer the
|
||||
# longer ones.
|
||||
for i in range(len(word)):
|
||||
hints.discard(word[:i+1])
|
||||
|
||||
hints.add(word)
|
||||
|
||||
yield from hints
|
||||
|
||||
def main():
|
||||
inlines = (line.rstrip() for line in sys.stdin)
|
||||
outlines = ("{}\n".format(hint) for hint in filter_hints(inlines))
|
||||
sys.stdout.writelines(outlines)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
Loading…
Reference in New Issue
Block a user