-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwordplay.js
115 lines (96 loc) · 2.87 KB
/
wordplay.js
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
115
import { RiTa } from 'rita';
function capitalize(word) {
let a = word.charAt(0);
a = a.toUpperCase();
return a + word.substring(1, word.length);
}
function addArticle(word) {
return /^[aeiou]/i.test(word) ? `an` : `a`;
}
function generateTweet(word, a, b, aPos, bPos) {
const templates = {
nn: [
'{word} is nothing more than {articleA} {a} and {articleB} {b}.',
'{word} is nothing more than {articleA} {a} and {articleB} {b}.',
],
an: ['{word} is nothing more than {articleA} {a} {b}.'],
vn: ['{word} is nothing more than to {a} {articleB} {b}.'],
va: ['{word} is nothing more than {a} to {b}.'],
vr: ['{word} is nothing more than to {a} {b}.'],
};
const key = `${aPos}${bPos}`;
const options = templates[key];
const template = options[Math.floor(Math.random() * options.length)];
return template
.replace('{word}', capitalize(word))
.replace('{a}', a)
.replace('{b}', b)
.replace('{articleA}', addArticle(a))
.replace('{articleB}', addArticle(b));
}
function findPair() {
for (let i = 0; i < 100; i++) {
let word = RiTa.randomWord({ minLength: 6 });
for (let j = 3; j <= word.length - 3; j++) {
const a = word.substring(0, j);
const b = word.substring(j, word.length);
if (RiTa.hasWord(a) && RiTa.hasWord(b)) {
return { word, a, b };
}
}
}
throw new Error('No valid pair found.');
}
export function findPairs() {
const tries = [];
for (let i = 0; i < 10; i++) {
let { a, b, word } = findPair();
let aPos = RiTa.pos(a, { simple: true })[0];
let bPos = RiTa.pos(b, { simple: true })[0];
const reverseThese = [
['n', 'a'],
['n', 'v'],
['r', 'v'],
['a', 'v'],
];
if (reverseThese.some(([pos1, pos2]) => aPos === pos1 && bPos === pos2)) {
[a, b] = [b, a];
[aPos, bPos] = [bPos, aPos];
}
const validCombinations = [
['a', 'n'], // Adjective + Noun
['v', 'n'], // Verb + Noun
['n', 'n'], // Noun + Noun
['v', 'r'], // Verb + Adverb
['v', 'a'], // Adjective + Verb
];
if (validCombinations.some(([pos1, pos2]) => aPos === pos1 && bPos === pos2)) {
const tweet = generateTweet(word, a, b, aPos, bPos);
tries.push(tweet);
} else {
console.log(`Invalid pair: ${a} (${aPos}), ${b} (${bPos})`);
}
}
return tries;
}
export async function centerOf() {
const tweets = [];
for (let i = 0; i < 50; i++) {
// A random word
const word = RiTa.randomWord();
let allMatches = await RiTa.search(word);
for (let match of allMatches) {
const regex = new RegExp(`.+${word}.+`);
if (regex.test(match)) {
const tweet = `At the center of ${match} is ${word}.`;
tweets.push(tweet);
}
}
}
if (tweets.length > 10) {
tweets.sort(() => Math.random() - 0.5);
return tweets.slice(0, 10);
}
return tweets;
}
centerOf();