-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
125 lines (114 loc) · 4.18 KB
/
index.php
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
116
117
118
119
120
121
122
123
124
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
//connect to a database named "jackson" on the host "localhost" with a username and password
$conn = pg_connect("host=localhost port=5432 dbname=jackson user=jackson password=your-password-here");
?>
<html>
<head>
<title>wordchef</title>
</head>
<body>
<h1>wordchef</h1>
<p>This is a webapp which uses PHP + pgSQL w/ pgvector to lookup word embeddings, find their sum, and do a fast semantic similarity search to find the nearest five words.</p>
</body>
</html>
<?php
// validate user input from forms
function validate_string($user_input,$max_len){
if (empty($user_input)) {
return false;
}
// To check that username only contains alphabets, numbers, and underscores
elseif (!preg_match("/^[a-zA-Z0-9_]*$/", $user_input)) {
return false;
}
elseif (strlen($user_input) > $max_len) {
return false;
}
else{
return true;
}
}
?>
<form action="/" method="POST">
Word 1: <input type="text" id="word_1" name="word_1">
Word 2: <input type="text" id="word_2" name="word_2">
<input type="submit" value="Submit">
</form>
<?php
//define vector functions for PHP arrays
function add_arrays($arr1,$arr2){
$n = count($arr1);
assert($n == count($arr2), "Error: arrays are different sizes.");
$sum_array = array_fill(0, $n, 0);
for ($i = 0; $i < $n; $i++) {
$sum_array[$i] = $arr1[$i] + $arr2[$i];
}
return $sum_array;
}
//define vector functions for PHP arrays
function scale_array($scalar,$arr){
$n = count($arr);
$scaled_array = array_fill(0, $n, 0);
for ($i = 0; $i < $n; $i++) {
$scaled_array[$i] = $scalar*$arr[$i];
}
return $scaled_array;
}
?>
<?php
$valid_input_1 = false;
if(isset($_POST['word_1'])){
$word_1 = $_POST['word_1'];
$valid_input_1 = validate_string($word_1,14);
if($valid_input_1){
$word_1_query = "SELECT * FROM wordembeddings WHERE word='$word_1'";
$result = pg_query($conn,$word_1_query) or die('Query failed: ' . pg_last_error());
//fetch the associative array of results. keys should be ['word','embedding']
while ($row = pg_fetch_array($result, null, PGSQL_ASSOC)) {
$embedding_string_1 = str_replace(array( '[', ']' ), '', $row['embedding']);
$embedding_array_1 = explode(',', $embedding_string_1);
echo "<br>";
}
}
}
$valid_input_2 = false;
if(isset($_POST['word_2'])){
$word_2 = $_POST['word_2'];
$valid_input_2 = validate_string($word_2,14);
if($valid_input_2){
$word_2_query = "SELECT * FROM wordembeddings WHERE word='$word_2'";
$result = pg_query($conn,$word_2_query) or die('Query failed: ' . pg_last_error());
//fetch the associative array of results. keys should be ['word','embedding']
while ($row = pg_fetch_array($result, null, PGSQL_ASSOC)) {
$embedding_string_2 = str_replace(array( '[', ']' ), '', $row['embedding']);
$embedding_array_2 = explode(',', $embedding_string_2);
echo "<br>";
}
}
}
if($valid_input_1 && $valid_input_2){
$sum_array = add_arrays($embedding_array_1,$embedding_array_2);
$average_array = scale_array(.5,$sum_array);
$average_array_string = '[' . implode(',', $average_array) . ']';
echo '(' . $word_1 . "+" . $word_2 . ")/2 --> " . $average_array_string;
// Performing SQL query
$query = "SELECT * FROM wordembeddings ORDER BY embedding <-> '$average_array_string' LIMIT 5;";
$result = pg_query($conn,$query) or die('Query failed: ' . pg_last_error());
// Printing results in HTML
echo "<table>\n";
while ($row = pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($row as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
// Free resultset
pg_free_result($result);
}
// Closing connection
pg_close($conn);
?>