How to build grammar in Universal Windows Platform? - win-universal-app

Coming from a WPF background, I was trying to find a counterpart of GrammarBuilder (in System.Speech.Recognition) to implement choices, repeat and priorities in a Universal Windows app development. But it appears more things are different in Windows.Media.SpeechRecognition than I expected.
It appears Grammar is called Contraint in this Windows.Media.SpeechRecognition namespace, which makes sense. But as I look for the replacement for GrammarBuilder and SpeechRecognitionEngine.LoadGrammar, the closest Constraint I can find is SpeechRecognitionListConstraint. The MSDN documentation says there are Commands and Probability.
But, how can we add choices and repeats into the Commands (string[])? Something like:
var builder = new GrammarBuilder();
builder.Append("What is the", 0, 1);
builder.Append("forecast for ");
builder.Append(dayChoices, 0, 3);
or in SRGS
<rule id="forecast">
<item repeat="0-1">What is the</item>
<item>forecast for</item>
<item repeat="1-">
<one-of >
<item>Monday</item>
<item>Tuesday</item>
<item>Wednesday</item>
<item>Thursday</item>
<item>Friday</item>
<item>Saturday</item>
<item>Sunday</item>
</one-of>
</item>
</rule>
The example code in the demo app only has string[] as commands.

Related

How should I do using multiple different two languages in NodeJs?

Can someone help me and can explain about this matter?
Currently, I'm just building a blog which I used which nodejs. In my projects, I want to use and display the two different languages which my local language and English.
As I showed up above like that website when I click change languages without showing like this example.com/mm. I'm just want to display like example.com without /mm or /en.
Example url: https://www.mmbusticket.com/
I'm not familiar with PHP. I'm the big fun of Nodejs.
How I have to do so for this case and which packages should I use for nodejs?
Thanks.
An option for you is the i18n module, and you can find similar options in many frontend frameworks as well. (You'll see this concept in app development too.)
The idea is that you have a directory with "locales" (the languages), each in a JSON file. The keys are the same in all locales. Like:
locales/en.json
{
"hello": "hello",
"greeting": "hey there, {{name}}"
}
locales/mm.json (used google translate, forgive me : )
{
"hello": "ဟယ်လို",
"greeting": "ဟေ့ဒီမှာ {{name}}"
}
In your app you'd do something like i18n.localize("hello") and depending on your current language setting (maybe passed in a cookie to the server if server-rendering, or set on the frontend page for client-side) you'll get the response.
Variables can be done above like i18n.localize(['greeting', {name: "clay"}]) and that will fill in the passed parameter name into the string defined at greeting. You can typically do nesting and other cool things, depending on the library used.
Just note that you end up using these special "key strings" everywhere, so the code is a little messier to read. Name those keys wisely : ) And if you want to translate the entire contents of your blog, that's a different service entirely.

NLog webservice target, slack icon based on log level

I'm new to NLog, and fighting with finding the solution to what I'm going for.
I'd like warning and up to get pushed to a slack channel with log level specific icons. I have got that working, technically. I have 2 pairs of targets/loggers that are hard coded to catch warnings only, and errors and above, and have 'warning' or 'error' icons hard coded into the layout template.
<target xsi:type="WebService"
name="slackWarningTarget"
url="https://hooks.slack.com/services/xx/xx/xx"
protocol="JsonPost"
encoding="utf-8"
>
<parameter name="text" type="System.String" layout=":warning: ${machinename} ${message}"/>
<parameter name="channel" type="System.String" layout="xx"/>
</target>
and
<logger name="*" minlevel="Warn" maxlevel="Warn" writeTo="slackWarningTarget">
Is there a way to better accomplish this? I want warning level to map to :warning:, etc.
I'd also like some info level stuff pushed there as well. To do this I created a named logger that always logs to the slack channel, but it results in duplicated messages if they are warning or above.
<logger name="noticeLogger" writeTo="slackInfoTarget" />
I'd guess there is a much more elegant way to do both of these things than i have come up with on my own.
You could do that from config with a ${when}, e.g.
layout="${when:when=${level}=='Warn':inner=\:warning\::else:${when:when=${level}== 'Error':inner=\:error\::else:todo}}"
but it get clumsy fast.
Another option is to register a custom layout renderer:
// register in the start of your program (e.g. main, app_start)
// usage ${slackIcon}
LayoutRenderer.Register("slackIcon", logEvent =>
{
if (logEvent.Level == LogLevel.Warn)
{
return ":warning:";
}
if (logEvent.Level == LogLevel.Error)
{
return ":error:";
}
return ":other:";
//etc
});
See How to write a custom layout renderer

