コード例 #1
0
import time
from sys import argv
from dictionary import check, load, size, unload

DICTIONARY = "large"
LENGTH =  45

if len(argv) != 2 and len(argv) != 3:
    exit("Usage: speller [dictionary] text")

time_loade, time_check, time_size, time_unload = 0.0, 0.0, 0.0, 0.0

dictionary = argv[1] if len(argv) == 3 else DICTIONARY

before time.process_time()
loaded = load(dictionary)
after time.process_time()

if not loaded:
    exit("Could not load {dictionary}.")

time_load = ater-before

text = argv[2] if len(argv) == 3 else argv[1]
file = open(text, "r", encoding="latin_1")

if not file:
    print(f"Colud not open {text}.")
    unload()
    exit(1)
コード例 #2
0
ファイル: checker.py プロジェクト: Galythia/bootcamp-python
        if len(word) > 0 and word[0] in punct:
            word = word[1:]
            should_delete = True
        word = word.strip()
    return word

if len(sys.argv) < 3:
    print "usage: %s dictionary-file text-file" % sys.argv[0]
    sys.exit(-1)

dictionary_name = sys.argv[1]
text_file_name = sys.argv[2]

start = time.clock()

dict = dictionary.load(dictionary_name)
if dict is None:
    print "dictionary load failed"
    sys.exit(-1)

text_file = open(text_file_name, "rb")
misspelled = set()
num_total = 0
for line in text_file:
    line = line.replace("-", " ")
    for word in line.split():
        word = sanitize_word(word)
        if len(word) == 0:
            continue
        if not dictionary.check(dict, word):
            num_total += 1
コード例 #3
0
        if len(word) > 0 and word[0] in punct:
            word = word[1:]
            should_delete = True
        word = word.strip()
    return word

if len(sys.argv) < 3:
    print "usage: %s dictionary-file text-file" % sys.argv[0]
    sys.exit(-1)

dictionary_name = sys.argv[1]
text_file_name = sys.argv[2]

start = time.clock()

dict = dictionary.load(dictionary_name)
if dict is None:
    print "dictionary load failed"
    sys.exit(-1)

text_file = open(text_file_name, "rb")
misspelled = set()
num_total = 0
for line in text_file:
    line = line.replace("-", " ")
    for word in line.split():
        word = sanitize_word(word)
        if len(word) == 0:
            continue
        if not dictionary.check(dict, word):
            num_total += 1
コード例 #4
0
ファイル: main.py プロジェクト: JIElite/Text-Segment
import argparse 
import segment
import dictionary

parser = argparse.ArgumentParser()
parser.add_argument("-f", "--file", help="using input context file to segment")
parser.add_argument("-s", "--simple", help="just add simple context to end of CLI to segment")
args = parser.parse_args()

dictionary = dictionary.Dictionary()
dictionary.load()

segments_agent = segment.SegmentSystem()
segments_agent.use_dictionary(dictionary)

if args.file:
    with open(args.file, 'r') as fd:
        for context in fd.readlines():
            print(segments_agent.segments(context))
elif args.simple:
    context = args.simple
    print(segments_agent.segments(context))
else:
    print("No Input Context")
    print("If you need any help, please try: python main.py -h")
コード例 #5
0
from socket import *
from random import randint # inclusive range

import json
import dictionary # dictionary of words loaded from a local file

myHost = '' # '' means all available interfaces on host
myPort = 6969 

# '' = all available interfaces on host
sockobj = socket(AF_INET, SOCK_STREAM) # make a TCP socket object
sockobj.bind((myHost, myPort)) 
sockobj.listen(5) # listen, allow 5 pending connections

words_dict = dictionary.load()
print('server ON, waiting for a connection')
while True: # listen until process killed
    connection, address = sockobj.accept() # wait for the next client connection
    print('Connection from', address) 
    data = connection.recv(1024) # read next line on client socket
    print(data)
    # difficulty = int.from_bytes(data[0], byteorder='big') % 3 # guaranteed i in [0,1,2]
    difficulty = (data[0] - 48) % 3
    print('difficulty', difficulty)
    list_words = words_dict[difficulty]
    rand_index = randint(0, len(list_words))
    print(rand_index)
    res = list_words[rand_index]
    print('Word sent =>', res)
    connection.send(str.encode(res))
    connection.close()