Saturday, March 20, 2021

Building a chat application on Android development platform: (continued...)

 Addendum: 

Firebase Cloud messaging Android extension provides message broker abilities for a client application and it works well on Android.  Client applications use the FirebaseMessaging API and must have Android Studio 1.4 or higher. Firebase can be added to the project if the device or emulator has Google Play services installed.  


The Android app can be connected to the Firebase using Option 1. Firebase console or Option 2. Android Studio Firebase assistant. The Option 1 requires the Firebase console to download the Firebase configuration files and then moved into the Android project. The Firebase configuration file can be used when the Google services plugin 'com.google.gms:google-services:4.3.5' can be added to the Gradle file. The Firebase SDK can be added to the application with the ‘implementation platform('com.google.firebase:firebase-bom:26.7.0')’   The Option #2 uses the Firebase Assistant which has preconfigured workflows to add a Firebase product to the application. 

If Kotlin is used in the Android application, the Kotlin extension (KTX) libraries need to be added.  


Any application written using Firebase needs to be registered. Once the configuration file is added, the application manifest also needs to be edited. A service that extends the FirebaseMessaging Service needs to be added. This Service extends the base class to add functionality for receiving notifications in the foreground, receiving data payload, and sending upstream messages. Metadata elements can be optionally added to the manifest to set icon and color. Notification channels can be added on a per queue basis. 


When the application is started, during initial time, the SDK generates a token for the client application. This new token is used with the onNewToken overridden method. Tokens can be rotated so the application must always retrieve the latest updated registration token. The current token can be retrieved using the FirebaseMessaging.getInstance().getToken() Token autogeneration can be prevented by disabling the Analytics collection and Firebase Cloud Messaging auto initialization. 

 

The storage can also be provisioned via Firebase. Cloud Firestore is just right for this case. It is a flexible scalable database for mobile, web and server development from Firebase and Google cloud. One of the primary advantages of this database is that it caches that data that the application is using which makes it easy to access the data even when the device is offline. 


It is a cloud hosted NoSQL database, and it is accessed directly via native SDKs as well as REST APIs. Data is stored in documents; documents are kept in collections and the data primitives support complex objects. Realtime listeners can be registered to retrieve just the updates instead of the entire database. 

 

 

Friday, March 19, 2021

Building a chat application on Android development platform:


Introduction: The chat application is one of the most used applications on the mobile device. It allows multiple parties to exchange messages with one another, does not require any treatment to the messages they exchange, and can be inherently store-less architecture.  This article elaborates on the development of such a mobile application on the Android platform.

Description: Mobile applications are increasingly becoming smarter with one of the two popular mobile platforms that have their own app stores and find universal appeal among their customers. The Android platform comes with support for Java programming and the tools associated with software development in this well-established language. The Android studio supports emulator mode running and debugging of the application that thoroughly vets the application just like it would be tested on a physical device. Modern Android development tools include Kotlin, Coroutines, Dagger-hilt, Architecture components, MVVM, Room, Coil and FireBase. The last one is a pre-packaged, open-source bundle of code called extensions to automate common development tasks.

The Firebase services that are required to be enabled in the Firebase console for our purpose include Phone Auth, Cloud Firestore, Realtime Database, Storage and Composite Indexes. The Android Architecture Components include the following: a Navigation Component that handles in-app navigation with a single Activity, LiveData which has data objects that notify views when the underlying database changes, ViewModel which stores UI-related data that isn't destroyed on UI changes, DataBinding that generates a binding class for each XML layout file present in that module and allows you to more easily write code that interacts with views and declaratively binds observable data to UI elements, WorkManager which is an API that makes it easy to schedule deferrable, asynchronous tasks that are expected to run even if the app exits or the device restarts and Room which is an Object Relational Mapping between SQLite object and POJO. 

The Dependency injection is handled via Dagger-Hilt which incorporates Inversion of control via Dagger Dependency Injection and the Hilt-ViewModel which injects dependencies to ViewModel.

The Firebase extensions support cloud messaging for sending notification to client application, the Cloud Firestore for flexible, scalable, NoSQL cloud database to store and sync data, Cloud Storage for store and serving user-generated content and Authentication for creating account with mobile number. 

