PUG – Python User’s Group
seeking Help for Comp Ling Methods Homework Assignment: Bigrams
- This topic has 5 replies, 4 voices, and was last updated Nov 5, 2019, 7:55 pm by .
-
AuthorPosts
-
October 17, 2019 at 7:19 pm #79927Shani TzorefMember
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 bigramspairs = []
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,
ShaniAttachments:
You must be logged in to view attached files.October 18, 2019 at 12:22 pm #79971Stephen ZweibelParticipantHi 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.
- This reply was modified 4 years, 5 months ago by Stephen Zweibel.
October 18, 2019 at 12:26 pm #79973Patrick Smyth, PhDParticipantHi 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:
Hope that helps a little.
Patrick
October 29, 2019 at 9:51 am #80324Shani TzorefMemberThanks 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.October 29, 2019 at 7:55 pm #80376Patrick Smyth, PhDParticipantCongratulations on the birth of your granddaughter!
November 5, 2019 at 7:55 pm #80652Lisa Marie RhodyMemberCongratulations, 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 -
AuthorPosts
You must be logged in to reply to this topic.
Need help with the Commons?
Email us at commonshelpsite@gmail.com