Using Google Artificial Intelligence Services in Couchbase N1QL

Google has started out with a mission to empower everything and everyone with Artificial Intelligence (A.I.). It has open-sourced Tensorflow and supporting libraries to enable developers and enterprises to build and train models, and infer (predict) using those. Building useful enterprise services with this may take time.

Google has also exposed many of the A.I services (via Cloud Machine learning APIs) that can be quite useful in your applications. Extracting features and text from images, translating text from one language to another, sentiment analysis on text can help you to improve user experience dramatically. There are new businesses and business models enabled by these services alone. Google has done the hard work of building and training the model. You simply call a REST API to exploit their machine learning APIs. These APIs simply act as a declarative service invocation method without needed to understand or maintain the deep infrastructure underneath.

These are all provided as REST APIs. See the Google site below for documentation and details of the service.

In Couchbase 5.0, N1QL R&D engineer, Isha Kandaswamy has developed and haswritten about the CURL() functionality. Using CURL(), you can use any of REST services with JSON endpoint. An example of using Google map API is shown here. The JSON result from the services can be naturally processed in N1QL because N1QL is designed to be SQL for JSON.

Note: You can use CURL() use A.I or other services from any publicly available service.

This blog shows the way to use Google Cloud AI API directly on the data you have in Couchbase, directly with a simple N1QL statement. All of the Google Cloud Machine Learning APIs return JSON. So, CURL() can be used to call any of the services. I’ll show you examples of using Google Vision API, Google translation API, and Google Natural Language API.

Note: You need to setup your project and enable each of the services and obtain the API-KEY which you need to pass. In the examples below, I’ve removed my key. You should replace PUT YOUR KEY HERE with your key.

You could be an insurance company where customers upload photos of their cars and identifications. You could be a college receiving images of transcripts. You want to extract the text in the image for veracity, improved user experience and making it easier to search for those when needed.

Google has gone from organizing world’s text to world’s multi-media content. Vision API gives you powerful features to extract a lot of metadata within an image and correlate that information to information and other images available on the web.

This query returns a huge 120K response describing all the polygons, associated texts. Here’s the response. Let’s issue the query again to and project the things we need. Vision API returns the concatenated list of strings. You can use the SPLIT() or TOKENS() function of N1QL to get each string separately.

This API does what it says: Translate from one language to another. It can automatically detect the source language. Arguments to this API is simply the source content and target language. In this example, let’s translate the customer reviews for a hotel from English to French.

“english”:“This has got to be the worse experience I have ever had at a hotel. Our reservation was placed two months in advance for a non-smoking room with two beds from July 2-7, 2010. We are staying five nights at $190 a night and this is what we got, a smoking room with one bed, and was only told at the front desk that it was going to be a smoking room, nothing about the one bed, when she asked us how many beds we needed, oh they provided a roll-a-way, only thing is I had to move the chair into the hallway to fit the bed. The t.v. was older than me and the speaker was shot. the bathroom was so small you have to step into the tub to close the door, no fridge in the room, doors are cheap and horrible. only sheets on the bed no blankets/quilts, plus on top of that, i had to pay $14.95 a night for internet. I will never recommend this hotel! how can you take a reservation and state that it will be held till 10am the following morning, yet not provide what was requested in the reservation? What if someone in my party was allergic to smoke, asthmatic, or worse… we wouldn’t have gotten a room? completely unacceptable no wonder I will continue to stay at the comfort suites… free internet, modern amenities(flat screens) oh and $85 a night. unsatisfied doesn’t even come close to how I feel. the only good thing out of this whole stay was the hot cookie when I got here and mine wasn’t even hot. You can take that cookie and… well i’m sure you can figure out the rest. Thanx for nothing!”,

“english”:“OK – I booked this place about 8 weeks prior to travel, when the rooms were still $116 for a Saturday night on Doubletree/Hilton site. As the travel date drew close, I would reprice and the rate climbed to over $200. $116 was a bargain, but the place isn’t worth $200. I had room 1022 – very small, some mildew on in the closet in the corner, and right down the hall from the housekeeping closets (they banged their doors day and night). The bed was comfortable and the staff was very courteous. I didn’t know this when I booked the hotel, but I was thrilled to see it was at the same intersection as Harrah’s Casino so I lost $50. The hotel is also right at a trolley car stop on Canal Street. $116 – yep, I’ld stay there again in a heartbeat. $200 – no can do. Book early and tolerate the inconveniences in exchange for a bargain price.”,

