Reformatting main.py

This commit is contained in:
Darmstadtium 2023-01-04 21:19:27 +01:00
parent 220c2ec641
commit f74f65750e

150
main.py
View File

@ -7,10 +7,11 @@ Aurélien VALENTIN - from 06/11/2022 to 29/12/2022
************************""" ************************"""
# Default parameters, feel free to change them as you want. # Default parameters, feel free to change them as you want.
NB_SPECIES = 100 # Number of species wanted by the user. Can't exceed 100 with the GBIF method. NB_SPECIES = (
50 # Number of species wanted by the user. Can't exceed 100 with the GBIF method.
)
LANGUAGE = "fr" # Please choose "fr" or "en" to get respectively French or English vernacular names. LANGUAGE = "fr" # Please choose "fr" or "en" to get respectively French or English vernacular names.
@ -18,6 +19,8 @@ SPECIE_LIST_PATH_IF_GBIF_METHOD = "" # A csv file from https://identify.plantnet
MY_OWN_LIST_PATH = "" # A txt file with one specie by line. Be careful to write the scientific name used in Pl@ntNet and change NB_SPECIES if needed. MY_OWN_LIST_PATH = "" # A txt file with one specie by line. Be careful to write the scientific name used in Pl@ntNet and change NB_SPECIES if needed.
MAX_NB_OF_LINKS_BY_TYPE_AND_BY_SPECIES = 100 # The code will accumulate links of pictures of leaves, flower, fruits and barks of eache species.
# The most photos you want, the most diversified your learning will be, but the longer it will take to load...
# Importation and other initialisations # Importation and other initialisations
@ -26,16 +29,23 @@ import genanki #https://github.com/kerrickstaley/genanki.git
from random import shuffle from random import shuffle
dict_common_plants = {} dict_common_plants = {}
with open("_dict_species.json", "r") as fpi : dict_common_names = json.load(fpi) # From the 3-1 and 3-2 supplementary codes with open("_dict_species.json", "r") as fpi:
dict_common_names = json.load(fpi) # From the 3-1 and 3-2 supplementary codes
# Definition of the progressbar function, from https://gist.github.com/ChesterChowWOV/2b35c551b339adbf459363322aac5b4b # Definition of the progressbar function, from https://gist.github.com/ChesterChowWOV/2b35c551b339adbf459363322aac5b4b
def progressbar(it, prefix="", size=60, file=sys.stdout): def progressbar(it, prefix="", size=60, file=sys.stdout):
count = len(it) count = len(it)
def show(j): def show(j):
x = int(size * j / count) x = int(size * j / count)
file.write("{}[{}{}] {}/{} {}%\r".format(prefix, ""*x, "."*(size-x), j, count, round(j / count * 100, 2))) file.write(
"{}[{}{}] {}/{} {}%\r".format(
prefix, "" * x, "." * (size - x), j, count, round(j / count * 100, 2)
)
)
file.flush() file.flush()
show(0) show(0)
for i, item in enumerate(it): for i, item in enumerate(it):
yield item yield item
@ -51,7 +61,8 @@ Step 1 : Listing the most common species (if without a manual list or a GBIF fil
if SPECIE_LIST_PATH_IF_GBIF_METHOD == MY_OWN_LIST_PATH: if SPECIE_LIST_PATH_IF_GBIF_METHOD == MY_OWN_LIST_PATH:
print("Listing the most common species") print("Listing the most common species")
# Loading of the whole list # Loading of the whole list
with open("_dict_species_by_rarety.json") as fpi : dict_plants_sorted = json.load(fpi) # From the first supplementary code with open("_dict_species_by_rarety.json") as fpi:
dict_plants_sorted = json.load(fpi) # From the first supplementary code
# Writing of the user's list # Writing of the user's list
with open("Selected_species.txt", "w") as fpo: with open("Selected_species.txt", "w") as fpo:
@ -65,35 +76,69 @@ Step 2 : Parsing data for each species.
# Initialisation of the dictionary (and addition of the family) from a file from the step 1... # Initialisation of the dictionary (and addition of the family) from a file from the step 1...
if SPECIE_LIST_PATH_IF_GBIF_METHOD == "": if SPECIE_LIST_PATH_IF_GBIF_METHOD == "":
with open("_dict_classification.json", "r") as fpi : dict_classification = json.load(fpi) # From the second supplementary code with open("_dict_classification.json", "r") as fpi:
dict_classification = json.load(fpi) # From the second supplementary code
try: fpi = open(MY_OWN_LIST_PATH) try:
except : fpi = open("Selected_species.txt") fpi = open(MY_OWN_LIST_PATH)
except:
fpi = open("Selected_species.txt")
for line in progressbar(fpi.readlines(), "Adding families", 40): for line in progressbar(fpi.readlines(), "Adding families", 40):
specie = line.split(" ")[0] + " " + line.split(" ")[1].lower().strip() specie = line.split(" ")[0] + " " + line.split(" ")[1].lower().strip()
dict_common_plants[specie] = {"Occurrence": {"leaf": 0, "flower": 0, "fruit": 0, "bark": 0}} dict_common_plants[specie] = {
try: dict_common_plants[specie]["Family"] = dict_classification[specie] "Occurrence": {"leaf": 0, "flower": 0, "fruit": 0, "bark": 0}
except: dict_common_plants[specie]["Family"] = "Family_not_found" }
dict_common_plants[specie]["Links"] = {"leaf": "", "flower": "", "fruit": "", "bark": ""} try:
dict_common_plants[specie]["Authors"] = {"leaf": "", "flower": "", "fruit": "", "bark": ""} dict_common_plants[specie]["Family"] = dict_classification[specie]
except:
dict_common_plants[specie]["Family"] = "Family not found"
dict_common_plants[specie]["Links"] = {
"leaf": "",
"flower": "",
"fruit": "",
"bark": "",
}
dict_common_plants[specie]["Authors"] = {
"leaf": "",
"flower": "",
"fruit": "",
"bark": "",
}
# ...or from a GBIF file from here : https://identify.plantnet.org/prediction # ...or from a GBIF file from here : https://identify.plantnet.org/prediction
else: else:
with open(SPECIE_LIST_PATH_IF_GBIF_METHOD) as fpi: with open(SPECIE_LIST_PATH_IF_GBIF_METHOD) as fpi:
fpi.readline() fpi.readline()
for line in progressbar(fpi.readlines(), "Adding families", 40): for line in progressbar(fpi.readlines(), "Adding families", 40):
specie = line.split(",")[1].split(" ")[0] + " " + line.split(",")[1].split(" ")[1] specie = (
dict_common_plants[specie] = {"Occurrence": {"leaf": 0, "flower": 0, "fruit": 0, "bark": 0}} line.split(",")[1].split(" ")[0]
+ " "
+ line.split(",")[1].split(" ")[1]
)
dict_common_plants[specie] = {
"Occurrence": {"leaf": 0, "flower": 0, "fruit": 0, "bark": 0}
}
dict_common_plants[specie]["Family"] = line.split(",")[2] dict_common_plants[specie]["Family"] = line.split(",")[2]
dict_common_plants[specie]["Links"] = {"leaf": "", "flower": "", "fruit": "", "bark": ""} dict_common_plants[specie]["Links"] = {
dict_common_plants[specie]["Authors"] = {"leaf": "", "flower": "", "fruit": "", "bark": ""} "leaf": "",
"flower": "",
"fruit": "",
"bark": "",
}
dict_common_plants[specie]["Authors"] = {
"leaf": "",
"flower": "",
"fruit": "",
"bark": "",
}
# Addition of vernacular name(s) # Addition of vernacular name(s)
for specie in progressbar(dict_common_plants.keys(), "Adding common names", 40): for specie in progressbar(dict_common_plants.keys(), "Adding common names", 40):
try: try:
dict_common_plants[specie]["Common names"] = dict_common_names[specie][LANGUAGE] dict_common_plants[specie]["Common names"] = dict_common_names[specie][LANGUAGE]
except: dict_common_plants[specie]["Common names"] = "No common name found." except:
dict_common_plants[specie]["Common names"] = "No common name found."
# Addition of photo links and author names # Addition of photo links and author names
with open("multimedia.txt", encoding="utf-8") as fpi: with open("multimedia.txt", encoding="utf-8") as fpi:
@ -104,12 +149,20 @@ with open("multimedia.txt", encoding="utf-8") as fpi:
link = line_split[3].strip(" ").split("o/")[1] link = line_split[3].strip(" ").split("o/")[1]
type = line_split[5].split(":")[1].strip(" ") type = line_split[5].split(":")[1].strip(" ")
author = line_split[len(line_split) - 1].strip() author = line_split[len(line_split) - 1].strip()
if specie in dict_common_plants.keys() and type in ["leaf", "flower", "fruit", "bark"]: if specie in dict_common_plants.keys() and type in [
"leaf",
"flower",
"fruit",
"bark",
]:
if dict_common_plants[specie]["Occurrence"][type] == 0: if dict_common_plants[specie]["Occurrence"][type] == 0:
dict_common_plants[specie]["Links"][type] += link dict_common_plants[specie]["Links"][type] += link
dict_common_plants[specie]["Authors"][type] += author dict_common_plants[specie]["Authors"][type] += author
dict_common_plants[specie]["Occurrence"][type] += 1 dict_common_plants[specie]["Occurrence"][type] += 1
elif dict_common_plants[specie]["Occurrence"][type] < 101: elif (
dict_common_plants[specie]["Occurrence"][type]
< MAX_NB_OF_LINKS_BY_TYPE_AND_BY_SPECIES + 1
):
dict_common_plants[specie]["Links"][type] += "," + link dict_common_plants[specie]["Links"][type] += "," + link
dict_common_plants[specie]["Authors"][type] += "," + author dict_common_plants[specie]["Authors"][type] += "," + author
dict_common_plants[specie]["Occurrence"][type] += 1 dict_common_plants[specie]["Occurrence"][type] += 1
@ -123,24 +176,24 @@ print("Preparing the Anki deck")
# Definition of the Anki card model # Definition of the Anki card model
my_model = genanki.Model( my_model = genanki.Model(
1091735104, 1091735104,
'Model for AnkIdentification', "Model for AnkIdentification",
fields=[ fields=[
{'name': 'Name'}, {"name": "Name"},
{'name': 'Common names'}, {"name": "Common names"},
{'name': 'Family'}, {"name": "Family"},
{'name': 'Leaf'}, {"name": "Leaf"},
{'name': 'Flower'}, {"name": "Flower"},
{'name': 'Fruit'}, {"name": "Fruit"},
{'name': 'Bark'}, {"name": "Bark"},
{'name': 'Leaf authors'}, {"name": "Leaf authors"},
{'name': 'Flower authors'}, {"name": "Flower authors"},
{'name': 'Fruit authors'}, {"name": "Fruit authors"},
{'name': 'Bark authors'} {"name": "Bark authors"},
], ],
templates=[ templates=[
{ {
'name': 'Card 1', "name": "Card 1",
'qfmt': """<style> "qfmt": """<style>
.card { .card {
font-family: arial; font-family: arial;
font-size: 20px; font-size: 20px;
@ -316,7 +369,7 @@ prepImg("{{Flower}}","{{Flower authors}}",'Flower');
prepImg("{{Fruit}}","{{Fruit authors}}",'Fruit'); prepImg("{{Fruit}}","{{Fruit authors}}",'Fruit');
prepImg("{{Bark}}","{{Bark authors}}",'Bark'); prepImg("{{Bark}}","{{Bark authors}}",'Bark');
</script>""", </script>""",
'afmt': """<div class="half"> "afmt": """<div class="half">
<h1><i>{{Name}}</i></h1> <h1><i>{{Name}}</i></h1>
<br> <br>
<b><i>{{Family}}</i></b> <b><i>{{Family}}</i></b>
@ -324,17 +377,15 @@ prepImg("{{Bark}}","{{Bark authors}}",'Bark');
{{Common names}} {{Common names}}
</div> </div>
{{FrontSide}}""", {{FrontSide}}""",
'bfont': 'Arial' "bfont": "Arial"
#'Styles': ".card {\n font-family: arial;\n font-size: 20px;\n text-align: center;\n color: black;\n background-color: white;\n}" #'Styles': ".card {\n font-family: arial;\n font-size: 20px;\n text-align: center;\n color: black;\n background-color: white;\n}"
}, },
], ],
css='.card {\n font-family: arial;\n font-size: 20px;\n text-align: center;\n color: black;\n background-color: white;\n}\n', css=".card {\n font-family: arial;\n font-size: 20px;\n text-align: center;\n color: black;\n background-color: white;\n}\n",
) )
# Creation of the deck # Creation of the deck
my_deck = genanki.Deck( my_deck = genanki.Deck(2059400110, "AnkIdentifaction")
2059400110,
'AnkIdentifaction')
# Randomization of the species order # Randomization of the species order
species_shuffled = list(dict_common_plants.keys()) species_shuffled = list(dict_common_plants.keys())
@ -345,11 +396,24 @@ for specie in progressbar(species_shuffled, "Creating notes", 40):
dict_specie = dict_common_plants[specie] dict_specie = dict_common_plants[specie]
my_note = genanki.Note( my_note = genanki.Note(
model=my_model, model=my_model,
fields = [specie, dict_specie["Common names"], dict_specie["Family"], dict_specie["Links"]["leaf"], dict_specie["Links"]["flower"], dict_specie["Links"]["fruit"], dict_specie["Links"]["bark"], dict_specie["Authors"]["leaf"], dict_specie["Authors"]["flower"], dict_specie["Authors"]["fruit"], dict_specie["Authors"]["bark"]], fields=[
tags = ["AnkIdentification::" + dict_specie["Family"]]) specie,
dict_specie["Common names"],
dict_specie["Family"],
dict_specie["Links"]["leaf"],
dict_specie["Links"]["flower"],
dict_specie["Links"]["fruit"],
dict_specie["Links"]["bark"],
dict_specie["Authors"]["leaf"],
dict_specie["Authors"]["flower"],
dict_specie["Authors"]["fruit"],
dict_specie["Authors"]["bark"],
],
tags=["AnkIdentification::" + dict_specie["Family"]],
)
my_deck.add_note(my_note) my_deck.add_note(my_note)
# Creation of the final file # Creation of the final file
print("Almost done!") print("Almost done!")
genanki.Package(my_deck).write_to_file('AnkIdentification.apkg') genanki.Package(my_deck).write_to_file("AnkIdentification.apkg")
print("Enjoy ^^") print("Enjoy ^^")