-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobyTest.py
116 lines (85 loc) · 2.87 KB
/
mobyTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import sys
import os
import io
import random
import time
import math
import datetime
import traceback
import uuid
import colorsys
import shelve
import logging
import multiprocessing
import queue
from collections import OrderedDict
from operator import itemgetter
from PIL import Image, ImageDraw, ImageFont, ImageFilter #, ImageEnhance
from PIL import ImageOps, ImageChops
from PIL.ImageColor import getrgb
import numpy as np, numpy.random
from io import StringIO, BytesIO
wordListsPath = "/mnt/u/code/pytwit/wordlists/"
wordsMoby = dict()
def getRandomWord_Moby(typeind=""):
## Noun N
## Plural p
## Noun Phrase h
## Verb (usu participle) V
## Verb (transitive) t
## Verb (intransitive) i
## Adjective A
## Adverb v
## Conjunction C
## Preposition P
## Interjection !
## Pronoun r
## Definite Article D
## Indefinite Article I
## Nominative o
global rootLogger
rootLogger.debug("getRandomWord_Moby typeind: " + typeind)
mobyfilepath = wordListsPath + 'mobyposi/mobylf.i'
global wordsMoby
if not wordsMoby:
#with open(mobyfilepath, "rb") as f:
with open(mobyfilepath, encoding = "cp437") as f:
for line in f:
word, typ = line.strip().split("╫")
for key in typ:
if key not in wordsMoby:
wordsMoby[key] = []
thislist = wordsMoby[key]
thislist.append(word)
random.seed()
choice = ""
if typeind != "":
if typeind in wordsMoby:
thislist = wordsMoby[typeind]
choice = random.choice(thislist)
else:
allkeys = list(wordsMoby.keys())
keychoice = random.choice(allkeys)
thislist = wordsMoby[keychoice]
choice = random.choice(thislist)
choice = checkWordEncoding(choice)
rootLogger.debug("getRandomWord_Moby complete: " + choice)
return choice
def checkWordEncoding(word):
try:
word = word.decode('utf-8')
except (UnicodeDecodeError, AttributeError):
pass
return word
rootLogger = logging.getLogger()
rootLogger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
rootLogger.addHandler(handler)
getRandomWord_Moby()
getRandomWord_Moby("h")
getRandomWord_Moby("V")
getRandomWord_Moby("!")
print(wordsMoby.keys())