“english”:“I was impressed with my room and the great service I received at the front desk. I found the staff helpful and very pleasant. The location was great with easy walking distances to the French Quarter, other points of interest and great restaurants in the immediate area. Room service was on time and my breakfast arrived hot and ready to enjoy. I would recommend this hotel for the start of a great stay in Big Easy.”,

“english”:“The hotel is located conveniently on Canal Street at the edge of the French Quarter near the river. When we arrived, the rooms were ready to go and checking was painless, the cookies were awesome! The louge downstairs was a good place to catch a drink before heading out each night. We were within walking distance to everything in the French Quarter and catching a cab in front of the hotel was easy. The trolley has a stop in front of the hotel, Harrah’s is next door, the aquarium , the mall and a move theater are all across the street. The hotel restaurant was good, not the best considering it’s New Orleans, but good for hotel food. I didn’t have a car this trip, but you have to pay for parking in most places in the quarter. The pool is small, but nice to hang out at mid afternoon and there is a small gym for a quick work out. I’ve staryed in many places in the French Quarter over the years and this is one of the better experiences I have had. I was pleasantly suprizes at how much I liked the location.”,

“english”:“The Doubltree is located near so many attractions, we hardly ever needed a cab the whole time we were there. Clean rooms that were well maintained were a treat to come home to after a long day. I never experienced one issue or problem the entire time I was there, from a warm a welcoming check-in (with a great cookie!) to an efficent check-out. I would certainly reccomend the hotel to anyone I knew that was traveling to the Big Easy.”,

“english”:“We stayed at the Doubletree Hotel New Orleans for the first time on February 14, 2009. The good: check in was quick, the rooms were comfortable and very clean. I forgot some amenities and staff were very quick to respond with bringing up what I needed. All staff were very polite. One bad: the walls are paper thin, you can literally hear every word in the rooms next to you and in the hallway. I would stay here again for the ideal location, but if you need a good nights rest, reconsider due to noise.”,

Since I don’t understand French well, I had the review translated to Kannada, my mother tongue. I must say, the translation was decent for a machine. Some sentences were convoluted (almost sounded old Kannada), but overall understandable. Here’s that translation.

“content”:“This has got to be the worse experience I have ever had at a hotel. Our reservation was placed two months in advance for a non-smoking room with two beds from July 2-7, 2010. We are staying five nights at $190 a night and this is what we got, a smoking room with one bed, and was only told at the front desk that it was going to be a smoking room, nothing about the one bed, when she asked us how many beds we needed, oh they provided a roll-a-way, only thing is I had to move the chair into the hallway to fit the bed. The t.v. was older than me and the speaker was shot. the bathroom was so small you have to step into the tub to close the door, no fridge in the room, doors are cheap and horrible. only sheets on the bed no blankets/quilts, plus on top of that, i had to pay $14.95 a night for internet. I will never recommend this hotel! how can you take a reservation and state that it will be held till 10am the following morning, yet not provide what was requested in the reservation? What if someone in my party was allergic to smoke, asthmatic, or worse… we wouldn’t have gotten a room? completely unacceptable no wonder I will continue to stay at the comfort suites… free internet, modern amenities(flat screens) oh and $85 a night. unsatisfied doesn’t even come close to how I feel. the only good thing out of this whole stay was the hot cookie when I got here and mine wasn’t even hot. You can take that cookie and… well i’m sure you can figure out the rest. Thanx for nothing!”,

“content”:“OK – I booked this place about 8 weeks prior to travel, when the rooms were still $116 for a Saturday night on Doubletree/Hilton site. As the travel date drew close, I would reprice and the rate climbed to over $200. $116 was a bargain, but the place isn’t worth $200. I had room 1022 – very small, some mildew on in the closet in the corner, and right down the hall from the housekeeping closets (they banged their doors day and night). The bed was comfortable and the staff was very courteous. I didn’t know this when I booked the hotel, but I was thrilled to see it was at the same intersection as Harrah’s Casino so I lost $50. The hotel is also right at a trolley car stop on Canal Street. $116 – yep, I’ld stay there again in a heartbeat. $200 – no can do. Book early and tolerate the inconveniences in exchange for a bargain price.”,

