Ursprünglich zweimal vor dem vorherigen Artikel [Python] Lassen Sie uns automatisch englische PDF-Dateien (ohne darauf beschränkt zu sein) mit DeepL- oder Google-Übersetzung übersetzen, um daraus eine Textdatei zu machen. Fortsetzung [Python] Lassen Sie uns automatisch englische PDF-Dateien (ohne darauf beschränkt zu sein) mit DeepL- oder Google-Übersetzung in eine Textdatei ohne HTML übersetzen.
Ich habe es für die Verwendung in geschrieben, aber es scheint nützlich zu sein, daher werde ich es separat vorstellen.
Ich habe keine detaillierten Kenntnisse über PDF, Es scheint, dass der Text in kleine Teile unterteilt und in das PDF geschrieben ist, und der kopierte Text enthält auch den Zeilenvorschubcode an der Position, die im PDF angezeigt wird.
Zum Beispiel in PDF
Bei Anzeige wie ist der kopierte Text
Und so weiter. (Das obige Beispiel ist für Windows)
Dann dachte ich, ich sollte den Zeilenvorschubcode löschen und die Sätze verbinden.
Auf diese Weise gibt es in diesem Beispiel einen Punkt, damit nicht jeder Satz gemischt wird.
Was ist mit den folgenden Fällen?
Wenn Sie den Zeilenvorschubcode einfach löschen,
Es wurde unmöglich, ohne einen Punkt zwischen der ersten und der zweiten Zeile zu unterscheiden.
Das Problem ist also ** Teile, die nicht immer Bruchmarken aufweisen, wie z. B. Punkte, z. B. Überschriften, Ableiten und Zerlegen aus dem kopierten Text, der nur Hinweise für Sätze und Zeilenvorschubcodes enthält. ** ** ** Das ist der Punkt.
Wir haben eine Methode wie. Es ist ziemlich einfach, aber die meisten Sätze
・ Überschrift · Absatz ・ Satz
Sie haben jetzt eine Funktion, die in jede der Einheiten aufgeteilt werden kann.
import re
import unicodedata
def len_(text):
cnt = 0
for t in text:
if unicodedata.east_asian_width(t) in "FWA":
cnt += 2
else:
cnt += 1
return cnt
def textParser(text, n=30, bracketDetect=True):
text = text.splitlines()
sentences = []
t = ""
bra_cnt = ket_cnt = bra_cnt_jp = ket_cnt_jp = 0
for i in range(len(text)):
if not bool(re.search("\S", text[i])): continue
if bracketDetect:
bra_cnt += len(re.findall("[\((]", text[i]))
ket_cnt += len(re.findall("[\))]", text[i]))
bra_cnt_jp += len(re.findall("[""]", text[i]))
ket_cnt_jp += len(re.findall("["" "]", text[i]))
if i != len(text) - 1:
if bool(re.fullmatch(r"[A-Z\s]+", text[i])):
if t != "": sentences.append(t)
t = ""
sentences.append(text[i])
elif bool(
re.match(
"(\d{1,2}[\.,、.]\s?(\d{1,2}[\.,、.]*)*\s?|I{1,3}V{0,1}X{0,1}[\.,、.]|V{0,1}X{0,1}I{1,3}[\.,、.]|[・ • ●])+\s",
text[i])) or re.match("\d{1,2}.\w", text[i]) or (
bool(re.match("[A-Z]", text[i][0]))
and abs(len_(text[i]) - len_(text[i + 1])) > n
and len_(text[i]) < n):
if t != "": sentences.append(t)
t = ""
sentences.append(text[i])
elif (
text[i][-1] not in ("。", ".", ".") and
(abs(len_(text[i]) - len_(text[i + 1])) < n or
(len_(t + text[i]) > len_(text[i + 1]) and bool(
re.search("[。\..]\s\d|..[。\..]|.[。\..]", text[i + 1][-3:])
or bool(re.match("[A-Z]", text[i + 1][:1]))))
or bool(re.match("\s?[a-z,\)]", text[i + 1]))
or bra_cnt > ket_cnt or bra_cnt_jp > ket_cnt_jp)):
t += text[i]
else:
sentences.append(t + text[i])
t = ""
else:
sentences.append(t + text[i])
return sentences
Wenn das Ergebnis nicht gut ist, versuchen Sie, den Wert von "n" anzupassen (größer ist mehr zusammen, kleiner ist unzusammenhängender). Wenn aus irgendeinem Grund die Anzahl der Klammern falsch ausgerichtet ist und der Text seltsamerweise eingefroren ist, setzen Sie "bracketDetect" auf "False".
Python 3.8.5 Documentation PDF (US-Letter-Papiergröße) \ tutorial.pdf Ab Seite 5 (S.11)
Eine Kopie des Originals
CHAPTER
TWO
USING THE PYTHON INTERPRETER
2.1 Invoking the Interpreter
The Python interpreter is usually installed as /usr/local/bin/python3.8 on those machines where it is available;
putting /usr/local/bin in your Unix shell’s search path makes it possible to start it by typing the command:
python3.8
to the shell.1 Since the choice of the directory where the interpreter lives is an installation option, other places are possible;
check with your local Python guru or system administrator. (E.g., /usr/local/python is a popular alternative
location.)
On Windows machines where you have installed Python from the Microsoft Store, the python3.8 command will be
available. If you have the py.exe launcher installed, you can use the py command. See setting-envvars for other ways to
launch Python.
Typing an end-of-file character (Control-D on Unix, Control-Z on Windows) at the primary prompt causes the
interpreter to exit with a zero exit status. If that doesn’t work, you can exit the interpreter by typing the following command:
quit().
The interpreter’s line-editing features include interactive editing, history substitution and code completion on systems that
support the GNU Readline library. Perhaps the quickest check to see whether command line editing is supported is typing
Control-P to the first Python prompt you get. If it beeps, you have command line editing; see Appendix Interactive
Input Editing and History Substitution for an introduction to the keys. If nothing appears to happen, or if ^P is echoed,
command line editing isn’t available; you’ll only be able to use backspace to remove characters from the current line.
The interpreter operates somewhat like the Unix shell: when called with standard input connected to a tty device, it reads
and executes commands interactively; when called with a file name argument or with a file as standard input, it reads and
executes a script from that file.
A second way of starting the interpreter is python -c command [arg] ..., which executes the statement(s) in
command, analogous to the shell’s -c option. Since Python statements often contain spaces or other characters that are
special to the shell, it is usually advised to quote command in its entirety with single quotes.
Some Python modules are also useful as scripts. These can be invoked using python -m module [arg] ...,
which executes the source file for module as if you had spelled out its full name on the command line.
When a script file is used, it is sometimes useful to be able to run the script and enter interactive mode afterwards. This
can be done by passing -i before the script.
All command line options are described in using-on-general.
1 On Unix, the Python 3.x interpreter is by default not installed with the executable named python, so that it does not conflict with a simultaneously
installed Python 2.x executable.
Am Ende jeder Zeile befindet sich ein Zeilenvorschubcode. Wenn Sie zum leichteren Verständnis "als Zeilenvorschubcode" anzeigen
CHAPTER\r\nTWO\r\nUSING THE PYTHON INTERPRETER\r\n2.1 Invoking the Interpreter\r\nThe Python interpreter is usually installed as /usr/local/bin/python3.8 on those machines where it is available;\r\nputting /usr/local/bin in your Unix shell’s search path makes it possible to start it by typing the command:\r\npython3.8\r\nto the shell.1 Since the choice of the directory where the interpreter lives is an installation option, other places are possible;\r\ncheck with your local Python guru or system administrator. (E.g., /usr/local/python is a popular alternative\r\nlocation.)\r\nOn Windows machines where you have installed Python from the Microsoft Store, the python3.8 command will be\r\navailable. If you have the py.exe launcher installed, you can use the py command. See setting-envvars for other ways to\r\nlaunch Python.\r\nTyping an end-of-file character (Control-D on Unix, Control-Z on Windows) at the primary prompt causes the\r\ninterpreter to exit with a zero exit status. If that doesn’t work, you can exit the interpreter by typing the following command:\r\nquit().\r\nThe interpreter’s line-editing features include interactive editing, history substitution and code completion on systems that\r\nsupport the GNU Readline library. Perhaps the quickest check to see whether command line editing is supported is typing\r\nControl-P to the first Python prompt you get. If it beeps, you have command line editing; see Appendix Interactive\r\nInput Editing and History Substitution for an introduction to the keys. If nothing appears to happen, or if ^P is echoed,\r\ncommand line editing isn’t available; you’ll only be able to use backspace to remove characters from the current line.\r\nThe interpreter operates somewhat like the Unix shell: when called with standard input connected to a tty device, it reads\r\nand executes commands interactively; when called with a file name argument or with a file as standard input, it reads and\r\nexecutes a script from that file.\r\nA second way of starting the interpreter is python -c command [arg] ..., which executes the statement(s) in\r\ncommand, analogous to the shell’s -c option. Since Python statements often contain spaces or other characters that are\r\nspecial to the shell, it is usually advised to quote command in its entirety with single quotes.\r\nSome Python modules are also useful as scripts. These can be invoked using python -m module [arg] ...,\r\nwhich executes the source file for module as if you had spelled out its full name on the command line.\r\nWhen a script file is used, it is sometimes useful to be able to run the script and enter interactive mode afterwards. This\r\ncan be done by passing -i before the script.\r\nAll command line options are described in using-on-general.\r\n1 On Unix, the Python 3.x interpreter is by default not installed with the executable named python, so that it does not conflict with a simultaneously\r\ninstalled Python 2.x executable.
Es sieht aus wie das.
Ich werde es in die Funktion werfen, die ich dieses Mal gemacht habe. Angenommen, der obige Satz wird in die Zwischenablage kopiert
from pyperclip import paste #Funktion zum Abrufen des Werts (Text) aus der Zwischenablage
print("\n".join(textParser(paste())))
out
CHAPTER
TWO
USING THE PYTHON INTERPRETER
2.1 Invoking the Interpreter
The Python interpreter is usually installed as /usr/local/bin/python3.8 on those machines where it is available;putting /usr/local/bin in your Unix shell’s search path makes it possible to start it by typing the command:python3.8to the shell.1 Since the choice of the directory where the interpreter lives is an installation option, other places are possible;check with your local Python guru or system administrator. (E.g., /usr/local/python is a popular alternativelocation.)On Windows machines where you have installed Python from the Microsoft Store, the python3.8 command will beavailable. If you have the py.exe launcher installed, you can use the py command. See setting-envvars for other ways tolaunch Python.
Typing an end-of-file character (Control-D on Unix, Control-Z on Windows) at the primary prompt causes theinterpreter to exit with a zero exit status. If that doesn’t work, you can exit the interpreter by typing the following command:quit().
The interpreter’s line-editing features include interactive editing, history substitution and code completion on systems thatsupport the GNU Readline library. Perhaps the quickest check to see whether command line editing is supported is typingControl-P to the first Python prompt you get. If it beeps, you have command line editing; see Appendix InteractiveInput Editing and History Substitution for an introduction to the keys. If nothing appears to happen, or if ^P is echoed,command line editing isn’t available; you’ll only be able to use backspace to remove characters from the current line.
The interpreter operates somewhat like the Unix shell: when called with standard input connected to a tty device, it readsand executes commands interactively; when called with a file name argument or with a file as standard input, it reads andexecutes a script from that file.
A second way of starting the interpreter is python -c command [arg] ..., which executes the statement(s) incommand, analogous to the shell’s -c option. Since Python statements often contain spaces or other characters that arespecial to the shell, it is usually advised to quote command in its entirety with single quotes.
Some Python modules are also useful as scripts. These can be invoked using python -m module [arg] ...,which executes the source file for module as if you had spelled out its full name on the command line.
When a script file is used, it is sometimes useful to be able to run the script and enter interactive mode afterwards. Thiscan be done by passing -i before the script.
installed Python 2.x executable.
Pretty good!
Das heißt, es ist natürlich, ein so schönes PDF sauber teilen zu können. Sie können es auch für kompliziertere PDFs verwenden, die zwischen 1 und 2 Spalten wechseln. Probieren Sie es also aus.
Es ist keineswegs perfekt für jedes PDF, aber es ist anständig. Japanisches PDF wird ebenfalls unterstützt, aber japanisches PDF wird häufig von OCR beeinflusst. In diesem Fall können Sie den Speicherplatz mit ".replace (" "," ")" löschen Ich denke, es wird sauber sein (obwohl diese Methode nicht verwendet werden kann, wenn englischer Text enthalten ist). Wie oben erwähnt, gibt es viele Verwendungsmöglichkeiten, wenn Sie ein PDF übersetzen möchten. Verwenden Sie es daher bitte.
Recommended Posts