Skip to content

Commit

Permalink
half way implementing quotations
Browse files Browse the repository at this point in the history
  • Loading branch information
lavedon committed Apr 17, 2021
1 parent 6799748 commit 3ef9587
Show file tree
Hide file tree
Showing 55 changed files with 603 additions and 33,143 deletions.
50 changes: 48 additions & 2 deletions API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public static void APICalls(CurrentQuery query)
var client = new HttpClient();
string baseURL = "https://oed-researcher-api.oxfordlanguages.com/oed/api/v0.2/";

// @TODO make this one Action delegate - with a method that parses the query and responses
Action<object> callWordsAPI = (Object obj) =>
{
Trace.WriteLine("Called callWordsAPI");
Expand All @@ -77,17 +78,23 @@ public static void APICalls(CurrentQuery query)
Action<object> callSensesAPI = (Object obj) =>
{
Trace.WriteLine("Called callSensesAPI");
Uri requestURL = new Uri(baseURL + @"monitor_nn01/senses/");
Uri requestURL = new Uri(baseURL + query.WordID + "/senses/");
Trace.WriteLine("Making the request");
Trace.WriteLine(client.GetStringAsync(requestURL));
};

Action<object> callQuotationsAPI = (Object obj) =>
{
Trace.WriteLine("Called callQuotationsAPI");
Uri requestURL = new Uri(baseURL + @"orchestra_nn01/quotations/");
Uri requestURL = new Uri(baseURL + "/word/" + query.WordID + "/quotations/");
Trace.WriteLine("Making the request");
Trace.WriteLine(client.GetStringAsync(requestURL));

var response = client.GetStreamAsync(requestURL).Result;
Trace.WriteLine("Got quotation responses.");
JSONResponse = JsonDocument.Parse(response);
Trace.WriteLine("Set JSONResponse to the response.");

};

Action<object> callRootsAPI = (Object obj) =>
Expand Down Expand Up @@ -140,6 +147,8 @@ public static void APICalls(CurrentQuery query)

if (query.QueryMode == Modes.Word)
{
// @TODO put this repeated parsing in a method
// Use enums to select which option
Trace.WriteLine("Found that QueryMode is set to words.");
Trace.WriteLine("Looking up the word:" );
Trace.WriteLine(query.UserEnteredWord);
Expand Down Expand Up @@ -225,6 +234,41 @@ public static void APICalls(CurrentQuery query)
{
Trace.WriteLine("Now to call the .");
}
else if (query.QueryMode == Modes.Senses)
{
Trace.WriteLine("API.cs is starting senses mode.");
if (query.HasLookedUpWord == false)
{
// @TODO remove this. Have it auto-call up the first ID.
Console.WriteLine("You need to first ask for a definition.");
}
else
{
resetHeaders(client);
Task getSenses = new Task(callSensesAPI, "CallSenses");
getSenses.RunSynchronously();
Trace.WriteLine("Ran senses synchronously.");
}
}
else if (query.QueryMode == Modes.Quotations)
{
Trace.WriteLine("API.cs is starting quotations mode.");
if (query.HasLookedUpWord == false)
{
Console.WriteLine("Quotations you need to ask for a definition.");
Console.WriteLine("first.");
}
else
{
resetHeaders(client);
Task getQuotes = new Task(callQuotationsAPI, "Call Quotations");
getQuotes.RunSynchronously();
Trace.WriteLine("Ran quotations synchronously.");
Trace.WriteLine("Parsing quotations JSON.");
JsonElement apiData = JSONResponse.RootElement.GetProperty("data");
Console.WriteLine(apiData.ToString());
}
}
else
{
Console.WriteLine("Query mode not correctly set.");
Expand All @@ -234,3 +278,5 @@ public static void APICalls(CurrentQuery query)
}
}
}
// @TODO If a word is not found return a message "word not found in dictionary."
// Instead of just a blank response.
33 changes: 23 additions & 10 deletions ConsoleUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ static void MainMenu(CurrentQuery query)
Console.WriteLine("Definition - D");
Console.WriteLine("Lammatize - L");
Console.WriteLine("Root - R");
Console.WriteLine("Get Senses - S");
Console.WriteLine("Get Quotations - Q");
Console.WriteLine("Senses - S");
Console.WriteLine("Quotations - Q");
Console.WriteLine("-------------------------");
Console.WriteLine("New Word (start over) - N");
Console.WriteLine("Export Results - E");
Expand Down Expand Up @@ -60,8 +60,9 @@ static void MainMenu(CurrentQuery query)
case ("l" or "lammatize"):
Trace.WriteLine("Lammatize selected.");
query.QueryMode = Modes.Lammatize;
Console.WriteLine("Not yet implemented.");
// Call WORD API
API.APICalls(query);
// API.APICalls(query);
// Return the lammatization of the word
break;

