Im ein Problem mit meinem Code mit:Positionen der Wörter innerhalb eines Satzes nicht ordnungsgemäß funktioniert
def sentence_recreation(grammar_choice, sentence):
new_sentence=''
for char in sentence:
if char not in grammar_choice:
new_sentence=new_sentence + char
sentence_list=new_sentence.split()
compression(sentence_list)
def validation(sentence):
if sentence=='':
print('Input invalid. Please enter a sentence: ')
compress_sentence()
else:
grammar_choice = input("Would you like to remove any punctuation or numbers in your sentence?('None', 'Both', 'Punctuation' or 'Numbers'): ")
grammar_choice.lower()
both=('''!()-[]{};:'"\,<>./[email protected]#$%^&*_~''')
punctuation=('''!()-[]{};:'"\,<>./[email protected]#$%^&*_~''')
numbers=('')
#These if statements decide to remove: nothing, punctuation, numbers or punctuation and numbers
if grammar_choice=='':
print('Input invalid. Please try again.')
validation(sentence)
if grammar_choice=="none":
sentence_list=sentence.split()
compression(sentence_list)
elif grammar_choice == "punctuation":
grammar_choice = punctuation
sentence_recreation(grammar_choice, sentence)
elif grammar_choice == "numbers":
grammar_choice = numbers
sentence_recreation(grammar_choice, sentence)
elif grammar_choice == "both":
grammar_choice = both
sentence_recreation(grammar_choice, sentence)
else:
print('Input invalid. Please try again.')
validation(sentence)
def compression(sentence_list):
words=[]
positions=[]
y={}
#This enumerate function allows the program to create two lists with the unique words as well as the positions of those words within the sentence
for i,x in enumerate(sentence_list):
if x in y:
positions.append(y[x])
else:
y[x]=i
positions.append(i)
for i,x in enumerate(sentence_list):
if sentence_list[i] not in words:
words.append(sentence_list[i])
print(words)
print(positions)
file=open('positions and words.txt','w')
file.write(str(words))
file.write(str(positions))
file.close
print('Goodbye')
import sys
sys.exit()
def compress_sentence():
sentence=input('Please enter your desired sentence: ')
validation(sentence)
compress_sentence()
Der Code funktioniert bis zu dem Punkt, wenn die Positionen der Wörter innerhalb des Satzes ausgibt, wo es scheint nicht aus irgendeinem Grund arbeiten, wie zum Beispiel:
>>>
Please enter your desired sentence: When you crack the code, you don't just crack the code, you crack all the codes 1.048596
Would you like to remove any punctuation or numbers in your sentence?('None', 'Both', 'Punctuation' or 'Numbers'): none
['When', 'you', 'crack', 'the', 'code,', "don't", 'just', 'all', 'codes', '1.048596']
[0, 1, 2, 3, 4, 1, 6, 7, 2, 3, 4, 1, 2, 13, 3, 15, 16]
Goodbye
>>>
Das Programm ist mit dem Ausgang soll die Positionen [0,1,2,3,4,1,5,6,2,3,4,1,2 , 7,3,8,9] jedoch nicht. Ich würde wirklich etwas Hilfe mit schätzen, da ich nicht sicher bin, was ich tun muss, um es zu reparieren, und ich habe vage Idee, warum es es macht. Hier
dank viel, das wirklich hilft – Jonny