Exemplo n.º 1
0
def as_tuples(strs):
    res = []
    for line in strs:
        page_nos = [int(x) for x in words(line)]
        for link in group2(page_nos):
            res += [link]
    return res
Exemplo n.º 2
0
def frequency_count(filename):
    word_count = Counter()
    with open(filename) as inp:
        for line in inp:
            for word in words(line):
                word_count[word] += 1
    return word_count
Exemplo n.º 3
0
def index_of_words(filename):
    index = defaultdict(list)
    with open(filename) as inp:
        i = 0
        for line in inp:
            for word in words(line):
                i += 1
                index[word] += [i]
    return index
Exemplo n.º 4
0
def main():
    filename = '/usr/share/dict/words'
    words_list = []
    with open(filename) as inp:
        for line in inp:
            words_list += words(line)
    reversed_words = reverse_strings(words_list)
    res = reverse_strings(sorted(reversed_words))
    for word in res:
        print(word)
def evaluate(string):
    operand_stack = LinkedStack()
    operator_stack = LinkedStack()
    tokens = (tok.strip() for tok in words(string))
    for tok in tokens:
        if is_operator(tok):
            operator_stack.push(tok)
        elif tok == ')':
            evaluate_single(operand_stack, operator_stack)
        else:
            operand_stack.push(tok)
    print(operand_stack)
Exemplo n.º 6
0
def index_of_words_(filename):
    index = BinarySearchTree()
    with open(filename) as inp:
        i = 0
        for line in inp:
            for word in words(line):
                i += 1
                if word in index:
                    index[word] = index[word] + [i]
                else:
                    index[word] = [i]
    return index
def main():
    corrections = {}
    with open('../misspellings.txt') as misspellings_corrrections:
        for line in misspellings_corrrections:
            misspelling, correct = split_line(line)
            corrections[misspelling] = correct

    for line in stdin:
        for word in words(line):
            if word in corrections:
                print(corrections[word])
            else:
                print(word)
Exemplo n.º 8
0
def readFloatMatrixOrVector():
    dimens = words(readStringArray(1)[0])
    dimens = [int(n) for n in dimens]
    if len(dimens) == 1:
        line = readStringArray(1)[0]
        return line_as_floats(line)

    [nb_row, nb_col] = dimens
    array = readStringArray(nb_row)
    res = []
    for row in array:
        res += [line_as_floats(row)]
    return res
def infix_to_postfix(string):
    operand_stack = LinkedStack()
    operator_stack = LinkedStack()
    tokens = (tok.strip() for tok in words(string))
    for tok in tokens:
        print(operator_stack)
        print(operand_stack)
        if tok == '(':
            continue
            # operand_stack.push(tok)
        elif is_operator(tok):
            operator_stack.push(tok)
        elif tok == ')':
            b, a = operand_stack.pop(), operand_stack.pop()
            op = operator_stack.pop()
            # res = '[' + a + ' ' + b +  op + ']'
            res = a + ' ' + b + ' ' + op
            operand_stack.push(res)
        else:
            operand_stack.push(tok)
    print(operand_stack)
        q = Queue()
        q.enqueue(val)
        queue.enqueue(q)
    while len(queue) > 1:
        q1 = queue.dequeue()
        q2 = queue.dequeue()
        queue.enqueue(merge(q1, q2))
    return tuple(queue.dequeue())






if __name__ == "__main__":
    a = Queue()
    a.enqueue(1)
    a.enqueue(2)
    a.enqueue(4)
    b = Queue()
    b.enqueue(2)
    b.enqueue(5)
    b.enqueue(6)
    print(merge(a, b))
    a = [random.randrange(10) for i in range(100)]
    print(merge_sort(a))
    filename = sys.argv[1]
    with open(filename) as lines:
        wordslist = [word for line in lines for word in words(line)]
    print(merge_sort(wordslist))
Exemplo n.º 11
0
import sys
from distance import squared_distance
from ioutils import read_strings
from strutils import words

if __name__ == "__main__":
    x = float(sys.argv[1])
    y = float(sys.argv[2])
    z = float(sys.argv[3])
    array = read_strings()
    res = []

    def closest(x, y, z, v1, v2):
        if squared_distance([x, y, z], v1) < squared_distance([x, y, z], v2):
            return v1
        return v2

    for line in array:
        [xi, yi, zi] = words(line)[0:3]
        [xi, yi, zi] = [float(xi), float(yi), float(zi)]
        if res == []:
            res = [xi, yi, zi]
        else:
            res = closest(x, y, z, res, [xi, yi, xi])

    print(res)
Exemplo n.º 12
0
import sys
import re
from strutils import words
from array_utils import shannon_entropy_list

filename = sys.argv[1]

with open(filename) as corpus:
    wordlist = []
    for line in corpus:
        for word in words(line):
            word = word.lower()
            word = re.sub(r'[^a-z]', '', word)
            wordlist += [word]
    print(shannon_entropy_list(wordlist))
Exemplo n.º 13
0
def line_as_floats(line):
    return [float(x) for x in words(line)]
from ioutils import read_strings
from strutils import words

if __name__ == "__main__":
    array = read_strings()
    for line in array:
        words_array = words(line)
        name = ' '.join(words_array[0:-2])
        a = int(words_array[-2])
        b = int(words_array[-1])
        res = a / b
        print('%25s' % name, '%3d' % a, '%3d' % b, '%3.3f' % res)
Exemplo n.º 15
0
from ioutils import read_strings
from strutils import words

array = read_strings()
mtot = 0
mxtot = 0
mytot = 0
for line in array:
    [m, x, y] = words(line)[0:3]
    m, x, y = float(m), float(x), float(y)
    mtot += m
    mxtot += (m * x)
    mytot += (m * y)

print(mxtot / mtot, mytot / mtot)
def main():
    lexicon = build_lexicon('../words.utf-8.txt')
    for line in stdin:
        for word in words(line):
            if word not in lexicon:
                print(word)