Expand All @@ -75,13 +76,25 @@ static void MainMenu(CurrentQuery query)
Trace.WriteLine("New word");
// Dispose object. Start over.
// For now just reset object?

query.HasLookedUpWord = false;
Trace.WriteLine("query.HasLookedUpWord");
Trace.WriteLine(query.HasLookedUpWord);
query.Definition = "";
Trace.WriteLine("query.Definition");
Trace.WriteLine(query.Definition);
// Wrap below in a Dispose method?
query.HasLookedUpWord = false;
Trace.WriteLine("query.HasLookedUpWord");
Trace.WriteLine(query.HasLookedUpWord);
query.Definition = "";
Trace.WriteLine("query.Definition");
Trace.WriteLine(query.Definition);
break;

case ("s" or "senses"):
Trace.WriteLine("Get senses selected.");
query.QueryMode = Modes.Senses;
API.APICalls(query);
break;

case ("q" or "quotations"):
Trace.WriteLine("Get quotaions based on word selected.");
query.QueryMode = Modes.Quotations;
API.APICalls(query);
break;

case ("x" or "exit"):
Expand Down
4 changes: 3 additions & 1 deletion CurrentQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ public enum Modes
{
Word,
Root,
Lammatize
Lammatize,
Senses,
Quotations
}
class CurrentQuery : IDisposable
{
Expand Down
36 changes: 32 additions & 4 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
using System;
using System.Diagnostics;
using System.IO;

namespace OxfordV2
{
class Program
{
static void Main(string[] args)
{
// Log to console
// Show Trace on console
// TextWriterTraceListener tr1 = new TextWriterTraceListener(System.Console.Out);
// Trace.Listeners.Add(tr1);
string path = string.Concat(Environment.CurrentDirectory, $"\\logs\\Log_OxfordApplication_{DateTime.Now.ToString("yyyyMMdd-HHmm")}.txt");
Trace.WriteLine("Path is {0}", path);
TextWriterTraceListener tr2 = new TextWriterTraceListener(System.IO.File.CreateText(path));

// Try to create the logs directory

string directoryPath = string.Concat(Environment.CurrentDirectory, "\\logs");

try
{
// Determine whether the directory exists
if (Directory.Exists(directoryPath))
{
Trace.WriteLine("The logs path already exists.");

}
else
{
// Try to create the directory
DirectoryInfo di = Directory.CreateDirectory(directoryPath);
}
string fullPath = string.Concat(Environment.CurrentDirectory, $"\\logs\\Log_OxfordApplication_{DateTime.Now.ToString("yyyyMMdd-HHmm")}.txt");
Trace.WriteLine("Path is {0}", fullPath);
TextWriterTraceListener tr2 = new TextWriterTraceListener(System.IO.File.CreateText(fullPath));

Trace.Listeners.Add(tr2);
}

catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
finally {}

Trace.WriteLine("Leaving Main method.");
ConsoleUI.Start();
Trace.Flush();
Expand Down
Binary file modified bin/Debug/net5.0/OxfordV2.dll
Binary file not shown.
Binary file modified bin/Debug/net5.0/OxfordV2.pdb
Binary file not shown.
Binary file modified bin/Debug/net5.0/ref/OxfordV2.dll
Binary file not shown.
37 changes: 0 additions & 37 deletions logs/Log_OxfordApplication_20210416-1641.txt

This file was deleted.

5 changes: 0 additions & 5 deletions logs/Log_OxfordApplication_20210416-1645.txt

This file was deleted.

37 changes: 0 additions & 37 deletions logs/Log_OxfordApplication_20210416-1802.txt

This file was deleted.

49 changes: 0 additions & 49 deletions logs/Log_OxfordApplication_20210416-1808.txt

This file was deleted.

Empty file.
40 changes: 0 additions & 40 deletions logs/Log_OxfordApplication_20210416-1816.txt

This file was deleted.

Empty file.
Loading

0 comments on commit 3ef9587

Please sign in to comment.