Performing text analysis
The field of Natural Language Processing (NLP) is used for many different tasks including text searching, language translation, sentiment analysis, speech recognition, and classification to mention a few. Processing text is difficult due to a number of reasons, including the inherent ambiguity of natural languages.
There are several different types of processing that can be performed such as:
- Identifying Stop words: These are words that are common and may not be necessary for processing
- Name Entity Recognition (NER): This is the process of identifying elements of text such as people's names, location, or things
- Parts of Speech (POS): This identifies the grammatical parts of a sentence such as noun, verb, adjective, and so on
- Relationships: Here we are concerned with identifying how parts of text are related to each other, such as the subject and object of a sentence
As with most data science problems, it is important to preprocess and clean text. In Chapter 9, Text Analysis, we examine the support Java provides for this area of data science.
For example, we will use Apache's OpenNLP (https://opennlp.apache.org/) library to find the parts of speech. This is just one of the several NLP APIs that we could have used including LingPipe (http://alias-i.com/lingpipe/), Apache UIMA (https://uima.apache.org/), and Standford NLP (http://nlp.stanford.edu/). We chose OpenNLP because it is easy to use for this example.
In the following example, a model used to identify POS elements is found in the en-pos-maxent.bin
file. An array of words is initialized and the POS model is created:
try (InputStream input = new FileInputStream( new File("en-pos-maxent.bin"));) { String sentence = "Let's parse this sentence."; ... String[] words; ... list.toArray(words); POSModel posModel = new POSModel(input); ... } catch (IOException ex) { // Handle exceptions }
The tag
method is passed an array of words
and returns an array of tags. The words and tags are then displayed.
String[] posTags = posTagger.tag(words); for(int i=0; i<posTags.length; i++) { out.println(words[i] + " - " + posTags[i]); }
The output for this example is as follows:
Let's - NNP parse - NN this - DT sentence. - NN
The abbreviations NNP
and DT
stand for a singular proper noun and determiner respectively. We examine several other NLP techniques in Chapter 9, Text Analysis.