“content”:“I was impressed with my room and the great service I received at the front desk. I found the staff helpful and very pleasant. The location was great with easy walking distances to the French Quarter, other points of interest and great restaurants in the immediate area. Room service was on time and my breakfast arrived hot and ready to enjoy. I would recommend this hotel for the start of a great stay in Big Easy.”,

“content”:“The hotel is located conveniently on Canal Street at the edge of the French Quarter near the river. When we arrived, the rooms were ready to go and checking was painless, the cookies were awesome! The louge downstairs was a good place to catch a drink before heading out each night. We were within walking distance to everything in the French Quarter and catching a cab in front of the hotel was easy. The trolley has a stop in front of the hotel, Harrah’s is next door, the aquarium , the mall and a move theater are all across the street. The hotel restaurant was good, not the best considering it’s New Orleans, but good for hotel food. I didn’t have a car this trip, but you have to pay for parking in most places in the quarter. The pool is small, but nice to hang out at mid afternoon and there is a small gym for a quick work out. I’ve staryed in many places in the French Quarter over the years and this is one of the better experiences I have had. I was pleasantly suprizes at how much I liked the location.”,

“content”:“The Doubltree is located near so many attractions, we hardly ever needed a cab the whole time we were there. Clean rooms that were well maintained were a treat to come home to after a long day. I never experienced one issue or problem the entire time I was there, from a warm a welcoming check-in (with a great cookie!) to an efficent check-out. I would certainly reccomend the hotel to anyone I knew that was traveling to the Big Easy.”,

“content”:“We stayed at the Doubletree Hotel New Orleans for the first time on February 14, 2009. The good: check in was quick, the rooms were comfortable and very clean. I forgot some amenities and staff were very quick to respond with bringing up what I needed. All staff were very polite. One bad: the walls are paper thin, you can literally hear every word in the rooms next to you and in the hallway. I would stay here again for the ideal location, but if you need a good nights rest, reconsider due to noise.”,

We’ve only projected the fields we’re interested here, review, magnitude, and sentiment score. See the Google docs for full description of what these mean and rest of the data it generates.

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

