-
Notifications
You must be signed in to change notification settings - Fork 0
/
RhymeGenerator.cs
114 lines (100 loc) · 4.03 KB
/
RhymeGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using Newtonsoft.Json;
using PhatAndPhresh.Models;
namespace PhatAndPhresh
{
public class RhymeGenerator : IRhymeGenerator
{
public string GetRhyme(string input, WordType type)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://api.datamuse.com/words");
HttpResponseMessage webResponse = client.GetAsync($"words?rel_rhy={input}&md=p").Result;
webResponse.EnsureSuccessStatusCode();
string resultString = webResponse.Content.ReadAsStringAsync().Result;
List<WordResponse> json = JsonConvert.DeserializeObject<List<WordResponse>>(resultString);
// If there were no rhymes, just get a related word.
if (json.Count == 0)
{
return GetRelatedWord(input, type);
}
Random rand = new Random();
WordResponse rhyme = null;
if (type == WordType.Noun)
{
var nouns = json.Where(r => ((r.tags != null) && r.tags.Contains("n"))).ToList();
if (nouns.Count() != 0) { rhyme = nouns.ElementAt(rand.Next(nouns.Count())); }
}
else if (type == WordType.Verb)
{
var verbs = json.Where(r => ((r.tags != null) && r.tags.Contains("v"))).ToList();
if (verbs.Count() != 0) { rhyme = verbs.ElementAt(rand.Next(verbs.Count())); }
}
else if (type == WordType.Adverb)
{
var adverbs = json.Where(r => ((r.tags != null) && r.tags.Contains("adv"))).ToList();
if (adverbs.Count() != 0) { rhyme = adverbs.ElementAt(rand.Next(adverbs.Count())); }
}
else if (type == WordType.Adjective)
{
var adjectives = json.Where(r => ((r.tags != null) && r.tags.Contains("adj"))).ToList();
if (adjectives.Count() != 0) { rhyme = adjectives.ElementAt(rand.Next(adjectives.Count())); }
}
// If we couldn't find a specific type of word, just grab any of them.
if (rhyme == null)
{
rhyme = json.ElementAt(rand.Next(json.Count()));
}
return rhyme.Word;
}
}
public string GetRelatedWord(string input, WordType type)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://api.datamuse.com/words");
HttpResponseMessage webResponse = client.GetAsync($"words?rel_trg={input}&md=p").Result;
webResponse.EnsureSuccessStatusCode();
string resultString = webResponse.Content.ReadAsStringAsync().Result;
List<WordResponse> json = JsonConvert.DeserializeObject<List<WordResponse>>(resultString);
// If there were no related words, just return a random word.
if (json.Count == 0)
{
return GetRelatedWord("gangsta", type);
}
Random rand = new Random();
WordResponse related = null;
if (type == WordType.Noun)
{
var nouns = json.Where(r => ((r.tags != null) && r.tags.Contains("n"))).ToList();
if (nouns.Count() != 0) { related = nouns.ElementAt(rand.Next(nouns.Count())); }
}
else if (type == WordType.Verb)
{
var verbs = json.Where(r => ((r.tags != null) && r.tags.Contains("v"))).ToList();
if (verbs.Count() != 0) { related = verbs.ElementAt(rand.Next(verbs.Count())); }
}
else if (type == WordType.Adverb)
{
var adverbs = json.Where(r => ((r.tags != null) && r.tags.Contains("adv"))).ToList();
if (adverbs.Count() != 0) { related = adverbs.ElementAt(rand.Next(adverbs.Count())); }
}
else if (type == WordType.Adjective)
{
var adjectives = json.Where(r => ((r.tags != null) && r.tags.Contains("adj"))).ToList();
if (adjectives.Count() != 0) { related = adjectives.ElementAt(rand.Next(adjectives.Count())); }
}
// If we couldn't find a specific type of word, just grab any of them.
if (related == null)
{
related = json.ElementAt(rand.Next(json.Count()));
}
return related.Word;
}
}
}
}