Add html optional argument to lookup_term() and terms() - vt - Print definitions from Vocabolario Treccani
(HTM) hg clone https://bitbucket.org/iamleot/vt
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) changeset 642f8e359aa41a9142b2b01072c58ff7cf4c39c6
(DIR) parent 4fc9d09a80ee95782fa850729ee095a40afb5a3b
(HTM) Author: Leonardo Taccari <iamleot@gmail.com>
Date: Wed, 27 Mar 2019 00:36:33
Add html optional argument to lookup_term() and terms()
If it is True the definition will not be converted to text and the
bs4.element.Tag will be returned as-is.
Diffstat:
vt.py | 15 +++++++++++----
1 files changed, 11 insertions(+), 4 deletions(-)
---
diff -r 4fc9d09a80ee -r 642f8e359aa4 vt.py
--- a/vt.py Fri Mar 22 14:33:19 2019 +0100
+++ b/vt.py Wed Mar 27 00:36:33 2019 +0100
@@ -42,10 +42,11 @@
from bs4 import BeautifulSoup, SoupStrainer
-def lookup_term(term):
+def lookup_term(term, html=False):
"""Lookup a term in the Treccani's Vocabolario
Given a term try to search it in Vocabolario Treccani.
+ Accept an optional html boolean flag to avoid converting to text.
Returns the relevant text output of the term or None if nothing relevant
was found.
@@ -59,21 +60,27 @@
t = BeautifulSoup(r, 'html.parser',
parse_only=SoupStrainer(class_='text spiega'))
- return t.p.text.strip() if t.p else None
+ res = t.p if t.p else None
+ if res and not html:
+ res = res.text.strip()
+
+ return res
-def terms(term):
+def terms(term, html=False):
"""Return a list of terms in the Treccani's Vocabolario
Given a term return a list of zero or more definition regarding the
term.
+ Accept an optional html boolean flag to avoid converting to text.
+
>>> terms('birba')
['birba1 s. f. [etimo incerto]. – Persona priva di scrupoli, cattivo soggetto (sinon., quindi, del più com. birbante): Vostra Eccellenza, che ... mi gabella per anti-tedesco Perché metto le b. alla berlina (Giusti); credeva senza dubbio d’aver da fare con una b. matricolata (Pirandello). Più spesso, con tono scherz., bambino, ragazzino furbo, vispo, impertinente e malizioso: quella b. di mio figlio. Anticam. ebbe anche sign. astratto di malizia, frode, in varie locuz. furbesche. ◆ Dim. scherz. birbétta, birberèlla, birbacchiòlo m. e birbacchiòla f., pegg. birbàccia e più com. birbaccióne m. e birbaccióna f.; v. anche birbone, che è propr. accr. di birba ma è sentito ormai come voce a sé.', 'birba2 s. f. [etimo incerto], region. ant. – Carrozza scoperta a due posti, tirata da due cavalli: Signore, come gli dissi, io son venuto in birba ... E sono venuto con due cavalli (Goldoni).']
"""
ts = []
- t = lookup_term(term)
+ t = lookup_term(term, html)
if t:
ts.append(t)
else: