我最近正致力于使用nltk从文本中提取关系.所以我建立了一个示例文本:"汤姆是微软的联合创始人." 并使用以下程序测试并返回任何内容.我无法弄清楚为什么.
我使用的是NLTK版本:3.2.1,python版本:3.5.2.
这是我的代码:
import re
import nltk
from nltk.sem.relextract import extract_rels, rtuple
from nltk.tokenize import sent_tokenize, word_tokenize
def test():
with open('sample.txt', 'r') as f:
sample = f.read() # "Tom is the cofounder of Microsoft"
sentences = sent_tokenize(sample)
tokenized_sentences = [word_tokenize(sentence) for sentence in sentences]
tagged_sentences = [nltk.tag.pos_tag(sentence) for sentence in tokenized_sentences]
OF = re.compile(r'.*\bof\b.*')
for i, sent in enumerate(tagged_sentences):
sent = nltk.chunk.ne_chunk(sent) # ne_chunk method expects one tagged sentence
rels = extract_rels('PER', 'GPE', sent, corpus='ace', pattern=OF, window=10)
for rel …Run Code Online (Sandbox Code Playgroud) 我一直在玩 miniKanren,试图通过将非常基本的 Prolog 教程转换成它来理解它。
我习惯使用 Python,所以我从 LogPy 库开始,该库后来被分叉和改进为一个实际上称为miniKanren 的库
从lib的README中给出的示例我们可以看到:
>>> from kanren import Relation, facts
>>> parent = Relation()
>>> facts(parent, ("Homer", "Bart"),
... ("Homer", "Lisa"),
... ("Abe", "Homer"))
>>> run(1, x, parent(x, "Bart"))
('Homer',)
Run Code Online (Sandbox Code Playgroud)
这与您在 Prolog 教程开始时可能看到的内容基本对应,例如:
% facts.pl
parent(homer, bart).
parent(homer, lisa).
parent(abe, homer).
?- consult('facts')
true.
?- parent(X, bart).
X = homer
Run Code Online (Sandbox Code Playgroud)
我对此感到很高兴...
后来我发现自己阅读了更多的 MiniKanren 文献(一般意义上,不是 Python 库),我意识到我还没有看到任何以这种方式使用事实数据库的示例,也没有提到过这样的示例。
我错过了吗?或者这实际上不是 MiniKanren 的一个“理性策划者”的功能?
我确实在 Clojure 实现中找到了这样的东西core.logic,其中有:https:
//github.com/clojure/core.logic/wiki/Features#simple-in-memory-database
它的工作方式非常相似,尽管比 python 更好,因为数据库是一个不同的实体,而不是库中的全局变量。
python lib 是否只是借用了非 kanren 的想法 …
minikanren clojure-core.logic knowledge-base-population reasoned-schemer