replaceWithHyponym 函数从文本中选择一个单词并用下位词替换它,导致索引超出范围

replaceWithHyponym function selects a word from the text and replaces it with a hyponym, resulting in index out of range

self.lines.append([random.randint(10,700),random.randint(10,950), \
                   "Once there was a dog and a cat","l0"])
self.lines.append([random.randint(10,700),random.randint(10,950), \
                   "Who decided to have an extended natter","l1"])

这是分析的文本。

def replaceWithHyponym(lines):
        ll = lines.pop(random.randrange(len(lines)))
        tokens = nltk.word_tokenize(ll[2])
        wordIdx = random.randrange(len(tokens))
        wordToBeReplaced = tokens[wordIdx]

        print('wordToBeReplaced')
        print(wordToBeReplaced)
        
        vehicle = wordnet.synsets(wordToBeReplaced)[0]
        typeofword = wordnet.synsets(vehicle.name())
        typesOfhyponyms = list(set([w for s in vehicle.closure(lambda s: s.hyponyms()) for w in s.lemma_names()]))
        print('typesOfhyponyms')            
        print(typesOfhyponyms)

        if typesOfhyponyms:
            tokens[wordIdx] = random.choice(typesOfhyponyms)
        else:
            tokens[wordIdx] = wordToBeReplaced
        print('tokens')
        print(tokens)

        newLine = " ".join(tokens)
        lines.append([ll[0],ll[1],newLine,ll[3]])
        return lines

此函数会不断运行,直到文本足够富有诗意。

Error:

*** in run

[[325, 130, 'Once there was a dog and a cat', 'l0'], [684, 268, 'Who decided to have an extended natter', 'l1']]

2

executing  <function replaceWithHypernym at 0x000001BFA0142708>

wordToBeReplaced:

a

typesOfhyponyms:

[]

tokens:

['Once', 'there', 'was', 'a', 'dog', 'and', 'a', 'cat']

*** in run

[[684, 268, 'Who decided to have an extended natter', 'l1'], [325, 130, 'Once there was a dog and a cat', 'l0']]

2
executing  <function replaceWithHypernym at 0x000001BFA0142708>

wordToBeReplaced:

decided

typesOfhyponyms:

['calliper', 'adjudicate', 'reverse', 'stick', 'vote', 'pick_out', 'dedicate', 'define', 'overthrow', 'cream_off', 'turn_thumbs_down', 'put_up', 'regularise', 'anoint', 'calibrate', 'sieve', 'standardise', 'standardize', 'override', 'will', 'redefine', 'orientate', 'shoot', 'determine', 'measure_out', 'sieve_out', 'mensurate', 'pick_over', 'take_out', 'empanel', 'propose', 'judge', 'panel', 'quantify', 'set', 'cull_out', 'field', 'vote_in', 'reset', 'overturn', 'sift', 'orient', 'choose', 'poll', 'guide_on', 'screen_out', 'order', 'adopt', 'ballot', 'reelect', 'excerpt', 'overrule', 'rule_in', 'rule', 'outvote', 'decree', 'espouse', 'winnow', 'set_apart', 'co-opt', 'select', 'regularize', 'vote_down', 'resolve', 'limit', 'regulate', 'govern', 'impanel', 'skim_off', 'assign', 'think_of', 'hand-pick', 'pick', 'rule_out', 'seal', 'return', 'adhere', 'measure', 'detail', 'gauge', 'elect', 'screen', 'name', 'caliper', 'guide', 'purpose', 'draw', 'reorientate', 'put_forward', 'fix', 'take', 'extract', 'triangulate', 'dial', 'follow', 'specify', 'reorient', 'court-martial', 'plump', 'single_out', 'nominate', 'zone', 'try', 'district', 'sort', 'write_in', 'go']

tokens:

['Who', 'overthrow', 'to', 'have', 'an', 'extended', 'natter']

*** in run

[[325, 130, 'Once there was a dog and a cat', 'l0'], [684, 268, 'Who overthrow to have an extended natter', 'l1']]

2

executing  <function replaceWithHypernym at 0x000001BFA0142708>

wordToBeReplaced:

a

typesOfhyponyms:

[]

tokens:

['Once', 'there', 'was', 'a', 'dog', 'and', 'a', 'cat']

*** in run

[[684, 268, 'Who overthrow to have an extended natter', 'l1'], [325, 130, 'Once there was a dog and a cat', 'l0']]

2

executing  <function replaceWithHypernym at 0x000001BFA0142708>

wordToBeReplaced:

a

typesOfhyponyms:

[]

tokens:

['Once', 'there', 'was', 'a', 'dog', 'and', 'a', 'cat']

*** in run

[[684, 268, 'Who overthrow to have an extended natter', 'l1'], [325, 130, 'Once there was a dog and a cat', 'l0']]

2

executing  <function replaceWithHypernym at 0x000001BFA0142708>

wordToBeReplaced:

there

typesOfhyponyms:

[]

tokens:
['Once', 'there', 'was', 'a', 'dog', 'and', 'a', 'cat']

*** in run

[[684, 268, 'Who overthrow to have an extended natter', 'l1'], [325, 130, 'Once there was a dog 
and a cat', 'l0']]

2

executing  <function replaceWithHypernym at 0x000001BFA0142708>

wordToBeReplaced:

extended

typesOfhyponyms:

['territorialise', 'stretch', 'territorialize', 'globalize', 'globalise', 'broaden']

tokens:

['Who', 'overthrow', 'to', 'have', 'an', 'broaden', 'natter']

*** in run

[[325, 130, 'Once there was a dog and a cat', 'l0'], [684, 268, 'Who overthrow to have an broaden natter', 'l1']]

2

executing  <function replaceWithHypernym at 0x000001BFA0142708>

wordToBeReplaced:

to

***Exception in Tkinter callback***

***Traceback (most recent call last):***

  ***File "C:\Users\essam\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__***
    ***return self.func(*args)***

  ***File "C:\Users\essam\Anaconda3\lib\tkinter\__init__.py", line 749, in callit***
    ***func(*args)***

  ***File "<ipython-input-145-8131f290a692>", line 25, in run***
   ***self.lines = ag(self.lines)***

  ***File "<ipython-input-145-8131f290a692>", line 45, in replaceWithHypernym***
    ***vehicle = wordnet.synsets(wordToBeReplaced)[0]***

***IndexError: list index out of range***

错误来自 vehicle = wordnet.synsets("to")[0](如果我正确理解了调试输出)。

错误说 [0] 超出范围,这一定意味着 wordnet.synsets("to") 已经 return 编辑了一个空列表。 IE。在任何同义词集中都找不到“to”。

所以你需要将wordnet.synsets(wordToBeReplaced)赋值给一个变量,并在得到[0]之前检查列表长度至少为1(如果为零则提前return?)。

(如果您在没有找到同义词集的情况下提前 return,请注意无限循环的可能性。我倾向于 return false,或抛出异常,以表明没有更改任何文字。)