key.avapose.com

ASP.NET Web PDF Document Viewer/Editor Control Library

In the previous section, I showed how you could use a bound method just like a function without losing the self parameter. That means that you can use methods in many of the fancy ways that I ve used functions previously, with handy tools such as map, filter, and reduce (see the section Throwing Functions Around in 6). In this section, I provide some examples of these capabilities. They should all be fairly self-explanatory. Let s start by creating a class: class FoodExpert: def init(self): self.goodFood = [] def addGoodFood(self, food): self.goodFood.append(food) def likes(self, x): return x in self.goodFood def prefers(self, x, y): x_rating = self.goodFood.index(x) y_rating = self.goodFood.index(y) if x_rating > y_rating: return y else: return x This class has more code than earlier examples, but it is still pretty simple. It is meant to represent some sort of food expert (as the name implies) who likes only some types of food, and likes some more than others. The init method simply initializes the objects by giving them an attribute called goodFood containing an empty list. The addGoodFood method adds a type of food to the list, where the first food type added is the expert s favorite, the next one is the second choice, and so on. The likes method simply checks whether the expert likes a certain type of food (whether it has been added to goodFood), and finally the prefers method is given two food types (both of which must be liked) and returns the preferred one (based on their position in goodFood). Now, let s play. In the following example, a FoodExpert is created and its taste buds initialized:

winforms pdf 417 reader, winforms qr code reader, winforms upc-a reader, winforms data matrix reader, winforms gs1 128, winforms ean 13 reader, c# remove text from pdf, replace text in pdf using itextsharp in c#, winforms code 39 reader, itextsharp remove text from pdf c#,

After you have the preprocessed input at your disposal, it s time to split it up into sentences and choose the best one. You can add another line to response_to:

working memory has only limited capacity to handle new information as that information arrives, as shown on the left in Figure 2-10, it also has unlimited capacity to pull in existing information from long-term memory, as shown on the right.

def response_to(input) prepared_input = preprocess(input.downcase) sentence = best_sentence(prepared_input) end

Then you can implement best_sentence as a private method:

unlimited in its capacity to process existing information from long-term memory (right).

def best_sentence(input) hot_words = @data[:responses].keys.select do |k| k.class == String && k =~ /^\w+$/ end WordPlay.best_sentence(input.sentences, hot_words) end

>>> f = FoodExpert() >>> f.init() >>> map(f.addGoodFood, ['SPAM', 'Eggs', 'Bacon', 'Rat', 'Spring Surprise']) [None, None, None, None, None] The first two lines instantiate FoodExpert and initialize the instance, which is assigned to f. The map call simply uses the method addGoodFood with its self parameter bound to f. Because this method doesn t return anything, the result is a list filled with None. However, a side effect is that f has been updated: >>> f.goodFood ['SPAM', 'Eggs', 'Bacon', 'Rat', 'Spring Surprise'] Let s use this expert to give us a list of recommendations: >>> menu = ['Filet Mignon', 'Pasta', 'Pizza', 'Eggs', 'Bacon', 'Tomato', 'SPAM'] >>> rec = filter(f.likes, menu) >>> rec ['Eggs', 'Bacon', 'SPAM'] What I did here was simply apply f.likes as a filter to a menu; the dishes the expert didn t like were simply discarded. But what if you want to find out which of these dishes the expert would prefer I once again turn to the trusty (if rarely used) reduce: >>> reduce(f.prefers, rec) 'SPAM' This basically works just like the example using reduce with max in 6 (in the section reduce ). If I had used a different expert, initialized with different preferences, of course, I d get completely different results, even though the method definitions would be exactly the same. This is the primary difference between standard functional programming and this quasi-functional programming using bound methods; the methods have access to a state that can be used to customize them.

First, best_sentence collects an array of single words from the keys in the :responses hash. It looks for all keys that are strings (you don t want the :default, :greeting, or :farewell symbols getting mixed in) and only a single word. You then use this list with the WordPlay.best_sentence method you developed earlier in this chapter to choose the sentence from the user input that matches the most hot words (if any). You could rewrite this method in any style you wish. If you only ever wanted to choose the first sentence in the user input, that s easy to do:

   Copyright 2020.