Finding all identifiers containing part of the token - c#

I know I can get a string from resources using
Resources.GetIdentifier(token, "string", ctx.ApplicationContext.PackageName)
(sorry, this is in C#, it's part of a Xamarin.Android project).
I know that if my elements are called foo_1, foo_2, foo_3, then I can iterate and grab the strings using something like
var myList = new List<string>();
for(var i = 0; i < 4; ++i)
{
var id = AppContent.GetIdentifier(token + i.ToString(), "string", "package_name");
if (id != 0)
myList.Add(AppContext.GetString(id));
}
My issue is that my token names all begin with "posn." (the posn can denote the position of anything, so you can have "posn.left_arm" and "posn.brokenose"). I want to be able to add to the list of posn elements, so I can't really store a list of the parts after the period. I can't use a string-array for this either (specific reason means I can't do this).
Is there a way that I can use something akin to "posn.*" in the getidentifer call to return the ids?

You can use some reflection foo to get what you want. It is not pretty at all but it works. The reflection stuff is based on https://gist.github.com/atsushieno/4e66da6e492dfb6c1dd0
private List<string> _stringNames;
private IEnumerable<int> GetIdentifiers(string contains)
{
if (_stringNames == null)
{
var eass = Assembly.GetExecutingAssembly();
Func<Assembly, Type> f = ass =>
ass.GetCustomAttributes(typeof(ResourceDesignerAttribute), true)
.OfType<ResourceDesignerAttribute>()
.Where(ca => ca.IsApplication)
.Select(ca => ass.GetType(ca.FullName))
.FirstOrDefault(ty => ty != null);
var t = f(eass) ??
AppDomain.CurrentDomain.GetAssemblies().Select(ass => f(ass)).FirstOrDefault(ty => ty != null);
if (t != null)
{
var strings = t.GetNestedTypes().FirstOrDefault(n => n.Name == "String");
if (strings != null)
{
var fields = strings.GetFields();
_stringNames = new List<string>();
foreach (var field in fields)
{
_stringNames.Add(field.Name);
}
}
}
}
if (_stringNames != null)
{
var names = _stringNames.Where(s => s.Contains(contains));
foreach (var name in names)
{
yield return Resources.GetIdentifier(name, "string", ComponentName.PackageName);
}
}
}
Then somewhere in your Activity you could do:
var ids = GetIdentifiers("action").ToList();
That will give you all the String Resources, which contain the string action.

Related

How can I convert IList<object> to string array?

How can I convert IList objects to string array?
While working on the telegram bot, some difficulties arose. I get a IList<object> from google table with some information. I need to convert this IList<object> to an array of strings. How can I do this?
static void ReadBudgetTypes()
{
var range = $"{settingsSheet}!B3:B";
var request = service.Spreadsheets.Values.Get(SpreadsheetId, range);
var response = request.Execute();
var values = response.Values; // here i get list of objects from google table
if (values != null && values.Count > 0)
{
foreach (var row in values)
{
Console.WriteLine("{0}", row[0]);
}
}
else
{
Console.WriteLine("No data!");
}
}
Assuming cells may not be strings and may (or may not) have null values, you can print for each cell of the row:
// assumes ToString() gives a meaningful string
var listOfStrings = row.Select(x => x?.ToString()).ToList();
foreach(string cell in listOfStrings)
Console.WriteLine(cell);
or the whole row, joined by a separator
Console.WriteLine(string.Join(", ", row);
If you know the cells are strings you can just cast
var listOfStrings = row.Cast<string>().ToList();
// or
var listOfStrings = row.Select(x => (string)x).ToList();
and then repeat either of the above (loop or string.Join).
If items could be null,
var listOfStrings = row.Select(x => (x ?? (object)"").ToString()).ToList();
you can try this:
var tempList=List<string>();
string[] arrayList=null;
if (values != null && values.Count > 0)
{
foreach (var row in values)
{
tempList.Add(row[0]);
}
arrayList=tempList.ToArray();
}
Try something like this:
IList<object> list = new List<object>(){ "something", "something else" };
string[] array = list.Select(item => (String)item).ToArray();

IEnumerable failed to set element

I have a ViewModel that contains different elements inside different tables that I tend to assign to it by query.
My problem is that I can't do this with IEnumerable (in GetAll() below), it keeps returning me null for RoomCode but for a single item (in GetDeviceId() below) then it works fine.
public IEnumerable<DeviceViewModel> GetAll()
{
var result = deviceRepository.GetAll().Select(x => x.ToViewModel<DeviceViewModel>());
for(int i = 0; i < result.Count(); i++)
{
int? deviceID = result.ElementAt(i).DeviceId;
result.ElementAt(i).RoomCode = deviceRepository.GetRoomCode(deviceID);
}
return result;
}
public DeviceViewModel GetDeviceID(int deviceID)
{
var result = new DeviceViewModel();
var device = deviceRepository.Find(deviceID);
if (device != null)
{
result = device.ToViewModel<DeviceViewModel>();
result.RoomCode = deviceRepository.GetRoomCode(deviceID);
}
else
{
throw new BaseException(ErrorMessages.DEVICE_LIST_EMPTY);
}
return result;
}
public string GetRoomCode(int? deviceID)
{
string roomCode;
var roomDevice = dbContext.Set<RoomDevice>().FirstOrDefault(x => x.DeviceId == deviceID && x.IsActive == true);
if (roomDevice != null)
{
var room = dbContext.Set<Room>().Find(roomDevice.RoomId);
roomCode = room.RoomCode;
}
else
{
roomCode = "";
}
return roomCode;
}
First, you need to materialize the query to a collection in local memory. Otherwise, the ElementAt(i) will query the db and give back some kind of temporary object each time it is used, discarding any change you do.
var result = deviceRepository.GetAll()
.Select(x => x.ToViewModel<DeviceViewModel>())
.ToList(); // this will materialize the query to a list in memory
// Now modifications of elements in the result IEnumerable will be persisted.
You can then go on with the rest of the code.
Second (and probably optional), I also recommend for clarity to use foreach to enumerate the elements. That's the C# idiomatic way to loop through an IEnumerable:
foreach (var element in result)
{
int? deviceID = element.DeviceId;
element.RoomCode = deviceRepository.GetRoomCode(deviceID);
}

For Watson's Speech-To-Text Unity SDK, how can you specify keywords?

I am trying to specify keywords in Watson's Speech-To-Text Unity SDK, but I'm unsure how to do this.
The details page doesn't show an example (see here: https://www.ibm.com/watson/developercloud/doc/speech-to-text/output.shtml),
and other forum posts are written for Java applications (see here: How to specify phonetic keywords for IBM Watson speech2text service?).
I've tried hard-coding these values in the RecognizeRequest class created in the "Recognize" function like so, but without success:
**EDIT - this function never gets called -- **
public bool Recognize(AudioClip clip, OnRecognize callback)
{
if (clip == null)
throw new ArgumentNullException("clip");
if (callback == null)
throw new ArgumentNullException("callback");
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, "/v1/recognize");
if (connector == null)
return false;
RecognizeRequest req = new RecognizeRequest();
req.Clip = clip;
req.Callback = callback;
req.Headers["Content-Type"] = "audio/wav";
req.Send = WaveFile.CreateWAV(clip);
if (req.Send.Length > MAX_RECOGNIZE_CLIP_SIZE)
{
Log.Error("SpeechToText", "AudioClip is too large for Recognize().");
return false;
}
req.Parameters["model"] = m_RecognizeModel;
req.Parameters["continuous"] = "false";
req.Parameters["max_alternatives"] = m_MaxAlternatives.ToString();
req.Parameters["timestamps"] = m_Timestamps ? "true" : "false";
req.Parameters["word_confidence"] = m_WordConfidence ? "true" :false";
//these "keywords" and "keywords_threshold" and "keywordsThreshold" parameters
//are just my guess for how to set these values
req.Parameters["keywords"] = new string[] {"fun", "match", "test" };
req.Parameters["keywordsThreshold"] = .2;
req.Parameters["keywords_threshold"] = .2;
//end my test insertions
req.OnResponse = OnRecognizeResponse;
return connector.Send(req);
}
but the returned SpeechRecognitionEvent result value does not contain any keywords_result. This is my aim. I'm trying to view the confidence for each keyword in the keywords_result object like so, but the keywords_result object comes back as null.
private void OnRecognize(SpeechRecognitionEvent result) {
Debug.Log("Recognizing!");
m_ResultOutput.SendData(new SpeechToTextData(result));
if (result != null && result.results.Length > 0) {
if (m_Transcript != null)
m_Transcript.text = "";
foreach (var res in result.results) {
//the res.keywords_result comes back as null
foreach (var keyword in res.keywords_result.keyword) {
string text = keyword.normalized_text;
float confidence = keyword.confidence;
Debug.Log(text + ": " + confidence);
}
}
}
}
Has anyone successfully implemented Keyword Confidence Evaluation with Watson's Speech-To-Text SDK in Unity or C#? All ideas and suggestions are welcome.
PS This is my first post :)
Turns out I needed to specify the keywords in the "SendStart" function like so:
private void SendStart() {
if (m_ListenSocket == null)
throw new WatsonException("SendStart() called with null connector.");
Dictionary<string, object> start = new Dictionary<string, object>();
start["action"] = "start";
start["content-type"] = "audio/l16;rate=" + m_RecordingHZ.ToString() + ";channels=1;";
start["continuous"] = EnableContinousRecognition;
start["max_alternatives"] = m_MaxAlternatives;
start["interim_results"] = EnableInterimResults;
start["word_confidence"] = m_WordConfidence;
start["timestamps"] = m_Timestamps;
//specify keywords here
start["keywords"] = keywordsToCheck.ToArray();
start["keywords_threshold"] = 0.05;
//end additions here
m_ListenSocket.Send(new WSConnector.TextMessage(Json.Serialize(start)));
m_LastStartSent = DateTime.Now;
}
and write some code to parse the keyword_results properly in the "ParseRecognizeResponse" function:
private SpeechRecognitionEvent ParseRecognizeResponse(IDictionary resp){
if (resp == null)
return null;
List<SpeechRecognitionResult> results = new List<SpeechRecognitionResult>();
IList iresults = resp["results"] as IList;
if (iresults == null)
return null;
foreach (var r in iresults)
{
IDictionary iresult = r as IDictionary;
if (iresults == null)
continue;
SpeechRecognitionResult result = new SpeechRecognitionResult();
//added this section, starting here
IDictionary iKeywords_result = iresult["keywords_result"] as IDictionary;
result.keywords_result = new KeywordResults();
List<KeywordResult> keywordResults = new List<KeywordResult>();
foreach (string key in keywordsToCheck) {
if (iKeywords_result[key] != null) {
IList keyword_Results = iKeywords_result[key] as IList;
if (keyword_Results == null) {
continue;
}
foreach (var res in keyword_Results) {
IDictionary kw_resultDic = res as IDictionary;
KeywordResult keyword_Result = new KeywordResult();
keyword_Result.confidence = (double)kw_resultDic["confidence"];
keyword_Result.end_time = (double)kw_resultDic["end_time"];
keyword_Result.start_time = (double)kw_resultDic["start_time"];
keyword_Result.normalized_text = (string)kw_resultDic["normalized_text"];
keywordResults.Add(keyword_Result);
}
}
}
result.keywords_result.keyword = keywordResults.ToArray();
//ends here
result.final = (bool)iresult["final"];
IList ialternatives = iresult["alternatives"] as IList;
if (ialternatives == null)
continue;
List<SpeechRecognitionAlternative> alternatives = new List<SpeechRecognitionAlternative>();
foreach (var a in ialternatives)
{
IDictionary ialternative = a as IDictionary;
if (ialternative == null)
continue;
SpeechRecognitionAlternative alternative = new SpeechRecognitionAlternative();
alternative.transcript = (string)ialternative["transcript"];
if (ialternative.Contains("confidence"))
alternative.confidence = (double)ialternative["confidence"];
if (ialternative.Contains("timestamps"))
{
IList itimestamps = ialternative["timestamps"] as IList;
TimeStamp[] timestamps = new TimeStamp[itimestamps.Count];
for (int i = 0; i < itimestamps.Count; ++i)
{
IList itimestamp = itimestamps[i] as IList;
if (itimestamp == null)
continue;
TimeStamp ts = new TimeStamp();
ts.Word = (string)itimestamp[0];
ts.Start = (double)itimestamp[1];
ts.End = (double)itimestamp[2];
timestamps[i] = ts;
}
alternative.Timestamps = timestamps;
}
if (ialternative.Contains("word_confidence"))
{
IList iconfidence = ialternative["word_confidence"] as IList;
WordConfidence[] confidence = new WordConfidence[iconfidence.Count];
for (int i = 0; i < iconfidence.Count; ++i)
{
IList iwordconf = iconfidence[i] as IList;
if (iwordconf == null)
continue;
WordConfidence wc = new WordConfidence();
wc.Word = (string)iwordconf[0];
wc.Confidence = (double)iwordconf[1];
confidence[i] = wc;
}
alternative.WordConfidence = confidence;
}
alternatives.Add(alternative);
}
result.alternatives = alternatives.ToArray();
results.Add(result);
}
return new SpeechRecognitionEvent(results.ToArray());
}
So that now, when OnRecognize gets passed this SpeechRecognitionEvent, I've changed the code for displaying word alternatives and their confidence score, to displaying keyword results and their confidence score, like so:
private void OnRecognize(SpeechRecognitionEvent result) {
//Debug.Log("Recognizing!");
m_ResultOutput.SendData(new SpeechToTextData(result));
if (result != null && result.results.Length > 0) {
if (m_Transcript != null)
m_Transcript.text = "";
foreach (var res in result.results) {
//start keyword recognition changes here
if (res.keywords_result != null) {
if (res.keywords_result.keyword != null) {
foreach (var keyword in res.keywords_result.keyword) {
m_Transcript.text += string.Format("{0} ({1}, {2:0.00})\n",
keyword.normalized_text, res.final ? "Final" : "Interim", keyword.confidence);
}
}
}
//end here
}
}
}
Note, using the keyword results confidence values is much more valuable than doing some hardcoded check to see if the word alternatives Watson is getting match your keywords, and then using the confidence value there. The confidence values come back much higher when checking the keyword_results.keyword[].confidence values because it's already checking against those words. That was the impetus for going through with this process and parsing the SpeechRecognitionEvent result value to properly include the keywords_result values.
For some background, I'm creating a rhythm game for children with dyslexia to learn word formation, so think Guitar Hero meets Sesame street.

How could I convert these foreach loops into a LINQ-expression?

I used ReSharper to inspect the code issues in my project and it notified me that the following loop could be converted into a LINQ-expression:
var dictionary = new Dictionary<string, string[]>
{
{ "400", new[] { "12345", "54321", "51423" } },
{ "500", new[] { "67890", "09876", "63727" } },
{ "600", new[] { "41713", "98234", "96547" } },
{ "700", new[] { "00000", "67990", "83752" } }
};
// ...
var targetValue = "41713";
foreach (string group in dictionary.Keys)
{
foreach (string name in dictionary[group])
{
if (name == targetValue)
return group;
}
}
return "User";
The loop basically checks the dictionary's values (string arrays) to see if targetValue belongs to any of them and returns the key of that array if found inside.
I tried doing the following, but clearly it just returns the value inside if its value is equivalent to targetValue.
var r = dictionary
.SelectMany(t => t.Value)
.FirstOrDefault(t => t == targetValue);
So you want to get the first key in the dictionary which string[]-value contains a given value?
var pairs = dictionary.Where(kv => kv.Value.Contains(myValue));
if (pairs.Any())
{
string group = pairs.First().Key;
}
or less readable but a little bit more efficient since it executes the query only once:
var pair = dictionary.FirstOrDefault(kv => kv.Value.Contains(myValue));
if (!pair.Equals(default(KeyValuePair<string, string[]>)))
{
string group = pair.Key;
}
last but not least another approach which is my favorite and also uses the "User"-default:
string group = dictionary.Where(kv => kv.Value.Contains(myValue))
.Select(kv=> kv.Key)
.DefaultIfEmpty("User")
.First();
var r = dictionary.FirstOrDefault(
x => x.Value.FirstOrDefault(y => y == myValue) != null);
This will also get the desired value back or null if it does not exist:
EDIT:
var result = dictionary.SkipWhile(n => !n.Value.Contains(myValue)).FirstOrDefault().Key;
//another way to get the key
//var result = dictionary.SingleOrDefault(n => n.Value.Contains(myValue)).Key;
if (result != null)
{
//do whatever with the result variable here
}

MVCCrud Using LinqToEntities

There is a sample application called MVCCrud. This example is quite good and I would like to use it as the framework on a project that I am working on.
The problem is that MVCCrud uses LingToSQL and I would like to use LinqToEntities. I got most everything to work correctly once I converted over to LinqToEntities except one place.
In the following code on the lines i = typeof(TModel).GetProperty(primaryKey).GetValue(p, null),
cell = getCells(p)
it gives a Linq to Entities does not recognize GetValue.
Can someone help me refactor the following code?
items = items.OrderBy(string.Format("{0} {1}", sidx, sord)).Skip(pageIndex * pageSize).Take(pageSize).AsQueryable();
// Generate JSON
var jsonData =
new
{
total = totalPages,
page,
records = totalRecords,
rows = items.Select(
p => new
{
// id column from repository
i = typeof(TModel).GetProperty(primaryKey).GetValue(p, null),
cell = getCells(p)
}).ToArray()
};
return Json(jsonData);
and here is the getCell method:
private string[] getCells(TModel p)
{
List<string> result = new List<string>();
string a = actionCell(p);
if (a != null)
{
result.Add(a);
}
foreach (string column in data_rows.Select(r => r.value))
{
try
{
// hack for tblcategory.name
string[] parts = column.Split('.');
// Set first part
PropertyInfo c = typeof(TModel).GetProperty(parts[0]);
object tmp = c.GetValue(p, null);
// loop through if there is more than one depth to the . eg tblCategory.name
for (int j = 1; j < parts.Length; j++)
{
c = tmp.GetType().GetProperty(parts[j]);
tmp = c.GetValue(tmp, null);
}
if (tmp.GetType() == typeof(DateTime))
{
result.Add(((DateTime)tmp).ToString(dateTimeFormat));
}
else if (tmp.GetType() == typeof(float))
{
result.Add(((float)tmp).ToString(decimalFormat));
}
else if (tmp.GetType() == typeof(double))
{
result.Add(((double)tmp).ToString(decimalFormat));
}
else if (tmp.GetType() == typeof(decimal))
{
result.Add(((decimal)tmp).ToString(decimalFormat));
}
else
{
result.Add(tmp.ToString());
}
}
catch (Exception)
{
result.Add(string.Empty);
}
}
return result.ToArray();
}
Do this ToList() instead of AsQueryable():
items = items.OrderBy(string.Format("{0} {1}", sidx, sord)).Skip(pageIndex * pageSize).Take(pageSize).ToList();
You can't execute any external method "within" linq query.
And may you say that was working in Linq2Sql then you should know when you call any external method "Like ToString()" Linq2Sql will fetch all data from database then handle your query in the memory and that maybe a serious harming if you have a lot of records.
For more information look at this

Categories