Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据布尔真值函数计算字符串_Python_Logic_Boolean_Nltk_Boolean Logic - Fatal编程技术网

Python 根据布尔真值函数计算字符串

Python 根据布尔真值函数计算字符串,python,logic,boolean,nltk,boolean-logic,Python,Logic,Boolean,Nltk,Boolean Logic,我已经搜索过了,这里仍然没有线索,所以请容忍我 我有字符串,每个字符串对应一个特定的特征矩阵。示例: 'a' = [-vegetable, +fruit, +apple, -orange] 'o' = [-vegetable, +fruit, -apple, +orange] 't' = [+vegetable, -fruit, -apple, -orange] 请注意,这正是我在这里选择用来表示矩阵的符号 我想做的是,取任意数量的这样的字符串,并根据一定数量的真值函数对它们求值。因此,对字符

我已经搜索过了,这里仍然没有线索,所以请容忍我

我有字符串,每个字符串对应一个特定的特征矩阵。示例:

'a' = [-vegetable, +fruit, +apple, -orange]
'o' = [-vegetable, +fruit, -apple, +orange]
't' = [+vegetable, -fruit, -apple, -orange]
请注意,这正是我在这里选择用来表示矩阵的符号

我想做的是,取任意数量的这样的字符串,并根据一定数量的真值函数对它们求值。因此,对字符串“aoaot”进行以下评估:

[+fruit] => [+apple]
equivalently: (not [+fruit]) or [+apple]
对于给定字符串,应返回此蕴涵为false的次数。要么像这样:

[True, False, True, False, True]

或评估数量的绝对计数为False,例如此处为2。用python做这件事的明智方法是什么?我正在研究NLTK,但不确定。

您可以使用set类型实现必要的逻辑


如果您想在自己的语法中有更大的灵活性,下面是一个简单的解析器,用于您给出的数据定义:

data = """\
a = [-vegetable, +fruit, +apple, -orange, -citrus] 
o = [-vegetable, +fruit, -apple, +orange, +citrus] 
t = [+vegetable, -fruit]"""

from pyparsing import Word, alphas, oneOf, Group, delimitedList

# a basic token for a word of alpha characters plus underscores
ident = Word(alphas + '_')

# define a token for leading '+' or '-', with parse action to convert to bool value
inclFlag = oneOf('+ -')
inclFlag.setParseAction(lambda t: t[0] == '+')

# define a feature as the combination of an inclFlag and a feature name
feature = Group(inclFlag('has') + ident('feature'))

# define a definition
defn = ident('name') + '=' + '[' + delimitedList(feature)('features') + ']'

# search through the input test data for defns, and print out the parsed data
# by name, and the associated features
defns = defn.searchString(data)
for d in defns:
    print d.dump()
    for f in d.features:
        print f.dump('  ')
    print
印刷品:

['a', '=', '[', [False, 'vegetable'], [True, 'fruit'], [True, 'apple'], [False, 'orange'], [False, 'citrus'], ']']
- features: [[False, 'vegetable'], [True, 'fruit'], [True, 'apple'], [False, 'orange'], [False, 'citrus']]
- name: a
  [False, 'vegetable']
  - feature: vegetable
  - has: False
  [True, 'fruit']
  - feature: fruit
  - has: True
  [True, 'apple']
  - feature: apple
  - has: True
  [False, 'orange']
  - feature: orange
  - has: False
  [False, 'citrus']
  - feature: citrus
  - has: False

['o', '=', '[', [False, 'vegetable'], [True, 'fruit'], [False, 'apple'], [True, 'orange'], [True, 'citrus'], ']']
- features: [[False, 'vegetable'], [True, 'fruit'], [False, 'apple'], [True, 'orange'], [True, 'citrus']]
- name: o
  [False, 'vegetable']
  - feature: vegetable
  - has: False
  [True, 'fruit']
  - feature: fruit
  - has: True
  [False, 'apple']
  - feature: apple
  - has: False
  [True, 'orange']
  - feature: orange
  - has: True
  [True, 'citrus']
  - feature: citrus
  - has: True

['t', '=', '[', [True, 'vegetable'], [False, 'fruit'], ']']
- features: [[True, 'vegetable'], [False, 'fruit']]
- name: t
  [True, 'vegetable']
  - feature: vegetable
  - has: True
  [False, 'fruit']
  - feature: fruit
  - has: False

Pyparsing为您完成了很多开销,比如迭代输入字符串、跳过不相关的空白以及使用命名属性返回解析后的数据。查看pyparsing wiki SimpleBool.py上的布尔求值器,或更完整的布尔求值器包booleano。

除非您想解析以自然语言提出的查询,否则不需要NLTK。解析布尔表达式,即使允许任意嵌套,也是相当简单的,因为大多数解析技术对某些解析技术来说几乎微不足道。我试图为语音约束排序建立一个最优理论评估函数。如果“t”代表“西红柿”,至少从植物学角度来说,这实际上是一种水果。这里到底比较的是什么
['a', '=', '[', [False, 'vegetable'], [True, 'fruit'], [True, 'apple'], [False, 'orange'], [False, 'citrus'], ']']
- features: [[False, 'vegetable'], [True, 'fruit'], [True, 'apple'], [False, 'orange'], [False, 'citrus']]
- name: a
  [False, 'vegetable']
  - feature: vegetable
  - has: False
  [True, 'fruit']
  - feature: fruit
  - has: True
  [True, 'apple']
  - feature: apple
  - has: True
  [False, 'orange']
  - feature: orange
  - has: False
  [False, 'citrus']
  - feature: citrus
  - has: False

['o', '=', '[', [False, 'vegetable'], [True, 'fruit'], [False, 'apple'], [True, 'orange'], [True, 'citrus'], ']']
- features: [[False, 'vegetable'], [True, 'fruit'], [False, 'apple'], [True, 'orange'], [True, 'citrus']]
- name: o
  [False, 'vegetable']
  - feature: vegetable
  - has: False
  [True, 'fruit']
  - feature: fruit
  - has: True
  [False, 'apple']
  - feature: apple
  - has: False
  [True, 'orange']
  - feature: orange
  - has: True
  [True, 'citrus']
  - feature: citrus
  - has: True

['t', '=', '[', [True, 'vegetable'], [False, 'fruit'], ']']
- features: [[True, 'vegetable'], [False, 'fruit']]
- name: t
  [True, 'vegetable']
  - feature: vegetable
  - has: True
  [False, 'fruit']
  - feature: fruit
  - has: False