Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
pengowray committed Jul 21, 2016
1 parent 2668724 commit 09f1a62
Show file tree
Hide file tree
Showing 13 changed files with 428 additions and 123 deletions.
5 changes: 5 additions & 0 deletions IUCN/Blurbs/BlurbBeforeSplit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public static string Text(TaxonNode node, RedStatus status, int depth, bool incl
return null;
}

if (status == RedStatus.EXplus) {
// until can get some better wording, just skip it.
return null;
}

//Dictionary<string, string> codes_en = new Dictionary<string, string>();
//codes_en["CR"] = "critically endangered";

Expand Down
2 changes: 1 addition & 1 deletion IUCN/Blurbs/TaxoSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Threading.Tasks;

namespace beastie {
class TaxoSection {
public class TaxoSection {
public string title; // e.g. "Species", "Subspecies" or "Possibly extinct varieties"
public IEnumerable<IUCNBitri> list;
public bool isDefault = false; // default sections don't need a title if they're the only section (e.g. "Species")
Expand Down
69 changes: 56 additions & 13 deletions IUCN/Blurbs/TaxonHeaderBlurb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public string VernacularStringLower() {


public static string ArticleBlurb(TaxonNode node, RedStatus status) {

if (status == RedStatus.EXplus)
return ExPlusBlurb(node, status);

StringBuilder blurb = new StringBuilder();
var cr_stats = node.GetStats(status);
var all_stats = node.GetStats();
Expand Down Expand Up @@ -145,12 +149,15 @@ public static string ArticleBlurb(TaxonNode node, RedStatus status) {
string alsoSubs = AlsoSubsp(node, status, showAlso);

if (!string.IsNullOrEmpty(alsoSubs)) {
blurb.AppendLine(alsoSubs);
blurb.AppendLine(alsoSubs.TrimEnd());
blurb.AppendLine();
}

blurb.AppendLine(Subpops(node, status));
blurb.AppendLine();
string subpopsLine = Subpops(node, status);
if (subpopsLine != null) {
blurb.AppendLine(Subpops(node, status).TrimEnd());
blurb.AppendLine();
}

string threatenedContextBlurb = ThreatenedContextBlurb(node, status);
if (!string.IsNullOrEmpty(threatenedContextBlurb)) {
Expand All @@ -160,7 +167,7 @@ public static string ArticleBlurb(TaxonNode node, RedStatus status) {

string ddinfo = DDInfo(node, status);
if (!string.IsNullOrEmpty(ddinfo)) {
blurb.Append(ddinfo);
blurb.Append(ddinfo.Trim());
blurb.AppendLine();
blurb.AppendLine();
}
Expand All @@ -176,6 +183,16 @@ public static string ArticleBlurb(TaxonNode node, RedStatus status) {

}

private static string ExPlusBlurb(TaxonNode node, RedStatus status) {
//throw new NotImplementedException();
return string.Format("As of {0}, the [[International Union for Conservation of Nature]] (IUCN) lists {1}.{2}{3}\n\n",
FileConfig.Instance().iucnRedListFileDate, // {0} date
node.GetSections(status).ToNewspaperQualtities(),
FileConfig.Instance().iucnRedListFileRef,
FileConfig.Instance().iucnPossiblyExtinctFileRef
);
}

static string AlsoSubsp(TaxonNode node, RedStatus status, bool showAlso) {
// The IUCN also lists 59 mammalian subspecies as critically endangered.
// TODO: work in the word "globally" ?
Expand All @@ -189,15 +206,31 @@ static string AlsoSubsp(TaxonNode node, RedStatus status, bool showAlso) {
return string.Empty;

if (status == RedStatus.Null) {
//TODO: varieties
return "The IUCN " + (showAlso ? "also " : "") + "has evaluated " + cr_subsp.NewspaperNumber() + " " + node.nodeName.Adjectivize(false, false, "subspecies", "within") + ". ";

} else {
return "The IUCN " + (showAlso ? "also " : "") + "lists " + cr_subsp.NewspaperNumber() + " " + node.nodeName.Adjectivize(false, false, "subspecies", "within") + " as " + status.Text() + ". ";
if (cr_stats.subspecies_varieties > 0) {
var items = new List<Tuple<int, string>>() {
{ cr_stats.subspecies_actual_subsp, "subspecies" },
{ cr_stats.subspecies_varieties, "varieties" }
};

//string thingsTheyList = (cr_stats.subspecies_actual_subsp + " subspecies, " + cr_stats.subspecies_varieties + " varieties").ToNewspaperQualtities();
string thingsTheyList = items.ToNewspaperQualtities();
return "The IUCN " + (showAlso ? "also " : "") + "lists " + thingsTheyList + " as " + status.Text() + ". ";

} else {
return "The IUCN " + (showAlso ? "also " : "") + "lists " + cr_subsp.NewspaperNumber() + " " + node.nodeName.Adjectivize(false, false, "subspecies", "within") + " as " + status.Text() + ". ";
}
}
}

static string Subpops(TaxonNode node, RedStatus status) {
// Of the mammalian subpopulations evaluated, 17 species subpopulations and 1 subspecies subpopulation have been assessed as critically endangered.
//if (cr_subsp > 0)
if (status == RedStatus.EXplus)
return null; // don't mention the small number of local extinctions tracked as subpopulations

var cr_stats = node.GetStats(status);

Expand Down Expand Up @@ -313,32 +346,42 @@ public static string LastParagraphNotes(TaxonNode node, RedStatus status) {
thisIsaListOf = "This is a list of all ";
//exceptions = ", except for blah and blah which are listed separately.";
}


var stats = node.GetStats(status);
bool isPlants = node.IsOrParentIs("Plantae");

StringBuilder note = new StringBuilder();

string whats = "species and subspecies";
if (stats.subspecies == 0) {
whats = "species";
} else if (isPlants && stats.subspecies_varieties > 0) {
whats = "species, subspecies and varieties";
}

string listOfWhat;
if (status == RedStatus.Null) {
listOfWhat = node.nodeName.Adjectivize(false, false, "species and subspecies", "in");
listOfWhat = node.nodeName.Adjectivize(false, false, whats, "in");
} else {
listOfWhat = status.Text() + " " + node.nodeName.Adjectivize(false, false, "species and subspecies", "in");
listOfWhat = status.Text() + " " + node.nodeName.Adjectivize(false, false, whats, "in");
}
note.Append(thisIsaListOf + listOfWhat + " as evaluated by the IUCN. "); // "evaluated" or "as evaluated"?
note.Append(thisIsaListOf + listOfWhat + " evaluated by the IUCN. "); // "evaluated" or "as evaluated"?

if (status == RedStatus.CR) {
note.Append("Species considered possibly extinct by the IUCN are marked as such. ");
}

if (status != RedStatus.Null && status != RedStatus.EXplus && status != RedStatus.EX
&& node.GetStats(status).subpops_total > 0) {
note.Append("Species or subspecies which have " + status.Text() + " subpopulations (or stocks) are indicated. ");
&& stats.subpops_total > 0) {
note.Append(whats.UpperCaseFirstChar() + " which have " + status.Text() + " subpopulations (or stocks) are indicated. ");
}

if (style == PrettyStyle.JustNames) {
//note.Append("Common names for taxa are displayed where possible. Links generally point to the scientific name used by the IUCN. ");
note.Append("Where possible common names for taxa are given while links point to the scientific name used by the IUCN.");
note.Append("Where possible common names for taxa are given while links point to the scientific name used by the IUCN. ");
}

return note.ToString();
return note.ToString().TrimEnd();
}
}

Expand Down
85 changes: 64 additions & 21 deletions IUCN/RedlistCSV.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
using System.IO;
using LumenWorks.Framework.IO.Csv;

using Humanizer.Inflections;
using Humanizer;

namespace beastie {
public class RedlistCSV
{
TaxonNode topNode;
//TaxonNode topNode;
Dictionary<string,string> possiblyExtinct; // lowercase keys for easy matching
int count = 0;

Expand Down Expand Up @@ -160,7 +163,7 @@ public void TallyThreatenedEpithets() {
}


public void ReadCSV() {
public TaxonNode ReadCSV(bool useRules) {

Console.WriteLine("Reading csv...");

Expand All @@ -177,17 +180,21 @@ public void ReadCSV() {

//var rules = new TaxonDisplayRules();
//rules.Compile();
var rules = TaxaRuleList.Instance();


//List<IUCNTaxonLadder> detailList = new List<IUCNTaxonLadder>();

TaxonNode topNode;

topNode = new TaxonNode();
topNode.ruleList = rules;
if (useRules) {
var rules = TaxaRuleList.Instance();
topNode.ruleList = rules;
}
topNode.name = "top";
topNode.rank = "top";
count = 0;


foreach (var details in RedListTaxons()) {

topNode.Add(details);
Expand All @@ -198,15 +205,25 @@ public void ReadCSV() {
count++;
}

Console.WriteLine("Done reading csv.");
Console.WriteLine("Done reading csv. Items: {0}", count);

return topNode;
}

public void CSVContents(int levels = 3) {
// list the top taxa in the red list CSV
//ReadCSV();

}

public void ListChildNodes(string taxon) {

public void ListChildNodes(string taxon = "top", bool useRules = false, int levels = 2) {

TaxonNode topNode = ReadCSV(useRules);

//TODO: sort option, e.g. sort by RLI
//TODO: optionally show common names

//TODO: with and without rules applies

var node = topNode.FindNode(taxon);

Console.WriteLine("//{0}:", node.name);
Expand All @@ -215,7 +232,11 @@ public void ListChildNodes(string taxon) {
}
}

public void OutputReports() {
public void OutputReports() {
//HumanizerTest();

TaxonNode topNode = ReadCSV(true);

// invertebrates:
// technically: Animalia excluding Vertebrata
// Using IUCN taxa: Animalia excluding Chordata
Expand Down Expand Up @@ -288,11 +309,11 @@ public void OutputReports() {
//CreateList("Arthropoda", RedStatus.LC);

CreateList(topNode, RedStatus.CD); // those odd remaining LR/cd species
CreateList("Animalia", RedStatus.CD);
CreateList("Plantae", RedStatus.CD);
CreateList(topNode, "Animalia", RedStatus.CD);
CreateList(topNode, "Plantae", RedStatus.CD);

CreateList("Animalia", RedStatus.EW);
CreateList("Plantae", RedStatus.EW);
CreateList(topNode, "Animalia", RedStatus.EW);
CreateList(topNode, "Plantae", RedStatus.EW);

//CreateList("Mammalia", RedStatus.EX);
//CreateList("Mammalia", RedStatus.PE);
Expand Down Expand Up @@ -326,8 +347,8 @@ public void OutputReports() {
}


void CreateLists(string group) {
TaxonNode subNode = topNode.FindNode(group);
void CreateLists(TaxonNode topSearchNode, string group) {
TaxonNode subNode = topSearchNode.FindNode(group);

if (subNode == null) {
Console.WriteLine("CreateLists: Failed to find subnode for group: " + group);
Expand Down Expand Up @@ -360,11 +381,10 @@ void CreateLists(TaxonNode subNode) {
foreach (RedStatus status in statusLists) {
CreateList(subNode, status);
}

}

void CreateChart(string group) {
TaxonNode subNode = topNode.FindNode(group);
void CreateChart(TaxonNode topSearchNode, string group) {
TaxonNode subNode = topSearchNode.FindNode(group);
if (subNode == null) {
Console.WriteLine("Failed to find subnode for category: " + group);
} else {
Expand Down Expand Up @@ -392,9 +412,9 @@ void CreateChart(TaxonNode subNode) {
}


void CreateList(string group, RedStatus status = RedStatus.Null) {
void CreateList(TaxonNode topSearchNode, string group, RedStatus status = RedStatus.Null) {
//var subNode = topNode.FindChildDeep("Animalia");
TaxonNode subNode = topNode.FindNode(group);
TaxonNode subNode = topSearchNode.FindNode(group);

//var subNode = topNode.FindChildDeep("CHIROPTERA"); // works
//var subNode = topNode.FindChildDeep("Fish");
Expand Down Expand Up @@ -427,6 +447,29 @@ void CreateList(TaxonNode subNode, RedStatus status = RedStatus.Null) {
Console.WriteLine("Items in list: " + subNode.StatsSummary(status));
}

void HumanizerTest() {
Console.WriteLine("variety".ToQuantity(1));
Console.WriteLine("variety".ToQuantity(2));
Console.WriteLine("varieties".ToQuantity(1));
Console.WriteLine("varieties".ToQuantity(2));
Console.WriteLine("species".ToQuantity(1));
Console.WriteLine("species".ToQuantity(2));
Console.WriteLine("subspecies".ToQuantity(1));
Console.WriteLine("subspecies".ToQuantity(2));

//Vocabularies.Default.AddIrregular("subspecies", "subspecies"); // pluralizes to "subspecy" by default
//Vocabularies.Default.AddSingular("(vert|ind)ices$", "$1ex");
//Vocabularies.Default.AddUncountable("subspecies");
//Vocabularies.Default.AddIrregular("species", "species");
//Vocabularies.Default.AddIrregular("person", "people", matchEnding: false);

Vocabularies.Default.AddIrregular("species", "species"); // fixes "subspecies"

Console.WriteLine(new string[] { "species".ToQuantity(1), "variety".ToQuantity(2), "subspecies".ToQuantity(1) }.Humanize());
Console.WriteLine(new string[] { "species".ToQuantity(1, ShowQuantityAs.Words), "variety".ToQuantity(2, ShowQuantityAs.Words), "subspecies".ToQuantity(1, ShowQuantityAs.Words) }.Humanize());
//Console.WriteLine(new string[] { "3 variety", "1 species" }.Humanize(separator: ","));
}


}
}
Expand Down
10 changes: 9 additions & 1 deletion IUCN/TaxaRuleList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ Piciformes means woodpecker-like
Lepilemuridae below Lemuroidea : superfamily // Sportive lemur
Megaladapidae below Lemuroidea : superfamily // extinct
Palaeopropithecidae below Lemuroidea : superfamily //extinct
Lemuroidea = lemur
Lemuroidea = lemur ! lemurs
// marsupials
Didelphimorphia below Marsupialia : infraclass
Expand Down Expand Up @@ -922,6 +922,14 @@ Columbidae plural pigeons and doves
//mollusks, Mollusca
Stylommatophora includes the majority of land snails and slugs
//plants
Bryophyta wikilink Moss // IUCN uses the newer definition of Bryophyta, with hornworts and liverworts listed separately
Bryophyta = moss ! mosses
Marchantiophyta = liverwort ! liverworts
Anthoceratophyta = hornwort ! hornworts // iucn spelling
Anthocerotophyta = hornwort ! hornworts // more common spelling
Shorea guiso = red balau // listed as 'Dfgadfg'
";

private static TaxaRuleList _instance;
Expand Down
Loading

0 comments on commit 09f1a62

Please sign in to comment.