Principles Of Tokenization
- Once we have options for where word-boundaries sit (Ali? Alice?, Alicew?), the number of possibilities could exponentially increase in the worst case and
- Numerical score may help to rank the possible outcomes.
- You cannot apply a simple string searching algorithm for searching keywords (i.e. search for the sub-string (まじ) within the text as it can easily introduce errors )
- The decision whether or not to tokenize can be affected by surrounding characters.
Approaches For Japanese Tokenization
Putting It Into Practice: Tips For Japanese CSDL
Advanced Filtering - Stemming
Working Example For Japanese Geo-Extraction
- Tokenization is an important technique to extract correct signals from East Asian languages.
- N-gram and Morphological analysis are the two main techniques available.
- Datasift has implemented a noise-tolerant Morphological approach for Japanese with some extensions to handle new words accurately.
- By adding our new modifier [language(ja)] in CSDL, you can activate our Japanese tokenization engine in our distributed system.
- We can mix Japanese and other languages within a CSDL filter to realize unified and centralized data analysis.
The LexisNexis Source
How Could You Use It?
- Reputation management: Spot important trends, new opportunities and potential threats and act on them before anyone else. By monitoring news content you can proactively monitor negative opinions, adverse developments and identify risks. Alongside LexisNexis you could add social data sources, so monitor reputation on both social networks and published media.
- Opportunity identification: By staying on top of the latest news stories, companies can anticipate customers' emerging needs and stay one step ahead of their competition. LexisNexis covers newspapers, press releases, specialist trade journals and regional publications so you can stay on top of breaking news.
- Risk monitoring: There are many factors that can impact business performance, including the state of local economies, political upheaval and legislative change. Using LexisNexis news and legal coverage, keep abreast of issues that impact your suppliers and clients, and changes in local markets that could harm your business around the globe.
An Example Filter
LexisNexis SmartIndexing Technology™
LexisNexis + VEDO
Can The LexisNexis Source Help You?
One key challenge for developer creating a solution is integrating, often many, data sources. DataSift destinations take away this headache, especially the recently released MySQL destination.
The MySQL destination allows you to map and flatten unstructured data to your database schema, avoid writing needless custom integration code and handles realtime delivery challenges such as dropped connections so you don't have to.
Relieving Integration Challenges
The DataSift platform offers many awesome places to push data, but often let's face it, we all like to see data in a good old fashioned database. Relational databases such as MySQL are still the backbone of enterprise solutions.
Receiving a stream of unstructured data, structuring it, then pushing the data into a relational database can cause a number of headaches. The new MySQL destination makes the job straight forward so that you can concentrate on getting maximum value out of your data. It provides the following features:
- Guaranteed delivery - Data delivery is buffered and caters for dropped connections and delivery failure
- Delivery control - Data delivery can be paused and resumed as you require under your control
- Data mapping - Specify precisely how you want fields (within each JSON object) to be mapped to your MySQL schema
These features combined make pushing data from DataSift into a MySQL database extremely easy.
The MySQL Destination
As with any other type of destination, the easiest way to get started is to go to the Destinations page. Choose to add a new MySQL destination to your account.
Note that the MySQL destination is only currently available to enterprise customers. Contact your sales representative or account manager if you do not see the destination listed in your account.
To set up the destination you need to enter a name, the host and port of your MySQL server, the destination database schema and authentication details.
You also need to provide a mappings file. This file tells the destination which fields within the JSON data you would like to be mapped to tables in your database schema. More details on this in a minute.
It's worth using the Test Connection button as this will check that your MySQL server is accessible to our platform, the database exists, the security credentials are valid and that the mapping file is valid.
Note that you can also create the destination via our API. This process is documented here.
Mapping Data To A Schema
The basic connection details above are self-explanatory, but the mapping file definitely needs a little more explanation. There are many things to consider when mapping unstructured data to a relational set of tables.
Let me take you through an example schema and mapping file to help clarify the process. These have been designed to work with Twitter data. The two files I'll be discussing are:
In the example schema the following tables are included, which give us a structure to store the tweets.
- interaction - Top-level properties of each interaction / tweet. All tables below reference interactions in this table.
- hashtags - Hashtags mentioned for each interaction
- mentions - Twitter mentions for each interaction
- links - Links for each interaction
- tag_labels - VEDO tags for each interaction
- tag_scores - VEDO scores for each interaction
The example schema is quite exhaustive, please don't be put off! You can more than likely use a subset of fields and tables to store the data you need for your solution. You might also choose to write views that transform data from these tables to fit your application.
Now's not the time to cover MySQL syntax, I'm sure if you're reading this post you'll be used to creating schemas. Instead I'll move on to the mapping file, which is where the magic lies.
The mapping file allows you to specify what tables, columns and data types the raw data should be mapped to in your schema. I can't cover every possibility in one post, so for full details see our technical documentation pages. To give you a good idea though, I'll pick out some significant lines from the example mapping file.
Let's pretend we have the following interaction in JSON (I removed many fields for brevity):
Tables, Datatypes & Transforms
The first line tells the processor you want to map the following columns of the 'interaction' table to fields in the JSON structure.
The next line, tells the processor to map the path interaction.id to the interaction_id column of the table:
interaction_id = interaction.id
Skipping a couple of lines, the following tells the processor to map interaction.created_at to the created_at column. You'll notice though that we have additional data_type and transform clauses.
created_at = interaction.created_at (data_type: datetime, transform: datetime)
If you don't explicitly specify a data_type then the processor will attempt to decide the best type for itself by inspecting the data value. In the majority of cases this is perfectly ok, but in this line we ensure that the type is a datetime.
The transform clause gives you access to some useful functions. Here we are using the datetime function to cast the string value in the data to a valid datetime value.
Later on for the same table you'll see this line which uses a different transform function:
is_retweet = twitter.retweeted.id (data_type: integer, transform: exists)
Here the function will return true if the JSON object has this path present, otherwise it will return false.
Now let's move down to the hashtags table mapping. You'll see this as the first line:
[hashtags :iter = list_iterator(interaction.hashtags)]
This table mapping uses an iterator to map the data from an array to rows in a table. The line specifies that any items within the interaction.hashtags array should each be mapped to one row of the hashtags table. For our example interaction, a row would be created for each of 'social' and 'marketing'.
Note that we can refer to the current item in the iterator by using the :iter variable we set in the table mapping declaration:
hashtag = :iter._value
Here _value is a reserved property representing the value of the item in the array. You can also access _path which is the relative path within the object of the value. If we were using a different type of iterator, for example over an array of objects we could reference properties of the current object, such as :iter.id.
There are a number of iterators you can use to handle different types of data structure:
- list_iterator - Maps an array of values at the given path to rows of a database table.
- objectlist_iterator - Like list_iterator, but instead is used to iterate over an array of objects, not simple values.
- path_iterator - Flattens all properties inside an object, and it's sub objects, to give you a complete list of properties in the structure.
- leaf_iterator - Like path_iterator, however instead of flattening object properties, instead flattens any values in arrays within the structure to one complete list.
- parallel_iterator - Given a path in the JSON object, this iterator takes all the arrays which are children and maps the items at each index to a row in the table. This is particularly useful for working with links.
The iterators are powerful and allow you to take deep JSON structures and flatten them to table rows. Please check out the documentation for each iterator for a concrete example.
As a further example, the following line specifies mapping for VEDO tags that appear in the tag_tree property of the interaction:
[tag_labels :iter = leaf_iterator(interaction.tag_tree)]
Here we are mapping all leaves under interaction.tag_tree to a row in the tag_labels table.
The final feature I wanted to cover is conditions. These are really useful if you want to put data in different tables or columns depending on their data type.
Although this might sound unusual, returning to our example this is useful when dealing with tags and scores under the tag_tree path.
Under the mapping declaration for the tag_labels table, there is this line:
label = :iter._value (data_type: string, condition: is_string)
This states that a value should only be put in the table if the value is a string. You'll see a very similar line for the tag_scores table below, which does the same but insists on a float value. The result is that tags (which are text labels) will be stored in the tag_labels table, whereas scores (which are float values) will be stored in the tag_scores table.
That concludes our whirlwind tour of the features. Mapping files give you a comprehensive set of tools to map unstructured data to your relational database tables. With your mapping file created you can start pushing data to your database quickly and easily.
This was quite a lengthy post, but hopefully it gave you an overview of the possibilities with the new MySQL destination. The key being that it makes it incredibly easy to push data reliably into your database. I've personally thrown away a lot of custom code I'd written to do the same job and now don't think twice about getting data into my databases.
To stay in touch with all the latest developer news please subscribe to our RSS feed at http://dev.datasift.com/blog/feed
At DataSift we pride ourselves on the power and flexibility of our filtering engine. One feature customers have asked for though is the ability to use wildcards in text-based filter conditions. Wildcards are now available on the platform to make writing powerful queries even simpler.
Firstly, it's probably worth clarifying what a wildcard is in this context. The wildcard operator on DataSift allows you to match (when filtering and tagging) against text values where there is a range of possibilities.
This can be extremely useful when you need to cover a collection of similar terms, misspellings, and incorrect or absent letter accents.
Think of the wildcard operator as another weapon in your arsenal. Although not as powerful as a regular expression, a wildcard is more easily understood and created, and incurs a lower cost. Of course in some situations the precision of a regular expression may still be the best choice.
Imagine you have a situation where you'd like to write a filter for a term, but there are multiple variations of that term. This is common in many languages, for instance imagine you'd like to filter for anything relating to printers and printing. Keywords would include:
print, prints, printer, printers, printing, printable
You could write a regular expression to cover these possibilities, but let's be honest regular expressions though powerful can be a real headache.
Instead using wildcards you can now simply write:
interaction.content wildcard "print*"
Here the * matches any character 0 or more times and would match all the words in our list.
In fact there are two wildcard characters you can make use of:
- * - Matches 0 or more characters
- ? - Matches exactly one character
The ? character is useful when looking for strings of a known pattern. For instance imagine filtering for a word that is commonly misspelt. Such as relevance:
interaction.content wildcard "relev?nce"
In fact you can give the wildcard operator a list of terms to match, such as:
interaction.content wildcard "relev?nce, relev?nt"
The wildcard operator is documented here.
Query Builder Support
You can use the wildcard operator in CSDL or the Query Builder tool. In Query Builder the option is list alongside "contain any" and the other text operators:
By default the wildcard operator is not case sensitive. You can though use the cs keyword to apply case sensitivity. For example to match common misspellings of Massachusetts:
interaction.content cs wildcard "Massachus*ts"
Often though authors are just as likely to not capitalise proper nouns, so I'd only recommend using this option when you are sure it is the appropriate implementation.
That's All For Now...
Hopefully you'll find the new operator will help you write your filters more easily than using a regular expression. Remember, it's just one of growing list of text operators that make text matching precise and powerful!
To stay in touch with all the latest developer news please subscribe to our RSS feed at http://dev.datasift.com/blog/feed
Recently I've covered the tagging and scoring features of DataSift VEDO. My post on scoring gave a top level overview and a simple example, but might have left you hungry for something a little more meaty. In this post I’ll take you through how we’ve started to build linear classifiers internally using machine learning techniques to tackle more complex classification projects. This post will explain what a linear classifier is, how it can help you and give you a method to get you started building your own.
What Is A Linear Classifier?
Until now you’re likely to have relied on boolean expressions to categorise your social data based on looking at data by eye. A linear classifier, made possible by our new scoring features, allows you to categorise data based on machine learned characteristics over much larger data sets.
A linear classifier is a machine learned method for categorising data. Machine learning is used to identify key characteristics in a training set of data and give each characteristic a weighting to reflect its influence. When the resulting classifier is run against new data each piece of data it is given a score for how likely the data is to belong in each category. The category with the highest score is considered the most appropriate category for the new piece of data.
Linear classifiers are not the most advanced or accurate method of classification, but they are a good match for high volume data sources due to their efficiency and so are perfect for social data. The accuracy of the classifier depends greatly on the definition of the categories, quality and size of the training set and effort to iteratively improve results through tuning.
For this post I will concentrate on how we built the customer service routing classifier in our library. This classifier is designed to help airlines triage incoming customer requests.
Before I start, we use Python for our data science development work. To make use of our scripts you’ll need the following set up:
- Python development environment (version 2.7 or above)
- Scikit Learn modules (http://scikit-learn.org/stable/install.html)
To build a classifier you’ll need to carry out the following steps:
- Define the categories you want to classify data into and the data points you need to consider
- Gather raw data for the training set
- Manually classify the raw data to form the training set
- Use machine learning to identify characteristics and generate scoring rules
- Test the rule set and iterate to improve the classifier’s accuracy
Let’s look at each of these in detail.
1. Define Your Categories & Targets
The first thing you need to consider is what categories are you going to classify your data into. It is essential to invest time upfront considering the categories, and to write for each a strong definition and include a few examples. The more precise and considered you can be here, the more efficient the learning process can be and the more useful your classifier will become.
Make sure your categories are a good fit for their eventual use. You must make sure that no categories overlap and that you have categories so that all possible interactions are covered. So for example you might want to include an 'other' category as we did below.
For the airline classifier, we spent a good amount of time looking into the kind of conversations that surround airline customer services and were inspired by this Altimeter study. We wanted to demonstrate how conversations could be classified for handling by a customer services team.
Thee categories we finally decided on were:
Rant: An emotionally charged criticism that may damage brand image
- “After tweeting negative comment about EasyJet, I have been refused boarding! My rights has been violated!!!”
Rave: Thanks or positive opinion about flight or services
- “Landing during storm, saved by EasyJet pilot, thanks”
Urgency: Request for resolving a real-time issue, including compensation
- “EasyJet Flight cancelled. I demand compensation now!”
Query: A polite or neutral question about how to contact the company, use the website, print boarding card etc.
- “Where can I find EasyJet hand luggage dimensions?”
Feedback: Statement about the flight or service, relating to how it could be improved, including complaints for delays without big anger.
- “Dear EasyJet, how about providing WiFi onboard”
Lead: Contact from a customer interested in purchasing a ticket or other product/service in the near future
- “EasyJet, do you sell group tickets to Prague?”
- Others: Anything that doesn’t fit into the categories above
As you might outsource the training process (explained later) to a third party or to colleagues clear definitions are extremely important.
With your categories defined, you now need to consider what fields of your interactions should be considered. For our classifier we decided that the interaction.content target contained the relevant information.
2. Gather Data For The Training Set
To carry out machine learning you will need to feed the algorithm a set of training data which has been classified by hand. The algorithm will use this data to identify features (keywords and phrases) that influence how a piece of content is classified.
To form the training set you can extract data from the platform (by running a historic query or recording a stream) and then manually putting each interaction into a category. If you choose to use our scripts use one of our push destinations to collect data as a JSON file choosing the JSON newline delimited format.
To gather raw data for our airline classifier we used the following filter:
We ran this filter as a historic query to collect a list of 2000 interactions as an initial training set. Of course the more data you are able to manually classify, the higher quality your final classifier is likely to be.
NOTE: Remember to remove any duplicates from the dataset you extract. Datasift guarantees to deliver each interaction at least once. If there is a push failure we will try to push data again, and you may receive duplicate interactions. If you are a UNIX platform you can do so at the command line:
sort raw.json | uniq -u > deduped.json
3. Manually Classify Data To Form The Training Set
Now that you have raw set of data, the interactions need to be manually assigned categories to form the training set.
As you are likely to have thousands of data points to classify, you may want to outsource this work. This is why it is vital to have well written definitions of your categories. We chose Airtasker to outsource the work. The advantage we found of Airtasker was that we could have assigned workers that we could communicate with and give feedback to.
We reformatted the raw JSON data as a CSV file to pass to our workers. The file contained the following fields:
- interaction.id - Used to rejoin the categories back on to the original interactions
- interaction.content - The field that the worker needs to examine
- Category - to be completed by the worker
Again as with the training set size, the more effort you can spend here the better the results will be. You might want to consider asking multiple people to manually classify the data and correlate the results. Even with well written definitions two humans may disagree on right category.
With the results back from Airtasker we now had a raw set of interactions (as a JSON file) and a list of classified interactions (as a CSV file). These two combined formed our training set.
4. Generating A Classifier
With a training set in place the next step is to apply machine learning principles to generate rules for the linear classifier, and generate CSDL scoring rules to implement the classifier.
We implemented the algorithm in Python using the scikit-learn libraries, and the source is available here on GitHub.
At a high level the algorithm carries out these steps:
For each interaction in the training set, consider the target fields (in this case interaction.content)
- Split into two sets, the first for training, the second for testing the classifier later
For each training interactions
- Chunk the content into words and phrases
- Build a list of candidate features to be considered for rules
- Add / remove features based on domain knowledge (see below)
- From the list of features select those with the most influence
- Generate the classifier based on the selected features, and the interactions that match these features
- Test the classifier against the training interactions and output results as a confusion matrix
Test the classifier against testing interactions put aside earlier
- For each logging the expected and actual categories assigned
- Outputting overall results as a confusion matrix
- Generate CSDL scoring rules from the classifier
The script takes in a raw JSON file of interactions (unclassified) and a CSV of classified interactions, matching the method I’ve explained. You can also specify keywords and phrases to include or exclude as an override to the automatically selected features.
See the GitHub repository for instructions on how to use the script.
The script allows you to specify keywords and phrases that must or must not be considered when generating the classifier. This allows you a level of input into the results based on human experience.
For example we specified the following words should be considered for the airline classifier as we knew they would give us strong signal:
5. Perfecting The Classifier
Your first classifier might not give you a great level of accuracy. Once you have a method working, you may need to spend considerable time iterating and improving your classifier.
You might want to extract a larger set of training data or you may wish to add or remove keywords as you learn more about the data.
The script also allows you to manipulate the parameters passed to the statistical algorithms. Refining these parameters can produce significantly different results.
I hope this post has given you some insight into building a machine learned classifier. It is impossible to give a full proof turnkey method as use cases vary so wildly.
As I said in the introduction, linear classifiers are suited to social data because of their efficiency. You may need to invest significant time perfecting your classifier, this is the nature of machine learning.
Check out our library for more examples of classifiers. We’ll be adding more linear classifiers soon!
To stay in touch with all the latest developer news please subscribe to our RSS feed at http://dev.datasift.com/blog/feed