PUG – Python User’s Group

Public Group active 4 days, 21 hours ago

PUG – Python User’s Group

Group logo of PUG – Python User’s Group

seeking Help for Comp Ling Methods Homework Assignment: Bigrams

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #79927
    Shani Tzoref
    Member

    Hi, group.
    I would greatly appreciate some debugging help, related to a homework assignment in the Comp Ling Methods I course.
    The assigned task was to define a function to list bigrams of a list of strings.
    Feeling stuck in the assignment, I decided to try something simpler:  rather than defining a function, I tried to create a list of bigrams for a specific list.
    For the following list: LSD = [“Lucy”, “in”, “the”, “sky”, “with”, “diamonds”]
    I want to produce code to give me a list of word pairs:
    [[“Lucy”, “in”], [“in”, “the”], [“the”, “sky”], [“sky”, “with”], [“with”, “diamonds”]]

    My attempted code gives me only the strings in the original list, setting them within individual lists, but not as pairs.  I’ve attached a PDF of my Jupyter notebook, and will also paste the failed code here:

    LSD = [“Lucy”, “in”, “the”, “sky”, “with”, “diamonds”]
    #produce list of bigrams

    pairs = []
    for x in range(len(LSD)):
    pair = LSD[x:x +1]
    pairs += [pair]

    print(pairs)

    The result I get is:
    [['Lucy'], ['in'], ['the'], ['sky'], ['with'], ['diamonds']]

    Thanks,
    Shani

     

    Attachments:
    You must be logged in to view attached files.
    #79971
    Stephen Zweibel
    Participant

    Hi Shani,
    You’re close. Take a look at this line:
    pair = LSD[x:x +1]
    You want to grab the next word on the list, but x + 1 won’t do that. you have to refer to the list and get the index of the word you want. And, in order to refer to the index in a for loop, you have to use ‘enumerate’. So here’s what I got:

    pairs = []
    for index, word in enumerate(LSD):
         try:
              next_word = LSD[index+1]
              pairs.append([word, next_word])
         except IndexError:
              pass
    print(pairs)

    I added the ‘try’ clause because otherwise you get an error at the end of the list, when it tried to look for the next word and there isn’t one.

    #79973
    Patrick Smyth, PhD
    Participant

    Hi Shani,

     

    I don’t want to give you too many hints, since I think you’re pretty close to a solution here. But this REPL session might give you an idea of what’s happening:

    https://gist.githubusercontent.com/smythp/f3aa9b9fa8cabd4560a1dc4625c34658/raw/7d609d7e80535e8f37b29275378d3212ec44793a/gistfile1.txt

    Hope that helps a little.

     

    Patrick

    #80324
    Shani Tzoref
    Member

    Thanks for replies!  Sorry I did not respond sooner.  I became overwhelmed and pre-occupied with some personal stuff– including the birth of my first granddaughter last week.  I ended up submitting my incomplete code for the assignment. I am trying to reboot my studies now, and hope to try your advice in the coming days.
    In any case, it was really helpful to get support when I was feeling stuck.  And you both seemed to have hit the right register– of advising and guiding, rather than just feeding me an answer.  What a great forum.

    #80376
    Patrick Smyth, PhD
    Participant

    Congratulations on the birth of your granddaughter!

    #80652

    Congratulations, Shani, on a new grandchild! And glad that you could find
    the help here. It was a fun challenge to watch you all work through. Hoping
    to see more of them!!!!
    Best,
    Lisa

    ~~~~~~~~~~~~~~~~~
    Lisa Rhody, Ph.D.
    Deputy Director of Digital Initiatives
    Director, Digital Fellowship Programs
    Director, Digital Humanities Research Institutes
    The Graduate Center, CUNY
    lrhody@gc.cuny.edu | @lmrhody

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.