句子中单词的位置无法正常工作

强尼

我的代码有问题:

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=('''!()-[]{};:'"\,<>./?@#$%^&*_~0123456789''')
        punctuation=('''!()-[]{};:'"\,<>./?@#$%^&*_~''')
        numbers=('0123456789')
        #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()

当输出句子中单词的位置时,由于某些原因,该代码似乎不起作用,因此代码可以工作到一定程度,例如:

>>> 
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
>>> 

该程序应该输出位置[0,1,2,3,4,1,5,6,2,3,4,1,2,7,3,8,9],但是不会。我真的很感谢您的帮助,因为我不确定要解决的问题,也不清楚为什么要这样做。

克里斯

这是您问题的根源:

positions.append(i)

这是从枚举函数中附加索引,该索引是附加每个唯一单词原始位置,因此数量不断增加。您想要做的是为每个新术语增加一个增量。可以通过将该行更改为以下内容来实现:

positions.append(len(y) -1)

输出:

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, 5, 6, 2, 3, 4, 1, 2, 7, 3, 8, 9]
Goodbye

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

查找句子中单词的位置和频率

来自分类Dev

从列表中随机选择单词无法正常工作(Python / Tkinter)

来自分类Dev

Xcode位置无法正常工作

来自分类Dev

Xcode位置无法正常工作

来自分类Dev

工具栏的重力(或位置)在CollapsingToolbarLayout中无法正常工作

来自分类Dev

IOS 8中的位置管理器无法正常工作

来自分类Dev

图像的背景位置在ipad3中无法正常工作

来自分类Dev

在HTML中更改div位置时,JavaScript无法正常工作

来自分类Dev

工具栏的重力(或位置)在CollapsingToolbarLayout中无法正常工作

来自分类Dev

jQuery转换为单词无法正常工作

来自分类Dev

如何使多个单词在句子中的某个位置旋转

来自分类Dev

如何使多个单词在句子中的某个位置旋转

来自分类Dev

如何更改句子中所有变体中的单词位置

来自分类Dev

Android ViewPager位置无法正常工作

来自分类Dev

固定位置无法正常工作

来自分类Dev

Foreach循环位置无法正常工作

来自分类Dev

Android ViewPager位置无法正常工作

来自分类Dev

位置管理器无法正常工作

来自分类Dev

地理位置API无法正常工作

来自分类Dev

反转句子中的单词

来自分类Dev

隐藏句子中的单词

来自分类Dev

句子中的单词重复

来自分类Dev

在句子中查找单词

来自分类Dev

搜索句子中的单词

来自分类Dev

检查句子中的单词

来自分类Dev

在Javascript中,“ this”无法正常工作

来自分类Dev

“ Not In”在sql中无法正常工作

来自分类Dev

相对跨度中包含的绝对位置在Chrome中无法正常工作

来自分类Dev

如何将单词列表和这些单词在句子中的位置保存为文件