Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 94 95 [96] 97 98 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 883441 times)

Vector

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1425 on: February 12, 2012, 01:52:42 am »

All right.

I need something that will break a string into words, and return those words in a list of strings.  However, this is not a job for .split(), because I also need it to remove punctuation.  For example, we'll end up with "you're base" -> ["you", "re", "base"].

First, this is what I'm feeding to map:

Code: [Select]
def cut(x):
    if x in ascii_letters:
         return x
    else: return ' '

Then this is the main function:

Code: [Select]
def extract_words(text):
    return str(map(cut, text)).split()

I actually do it with a lambda, but it's the same thing in the end.

Got any ideas >:?
Logged
"The question of the usefulness of poetry arises only in periods of its decline, while in periods of its flowering, no one doubts its total uselessness." - Boris Pasternak

nonbinary/genderfluid/genderqueer renegade mathematician and mafia subforum limpet. please avoid quoting me.

pronouns: prefer neutral ones, others are fine. height: 5'3".

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1426 on: February 12, 2012, 02:09:12 am »

You should totally be using regex. Ignore that, pointless black magic that is made from pagan programming symbols.

Oh hey look, map is a thing that is already a thing. Ok I see what you are doing now. What error are you getting? Anyway, I think what you want looks like this.

Code: [Select]
def extract_words(text):
    return (''.join(map(cut, text))).split()

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1427 on: February 12, 2012, 02:20:00 am »

All right.

I need something that will break a string into words, and return those words in a list of strings.  However, this is not a job for .split(), because I also need it to remove punctuation.  For example, we'll end up with "you're base" -> ["you", "re", "base"].



I made a function that does this, as part of my pig latin translator, but it's c++
but I didn't remove punctuation, but it shouldn't be too much harder,
my idea is basically search the string for spaces and split there, then search new string for each punctuation type one at a time.
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1428 on: February 12, 2012, 02:23:38 am »

Vector is using a similar logic, but it searches for any invalid punctuation first, then splits the string into words. We need to search first, because 'you're' needs to be ['you', 're'] rather than ['youre'] or ['you re']

Vector

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1429 on: February 12, 2012, 02:24:21 am »

Urgh, that sounds really, really slow =/


Thanks, Max, that worked.  I wish I'd remembered using that from dicking around in the first project... *sigh*
Logged
"The question of the usefulness of poetry arises only in periods of its decline, while in periods of its flowering, no one doubts its total uselessness." - Boris Pasternak

nonbinary/genderfluid/genderqueer renegade mathematician and mafia subforum limpet. please avoid quoting me.

pronouns: prefer neutral ones, others are fine. height: 5'3".

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1430 on: February 12, 2012, 02:29:16 am »

what language is that by the way?

so far i'm only at the point that I can tell if something is / isn't c++,
(and sorry in advance for not having everyones pref. language memorized)
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Vector

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1431 on: February 12, 2012, 02:32:21 am »

Python.  It's easier than C++, or so I hear.

All I know is that it's basically LISP with fewer parentheses, so I like it okay.  It's not my "favorite language," though.  I only know a little Scheme, a little Java, and a bit more Python, so it's not like I can pick and choose favorites right now.
Logged
"The question of the usefulness of poetry arises only in periods of its decline, while in periods of its flowering, no one doubts its total uselessness." - Boris Pasternak

nonbinary/genderfluid/genderqueer renegade mathematician and mafia subforum limpet. please avoid quoting me.

pronouns: prefer neutral ones, others are fine. height: 5'3".

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1432 on: February 12, 2012, 02:35:01 am »

It is python! A scripting language most distinct from the fact that one of its ideals are that we shouldn't have one standard for syntax, and one for style, so it attempts to use style for syntax. For example, it separates blocks of code not by using {} like c++ and others based on it, but instead using levels of indenting. Good learning language, as it beats some good practice into you.

On the other hand, it is so fast and loose with types... Sort of annoying.

PrePost Edit: Yea, a lot easier than c++.

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1433 on: February 12, 2012, 02:40:25 am »

I only know a bit of c++,
everyone said don't learn c++ as a first language because it's difficult,
but I honestly haven't run into any difficulties with it really. Maybe the hard parts are yet to come?
well really I guess comparing the snippits of code, it looks much easier to split a string into smaller strings in python.


Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1434 on: February 12, 2012, 02:43:43 am »

Valid, if you keep doing that, I WILL EAT YOUR EYEBALLS.
Correct code is this
Spoiler (click to show/hide)
Notice the difference? It is subtle, but trust me it helps... Right next to your 'q' button, use it.

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1435 on: February 12, 2012, 02:49:57 am »

that made me smile,  ya I have a very bad habit of not indenting.
you're supposed to indent for every scope, ya?
I'll try to get in the habit of indenting, .... i like my eyeballs.
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1436 on: February 12, 2012, 02:53:32 am »

Yep, every level of scope gets another indent, that means loops and ifs as well.
I can't stand it when people don't indent, makes reading code near impossible...

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1437 on: February 12, 2012, 02:59:02 am »

after looking through the rest of my program, I found all the code I write in the IDE is self indenting (for the most part),
but things I wrote in notepad and copied over, like that entire function, aren't indented.
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1438 on: February 12, 2012, 03:01:43 am »

You really need to get used to writing in the IDE more. You know how to use comments, right?

Vector

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1439 on: February 12, 2012, 03:16:36 am »

RAWR

About 1/2 of next week's project comprete!  It only took about an hour or two, too.  I'm getting a lot better at this.
Logged
"The question of the usefulness of poetry arises only in periods of its decline, while in periods of its flowering, no one doubts its total uselessness." - Boris Pasternak

nonbinary/genderfluid/genderqueer renegade mathematician and mafia subforum limpet. please avoid quoting me.

pronouns: prefer neutral ones, others are fine. height: 5'3".
Pages: 1 ... 94 95 [96] 97 98 ... 796