-1

I need to be able to write a program that takes a file then returns an array of tokens and then Loop through the array, printing each token one per line, then removes all the punctuation and lower cases each word, then counts how many times the words asked for occur throughout the file. The problem is im having trouble returning the array, I'm not quite sure how to make array that allows the user to type in any word or as many words as he want and then have the program search through the file to find those words.....ive spent 8 hrs and this is all i currently have:

import sys
from scanner import *


def main():
    tokens = readTokens("shakespeare.txt")
    for i in range(0,len(tokens),1):
        print(tokens[i])
def readTokens(fileName):
    with open("shakespeare.txt", 'r') as f:
        return f.read().split()
    token = s.readtoken()
    while (token != ""):
        print(token)
        token = s.readtoken()

    s.close()
main()
FootOfGork
  • 55
  • 6

2 Answers2

2

This is StackOverflow so there is no way you'll get someone to write the whole program for you ... I'll assume your question is more about the how to approach the problem and you're working in python

Divide and conquer is your friend

  1. Read the lines of a file: Here or Here
  2. Store the lines into an array: Here or Here
  3. Loop through every element in the array: Here
  4. Check to see if each string found contains a punctuation: Here
  5. If it contains a punctuation, remove it (I'll leave this one up to you)

Try the above and come up with a more functional solution (your code currently looks like you're still learning certain core programming ideas). After that, you can look into making it faster/more readable, etc. It will help the community better if you detailed your attempts.

Community
  • 1
  • 1
Daniel Kotin
  • 326
  • 2
  • 8
  • LOL Yea I definitely am a rookie, but those steps did help me find the right track, thanks – FootOfGork Feb 20 '14 at 21:02
  • Glad it helped :). Once you have code that works (or is very close), you can search for similar threads of head down to codereview for more tricks [example, using regular expressions, using the `yield` functionality to make things shorter, etc.]. Or better yet, edit this question with the changes you've made for more guidance. – Daniel Kotin Feb 20 '14 at 21:05
1

This is how you read the whole file and the split it at all spaces:

def readTokens(filename):
    with open(filename, 'r') as f:
        return f.read().split()
LucasB
  • 3,253
  • 1
  • 28
  • 31