{

“requestID”:“c7b66165-3d18-4a61-8b24-732afdd4714a”,

“signature”:{

“magnitude”:“json”,

“review”:“json”,

“score”:“json”

},

“results”:[

{

“magnitude”:8.7,

“review”:“This has got to be the worse experience I have ever had at a hotel. Our reservation was placed two months in advance for a non-smoking room with two beds from July 2-7, 2010. We are staying five nights at $190 a night and this is what we got, a smoking room with one bed, and was only told at the front desk that it was going to be a smoking room, nothing about the one bed, when she asked us how many beds we needed, oh they provided a roll-a-way, only thing is I had to move the chair into the hallway to fit the bed. The t.v. was older than me and the speaker was shot. the bathroom was so small you have to step into the tub to close the door, no fridge in the room, doors are cheap and horrible. only sheets on the bed no blankets/quilts, plus on top of that, i had to pay $14.95 a night for internet. I will never recommend this hotel! how can you take a reservation and state that it will be held till 10am the following morning, yet not provide what was requested in the reservation? What if someone in my party was allergic to smoke, asthmatic, or worse… we wouldn’t have gotten a room? completely unacceptable no wonder I will continue to stay at the comfort suites… free internet, modern amenities(flat screens) oh and $85 a night. unsatisfied doesn’t even come close to how I feel. the only good thing out of this whole stay was the hot cookie when I got here and mine wasn’t even hot. You can take that cookie and… well i’m sure you can figure out the rest. Thanx for nothing!”,

“score”:–0.4

},

{

“magnitude”:4.1,

“review”:“OK – I booked this place about 8 weeks prior to travel, when the rooms were still $116 for a Saturday night on Doubletree/Hilton site. As the travel date drew close, I would reprice and the rate climbed to over $200. $116 was a bargain, but the place isn’t worth $200. I had room 1022 – very small, some mildew on in the closet in the corner, and right down the hall from the housekeeping closets (they banged their doors day and night). The bed was comfortable and the staff was very courteous. I didn’t know this when I booked the hotel, but I was thrilled to see it was at the same intersection as Harrah’s Casino so I lost $50. The hotel is also right at a trolley car stop on Canal Street. $116 – yep, I’ld stay there again in a heartbeat. $200 – no can do. Book early and tolerate the inconveniences in exchange for a bargain price.”,

“score”:0.1

},

{

“magnitude”:4.5,

“review”:“I was impressed with my room and the great service I received at the front desk. I found the staff helpful and very pleasant. The location was great with easy walking distances to the French Quarter, other points of interest and great restaurants in the immediate area. Room service was on time and my breakfast arrived hot and ready to enjoy. I would recommend this hotel for the start of a great stay in Big Easy.”,

“score”:0.9

},

{

“magnitude”:5.9,

“review”:“The hotel is located conveniently on Canal Street at the edge of the French Quarter near the river. When we arrived, the rooms were ready to go and checking was painless, the cookies were awesome! The louge downstairs was a good place to catch a drink before heading out each night. We were within walking distance to everything in the French Quarter and catching a cab in front of the hotel was easy. The trolley has a stop in front of the hotel, Harrah’s is next door, the aquarium , the mall and a move theater are all across the street. The hotel restaurant was good, not the best considering it’s New Orleans, but good for hotel food. I didn’t have a car this trip, but you have to pay for parking in most places in the quarter. The pool is small, but nice to hang out at mid afternoon and there is a small gym for a quick work out. I’ve staryed in many places in the French Quarter over the years and this is one of the better experiences I have had. I was pleasantly suprizes at how much I liked the location.”,

“score”:0.5

},

{

“magnitude”:3.3,

“review”:“The Doubltree is located near so many attractions, we hardly ever needed a cab the whole time we were there. Clean rooms that were well maintained were a treat to come home to after a long day. I never experienced one issue or problem the entire time I was there, from a warm a welcoming check-in (with a great cookie!) to an efficent check-out. I would certainly reccomend the hotel to anyone I knew that was traveling to the Big Easy.”,

“score”:0.8

},

{

“magnitude”:3.8,

“review”:“We stayed at the Doubletree Hotel New Orleans for the first time on February 14, 2009. The good: check in was quick, the rooms were comfortable and very clean. I forgot some amenities and staff were very quick to respond with bringing up what I needed. All staff were very polite. One bad: the walls are paper thin, you can literally hear every word in the rooms next to you and in the hallway. I would stay here again for the ideal location, but if you need a good nights rest, reconsider due to noise.”,

“score”:0.5

}

],

“status”:“success”,

“metrics”:{

“elapsedTime”:“3.905025161s”,

“executionTime”:“3.893259752s”,

“resultCount”:6,

“resultSize”:5256

}

}

Google natural language API can also extract entities, their web references. Let’s look at an example of that as well.

The entity analysis API correctly identifies the location and gives us the web references (URLs) to the landmark. When the references are not quite specific, like the William Team Rooms here, the API can give very high-level generic references.

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

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

{

“requestID”:“4e104224-fe2d-43a8-b86f-aca371987d75”,

“signature”:{

“ginfo”:“json”

},

“results”:[

{

“ginfo”:{

“content”:“During the temperance movement, the idea of &quot;tearooms&quot;, places where you could relax and enjoy non-alcoholic refreshments in differently themed rooms, became popular in Glasgow. This one, designed by Charles Rennie Mackintosh in 1904, was the most popular of its time and has been lovingly restored.”,

Summary:

Couchbase N1QL applications can use Google machine learning APIs easily to improve the customer experience. CURL() gives a flexible method to use the A.I services not only from Google but also from other vendors. Try it out.

Posted by Keshav Murthy

Keshav Murthy is a Senior Director at Couchbase R&D. Previously, he was Senior Director of Product Management at MapR, Senior Architect for IBM, with more than 20 years experience in database design & development. He lead the SQL and NoSQL query processing team IBM Informix database. He received a President’s Club award at Couchbase, two Outstanding Technical Achievement Awards at IBM. Keshav has a bachelors degree in Computer Science and Engineering from the University of Mysore, India and holds eight US patents.