Why is Brainjs not learning and giving random characters? - javascript

I am trying to make a neural network with Brain.js that can recognize an ingredient from a sentence.
For example the input:
large carrots, cut into half-moons
should output:
carrot
So I have a list of 200 ingredients with 3 different sentences for each.
But when I train and test the LSTM it only outputs random characters.
I also tried to use NeuralNetwork instead of LSTM, but it didn't work any better.

Related

Can I configure Brain.js to have it output an array of numbers between 0 and 1?

I'm training an LSTM to try to predict the next most probable keys of (a-z) after each keystroke. (and show only the 8 most probable successor keys to the user, which should result in an 8 key keyboard, like I did earlier here but that is without brain.js)
The network is trained with a chunk of text from which is taken all sets of two letters as input for the network. The corresponding output is the letter following the set of two.
I tried different config settings and training data models:
Train the network with strings -> it outputs one character (which is not enough)
Train the network with a set of weighted arrays of 27 items (one entry for each letter + space) -> it outputs a string of 26 zeros and a 1 - the position of the 1 corresponding to a letter. Although I trained it with an array as output. -> Still just one letter
When I run the network with two letters as input, it outputs one letter - as I trained it to.
Now how could build/model my training data to train it to output an array of the most probable successor letters.
Is this possible at all with brain.js?
Your initial training data should be based from a long text, from which you can extract the probabilities of the next letter for each letter. To prepare this training set you don't need NN, just pure math. Then train the network with this values (input one char, output one array of 26 positions with different probabilities for each char) and the predicted values should match with the "style" of the text as reference.
To be a "live" example feeded with the input of the user, you should re-train your network with every key stroke. This would probably be computationally expensive.

Feet inch regex of "universal" combinations

