【问题标题】:I want to print a sentence from list of list of list with random choice synonyms我想用随机选择同义词从列表列表中打印一个句子
【发布时间】:2020-12-12 00:48:46
【问题描述】:

我正在用 nltk 制作释义程序,但我一直坚持这个问题。 需要注意的函数是 synonymifExist() 和 paraphrase() 这段代码是打印列表列表的列表。输出如下所示。我想用同义词打印一个简单的最后一句话。

from nltk.tokenize import word_tokenize
from nltk.tag import pos_tag
from nltk.corpus import wordnet as wn
import random

def tag(sentence):
    words = word_tokenize(sentence)
    words = pos_tag(words)
    return words

def paraphraseable(tag):
    return tag.startswith('NN') or tag == 'VB' or tag.startswith('JJ')

def pos(tag):
    if tag.startswith('NN'):
        return wn.NOUN
    elif tag.startswith('V'):
        return wn.VERB

def synonyms(word, tag):
    lemma_lists = [ss.lemmas() for ss in wn.synsets(word, pos(tag))]
    lemmas = [lemma.name() for lemma in sum(lemma_lists, [])]
    return set(lemmas)

**def synonymIfExists(sentence):
    for (word, t) in tag(sentence):
        if paraphraseable(t):
            syns = synonyms(word, t)
            if syns:
                if len(syns) > 1:
                    yield[word, list(syns)]
                    continue
        yield [word, []]

def paraphrase(sentence):
    result = [x for x in synonymIfExists(sentence)]
    return result
**
print(paraphrase("The quick brown fox jumps over the lazy dog"))
output: [['The', []], ['quick', ['flying', 'speedy', 'nimble', 'spry', 'promptly', 'straightaway', 'quick', 'agile', 'ready', 'warm', 'fast', 'immediate', 'quickly', 'prompt']], ['brown', ['brownness', 'Brown_University', 'John_Brown', 'Robert_Brown', 'brown', 'Brown']], ['fox', ['dodger', 'George_Fox', 'slyboots', 'fox', 'Charles_James_Fox', 'Fox']], ['jumps', []], ['over', []], ['the', []], ['lazy', ['otiose', 'slothful', 'faineant', 'work-shy', 'lazy', 'indolent']], ['dog', ['weenie', 'Canis_familiaris', 'hound', 'dog-iron', 'domestic_dog', 'wienerwurst', 'firedog', 'andiron', 'frankfurter', 'bounder', 'click', 'pawl', 'wiener', 'frump', 'heel', 'dog', 'hotdog', 'blackguard', 'detent', 'frank', 'cad', 'hot_dog']]]

【问题讨论】:

  • for item in output: if item[1]: random.choice(item[1]) ?
  • 我不确定,但你可以用result = [x for x in synonymIfExists(sentence)] 代替result = list(synonymIfExists(sentence))

标签: python python-3.x random nlp nltk


【解决方案1】:
import random

output = [
    ['The', []],
    ['quick', ['flying', 'speedy', 'nimble', 'spry', 'promptly', 'straightaway', 'quick', 'agile', 'ready', 'warm', 'fast', 'immediate', 'quickly', 'prompt']], ['brown', ['brownness', 'Brown_University', 'John_Brown', 'Robert_Brown', 'brown', 'Brown']], ['fox', ['dodger', 'George_Fox', 'slyboots', 'fox', 'Charles_James_Fox', 'Fox']],
    ['jumps', []],
    ['over', []],
    ['the', []],
    ['lazy', ['otiose', 'slothful', 'faineant', 'work-shy', 'lazy', 'indolent']],
    ['dog', ['weenie', 'Canis_familiaris', 'hound', 'dog-iron', 'domestic_dog', 'wienerwurst', 'firedog', 'andiron', 'frankfurter', 'bounder', 'click', 'pawl', 'wiener', 'frump', 'heel', 'dog', 'hotdog', 'blackguard', 'detent', 'frank', 'cad', 'hot_dog']]
]
    
    for item in output:
        if item[1]: # check if list is not empty
            print(random.choice(item[1]))

结果:

straightaway
brown
fox
slothful
click

编辑:

因为一些同义词列表是空的,所以你也可以使用else 来放置? 或放置原始词(或者你可以尝试确定你是否真的需要同义词 - 即the 或类似的)

import random

output = [
    ['The', []],
    ['quick', ['flying', 'speedy', 'nimble', 'spry', 'promptly', 'straightaway', 'quick', 'agile', 'ready', 'warm', 'fast', 'immediate', 'quickly', 'prompt']], ['brown', ['brownness', 'Brown_University', 'John_Brown', 'Robert_Brown', 'brown', 'Brown']], ['fox', ['dodger', 'George_Fox', 'slyboots', 'fox', 'Charles_James_Fox', 'Fox']],
    ['jumps', []],
    ['over', []],
    ['the', []],
    ['lazy', ['otiose', 'slothful', 'faineant', 'work-shy', 'lazy', 'indolent']],
    ['dog', ['weenie', 'Canis_familiaris', 'hound', 'dog-iron', 'domestic_dog', 'wienerwurst', 'firedog', 'andiron', 'frankfurter', 'bounder', 'click', 'pawl', 'wiener', 'frump', 'heel', 'dog', 'hotdog', 'blackguard', 'detent', 'frank', 'cad', 'hot_dog']]
]

for original, synonyms in output:
    if synonyms:
        print(original, '->', random.choice(synonyms))
    else:
        #print(original, '->', '???')
        print(original, '->', original, '[original]')

结果

The -> The [oryginal]
quick -> nimble
brown -> Brown_University
fox -> George_Fox
jumps -> jumps [oryginal]
over -> over [oryginal]
the -> the [oryginal]
lazy -> faineant
dog -> Canis_familiaris

现在您可以将新单词放入列表而不是打印

import random

output = [
    ['The', []],
    ['quick', ['flying', 'speedy', 'nimble', 'spry', 'promptly', 'straightaway', 'quick', 'agile', 'ready', 'warm', 'fast', 'immediate', 'quickly', 'prompt']], ['brown', ['brownness', 'Brown_University', 'John_Brown', 'Robert_Brown', 'brown', 'Brown']], ['fox', ['dodger', 'George_Fox', 'slyboots', 'fox', 'Charles_James_Fox', 'Fox']],
    ['jumps', []],
    ['over', []],
    ['the', []],
    ['lazy', ['otiose', 'slothful', 'faineant', 'work-shy', 'lazy', 'indolent']],
    ['dog', ['weenie', 'Canis_familiaris', 'hound', 'dog-iron', 'domestic_dog', 'wienerwurst', 'firedog', 'andiron', 'frankfurter', 'bounder', 'click', 'pawl', 'wiener', 'frump', 'heel', 'dog', 'hotdog', 'blackguard', 'detent', 'frank', 'cad', 'hot_dog']]
]

sentence = []

for original, synonyms in output:
    if synonyms:
        new = random.choice(synonyms)
        #print(original, '->', new)
        sentence.append(new)
    else:
        #print(original, '->', original, '[original]')
        sentence.append(original)

print( " ".join(sentence) )

结果

The quickly brownness Fox jumps over the otiose Canis_familiaris

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多