Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- sentence=input('enter sentence')
- punctuation=(re.findall(r"[w'"]+|[.,!?;:_-]", sentence))
- print(punctuation)
- positions = [punctuation.index(x) for x in punctuation]
- print(positions)
- test1=(",".join(str(i) for i in positions))
- words=" ".join(sorted(set(punctuation), key=punctuation.index))
- print(words)
- with open('1.txt', 'w') as f:#this creates the name of the file called task 2 which will contain my data ad allows the computer to locate the fle and add in the data. The 'w' makes the file writable so i can add in my data, without it displaying an error, using f as a variable.
- f.write(str(words))
- with open('2.txt', 'w') as f:
- f.write(str(test1))
- f.close()
- openfile=input('what is your file name for the compressed sentence?')
- openfile = open(openfile, "r")
- print ( openfile.read())
- openfile=input('what is your file name for the compressed positions?')
- openfile = open(openfile, "r")
- print ( openfile.read())
- openfile = open('1.txt', "r")
- test=openfile.read()
- #print(test)
- blankarray=[]
- for i in test.split(" "):
- blankarray.append(i)
- #print(blankarray)
- openfile.close
- openfile = open('2.txt', "r")
- test=openfile.read()
- #print(test)
- blankarray2=[]
- for i in test.split(","):
- blankarray2.append(int(i))
- #print(blankarray2)
- openfile.close
- blankstring=""
- for i in range(len(blankarray2)):
- #print(i)
- blankstring=blankstring+blankarray[blankarray2[i]] + " "
- print(blankstring)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement