Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9009167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:07:19+00:00 2026-06-16T02:07:19+00:00

I’m a beginner to programming and work with Python. At the moment I’m trying

  • 0

I’m a beginner to programming and work with Python. At the moment I’m trying to understand some code from Jurafsky and Martin’s 2008 book on Speech and Language Processing (exercise 13.1 on syntactic parsing). I’ll copy it below (apart from the last 4 lines, I didn’t write this code myself).

My question is quite simple: instead of printing grammar rules, I get output like this:

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

I know I should do something with str(self), but I tried a few things and still don’t get a normal output. I suspect the solution is quite simple, but I just don’t know what to do. Any help is very much appreciated. Probably you don’t need to read and understand all the code below to see what is not working.

Thanks a lot!

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:07:20+00:00Added an answer on June 16, 2026 at 2:07 am

    you can implemetn __repr__ in your Rule class

    you can use __str__ for the informal representation, (if str isn’t present it falls back to repr

    these are done something like:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Basically, what I'm trying to create is a page of div tags, each has
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.