The Kotlin serializer converts specific classes to and from the JSON.Runtime library with core serialization API and supports libraries with different serialization formats. Coil-Kt is used as an image loading library for Android backends by Kotlin Coroutines. 

Please note that the application can be run with little or no store requirements and relying exclusively on a message broker and Paxos algorithm as described herein.  The above technologies leverage the latest trends in mobile application development.

Conclusion: Creating a webchat application is easily done with pre-packaged extensions and native Android development tools and framework. The components just need to be selected and wired together. Sample Android application is included here.


Thursday, March 18, 2021

Limited dimensions and sequences in the discovery of semantic embeddings (continued)

We discuss just the RNN encoder-decoder technique that is widely used for text mining and is easy to apply with a library such as TensorFlow. The use of the library does not necessarily standardize the technique, but it proves to be common usage. 

This technique requires some preprocessing of text and other upstream and downstream activities that are typical to any techniques that work with text and in this case, we start with two dictionaries – one for index2word and another for word2index. The index2word is an alphabetical ordering of the top frequent words from the vocabulary. The word2index is the inverted set of words from the ordering to their rankings. The frequency distribution for a vocabulary is never discarded. It has an interesting trend that is typically seen with any association or market-basket analysis. The frequently occurring words are in the top 10% of the overall vocabulary. We mention association only because we will need vocabulary in both the x and the y directions when building the encoder-decoder model. 

The encoder and decoder sequences as well as the labels representing the real outputs are of fixed size and format so their data structures can be initialized upfront and populated as the model is trained and tested. Each neuron in the RNN can be a Long-Short-Term-Memory/LSTM or a Gated Recurrent Unit (GRU) and it remembers useful information, discards unnecessary information, and emits relevant information at each timestep. The cells are stacked together in an n-layered model. TensorFlow has a high-level function called embedding_rnn_seq2seq which creates a model and does the word embeddings internally using the constructs just described. During training, the input to a timestep for the decoder is taken from the label sequence. During testing, another instance of the model is initialized where the decoder takes the output of the previous time step as input to the current timestep. Training is defined as a method that runs the train operation of the library’s model and minimizes a loss function. It runs for a given number of epochs or iterations. If the training is interrupted, it can resume from the last saved checkpoint. The model is periodically evaluated on the validation set. The predictor does a forward step of the most probable word returned by the model.  

The cells are stacked in layers and each layer alters the interpretation to some extent. If there are several layers, the information encoded in a sequence can be comprehensive, but the data structures become much larger, and the operations require more time per epoch. 


  

Wednesday, March 17, 2021

Limited dimensions and sequences in the discovery of semantic embeddings (continued)

 Dimensions are not the only ones affected by limitations. Sequences are affected too. A sequence is a series of words. For example, a series of words across sentences might reappear in another context. These sequences can even be compared with the identifiers of any sortable data maintained in a separate table. Sequences became popular with Recurrent Neural Networks (RNNs) are a special kind of neural network which processes sequences rather than symbols that constitute the sequence. This technique does not need to know what the parts of the sequence represent whether they are words or video frames. It can infer the meaning of those symbols. When raw data is shredded into sequences, this technique keeps state information per sequence that it infers from that sequence. This state is the essence of the sequence. Using this state, it can simply translate input sequences (text) to output sequences (summary). It can also be used to interpret the input sequence to generate an output sequence (like a chatbot). The RNN encoder-decoder model was proposed by Bahdanau et al in 2014 and it can be used to write any kind of decoder that generates custom output.  

 

The sequence length for this technique employs about 10 to 20 words and each constituent of the sequence is processed in a single timestep, so it is best to sample them from a few sentences. This reduction of the sample size for a selection of sequence can be considered as restrictive as the curse of the limited dimensions but it is also similarly overcome by using more batches and a collection of text rather than any one selection. Sometimes it is helpful even to pick out only the first few sentences from each sample text in a corpus.  

 

The restrictions on dimensions and sequences are not necessarily introduced by resources. Cloud computing has made it easy to store large amounts of data even if it were per iteration. The algorithms for their processing have an inherent limitation of being quite intensive even for a size of a hundred. One technique called memorization that saves the results of some computations has already provided benefit in undoing some of the repetitions in these computations. 

 