HWND handle being returned via FindWindowW differs from top-level parent

I'm trying to create a utility that will selectively hide and show windows based on pre-assigned hotkeys and I'm working with the Windows API code.
I use a FindWindowW call to get a handle to a window as a test (in my case, a window with the text "Calculator - Calculator", which matched an open calculator window) and use that handle in a ShowWindow function.
Code below:
var user32path = 'C:\\Windows\\System32\\user32.dll';
function TEXT(text){
return new Buffer(text, 'ucs2').toString('binary');
}
var user32 = new FFI.Library(user32path, {
'FindWindowW': ['int', ['string', 'string']],
'ShowWindow': ['int', ['int', 'int']],
'ShowWindowAsync': ['int', ['int', 'int']],
'FindWindowExW': ['int', ['int', 'int', 'string', 'string']],
'BringWindowToTop': ['int', ['int']],
'GetActiveWindow': ['int', ['int']]
var handle = user32.FindWindowW(null,TEXT("Calculator ‎- Calculator"));
user32.ShowWindow(
handle, 'SW_Hide');
//associatedWindowHandle is a manually-created variable with the Spy++ variable.
//The Spy++ doesn't match and I'm not sure why.
user32.ShowWindowAsync(activeHandle, 'SW_Hide');
var pruneLength = Object.keys(prunedData).length;
for (let i = 0; i < pruneLength-1; i++){
if (Object.entries(prunedData)[i][1] === hotkey){
for(let j = 1; j <= prunedData.assocWindows.length; j++){
let associatedWindow = Object.entries(prunedData)[i+1][j].toString();
let associatedWindowHandle = parseInt(associatedWindow);
user32.ShowWindowAsync(associatedWindowHandle, 'SW_Hide');
user32.BringWindowToTop(associatedWindowHandle[i+1][j]);
}
}
}
2 main issues:
When I try hiding and/or minimizing the open calculator window, I can't seem to show it again when clicking on it. the preview image disappers and I notice a "Process Broker" is thrown.
I can't seem to actually find the window handle given with tools like Spy++, which makes it somewhat hard to debug to see if I need to grab a different handle. The parent-level calculator window's handle doesn't seem to match, and I verified that it was the same tool.
I'd also like to be pointed to some decent resources to help self-educate on this so I can better troubleshoot this in the future.
Many thanks!
Firstly, I'd echo Hans Passant's remarks that you're probably better off not trying to so this with a UWP app like Calculator, but then again these apps are not going to go away so perhaps you might want to try anyway.
The shell doesn't appear to appreciate you trying to hide a UWP app (Win32 apps work fine though, go figure). As you have observed, it's icon remains visible in the toolbar but behaves strangely while the window is hidden. So, short version, don't do that.
Instead, try this:
PostMessage (hWnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
Then things work a lot better, although the user can still undo all your good work by reopening the window of course.
As for Spy++, I have no trouble locating the top-level window of a UWP app using the 'Finder tool' (Menu -> Search -> Find Window). You just have to walk a couple of levels up the window hierarchy afterwards until you get to the one you really want.
Spy++ seems not to be able to log messages being sent to such a window however, see (shameless plug): Why can't Spy++ see messages sent to UWP apps?. I plan to look into this a bit more when I have time.
Finally, what do you mean by 'a "Process Broker" is thrown' please? I don't understand that comment. There's something called RuntimeBroker, which shows up in Process Explorer and appears to be connected with UWP apps in some way, but I don't know if that's what you mean and and I don't know anything about it even if you did.

Example to show how mobify works

I have been looking at the mobify.js website for a while now, but I fail to understand the benefits of using it. (I am stumped to see why would one replace all the images on the page by GrumpyCat image?).
Could you kindly point me to a clear and lucid example, wherein, I can see that depending on the browser resolution my image size changes.
I have done the following tasks till now:
0. Included mobify.js header information
1. Used the mountains.jpg and forest.jpg image in my hosted website (The page contains only these two images)
2. Request the page from a desktop machine, from a tablet (Samsung Galaxy 10 inch), from an android mobile phone.
3. In all the three cases, I see the same image getting downloaded, the size of the image stays the same in all the cases.
I understand that the magic of size reduction can't happen on the fly, but how do I achieve this?
I realize that the Grumpy Cat example is a bit cheeky, but the same concept applies to solve your problem. Instead of replacing the images with Grumpy Cat images, you could write some logic to replace the images with lower-resolution images (i.e. mountains-320.jpg and forest-320.jpg).
With Mobify.js, you need to write the adaptations in the JavaScript snippet that you added to your site. So, to load smaller images for mobile, you could define the path to the lower resolution image in your original HTML like this:
<img src="mountain.jpg" data-mobile-src="mountain-320.jpg" />
<img src="forest.jpg" data-mobile-src="forest-320.jpg" />
Then, in the JavaScript snippet, you could modify it to grab the image in the data-mobile-src attribute instead like this:
if (capturing) {
// Grab reference to a newly created document
Mobify.Capture.init(function(capture){
// Grab reference to the captured document in progres
var capturedDoc = capture.capturedDoc;
var imgs = capturedDoc.getElementsByTagName("img[data-mobile-src]");
for(var i = 0; i < imgs.length; i++) {
var img = imgs[i];
var ogImage = img.getAttribute("x-src");
var mobileImage = img.getAttribute("data-mobile-src");
img.setAttribute("x-src", mobileImage);
img.setAttribute("old-src", ogImage);
}
// Render source DOM to document
capture.renderCapturedDoc();
});
}
Then, you'll see that the mobile site will download and render mountain-320.jpg or forest-320.jpg, but it will not download mountain.jpg or forest.jpg.
Just out of curiousity, what site are you wanting to use Mobify.js on?

How to set custom template for home page?

I've tried to set 1column template for home page using my local.xml file:
<cms_index_index>
<reference name="root">
<action method="setTemplate"><template>page/1column.phtml</template></action>
</reference>
</cms_index_index>
But this is doesn't work. How can I do this?
Homepage is a CMS page. Unfortunately, you can't assign root template for CMS pages using layout, because they have own attribute "root_template" (cms_page table).
You can change this attribute in the backend (CMS - Pages).
Or you can change it in code:
$homePage = Mage::getModel('cms/page')->load('home', 'identifier');
$homePage->setRootTemplate('one_column');
$homePage->save();
I recommend you to write sql data upgrade, which will update root template value for homepage:
$installer = $this;
/* #var $installer Mage_Core_Model_Resource_Setup */
$installer->startSetup();
$installer->run("
UPDATE `{$this->getTable('cms_page')}` SET `root_template` = 'one_column' WHERE `identifier` = 'home';
");
$installer->endSetup();
I'm sure the other suggestions work well, but that all looks way too complicated to me. What I've done which seems to work great, is to simply put the following into the Layout Update XML for the CMS page in question (in this case, your home page)
<reference name="root">
<action method="setTemplate">
<template>page/1column.phtml</template>
</action>
</reference>
The problem lays in Mage_Cms_Helper_Page::_renderPage. Layout updates are applied -before- root template (configured from the backend) is applied:
Mage::dispatchEvent('cms_page_render', array('page' => $page, 'controller_action' => $action));
$action->loadLayoutUpdates();
$layoutUpdate = ($page->getCustomLayoutUpdateXml() && $inRange) ? $page->getCustomLayoutUpdateXml() : $page->getLayoutUpdateXml();
$action->getLayout()->getUpdate()->addUpdate($layoutUpdate);
$action->generateLayoutXml()->generateLayoutBlocks();
...
if ($page->getRootTemplate()) {
$action->getLayout()->helper('page/layout')
->applyTemplate($page->getRootTemplate());
}
Also notice how the only event in this method is inconveniently placed above all of this... Should you want to fix this cleanly (without queries), you should observe the following event:
controller_action_postdispatch_cms_index_index
Then do the following (untested, but should work):
$this->getEvent()->getControllerAction()->getLayout()->helper('page/layout')->applyTemplate('one_column');
Then render the layout again. This is all just a guideline how to solve this through observers.

Resources