Ejemplo n.º 1
0
def main():
    if len(sys.argv) != 2:
        print('Usage: python speller.py <text_file>')
        sys.exit(0)

    dict_file = DICTIONARY
    text_file = sys.argv[1]

    # Create instance of dictionary
    dictionary = Dictionary()

    # Use class method to load dictionary
    loaded = dictionary.load(dict_file)
    if not loaded:
        print("Could not load dictionary.")

    # Open text file that needs to be spellchecked
    text = open(text_file, "r")
    words = []
    misspelled = []

    # Go through all words in given file, check spelling one by one
    for line in text:
        # line.split() creates list of words from the file,
        # separating them based on spaces
        for word in line.split():

            # Remove punctuation from each word (i.e. commas)
            word = re.sub(r'[^\w\s]', '', word)

            # ensure there are no non-alphabetical characters in word (i.e. numbers)
            if word.isalpha():
                words.append(word)
                # Check word spelling against word in dictionary
                if not dictionary.check(word):
                    misspelled.append(word)
    text.close()

    print("Total number of words in text: " + str(len(words)))
    print("Number of misspelled words: " + str(len(misspelled)))
Ejemplo n.º 2
0
def main():
    dict_file = "large.txt"
    text_file = "ralph.txt"

    # Create instance of dictionary
    dictionary = Dictionary()

    # Use class method to load dictionary
    loaded = dictionary.load(dict_file)
    if not loaded:
        print("Could not load dictionary.")

    # Open text file that needs to be spellchecked
    text = open(text_file, "r")
    words = []
    misspelled = []

    # Go through all words in given file, check spelling one by one
    for line in text:
        # line.split() creates list of words from the file,
        # separating them based on spaces
        for word in line.split():

            # Remove punctuation from each word (i.e. commas)
            word = word.translate(None, string.punctuation)

            # ensure there are no non-alphabetical characters in word (i.e. numbers)
            if word.isalpha():
                words.append(word)
                # Check word spelling against word in dictionary
                if not dictionary.check(word):
                    misspelled.append(word)
    text.close()

    print("Total number of words in text: " + str(len(words)))
    print("Number of misspelled words: " + str(len(misspelled)))
    print("Misspelled words:")
    print(misspelled)
Ejemplo n.º 3
0
def main():
    if len(sys.argv) != 2:
        sys.exit("Usage: enter name of text-file after name of your programm")
    dictionary_way = os.path.join(sys.path[0], "test.txt")
    text_way = os.path.join(sys.path[0], sys.argv[1])
    dictionary = Dictionary()
    if not dictionary.load(dictionary_way):
        sys.exit("Could not load dictionary")
    word = ""
    words, miss = 0, 0
    with open(text_way, "r") as text:
        for line in text:
            for letter in line:
                if letter.isalpha() or (letter == "\'" and len(word) > 0):
                    word += letter.lower()
                elif len(word) > 0:
                    words += 1
                    if not dictionary.check(word):
                        print(word)
                        miss += 1
                    word = ""

    print("{}\n{}\n{}".format(miss, dictionary.size(), words))
Ejemplo n.º 4
0
            c = fp.read(1)
            if not c or (not c.isalpha() and not c.isdigit()):
                break

        # prepare for new word
        index, word = 0, ""

    # we must have found a whole word
    elif index > 0:

        # update counter
        words += 1

        # check word's spelling
        before = time.process_time()
        misspelled = not d.check(word)
        after = time.process_time()

        # update benchmark
        time_check += after - before

        # print word if misspelled
        if misspelled:
            print(word)
            misspellings += 1

        # prepare for next word
        index, word = 0, ""

# close file
fp.close()
Ejemplo n.º 5
0
while True:
    c = fp.read(1)
    if not c:
        break
    if re.match(r'[\S]', c):
        word += c
        index += 1
        if index > LENGTH:
            while True:
                c = fp.read(1)
                if not c or not re.match(r'[\S]', c):
                    break
            index, word = 0, ""
        elif index > 0:
            found = d.check(word)
            if found:
                foundSet.add(word)
                print(word)
                findings += 1
    elif re.match(r'[\s]', c):
        words += 1
        index, word = 0, ""
fp.close()

n = d.size()
unloaded = d.unload()

if not unloaded:
    print("Could not unload dictionary.")
    exit(3)
Ejemplo n.º 6
0
from dictionary import Dictionary
import time

# Create an instance
dic = Dictionary()

start = time.process_time()
load_stts = dic.load("dictionaries/small")
end = time.process_time()
print(f"Loading(walclock) time: {end - start}")

check_stts = dic.check("caterpillar")

print(load_stts)
print(check_stts)