iOS Custom Keyboard, Suggestions from txt file - ios

I'm making a custom keyboard for lawyers, and trying to load law related words in suggestion/prediction bar on top the keyboard based on what user types. Just like in stock keyboard. I have searched around but did not find any concrete answer.
I want to display suggestions of law related terms that I have in a txt file, all words are sorted alphabetically.
DEMO
Here is what I have tried:
UILexicon
let myLexicon = NSMutableDictionary()
self.requestSupplementaryLexiconWithCompletion { (theLexicon: UILexicon!) -> Void in
let lexiconEntries = theLexicon.entries
// Completion handler
for item in lexiconEntries {
self.myLexicon.setObject(item.documentText, forKey: item.userInput)
}
}
This code just gives 23 nil objects.
UITextChecker This is an iOS class that is designed to spot spelling errors, which makes it perfect for knowing if a given word is real or not. This is seems to be mainly for autocorrection, not for suggestion. Correct me if I'm wrong please.
I cannot somehow make sense out of these two classes.
How do I tell custom keyboard, "Hey if user enters "V" show the top 3 words that start from V, then if user enter a, fill the suggestion bar with words that start with "Va" and so on.
EDIT: Looks like someone ran into same problem. Here is a quote how they solved it, I will update with code once I finish figuring this out myself.
However, this was far from the truth - in fact, Apple do not allow access to their dictionary full stop, only offering a UILexicon class instead as stated in their docs:
Make use of this class, along with a lexicon of your own design, to provide suggestions and autocorrections as users are entering text.
As it turns out, the UILexicon class only really contains contact names along with any shortcuts (like the default On My Way!) defined on the device. So before writing the logic for a keyboard, you first have to implement your own autocorrect library.
We browsed through a few external projects to see if we could include them in the keyboard - most notably Hunspell, which is used by OpenOffice, and Presage, an intelligent predictive text library.
I spent a long time integrating the C++ libraries with the code, but in the end, in order to keep complexity down, we opted to use a combination of UITextChecker (which provides some basic corrections) and our own custom dictionary, containing a few commonly mispelled words.
Link to the Article
Thanks!

You have to implement your own autocorrection system. UILexicon will only give you shortcuts the user has set up, words that they have added to the iOS Dictionary, and names of contacts. It has no awareness of any words that you yourself provide, whether in a txt file or in any other form.
If you want to use the TOMSSuggestionBar, it appears from the sample code that the onus is on you to convert your txt file into a core data model, and indicate to the suggestion bar how it is to interpret the contents of that model. You may also want to implement the data source protocol to get more fine grained control over the suggestions.
Autocorrection and next word prediction are not solved problems; I suggest you do your own research and find the solution that is best suited to your goals.

Related

Swift: Is there an OS call in macOS/iOS similar to NSSpellCheck but for getting a word definition from the dictionary,

I can spell check words like this:
import Cocoa
let words = ["happy","flingey","bookinto"]
let spellCheck = NSSpellChecker()
for word in words {
if spellCheck.checkSpelling(of: word, startingAt: 0).length == 0 {
print("Word: \(word) is good")
} else {
print("Word: \(word) is bad")
}
}
However, the output is:
Word: happy is good
Word: flingey is bad
Word: bookinto is good
There are some (only some) combinations of valid words, like "book into" but with a missing space which the spellchecker still accepts as valid.
I suspect I might be able to tweak how NSSpellChecker operates to eliminate the issue, however, if I can get the next part working, I won't need to worry about this one.
For a later part of the App (a quiz) I want to let the user see a definition of the word within the App. Something like this
import Cocoa
let definer = NSDictionaryDefinition()
let word = "Happy"
if let meaning = definer.getDefinition(for: word) {
print("\(word): \(meaning.getText())")
}
Which would then give:
Happy: feeling or showing pleasure or contentment
NSDictionaryDefinition is just something I made up as the kind of API name I was expecting to find, and obviously given how macOS APIs work it won't be as easy as that, it will probably be within a collection within a container within a callback, or whatever, but I'm happy dealing with all that when I get there, my problem is I can't find any system API that remotely resembles something like NSDictionaryDefinition.
My web search is hampered by the fact that Dictionary is also a Swift Collection Type, so I get hundreds of false hits, but even trawling through many of those, I didn't get lucky.
So next I started looking in the API references on the Apple developer site, which is just as painful because it's so vast and sprawling with little by way of a route map through it, but even then I couldn't locate anything.
Does anyone know if this system API exist for Swift (or even Objective-C). I've always assumed it did because the dictionary comes as standard in macOS and most things in macOS are programable via an API. It needs to be a system native API to work offline and also localise to the user automatically.
Please don't suggest third party resources, as I notice a previous question about that got marked as off-topic here. I'm assuming this question is on topic but I genuinely struggle to gauge it.
Thanks.
Something like this exists on iOS. Look into UIReferenceLibraryViewController. It doesn't give you back the definitions as strings, but you can check if there is a definition for a word and you can show the system-provided dictionary view.

