【问题标题】:error when recreating sentence重新创建句子时出错
【发布时间】:2017-04-02 11:20:30
【问题描述】:

所以我有一个程序可以查看 .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()

现在描述一个示例测试。假设有一个名为“ohdear”的 .txt 文件,其中包含以下句子:“hello hello hello hello”

现在程序如下所示:

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() 函数中。感谢您提前提供任何帮助。我已经被困了一段时间......

【问题讨论】:

    标签: python json compression


    【解决方案1】:
    words = orgsentence.split(' ')
    

    这意味着words 是一个字符串列表,而orgsentence 只是一个大字符串。但后来你得到:

    orgsentence[int(number)-1]
    

    这只是一个角色。而是得到words[int(number)-1]

    还有:

    find = dict((sentence, words.index(sentence)+1) for sentence in list(words))
    

    只给你每个'sentence'的第一次出现,因为这是.index所做的,因此你有输出:

    The positions of the word(s) in the sentence are: [1, 1, 1, 1]
    

    这显然是错误的。

    顺便说一句,sentence 在那个时候是一个糟糕的变量名,为什么不是word

    【讨论】:

      猜你喜欢
      • 2013-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-17
      • 2015-09-14
      • 1970-01-01
      相关资源
      最近更新 更多