So, there are plenty of questions and answers about standard ways of parsing feet and inches with regex. A casual search led me easily to:
Link1
Link2
But these options are very specific with what they are looking for and are not versatile enough for what I would want an expression to do. Desirably, I wanted a regex to match all ft inches dimensions like those below:
Example matches (12 and 3 and 1/4 are just examples, matches should not be by line (not just $)):
12' 3"
12 feet 3 inches
12 ft 3 in
12 ft. 3 in.
12' -3"
-12 feet 3 inches (Should capture the negatives)
12'- 3" (Should not mistaken as -ve 3, but not necessarily needed to be processed with regex)
12' 3 1/4"
12' 3.25"
12 ft 3 1/4 in.
12' (Need to capture "single" dimensions provided that it is not logically matched to next or prior)
3"
3 1/4 inches
-3.25"
3 1/4feet
Desired non-matches
12' 12'
3inches 12'
3 inches 3 inches (need to match separately = matches 3 inches twice)
3 - 2ft (need to be able to exclude the 3 and only match -2ft)
I started out trying to write something and came up with:
/(-*[\d .]+(\/\d)* *){1}(?:FEET|FT\.*|'|INCH|INCHES|IN\.*|")+(?:[ -]*)/gi
But it's too greedy and would accept 12' 12' as one thing. So I started doing some exclusions like what they did within Link 1 up there, but I couldn't get it such that it will work. I tried this:
(-*[\d .]+(\/\d)* *){1}(?:FEET|FT\.*|'|INCH|INCHES|IN\.*|")+(?:[ -]*)(?!=(-*[\d .]+(\/\d)* *){1}(?:FEET|FT\.*|')+(?:[ -]*)){1,2}
and also
((?<!((-*[\d .]+(\/\d)* *){1}(?:INCH|INCHES|IN\.*|")+)))(-*[\d .]+(\/\d)* *){1}(?:FEET|FT\.*|'|INCH|INCHES|IN\.*|")+(?:[ -]*)(?!=(-*[\d .]+(\/\d)* *){1}(?:FEET|FT\.*|')+(?:[ -]*)){1,2}
and I have tried some other approaches, such as
(([-*\d+ *])+(?:FEET|FT\.*|')+(?:\s*-)*){0,1}((\s*\d+[./]*\d*\s*)+(?:INCH|INCHES|IN\.*|")+(?: )+){0,1}
and it works the way I desired, but it also matches a lot of empty strings.
Maybe I am just not looking hard enough or searching the right terms, but I don't think I came across an old post that has something as versatile as I would have liked. If there has been an answer previously that does exactly what I would like, feel free to point it out for me. Thanks!
I came up with this regex based on the test cases provided:
/(?:-[ \t]*)?((?:\d+(?:\.\d*)?|(?:\d+[ \t]+)?\d+[ \t]*\/[ \t]*\d+)[ \t]*(?:[']|feet|ft\.?)(?:[ \t]*(?:-[ \t]*)?(?:\d+(?:\.\d*)?|(?:\d+[ \t]+)?\d+[ \t]*\/[ \t]*\d+)[ \t]*(?:["]|inch(?:es)?|in\.?))?|(?:(?:\d+(?:\.\d*)?|(?:\d+[ \t]+)?\d+[ \t]*\/[ \t]*\d+)[ \t]*(?:["]|inch(?:es)?|in\.?)))/g
Regex101
Basically, the regex is constructed as such:
(?:-[ \t]*)?: Optional negative sign
(?:\d+(?:\.\d*)?|(?:\d+[ \t]+)?\d+[ \t]*\/[ \t]*\d+): Matches whole number (e.g. 10), real number (e.g. 3.45), or fractional number (e.g. 3 1/4, 10/4). Let us denote this part as <number> so that we can see the bigger picture
<number>[ \t]*(?:[']|feet|ft\.?): Feet part. Number and unit optionally separated by space
<number>[ \t]*(?:["]|inch(?:es)?|in\.?): Inch part. Feet part. Number and unit optionally separated by space
(<feet part>(?:[ \t]*(?:-[ \t]*)?<inch part>)?|(?:<inch part>)): Matches string with feet part and optional inch part (optionally separated by hyphen), or only inch part
The code assumes everything on a single line - if you want to match across lines, replace [ \t] with \s.
The regex will pick up valid substrings in non-matching cases - it only cares what it matches is valid, it doesn't care about the context of the match.

How can I get a massive text file into a JavaScript array?

I'm using a list of words with positive and negative sentiment from AFINN to do some text analysis.
Problem is, the list comes in a .txt file in the following format (word on the left, pos vs neg index at right):
casualty -2
catastrophe -3
catastrophic -4
cautious -1
celebrate 3
celebrated 3
celebrates 3
celebrating 3
To work with it, I need it in the following format:
var array = [{word:"casualty",score:-2},{word:"catastrophe",score:-3},{word:"catastrophic",score:-4}, etc etc]
I'd actually prefer to do this once with a shell script, rather than in the browser. Which is why I'm thinking Node.js could come in handy here. But I'm not very familiar with Node.
Direct link to the zip containing the raw text files.
In case you don't really care about how to read text into a javascript array, and you just need AFINN in JSON, I just found a version here.

Matching people with similar rating?

Suppose you have a collection of people with a sample size of 10,000. Each person in the collection has a rating score in the form of a winning percentage: 0.00 < x < 1.00.
Currently my system randomly picks two people and matches them together. I would like to improve matchmaking by pairing up people who have a high winning percentage with others that have a high winning percentage.
Have you ever played World of Warcraft arenas? Typically if you are in 2000 bracket you are matched with teams who are in 2000 bracket. If you are in 1500 bracket, you are matched with people who have similar ranking.
What is the the easiest way to implement such matchmaking system? While implementation doesn't really matter, even a pseudo-code would help, but I would greatly appreciate if you can guide me in the right direction using JavaScript, Backbone, and Underscore as a toolbelt.
Place everybody in a balanced binary tree (if you'll be frequently adding and removing people) or in a sorted array (if the data set is more or less static), using their winning percentage as the sort key. To match somebody, locate them in the tree or array, then match them with somebody within, say, +/- 10 rankings using a random number generator (e.g., if you're using an array and the person is in the ith index, then match them with the person at the i + rand(10) + 1 index).
I'm assuming that somebody's winning percentage will only change by small increments, which means that updating the tree or array will usually be a constant time operation since you'll just be swapping adjacent elements.

Getting the first dollar amount using JavaScript with Razor and .Net

I asked a similar question on how to do this on the server side (SQL), however it makes more sense to accomplish this on the client side, based on the app architecture.
I've got a MVC3 app with Razor on the .Net framework, where I have model data available that I would like to parse and return the first dollar value from a given string using Javascript / regex,
For example, each of the following lines represents a sample data set:
Used knife set for sale $200.00 or best offer.
$4,500 Persian rug for sale.
Today only, $100 rebate.
Five items for sale: $20 Motorola phone car charger, $150 PS2, $50.00 3 foot high shelf.
I've seen a few issues already including the # in JS and a few other pitfalls I would like to try to avoid.
Thanks.
var m = line.match(/\$[0-9,]+\.?\d*/);
if (m)
return m[0];
should give you a hint. This Regex returns you a string which consists of a dollar sign, some numbers or commata, and optional a dot another few numbers behind it. You might want to limit its wideness (only 2 decimals, not starting with zero etc).

Categories