Sequence size behaves differently from feature dimensions. While the addition of dimensions improves the fidelity of the word vectors, the elongation of sequences changes its nature often making it more nebulous than before. We must look at how the sequences are used in order to appreciate this difference.  

Tuesday, March 16, 2021

Limited dimensions and sequences in the discovery of semantic embeddings:


Words are all that are used to describe the thoughts of a person. Yet their relative significance is hard to find since everyone uses them in their own style. A formula to bring out the salient topics or keywords in a text has been an exciting journey and more so this last decade.  

 

Mikolov’s 2013 word2vec model introduced a neural net technique that used a classifier to discover words that occur frequently together which brings out the latent meaning in the text. Similar words have similar vectors. This hidden matrix of feature weights that were assigned to the words was called word embeddings. The matrix was limited because the words represented as a vector formed with a set of other words as features could not be arbitrarily large otherwise the sparse matrix would become even more unwieldy and the iterations even in a single layer of hundreds of neurons would become expensive.  A corpus of text also dramatically increases the input so this adds to the computations. Fortunately, the curse of limited dimensions is overcome by choosing several training samples per class to be at least five to ten times the number of features. 

 
 

Dimensionality reduction is a technique that eliminates noise by choosing important features and lowers cost without significant loss of information. It can use both linear and non-linear transformations. Given a data set X with n data points that needs to be reduced to d dimensions, a linear transformation proceeds by selecting V data set that have d dimensions corresponding to each other and matrix multiplying their transpose to each of the points in the X data set to get Y. Since the V has d dimensions the resulting linear transformations also have d dimensions. Non-linear dimensionality reduction techniques may even learn an internal model within the data. This is the case with a technique called manifold learning. In this case, a high dimensionality data set may be projected onto a smaller dimension set while trying to preserve the structure of inter-point distances from the high dimensional space in the lower dimension projection. It is called non-linear because the mapping cannot be represented as a linear combination of original variables.  


(...to be continued)

Monday, March 15, 2021

 Automating the publishing of puzzle books such as Sudoku and Word Search: 

Problem statement: Puzzle books sell for timepass activities. They are considered commodity items, are highly labor-intensive, available to purchase from a store or even download for free, widely varying in quality and content, and usually have a poor retention span. How do we make it easy to generate, tailor, and more interesting puzzles for the end-user? Can we disrupt this staid market with logic and automation? This article attempts to elaborate on an affirmative note.  

Solution: There are a few interesting patterns to observe with puzzles such as Sudoku and Word Search:  

  1. They have definite unequivocal answers which are easy to tally against, once the solving is done.  

  1. The same solution can be used to create many puzzles simply by selective hiding or nominal changes in positions. A sudoku key can be used to cut different puzzles by hiding different positions. A Word search can realign different orientations of the hidden words without replacing them to create different puzzles. The automation for generating different puzzles depends only on randomizing the selection which is easily programmable. Refer to the sample application program included for Sudoku and Word Search in the references.  

  1. The content of the word search is a potpourri of words from a dictionary. This is hardly interesting to the end-user. On the other hand, the domain of a person’s profession or her interests leads to more enticing puzzles to solve. The candidate words for a puzzle can also be mined from the person’s handheld device or data source which is both temporally and spatially closer to the user’s interests. Even proper nouns such as names of friends, things, or places can be included in the word search.  

  1. Puzzles available to download from an Application Store on a mobile device compete with one another on the look and the feel of the puzzles. If they used content from a source that is specific to the user, it would be disruptive to the market. A users’ interests can be learned with natural language processing from the public web pages visited. If the user allows access to files on her device such as downloads, then they provide a better data source. Data may arrive continuously which allows us to batch until enough candidates have been collected.  

  1. The organization of candidates for both kinds of puzzles – Sudoku and Word Search can be such that they are done once and stored in a single data store available universally. Then it can be used to dynamically cut the puzzles.  

  1. The logic to cut the puzzle and present it can be packaged in an application that can be downloaded, installed, and run on the buyers’ device. The content and the recommendations for the candidates can be organized in the backend which could be hosted in a cloud computing environment. 

Conclusion: The process for creating these puzzles is automatable. Intelligence to present an enticing puzzle to the user can also be incorporated.