从数据库中替换句子中的单词(Python / Django)

Posted

技术标签:

【中文标题】从数据库中替换句子中的单词(Python / Django)【英文标题】:Replacing words in a sentence from database (Python/Django) 【发布时间】:2012-09-26 14:39:29 【问题描述】:

我有一个这样的字符串句子结构:

从 $Duration% 开始,$Noun$ 将是 $Adjective$

我需要一个视图来检查“$$”中的哪些单词,并将它们替换为从 SQL 文件导入的数据库中的随机单词。

这里是模型/sql 文件:

from django.db import models
from django.utils import timezone

class Type(models.Model):
    type = models.CharField(max_length=50)
    value = models.CharField(max_length=1)


class Word(models.Model):
    dict = models.CharField(max_length=200)
    kind = models.CharField(max_length=1)

    def randword(self):
        words = self.objects.all()
        number = randrang(0,len(words))
        return words[number]


class Question(models.Model):
    question = models.CharField(max_length=200)

    def randquest(self):
        quests = self.objects.all()
        numbeq = randrang(0, len(quests))
        return quests[numbeq]


class Meta(models.Model):
    question = models.ForeignKey(Question)
    score = models.ForeignKey("Score")
    user = models.CharField(max_length=200)
    time = models.DateTimeField('date published')


class Score(models.Model):
    word = models.ForeignKey(Word)
    score = models.IntegerField()
    question = models.ManyToManyField(Question, through=Meta)

    def __unicode__(self):
        return self.score

SQL 文件

INSERT INTO quest_question (question) VALUES ('Starting in $Duration$, the $Noun$ will be $Adjective$');

INSERT INTO quest_word (dict, kind) VALUES ('Coffee Maker', '1');
INSERT INTO quest_word (dict, kind) VALUES ('24 hours', '3');
INSERT INTO quest_word (dict, kind) VALUES ('today', '3');
INSERT INTO quest_word (dict, kind) VALUES ('broken', '2');
INSERT INTO quest_word (dict, kind) VALUES ('Email server', '1');
INSERT INTO quest_word (dict, kind) VALUES ('15 minutes', '3');
INSERT INTO quest_word (dict, kind) VALUES ('tomorrow', '3');
INSERT INTO quest_word (dict, kind) VALUES ('unavailable', '2');


INSERT INTO quest_type (type, value) VALUES ('Noun', '1');
INSERT INTO quest_type (type, value) VALUES ('Adjective', '2');
INSERT INTO quest_type (type, value) VALUES ('Duration', '3');

我开始写视图。需要一些帮助:

from quest.models import Word, Type, Score, Question, Meta
from django.utils import timezone


def question(request):
    sentence = Question.objects.all()
    find_words = re.findall(r"\w+%0-9a-zA-Z%", sentence.question)

【问题讨论】:

$...$ 符号是否固定? 没有。我只是用'$'作为标记。它可以被替换。 【参考方案1】:

如果您愿意更改符号,您有两种选择:

    Use Python.

    >>> 'Starting in Duration, the Noun will be Adjective'.format(Duration='24 hours', Noun='Coffee Maker', Adjective='broken')
    'Starting in 24 hours, the Coffee Maker will be broken'
    >>> 'Starting in %(Duration)s, the %(Noun)s will be %(Adjective)s' % dict(Duration='24 hours', Noun='Coffee Maker', Adjective='broken')
    'Starting in 24 hours, the Coffee Maker will be broken'
    

    Use Django.

    >>> from django.template import Context, Template
    >>> t = Template('Starting in Duration, the Noun will be Adjective') 
    >>> c = Context(dict(Duration='24 hours', Noun='Coffee Maker', Adjective='broken'))
    >>> t.render(c)
    u'Starting in 24 hours, the Coffee Maker will be broken'
    

剩下的就是从数据库中提取并选择random处的单词,这是最简单的部分。

【讨论】:

是的,我可以更改表示法。使用 't = Template('Starting in Duration, the Noun will be Adjective''这一行,你是否假设问题保持不变?我也想为任何问题。 你可以将任何你喜欢的东西传递给Template构造函数。 如何更改 >>> c = Context(dict(Duration='24 hours', Noun='Coffee Maker', Adjective='broken')) 以便生成随机单词? 你有没有花点时间看一下答案最后一句中的链接?

以上是关于从数据库中替换句子中的单词(Python / Django)的主要内容,如果未能解决你的问题,请参考以下文章

如何替换包含完整句子的列的每一行中的多个单词?

替换字符串中的特定单词(Python)

Python scikit-learn 从演讲者的句子中选择最佳单词

如何让 Python 循环遍历句子,但它给了我句子中的单词而不是句子中的每个字母? [复制]

Leetcode 648.单词替换

pandas:只替换单词而不是整个句子