python-3.x 使用PyDatasheet的Maven系统对各种家庭关系进行建模和推理

qf9go6mv  于 2023-10-21  发布在  Python
关注(0)|答案(1)|浏览(112)

我正在开发一个Maven系统,使用PyDatalog来建模和推断各种家庭关系。
这是使用的数据(从CSV文件加载):

name father_name mother_name  gender
0      Ahmad      Bachar       Rafah    Male
1      Amjad      Bachar       Rafah    Male
2      Danah      Bachar       Rafah  Female
3      Yazan      Hassan      Ghalia    Male
4       Leen      Hassan      Ghalia  Female
5       Dema       Faiaz       Sahar  Female
6      Dania       Faiaz       Sahar  Female
7      Tareq       Faiaz       Sahar    Male
8      Asmaa      Waleed       Hanan  Female
9       Alaa      Waleed       Hanan  Female
10   Tasneem      Waleed       Hanan  Female
11     Firas      Waleed       Hanan    Male
12    Farouk     Shareef      Sameah    Male
13    Usaema       Saeed      Adebeh  Female
14    Bachar      Farouk      Usaema    Male
15    Hassan      Farouk      Usaema    Male
16     Rafah      Zuhair     Rukaieh  Female
17    Zoukaa      Zuhair     Rukaieh  Female
18    Lujain       Adnan      Zoukaa  Female
19  Mohammad       Adnan      Zoukaa    Male

首先,我添加了以下条款:

pyDatalog.create_terms('X,Y,Z,W, parent, male, female, father_of, mother_of, 
son, daughter, aunt, uncle, cousin, niece, nephew, sibling, brother, sister')

这就是我如何定义规则son, male, daughter, female, father_of, mother_of

for index, row in data.iterrows():
    name, father_name, mother_name, gender = row['name'], row['father_name'], row['mother_name'], row['gender']
    if gender == 'Male':
        +son(name, father_name)
        +son(name, mother_name)
        +male(name)
    else:
        +daughter(name, father_name)
        +daughter(name, mother_name)
        +female(name)

    +father_of(father_name, name)
    +mother_of(mother_name, name)

测试规则给出了正确的答案:

print(son(X, "Bachar"))
print(son(X, "Sahar"))
# Output:
X    
-----
Amjad
Ahmad
X    
-----
Tareq

这也适用于parent, brother, sister, sibling:* (不包括结果,以避免使问题很长,但测试和工作100%)*

parent(X, Y) <= father_of(X, Y)
parent(X, Y) <= mother_of(X, Y)
brother(X, Y) <= (parent(Z, X) & parent(Z, Y) & male(X) & (X != Y))
sister(X, Y) <= (parent(Z, X) & parent(Z, Y) & (X != Y) & female(X))
sibling(X,Y) <= parent(Z, X) & parent(Z, Y) & (X != Y)

这个问题的问题和目的是定义uncleaunt:我这样写道:

uncle(X, Y) <= brother(X, Z) & parent(Z, Y) & male(X)
aunt(X, Y) <= sister(X, Z) & parent(Z, Y) & female(X)
cousin(X, Y) <= parent(Z, X) & (sister(X, Z) & parent(Z, Y))

用“Yazan”和“Ahmad”测试它是可以的,但是用“Asmaa”它输出一个空列表,如果你去上面的数据,输出应该是“Bachar,哈桑”。同样的,auntcousin规则也会输出错误的答案。有什么我错过了或者需要纠正的吗?

z4iuyo4d

z4iuyo4d1#

我去了PyDatalog测试版,检查了使用他们的交互式教程编写的代码,我发现一个问题与Google Colab本身有关,我不得不终止会话并启动一个新的会话。
解决这个问题需要包含额外的数据,因此根据PyDatalog,空列表[]也可能是逻辑或Facts and rules声明中错误的指示符,而这反过来又不容易解决或检测。
我做了什么来解决这个问题?简单的答案是改变reltives family relationships的定义逻辑,并添加有关堂兄父母的信息,如i.e.。祖父母和兄弟姐妹。

相关问题