|
You last visited: Today at 05:18
Advertisement
Python-Help needed
Discussion on Python-Help needed within the General Coding forum part of the Coders Den category.
06/03/2019, 01:38
|
#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:
Hey, habe folgende Aufgabe:
Ich habe einen Input "sentence" (ein Satz) den ich splitten muss in eine liste.
Also nehme ich den Input und mache zB folgendes:
Code:
x = sentence.split()
Dieser Part funktioniert.
Das Problem ist, jetzt soll ich den Input so splitten, dass er beim Splitten auch auf Punkte, Kommas etc. achtet und diese als einzige Einträge in der Liste sieht.
Also zB der Satz "Hi, Ich bins."
ist momentan mit "sentence.split() folgendes:
["Hi," "Ich", "bins."]
soll aber jetzt folgendes werden:
["Hi", ",", "Ich", "bins", "."]
Wie kriege ich das hin?
Anscheinend muss ich eine for-loop benutzen, aber ehrlich gesagt kp
|
|
|
06/03/2019, 05:25
|
#2
|
elite*gold: 600
Join Date: Sep 2008
Posts: 10,548
Received Thanks: 3,093
|
Ooops no regex. Ok sorry.
|
|
|
06/03/2019, 16:06
|
#3
|
elite*gold: 7
Join Date: Dec 2013
Posts: 446
Received Thanks: 187
|
Quote:
Originally Posted by ηøℓι
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++:
Code:
std::string input = "Hi, Ich bins.";
std::vector<std::string> result;
std::string buf;
for (char const& chr : input) {
if (chr == ' ') {
if (!buf.empty()) {
result.push_back(buf);
}
buf.clear();
continue;
}
if (chr == ',' || chr == '.') {
if (!buf.empty()) {
result.push_back(buf);
}
buf.clear();
result.push_back({chr});
continue;
}
buf += chr;
}
// result is now ["Hi", ",", "Ich", "bins", "."]
|
|
|
06/04/2019, 01:47
|
#4
|
elite*gold: 0
Join Date: Apr 2011
Posts: 363
Received Thanks: 167
|
Quote:
Originally Posted by ηøℓι
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:
Hey, habe folgende Aufgabe:
Ich habe einen Input "sentence" (ein Satz) den ich splitten muss in eine liste.
Also nehme ich den Input und mache zB folgendes:
Code:
x = sentence.split()
Dieser Part funktioniert.
Das Problem ist, jetzt soll ich den Input so splitten, dass er beim Splitten auch auf Punkte, Kommas etc. achtet und diese als einzige Einträge in der Liste sieht.
Also zB der Satz "Hi, Ich bins."
ist momentan mit "sentence.split() folgendes:
["Hi," "Ich", "bins."]
soll aber jetzt folgendes werden:
["Hi", ",", "Ich", "bins", "."]
Wie kriege ich das hin?
Anscheinend muss ich eine for-loop benutzen, aber ehrlich gesagt kp
|
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(" "))
|
|
|
06/04/2019, 11:13
|
#5
|
elite*gold: 7
Join Date: Dec 2013
Posts: 446
Received Thanks: 187
|
Quote:
Originally Posted by elmarcia
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.
|
|
|
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
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++:
Code:
std::string input = "Hi, Ich bins.";
std::vector<std::string> result;
std::string buf;
for (char const& chr : input) {
if (chr == ' ') {
if (!buf.empty()) {
result.push_back(buf);
}
buf.clear();
continue;
}
if (chr == ',' || chr == '.') {
if (!buf.empty()) {
result.push_back(buf);
}
buf.clear();
result.push_back({chr});
continue;
}
buf += chr;
}
// result is now ["Hi", ",", "Ich", "bins", "."]
|
Thank you, my solution in the end looked like this:
(the code might be weird, but hey.. it works! )
Code:
x = sentence.split()
y = len(x)
f = 0
for entry in x:
z = x[y-1][-1] # last character of last word in sentence
y -= 1 # adds -1 to last word so we get 2. last word
if z == ".":
w = x[y][f - 2]
w = w.replace(".", " ")
w = w + " ."
w = x[y][:-1] + " ."
x[y] = w
#done = x[y].split()
elif z == ",":
w = x[y][f - 2]
w = w.replace(",", " ")
w = w + " ,"
w = x[y][:-1] + " ,"
x[y] = w
##done = x[y].split()
elif z == "?":
w = x[y][f - 2]
w.replace("?", " ")
w = w + " ?"
w = x[y][:-1] + " ?"
x[y] = w
#done = x[y].split()
elif z == "!":
w = x[y][f - 2]
w.replace("!", " ")
w = w + " !"
w = x[y][:-1] + " !"
x[y] = w
#done = x[y].split()
elif z == ":":
w = x[y][f - 2]
w.replace(":", " ")
w = w + " :"
w = x[y][:-1] + " :"
x[y] = w
#done = x[y].split()
elif z == ";":
w = x[y][f - 2]
w.replace(";", " ")
w = w + " ;"
w = x[y][:-1] + " ;"
x[y] = w
#done = x[y].split()
a = x # (idk why this is here lol)
b = " ".join(a)
kek = b.split()
#print(kek)
return kek
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!
|
|
|
 |
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.
|
|