Библиотеки или инструменты для генерации случайного но реалистического текста

Ответ Claudiu корректен, но можно также обмануть путем получения имени класса от self аргумент. Это даст вводящие в заблуждение операторы журнала в случаях наследования, но скажет Вам класс объекта, метод которого называют. Например:

from functools import wraps  # use this to preserve function signatures and docstrings
def logger(func):
    @wraps(func)
    def with_logging(*args, **kwargs):
        print "Entering %s.%s" % (args[0].__class__.__name__, func.__name__)
        return func(*args, **kwargs)
    return with_logging

class C(object):
    @logger
    def f(self):
        pass

C().f()

, Поскольку я сказал, это не будет работать правильно в случаях, где Вы наследовали функцию от родительского класса; в этом случае Вы могли бы сказать

class B(C):
    pass

b = B()
b.f()

и получить сообщение Entering B.f, где Вы на самом деле хотите получить сообщение Entering C.f, так как это - корректный класс. С другой стороны, это могло бы быть приемлемо, в этом случае я рекомендую этот подход по предложению Claudiu.

5
задан Community 23 May 2017 в 10:25
поделиться

2 ответа

Extending your own Markov chain generator is probably your best bet, if you want "random" text. Generating something that has context is an open research problem.

Try (if you haven't):

  • Tokenising punctuation separately, or include punctuation in your chain if you're not already. This includes paragraph marks.
  • If you're using a 2- or 3- history Markov chain, try resetting to using a 1-history one when you encounter full stops or newlines.

Alternatively, you could use WordNet in two passes with your corpus:

  1. Analyse sentences to determine common sequences of word types, ie nouns, verbs, adjectives, and adverbs. WordNet includes these. Everything else (pronouns, conjunctions, whatever) is excluded, but you could essentially pass those straight through. This would turn "The quick brown fox jumps over the lazy dog" into "The [adjective] [adjective] [noun] [verb(s)] over the [adjective] [noun]"
  2. Reproduce sentences by randomly choosing a template sentence and replacing [adjective], [nouns] and [verbs] with actual adjectives nouns and verbs.

There are quite a few problems with this approach too: for example, you need context from the surrounding words to know which homonym to choose. Looking up "quick" in wordnet yields the stuff about being fast, but also the bit of your fingernail.


I know this doesn't solve your requirement for a library or a tool, but might give you some ideas.

7
ответ дан 14 December 2019 в 08:53
поделиться

Что-то вроде этого генератора ipsum Lorem? Также есть ссылки на несколько API.

0
ответ дан 14 December 2019 в 08:53
поделиться
Другие вопросы по тегам:

Похожие вопросы: