gpt4 book ai didi

python - 我很难打印涉及对象的东西

转载 作者:太空宇宙 更新时间:2023-11-03 11:32:51 27 4
gpt4 key购买 nike

我是编程和使用 Python 的初学者。目前,我正在尝试理解 Jurafsky 和 ​​Martin 2008 年出版的关于语音和语言处理的书(关于句法分析的练习 13.1)中的一些代码。我将其复制在下面(除了最后 4 行之外,这段代码不是我自己编写的)。

我的问题很简单:我没有打印语法规则,而是得到这样的输出:

set([<__main__.Rule object at 0x011E1810>, <__main__.Rule object at 0x011E1790>, <__main__.Rule object at 0x011E15F0>, ...)

我知道我应该用 str(self) 做一些事情,但我尝试了一些事情,但仍然没有得到正常的输出。我怀疑解决方案很简单,但我只是不知道该怎么做。很感谢任何形式的帮助。可能您不需要阅读和理解下面的所有代码就可以看到什么不起作用。

非常感谢!

def chomsky_normal_form(grammar):
grammar = set(grammar)
nonterminals = set(rule.head for rule in grammar)

# remove single symbol nonterminal rules
for rule, symbol in _unary_rules(grammar, nonterminals):
grammar.discard(rule)
for rule2 in _rules_headed_by(grammar, symbol):
grammar.add(Rule(rule.head, tuple(rule2.symbols)))
if all(symbol not in rule.symbols for rule in grammar):
for rule2 in _rules_headed_by(grammar, symbol):
grammar.discard(rule2)

# move terminals to their own rules
for rule in list(grammar):
if len(rule.symbols) >= 2:
for i, symbol in enumerate(rule.symbols):
if all(rule.head != symbol for rule in grammar):
rule = _new_symbol(grammar, rule, i, i + 1)

# ensure there are only two nonterminals per rule
for rule in _multi_symbol_rules(grammar):
_new_symbol(grammar, rule, 0, 2)

# return the grammar in CNF
return grammar

# find A -> B rules, allowing concurrent modifications
def _unary_rules(grammar, nonterminals):
while True:
g = ((rule, rule.symbols[0])
for rule in grammar
if len(rule.symbols) == 1
if rule.symbols[0] in nonterminals)
yield g.next()

# find all rules headed by the given symbol
def _rules_headed_by(grammar, symbol):
return [rule for rule in grammar if rule.head == symbol]

# create a new symbol which derives the given span of symbols
def _new_symbol(grammar, rule, start, stop):
symbols = rule.symbols
new_head = '_'.join(symbols[start:stop]).upper()
new_symbols = symbols[:start] + (new_head,) + symbols[stop:]
new_rule = Rule(rule.head, new_symbols)
grammar.discard(rule)
grammar.add(new_rule)
grammar.add(Rule(new_head, symbols[start:stop]))
return new_rule

# find A -> BCD... rules, allowing concurrent modifications
def _multi_symbol_rules(grammar):
while True:
g = (rule for rule in grammar if len(rule.symbols) >= 3)
yield g.next()

# representation of a rule A -> B...C
class Rule(object):
def __init__(self, head, symbols):
self.head = head
self.symbols = symbols
self._key = head, symbols
def __eq__(self, other):
return self._key == other._key
def __hash__(self):
return hash(self._key)
def __str__(self):
rep = grammar_cnf
return rep

# build a grammar from a string of lines like "X -> YZ | b"
def get_grammar(string):
grammar = set()
for line in string.splitlines():
head, symbols_str = line.split(' -> ')
for symbols_str in symbols_str.split(' | '):
symbols = tuple(symbols_str.split())
grammar.add(Rule(head, symbols))
return grammar


grammar = get_grammar("""S -> NP VP | Aux NP VP | VP
NP -> Pronoun | Proper-Noun | Det Nominal
Nominal -> Noun | Nominal Noun | Nominal PP
VP -> Verb | Verb NP | Verb NP PP | Verb PP | VP PP
PP -> Preposition NP
Det -> that | this | a
Noun -> book | flight | meal | money
Verb -> book | include | prefer
Pronoun -> I | she | me
Proper-Noun -> Houston | TWA
Aux -> does
Preposition -> from | to | on | near | through""")

grammar_cnf = chomsky_normal_form(grammar)
print(grammar_cnf)

最佳答案

你可以实现__repr__在你的规则类中

您可以使用 __str__ 作为非正式表示,(如果 str 不存在,它会退回到 repr

这些是像这样完成的:

class Rule(object):

def __init__(self, name):
self.name = name

def __repr__(self):
return 'Rule({0})'.format(self.name)

def __str__(self):
return self.name


rule = Rule('test')
print(rule) # test

关于python - 我很难打印涉及对象的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13936170/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com