UITextView, with spell checking, how to use `ignoreWord`?

Regarding the spell checking in iOS, it's possible to tell the checker to ignore a word (or learn a word),
https://developer.apple.com/documentation/uikit/uitextchecker
func ignoreWord(String)
Tells the receiver to ignore the specified word when spell-checking.
- Apple doco
Say I have a UITextView which opens. I want spell checking On.
I know the user may type "fattie" which would get the red underline.
How do I tell that text view in that instance, to, ignore "fattie" ?
An obvious use case ...
User is typing in "#tag" type friends; in our data of course we know what all the tags are, it's absurd they get marked as spelling errors.
It seems incredible one can't just say "don't underline these words - - list".
Code example ....
So we have
var t: UITextView
and then, there must be "some way" to:
yourTextView.something->something.textChecker.ignoreWord("fattie"
.. some way to get to the text view's textChecker instance! How ?!?!
Partial answer: I just stumbled on to that, bizarrely, you can just call
UITextChecker.learnWord("fattie")
UITextChecker.learnWord("blahdee")
from, apparently, just anywhere in an app.
However this raises many issues,
• How to call the 'ignore' one, which seems better
• That one still makes the user tap the annoying, stupid, "in quotes" OK box in the suggestions bar - it seems to have not really "learned" anything
• Disturbingly, I think this goes for the "WHOLE PHONE". I only want it in that instance of the user using that text view.
A mystery!

OCR validations with Rails building a business card scanner

My goal is to write a validation class for Rails that is capable of using an OCR recognised text from a business card and is able to detect string snippets and assign them to the correct attributes. I know this cannot be probably 100% perfect but I want to get as close as possible. Here is my approach so far:
I scan business cards via jquery's navigator.mediaDevices
I send the scanned image to a third party API Service, called OCRSpace (a gem is available here: https://github.com/suyesh/ocr_space)
I then get a unformatted array of recognised text snippets back, for example:
result = [['John Doe'], ['+49 160 123456'], ['Mainstr. 45a'], ['12345 Berlin'], ['CEO'], ['johndoe#business-website.de'], ['www.business-website.de']]
I then iterate through the array and do some checks, for example
Using the people library (https://github.com/mericson/people)
to split the name in firstname and lastname (additionally the title
or middlenames) Using the phonelib library
(https://github.com/daddyz/phonelib) to look up a valid phone number
and format it in an international string
Doing a basic regex check on the email address and store it
What I miss now is:
How can I find out what the name-string would possibly be? Right now I let the user choose it (in my example he defines "John Doe" as the name and then the library does the rest). I'm sure I would run into conflicts when using a regex as strings like "Main Street" would then also be recognized as a name?
How do I regex a combination of ZIP-Code and City name? I'm not a regex expert, do you know any good sources that would help? Couldn't find any so far except some regex-checkers in general.
In general: Do you like my approach or is this way too complicated? And do you know some best-practices that look better?
Don't consider this a full answer, but it was too much to make it a comment.
Your way of working seems Ok but I wouldn't use the OCR Service since there are other ways , Tesseract is the best known.
If you do and all the results are comparible presented it seems not too difficult since every piece of info has it's own characteristics.
You can identify the name part because it won't have numbers in it, the rest does, also you can expect to contain it "Mr." or "Mrs." or the such and not "Str.", "street" and so on. You could also use Google Maps to check for correct adresses, there are Ruby gems but have no experience with them.
Your people gem could also help.
You could guess all of this, present the results in you webpage and let the user confirm or adjust.
You could also RegExpr the post-city combination by looking fo a number and string combination in either order but you could also use a gem like ZipCodes to help.
I'm sorry, don't have the time now to test some Regular Expressions now and I don't publish code without testing.
Hope this was some help, success !

Amending Translation Values in CRM 2011 Outside of the Translation Files

I have received a requirement to amend the translations of the 'Add new..' and 'Add existing..' buttons in French. A typical default button is shown below:
The desired new translations are essentially to remove the gender neutrality (if my rather poor school French is correct!). So rather than 'Ajouter un(e)' we have 'Ajouter un'.
Unfortunately these button labels are not included in the standard translations file (Settings -> Customisations -> Export Translations).
Are there any other options available? Given the nature of the requirement, I am happy to consider pretty much any kind of hack. CRM must be generating these values from somewhere, any ideas where they are hidden?
Note: I profoundly disagree with this requirement for fairly obvious reasons and I am sure most people reading this question will as well. However, requirements are what they are and sometimes customers/clients just will not budge. This is one of those cases.
You can define the labels for CRM Ribbon controls. See Use Localized Labels with Ribbons.
In order to make this work, you need to export the ribbon definitions and identify where these controls are defined.
You should be able to define a CustomAction to override the definition of the existing control but include the label text you want to have instead of the default label text. See Define Custom Actions to Modify the Ribbon.
If you are already experienced with customizing the ribbon this shouldn't be too hard, but there is a signifcant learning curve when it comes to customizing the ribbon.

auto_complete_for: prevent the first item from being auto-selected

The auto_complete_for dealio from script.aculo.us is great an all, but is there a way for me to selectively disable the fact that it always auto-selects the first item in the list?
The problem is that, if I want to type my own entry that is new, and novel, I don't want the first item in the list to be auto-selected. The reason is because when I TAB out of the field, it selects, and fills the text box with that first item.
I got around that, somewhat, by making the first item in the list the same as what I'm typing, but that's not perfect either, because the auto_complete list doesn't always update with every keystroke, depending on how fast I type. I've tried setting the list refresh rate to the lowest value (1 millisecond) but no go.
What I really want is an option in "auto_complete_for" that doesn't select that first item at all - the same way that Google Instant doesn't automatically select the first suggested search phrase - you have to arrow-down to select one.
Maybe I can do this via an HTML option that I'm missing?
Looking at the source, there doesn't appear to be an option for that, but I bet if you changed line 284 of controls.js to this.index = -1; it would do what you want.
Otherwise, it might be time to look for a different autocomplete widget.
If your requirements are too far away from the available plugin, then I guess there is no point in tinkering around. Its best to write your own JS code.
You might want to consider this: https://github.com/laktek/jQuery-Smart-Auto-Complete
or this : https://github.com/reinh/jquery-autocomplete
I'll add another alternative that works great with Rails 3:
http://github.com/crowdint/rails3-jquery-autocomplete
I recently implemented auto complete for more than a field for Rails 2.0.2.
The plugin I used is:- https://github.com/david-kerins/auto_complete . Not sure if it supports Rails 3.
I have also encountered issues on implementing the above scenario and have posted questions( Implementing auto complete for more than one field in Rails ; Implementing a OnClick kind of functionality and formatting wrt Rails Partial-Views ) on stackoverflow for the same, I have been lucky on getting things working for me based on my requirement.
Kindly refer to these questions, they might have relevance to your requirement.

Resources