假设我有以下模型:
class NamedEntity(models.Model):
name = models.CharField(max_length=100)
NE_CHOICES = (
('PER', 'Person'),
('TTL', 'Title / Role'),
('ORG', 'Organization'),
('LOC', 'Location')
)
ne_type = models.CharField(max_length=30, choices=NE_CHOICES)
class Document(models.Model):
content = models.TextField()
entities = models.ManyToManyField(NamedEntity)
def find_entities(self):
## First locate user entities
## Locate general Named Entities
print "Finding entities..."假设数据库中大约有一千个命名实体。索引/搜索文档的内容字段、查找整个命名实体列表的所有可能实例作为文档模型的一种方法的最佳方式是什么?
示例数据:
Document.content字段可能包含以下字符串:
"Hey Joe,
I wanted to see if you and Cassy might be interested in going to Franky's on friday night.
-Fred"NamedEntities的完整表格将包含如下所示的.name字段条目:
"Mark"
"Peter"
"Franky's"
"Seattle"
"Fred"
"Amber"
"Joe"
...assuming a couple thousand entries.我希望在Document.content字段中找到这些NamedEntity.name值的所有可能实例。就我希望结果的外观而言,我可以使用原始字符串的标记版本:
"Hey \(NE/01254)Joe,
I wanted to see if you and \(NE/01942)Cassy might be interested in going to \(NE/02223)Franky's on friday night.
-\(NE/023432)Fred"或字符串索引的字典:
{ 01254 : (4,6),
01942 : (33, 37),
02223 : ... } 发布于 2011-07-07 11:11:56
根据我的理解,我的想法如下:
<代码>H113搜索以查看数据库中是否存在任何单词<代码>H214<代码>F215
我假设您已经考虑过对NamedEntity.name字段进行索引。
如果单词列表很小,则使用或语句似乎是在数据库中搜索姓名的最佳选择。但是,我不确定语句的大小有多大。此外,我不知道内容字段会有多大。
如果TextField有比数据库更多的唯一词,这可能不是最好的方法。
希望我的理解是正确的。
https://stackoverflow.com/questions/6565482
复制相似问题