Why does this python code is not translating the words?
Question:
why is this python code not working? I can’t figure out the problem.
he always print the same word…
woerterbuch = {"He" :" Er", "She" : "Sie", "it":"es", "Hello":"Hallo", "How":"Wie", "Are":"Geht", "You":"Dir"}
wort = input("Geben Sie den zu übersetzenden Satz ein: ")
woerter = wort.split()
uebersetzung_wort = ""
for wort in woerter:
wort = wort.lower()
if wort in woerterbuch:
uebersetzung = woerterbuch[wort]
else:
uebersetzung = wort
uebersetzung_wort = uebersetzung_wort + " " + uebersetzung
print("Die Übersetzung dieses Satzes ist: ", uebersetzung_wort)
Answers:
The problem is that you lowercase your word here: wort = wort.lower()
but the key in your dict are not lowercased.
You can do the following:
woerterbuch = {"He" :" Er", "She" : "Sie", "it":"es", "Hello":"Hallo", "How":"Wie", "Are":"Geht", "You":"Dir"}
woerterbuch_lowered = {key.lower():value for key,value in woerterbuch.items()}
for wort in woerter:
wort = wort.lower()
uebersetzung = woerterbuch_lowered.get(wort,wort)
uebersetzung_wort = uebersetzung_wort + " " + uebersetzung
You convert your words (woerter? ;->) to lowercase with:
wort = wort.lower()
but some keys in your dictionary start with uppercase. Remove the above line and your code should work (despite being unglaublich hässlich)
There are a couple reasons that you’ll run into issue translating from a technical perspective. From a linguistic perspective is another question. As Gabip already pointed out, the keys in your dictionary are not lowercased.
In addition to that, you should sanitize your input of punctuation. When I input “Hallo, wie geht es?”, neither “Hallo” nor “es” will be translated because your dictionary never gets those as keys. It gets “Hallo,” and “es?” which are not keys in your dictionary.
You have capital letters in woerterbuch but you look for only lower case because of wort = wort.lower(). Try changing woerterbuch to :
woerterbuch = {"he" :" Er", "she" : "Sie", "it":"es", "hello":"Hallo", "how":"Wie", "are":"Geht", "you":"Dir"}
and then it will work correctly.
import cv2
import pathlib
mycascade = pathlib.Path(cv2.file).parent.absolute() / "data/haarcascade_frontalface_def"
clf = cv2.CascadeClassifier(str(mycascade))
cam = cv2.videoCapture(0)
with True:
_ , frame = cam.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = clf.detectMultiScele(
gray,
scaleFactor = 1.1,
minNeighbors = 3,
minsize = (30,30),
flogs = cv2.CASCADE_SCALE_IMAGE
)
for (x,y,width,height) in faces:
cv2.rectangle(frame, (x,y), [x + width, y + height], [(0,0,255)],4)
cv2.imshow(‘MrRobot’, frame)
if cv2.waitKey(1) == ord("q"):
break
why is this python code not working? I can’t figure out the problem.
he always print the same word…
woerterbuch = {"He" :" Er", "She" : "Sie", "it":"es", "Hello":"Hallo", "How":"Wie", "Are":"Geht", "You":"Dir"}
wort = input("Geben Sie den zu übersetzenden Satz ein: ")
woerter = wort.split()
uebersetzung_wort = ""
for wort in woerter:
wort = wort.lower()
if wort in woerterbuch:
uebersetzung = woerterbuch[wort]
else:
uebersetzung = wort
uebersetzung_wort = uebersetzung_wort + " " + uebersetzung
print("Die Übersetzung dieses Satzes ist: ", uebersetzung_wort)
The problem is that you lowercase your word here: wort = wort.lower()
but the key in your dict are not lowercased.
You can do the following:
woerterbuch = {"He" :" Er", "She" : "Sie", "it":"es", "Hello":"Hallo", "How":"Wie", "Are":"Geht", "You":"Dir"}
woerterbuch_lowered = {key.lower():value for key,value in woerterbuch.items()}
for wort in woerter:
wort = wort.lower()
uebersetzung = woerterbuch_lowered.get(wort,wort)
uebersetzung_wort = uebersetzung_wort + " " + uebersetzung
You convert your words (woerter? ;->) to lowercase with:
wort = wort.lower()
but some keys in your dictionary start with uppercase. Remove the above line and your code should work (despite being unglaublich hässlich)
There are a couple reasons that you’ll run into issue translating from a technical perspective. From a linguistic perspective is another question. As Gabip already pointed out, the keys in your dictionary are not lowercased.
In addition to that, you should sanitize your input of punctuation. When I input “Hallo, wie geht es?”, neither “Hallo” nor “es” will be translated because your dictionary never gets those as keys. It gets “Hallo,” and “es?” which are not keys in your dictionary.
You have capital letters in woerterbuch but you look for only lower case because of wort = wort.lower(). Try changing woerterbuch to :
woerterbuch = {"he" :" Er", "she" : "Sie", "it":"es", "hello":"Hallo", "how":"Wie", "are":"Geht", "you":"Dir"}
and then it will work correctly.
import cv2
import pathlib
mycascade = pathlib.Path(cv2.file).parent.absolute() / "data/haarcascade_frontalface_def"
clf = cv2.CascadeClassifier(str(mycascade))
cam = cv2.videoCapture(0)
with True:
_ , frame = cam.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = clf.detectMultiScele(
gray,
scaleFactor = 1.1,
minNeighbors = 3,
minsize = (30,30),
flogs = cv2.CASCADE_SCALE_IMAGE
)
for (x,y,width,height) in faces:
cv2.rectangle(frame, (x,y), [x + width, y + height], [(0,0,255)],4)
cv2.imshow(‘MrRobot’, frame)
if cv2.waitKey(1) == ord("q"):
break