Python 重新创建句子时出错

Python 重新创建句子时出错,python,json,compression,Python,Json,Compression,所以我有一个程序可以查看.txt文件中的一个句子。然后,程序会找到句子中每个单词的位置以及句子中唯一的单词。这两个列表在程序中输出,然后程序尝试根据句子中的唯一单词和单词在句子中的位置在.txt文件中重新创建原始句子,然后应在程序中输出。到目前为止,我的代码如下所示: import json import os.path def InputFile(): global compfilename compfilename = input("Please enter an exis

所以我有一个程序可以查看.txt文件中的一个句子。然后,程序会找到句子中每个单词的位置以及句子中唯一的单词。这两个列表在程序中输出,然后程序尝试根据句子中的唯一单词和单词在句子中的位置在.txt文件中重新创建原始句子,然后应在程序中输出。到目前为止,我的代码如下所示:

import json
import os.path

def InputFile():
    global compfilename
    compfilename = input("Please enter an existing compressed file to be decompressed: ")

def Validation2():
    if compfilename == (""):
        print ("Nothing was entered for the filename. Please re-enter a valid filename.")
        Error()
    if os.path.exists(compfilename + ".txt") == False:
        print ("No such file exists. Please enter a valid existing file.")
        Error()

def OutputDecompressed():
    global words
    global orgsentence
    newfile = open((compfilename)+'.txt', 'r')
    saveddata = json.load(newfile)
    orgsentence = saveddata
    words = orgsentence.split(' ')
    print ("Words in the sentence: " + str(words))

def Uniquewords():
    for i in range(len(words)):
        if words[i] not in unilist:
            unilist.append(words[i])
    print ("Unique words: " + str(unilist))

def PosText():
    global pos
    find = dict((sentence, words.index(sentence)+1) for sentence in list(words))
    pos = (list(map(lambda sentence: find [sentence], words)))
    return (pos)

def Error():
    MainCompression()

def OutputDecompressed2():
    for number in pos:
    decompression.append(orgsentence[int(number)-1])
    finalsentence = (" ".join(decompression))
    print ("Original sentence from file: " + finalsentence)

def OutputText():
    print ("The positions of the word(s) in the sentence are: " + str(pos))

def MainCompression():
    global decompression
    decompression = []
    global unilist
    unilist = []
    InputFile()
    Validation2()
    OutputDecompressed()
    Uniquewords()
    PosText()
    OutputText()
    OutputDecompressed2()

MainCompression()
Please enter an existing compressed file to be decompressed: ohdear
Words in the sentence: ['hello', 'hello', 'hello', 'hello']
Unique words: ['hello']
The positions of the word(s) in the sentence are: [1, 1, 1, 1]
Original sentence from file: h h h h
现在描述一个示例测试。假设有一个名为“ohdear”的.txt文件,其中包含一句话:“hello”

现在程序如下所示:

import json
import os.path

def InputFile():
    global compfilename
    compfilename = input("Please enter an existing compressed file to be decompressed: ")

def Validation2():
    if compfilename == (""):
        print ("Nothing was entered for the filename. Please re-enter a valid filename.")
        Error()
    if os.path.exists(compfilename + ".txt") == False:
        print ("No such file exists. Please enter a valid existing file.")
        Error()

def OutputDecompressed():
    global words
    global orgsentence
    newfile = open((compfilename)+'.txt', 'r')
    saveddata = json.load(newfile)
    orgsentence = saveddata
    words = orgsentence.split(' ')
    print ("Words in the sentence: " + str(words))

def Uniquewords():
    for i in range(len(words)):
        if words[i] not in unilist:
            unilist.append(words[i])
    print ("Unique words: " + str(unilist))

def PosText():
    global pos
    find = dict((sentence, words.index(sentence)+1) for sentence in list(words))
    pos = (list(map(lambda sentence: find [sentence], words)))
    return (pos)

def Error():
    MainCompression()

def OutputDecompressed2():
    for number in pos:
    decompression.append(orgsentence[int(number)-1])
    finalsentence = (" ".join(decompression))
    print ("Original sentence from file: " + finalsentence)

def OutputText():
    print ("The positions of the word(s) in the sentence are: " + str(pos))

def MainCompression():
    global decompression
    decompression = []
    global unilist
    unilist = []
    InputFile()
    Validation2()
    OutputDecompressed()
    Uniquewords()
    PosText()
    OutputText()
    OutputDecompressed2()

MainCompression()
Please enter an existing compressed file to be decompressed: ohdear
Words in the sentence: ['hello', 'hello', 'hello', 'hello']
Unique words: ['hello']
The positions of the word(s) in the sentence are: [1, 1, 1, 1]
Original sentence from file: h h h h
正如你所看到的,原来的句子不是根据句子中独特的单词和单词的位置重新创建的——奇怪的是,显示了4个h。有人能帮我纠正这个错误吗?因为我不知道如何从句子中唯一的单词和单词的位置重新创建原始句子。问题出在OutputDecompressed2()函数中。提前谢谢你的帮助。我已经在这上面呆了一段时间了

words = orgsentence.split(' ')
这意味着
words
是一个字符串列表,
orgstence
只是一个大字符串。但后来你会发现:

orgsentence[int(number)-1]
这将是一个角色。而是获取
单词[int(number)-1]

此外:

只给出每个“
句子
”的第一次出现,因为这是
.index
所做的,因此您有以下输出:

The positions of the word(s) in the sentence are: [1, 1, 1, 1]
这显然是错误的

顺便说一下,
句子
在这一点上是一个糟糕的变量名,为什么不
单词