Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import json
  2. crData = {}
  3. with open('data/cr.json') as file:
  4. crData = json.load(file)
  5. crDataNames = crData['glossary'].keys()
  6. crDataNumbers = crData['sections'].keys()
  7. print('Registered {} CR glossary terms and {} CR sections'.format(len(crDataNames), len(crDataNumbers)))
  8. def cr_search(words):
  9. if not words:
  10. return 'I need some clues to search for, my master!'
  11. words = [word.casefold() for word in words]
  12. if words[0][0].isdigit():
  13. lang = 'en'
  14. if len(words) > 1 and words[1] == 'ru':
  15. lang = 'ru'
  16. results = []
  17. other = []
  18. section = words[0].casefold()
  19. pos = len(section)
  20. for name in sorted([name for name in crDataNumbers if name.startswith(section)]):
  21. diff = name[pos:].strip('.')
  22. if len(diff) < 2 and (len(diff) == 0 or diff.isalpha()):
  23. results.append(name)
  24. elif not diff[-1:].isalpha():
  25. other.append(name)
  26. if not results:
  27. return 'This section doesn\'t exist, my master!'
  28. text = '\n'.join(['<b>{}</b> {}'.format(name, crData['sections'][name][lang]) for name in results])
  29. if other:
  30. text += '\n<i>(Subsections: {}-{})</i>'.format(other[0], other[-1])
  31. if len(text) > 4000:
  32. text = '<b>{}</b> {}\n<i>(See also: {}-{})</i>'.format(results[0], crData['sections'][results[0]][lang], results[1], results[-1])
  33. else:
  34. nameCandidates = [name for name in crDataNames if all(word in name.casefold() for word in words)]
  35. term = ' '.join(words)
  36. if len(words) > 1:
  37. goodCandidates = [name for name in nameCandidates if term in name.casefold()]
  38. if goodCandidates:
  39. nameCandidates = goodCandidates
  40. bestCandidates = [name for name in nameCandidates if name.casefold().startswith(term)]
  41. if bestCandidates:
  42. nameCandidates = bestCandidates
  43. excellentCandidate = [name for name in nameCandidates if name.casefold() == term]
  44. if excellentCandidate:
  45. nameCandidates = excellentCandidate
  46. if not nameCandidates:
  47. return 'I searched very thoroughly, but returned empty-handed, my master!'
  48. if len(nameCandidates) > 20:
  49. return 'I need more specific clues, my master! This would return {} names'.format(len(nameCandidates))
  50. text = '\n'.join(['<b>{}</b> {}'.format(name, crData['glossary'][name]) for name in sorted(nameCandidates)])
  51. return text