Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > General Coding
You last visited: Today at 05:18

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Python-Help needed

Discussion on Python-Help needed within the General Coding forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 60
Join Date: Dec 2009
Posts: 6,456
Received Thanks: 5,142
Python-Help needed

EN:
Hey, I have to write a function which tokenizes the Input "sentence".
Currently I do that with:
Code:
x = sentence.split()
This code works fine.
But now I have to make sure that punctuation (. , : etc.) gets treated like a part of the strings so that e.g.:
sentence = "Hi, Ich bins."

is NOT just:

["Hi," "Ich", "bins."] (like it would be with .split() )

but that the output is:

["Hi", ",", "Ich", "bins", "."]

How can I get to that?
Apparently I need to use a for-loop for it, but I have no clue how.
(i cannot use NLTK or regex)

DE:
ηøℓι is offline  
Old 06/03/2019, 05:25   #2



 
cypher's Avatar
 
elite*gold: 600
The Black Market: 1061/0/0
Join Date: Sep 2008
Posts: 10,548
Received Thanks: 3,093
Ooops no regex. Ok sorry.
cypher is offline  
Old 06/03/2019, 16:06   #3
 
0xFADED's Avatar
 
elite*gold: 7
Join Date: Dec 2013
Posts: 446
Received Thanks: 187
Quote:
Originally Posted by ηøℓι View Post
Apparently I need to use a for-loop for it, but I have no clue how.
(i cannot use NLTK or regex)
I dont know python, but basically you just need to loop through the characters and buffer them until you hit a space or punctuation.

Here's an example in C++:
0xFADED is offline  
Old 06/04/2019, 01:47   #4
 
elite*gold: 0
Join Date: Apr 2011
Posts: 363
Received Thanks: 167
Quote:
Originally Posted by ηøℓι View Post
EN:
Hey, I have to write a function which tokenizes the Input "sentence".
Currently I do that with:
Code:
x = sentence.split()
This code works fine.
But now I have to make sure that punctuation (. , : etc.) gets treated like a part of the strings so that e.g.:
sentence = "Hi, Ich bins."

is NOT just:

["Hi," "Ich", "bins."] (like it would be with .split() )

but that the output is:

["Hi", ",", "Ich", "bins", "."]

How can I get to that?
Apparently I need to use a for-loop for it, but I have no clue how.
(i cannot use NLTK or regex)

DE:
If you go to the python docs and read string.split function u will find out that you can specify a separator char, then using something like this should do the trick
Code:
str = "im a string, and have punctuation too."

print(str.split(" "))
elmarcia is offline  
Old 06/04/2019, 11:13   #5
 
0xFADED's Avatar
 
elite*gold: 7
Join Date: Dec 2013
Posts: 446
Received Thanks: 187
Quote:
Originally Posted by elmarcia View Post
u will find out that you can specify a separator char
Read the question again elmarcia.
OP specifically asked for a solution that treats punctuation like it was enclosed in whitespace.
Split is not enough here, it will yield the false ["string,"] result where a ["string", ","] was expected.
0xFADED is offline  
Old 06/04/2019, 11:58   #6
 
elite*gold: 60
Join Date: Dec 2009
Posts: 6,456
Received Thanks: 5,142
Quote:
Originally Posted by 0xFADED View Post
I dont know python, but basically you just need to loop through the characters and buffer them until you hit a space or punctuation.

Here's an example in C++:
Thank you, my solution in the end looked like this:
(the code might be weird, but hey.. it works! )




and the official / easiest solution seems to be:
Code:
emptylist = []

    for y in x.split():                    #x = input-sentence
        if x[-1] in ".,!?;":
            emptylist.extend([x[:-1], x[-1]])
        else:
            emptylist.append(x)

    return emptylist
Glad this could be solved, thank you for helping!
ηøℓι is offline  
Thanks
1 User
Reply


Similar Threads Similar Threads
PROBLEM WITH PYTHON PLEASE HELP ME/ PROBLEM MIT PYTHON BITTE HELFEN MICH
05/26/2017 - Metin2 Private Server - 0 Replies
What are the values to change to move the search for the bonus in the item that is in the inventory's first slot? I would like to move it to the second slot (python switcher) Was sind die Werte, die geändert werden sollen, um die Suche nach dem Bonus in dem Element zu verschieben, der sich im ersten Slot des Inventars befindet? Ich möchte es auf den zweiten Slot (Python Switcher)
[Python-Modul]EXP-Donator (kompatibel mit Python Loader)
11/23/2013 - Metin2 Hacks, Bots, Cheats, Exploits & Macros - 27 Replies
Moin, da man mich danach gefragt hat und ich sowieso mal ein Beispiel für die Benutzung meines Python Loaders veröffentlichen wollte, habe ich die Gelegenheit genutzt und euch eben einen EXP-Spendebot geschrieben. Man kann ihn einfach mit dem oben verlinkten Python Module Loader laden und ihn mit F5 aktivieren/deaktivieren. Sobald ihr mehr als 99 Erfahrungspunkte habt (man kann nur in 100er Schritten spenden), werden alle Erfahrungspunkte an eure Gilde gespendet. Wer Lust hat und...
Help to make a python file works with python loader
03/03/2013 - Metin2 - 2 Replies
Hey epvp! I want make a very. Little hack works on pythonn loader can anybody help me please?
Python + Eric Python IDE installieren ?!
07/05/2011 - General Coding - 0 Replies
hat sich erledigt.
Python castle's dungeon [Help needed]
09/17/2008 - Dekaron Private Server - 10 Replies
Hey guys :], I've been trying to go inside the python castle, so I can go to the dungeon... but I can't o.o I mean, I talk with the door and obviously I can't understand a sh!$, so yeah... do I have do a quest before? :O Can you tell me which? or what to do? :]



All times are GMT +2. The time now is 05:18.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.