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)
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
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)
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(" "))
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.
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
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...
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? :]