concept text in category data

appears as: text, text, texts, texts
Data Science Bookcamp: Five Python Projects

This is an excerpt from Manning's book Data Science Bookcamp: Five Python Projects.

The GeoNames database (geonames.org) is an excellent resource for obtaining geographic data.. GeoNames contains over eleven million place-names spanning all the countries in the World. Beyond just place-names, GeoNames also stores valuable information such as latitude and longitude. Thus, we can leverage the database to determine the precise geographic locations of cities and countries discovered in text.

Rapid text analysis is able to save lives. Consider this actual real-world incident, when US soldiers stormed a terrorist compound. In the compound, they discovered a computer containing terabytes of archived data. The data included documents, text-messages, and e-mails pertaining to terrorist activities. The documents were too numerous to be read by any single human being. Fortunately, the soldiers were equipped with special software for very fast analysis of text. The software allowed the soldiers process all text-data without even having to leave the compound. The onsite analysis immediately revealed an active terrorist plot in a nearby neighborhood. The soldiers instantaneously responded to the plot, and prevented a terrorist attack.

Our goal is to determine whether text1 is more similar to text2 or to text3. We’ll start by assigning the texts to three variables.

Listing 13.1. Assigning texts to variables
text1 = 'She sells seashells by the seashore.'
text2 = '"Seashells! The seashells are on sale! By the seashore."'
text3 = 'She sells 3 seashells to John, who lives by the lake.'

Now, we need to quantify the differences between texts. One basic approach is to simply count the words shared between each pair of texts. The approach requires us to split each text into a list of words. Text-splitting in Python can be carried out using the built-in string split method.

Texts 1 and 2 share these 4 words:
{'seashore', 'by', 'the', 'seashells'}

Texts 1 and 3 share these 5 words:
{'sells', 'by', 'she', 'the', 'seashells'}

Texts 1 and 2 share four words. Meanwhile, texts 1 and 3 share five words. Does this mean that text1 is more similar to text3 than to text2? Not necessarily. While texts 1 and 3 share 5 overlapping words, they also contain numerous diverging words. These diverging words appear in one of the texts, but never in both. Lets count and print all diverging words between text-pairs (text1, text2) and (text1, text3). We’ll leverage the ^ operator to extract diverging elements between each pair of word-sets.

Listing 13.6. Extracting diverging words between two texts
for i, words_set in enumerate(words_sets[1:], 2):
    diverging_words = words_set1 ^ words_set
    print(f"Texts 1 and {i} don't share these {len(diverging_words)} words:")
    print(f"{diverging_words}\n")
Texts 1 and 2 don't share these 5 words:
{'are', 'sells', 'sale', 'on', 'she'}

Texts 1 and 3 don't share these 7 words:
{'to', 'lake', 'lives', 'seashore', 'john', '3', 'who'}

Texts 1 and 3 contain two more diverging words than texts 1 and 2. Thus, texts 1 and 3 show significant word-overlap, but also significant divergence. We need to combine overlap and divergence into a single similarity score. Doing so is trivial. Imagine if we combined all overlapping words and all diverging words between two texts. The combination shoud contain all the unique words across the two texts. This aggregation of all unique words is called a union. Given two Python sets set_a and set_b, we can compute their union by running set_a | set_b.

Figure 13.1. A visualized representation of the union, intersection, and divergence between two texts.
fig13 1

The differences between word divergence, intersection, and union are illustrated in Figure 13.1. Here, the unique words in texts 1 and 2 are displayed within three rectangular boxes. The leftmost box and the rightmost box represent the diverging words between texts 1 and 2, respectively. Meanwhile, the middle box contains all the shared words that lie at the intersection of texts 1 and 2. Together, the three boxes represent the union of all words within the two texts.

Mastering Large Datasets with Python: Parallelize and Distribute Your Python Code

This is an excerpt from Manning's book Mastering Large Datasets with Python: Parallelize and Distribute Your Python Code.

Often, we’ll want to use map and reduce together, one right after another. This pattern gives rise to the MapReduce programming pattern. The MapReduce programming pattern relies on the map function to transform some data into another type of data and then uses the reduce function to combine that data. A mathematical example might be taking the sum of the greatest prime factor of a sequence of numbers. We can use map to transform each number into its greatest prime factor and then use reduce to take their sum. A more practical example may be finding the longest word on a sequence of web pages, when all we have is the URLs. We can use map to turn the URLs into text and reduce to find the longest word (figure 1.7).

In the last chapter, we saw how you can use map to replace for loops and how using map makes parallel computing straightforward: a small modification to map, and Python will take care of the rest. But so far with map, we’ve been working with simple functions. Even in the Wikipedia scraping example from chapter 2, our hardest working function only pulled text off the internet. If we want to make parallel programming really useful, we’ll want to use map in more complex ways. This chapter introduces how to do complex things with map. Specifically, we’re going to introduce two new concepts:

Applying the pipeline to tweet text

Applying the pipeline to tweet text directly will look very similar to applying the pipeline to Tweet IDs, as shown in the following listing.

Listing 3.9. Applying the gender prediction pipeline to tweet text
user_tweets = [                                                            #1
        ["i think product x is so great", "i use product x for everything",
        "i couldn't be happier with product x"],
        ["i have to throw product x in the trash",
        "product x... the worst value for your money"],
        ["product x is mostly fine", "i have no opinion of product x"]]

with Pool() as P:                                                          #2
    print(P.map(gender_prediction_pipeline, users_tweets))