Flutter, Dart Split sentence every one character to 2 characters size, for flutter custom tts project - flutter

Example:
var sentence = (hello there, I am Bob.)
var result = [
'he',
'el',
'll',
'lo',
' ',
'th',
'he',
'er',
're',
',',
' ',
'I',
' ',
'am',
' ',
'bo',
'ob',
'.']
I've found here working example, though it is in Javascript and I don't really know how to adopt it for Dart, and not sure how will behave once white space and punctuation is added in. Punctation and white space I need always split on its own not in combination with letters, I need them as well, as I will use them to add pauses in between words and sentences.
Thank you
var a = 12345678;
a= a.toString();
var arr=[];
for (var i =0; i<a.length-1; i++) {
arr.push(Number(a[i]+''+a[i+1]));
}
console.log(arr);

You could use regular expressions to split the sentence. For example:
void main() {
var exp = RegExp('([A-Za-z]{1,2}|[,!.?\s ])');
var str = "hello there, I am Bob.";
var matches = exp.allMatches(str);
for (var m in matches) {
print(m.group(0));
}
}
This looks for letters (A-Z or a-z) in groups of either 1 or 2, or single punctuation characters (,!.?) \s represents a white space.
Running the above would produce:
he
ll
o
th
er
e
,
I
am
Bo
b
.

Another approach
void main() {
var a = "1234!5678";
a = a.toString();
var arr = [];
for (var i = 0; i < a.length - 1; i++) {
if (a[i + 1] == '!') {
continue;
}
if (a[i] == '!') {
arr.add(a[i]);
continue;
}
arr.add(a[i] + '' + a[i + 1]);
}
print(arr);
}
I don't know dart much but I wrote this simple algorithm on dartpad and it works

If someone is having same issue, this is how I solved it
void main(String string) {
var test = "I Hello there I am Bob 23!";
List<String> nameArray = test.split('');
for (int curIndex = 0; curIndex < nameArray.length; curIndex++) {
if (curIndex >= 1 && nameArray[curIndex].contains(new RegExp(r'[a-zA-Z]')) && nameArray[curIndex-1].contains(new RegExp(r'[a-zA-Z]'))) {
print(nameArray[curIndex-1] + nameArray[curIndex]); // checks if current curIndex and previous curIndex are letters, if so returns previous and curent letters joined
} else {
if (curIndex >= 1 && nameArray[curIndex].contains(new RegExp(r'[a-zA-Z]')) && nameArray[curIndex+1].contains(new RegExp(r'[a-zA-Z]'))) {
null; // checks if curIndex and next curIndex are letters, if so returns null
}else{
print(nameArray[curIndex]);
}
}
}
}
Which returns
I
He
el
ll
lo
th
he
er
re
I
am
Bo
ob
2
3
!

Related

Flutter: Return list of Strings using single string in a specific format

I need to upload array of string to firestore. I need to return list of Strings using single string as input.
If the input is 'Firestore'.
I should return list like this:
[f,fi,fir,firs,first,firsto,firstor,firestore]
If the input is 'Google Cloud'.
I should return list like this:
[g,
go,
goo,
goo,
goog,
googl,
google,
c,
cl,
clo,
clou,
cloud]
For having [g, go, goo, goo, goog, googl, google, c, cl, clo, clou, cloud] as result
use this :
String test = 'Google Cloud';
List<String> chars = [];
for (var word in test.split(" ")) {
for (var i = 0; i <= word.length; i++) {
if (word.substring(0, i).isNotEmpty) {
chars.add(word.substring(0, i));
}
}
}
print(chars);
it's a good practice to share what you have already done. Here is a simple way of achieving what you want (excluding white spaces) :
String test = 'firestore';
List<String> chars = [];
for (var i = 0; i <= test.length; i++) {
if (test.substring(0, i).isNotEmpty) {
chars.add(test.substring(0, i));
}
}
print(chars);
The above prints [f, fi, fir, fire, fires, firest, firesto, firestor, firestore]

Accent insensitive in filter backend to datatable in ASP.NET MVC 5

I made a method on the back-end side to handle the filter of my datatable.
On the other hand, this one does not manage the accents of the French language, so if I have "école" and I write "ecole" it cannot find it.
I found this method on another question on stackoverflow
public static String RemoveDiacritics(this String s)
{
String normalizedString = s.Normalize(NormalizationForm.FormD);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < normalizedString.Length; i++)
{
Char c = normalizedString[i];
if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
{
stringBuilder.Append(c);
}
}
return stringBuilder.ToString().Normalize(NormalizationForm.FormC);
}
and it works, but only for part of my problem. It works on the letter or the word that is written in the search, but I am not able to apply it in my linq query, so with the .RemoveDiacritics() method my "école" becomes "ecole", but I don't am not able to apply it in the column of my table and it always looks for "école".
Here the code for the search:
if (search != null)
{
int n;
search = search.Trim();
var isNumeric = int.TryParse(search, out n);
if (isNumeric)
{
IdFilter = n;
query = query.Where(x => x.UsagerId == IdFilter || x.Niveau == IdFilter);
}
else if (search != "")
{
// this line work
textFilter = search.ToLower().RemoveDiacritics();
// This is the full line, but absolutely takes the accents out to get the right information out
// query = query.Where(x => x.Nom.ToLower().Contains(textFilter) || x.Prenom.ToLower().Contains(textFilter) || x.Username.ToLower().Contains(textFilter) || x.Email.ToLower().Contains(textFilter) || x.EtabNom.ToLower().Contains(textFilter) || x.ActifStatut.ToLower().Contains(textFilter));
// This is the line that will replace the line above, which I try and it doesn't work ( this part: x.Prenom.ToLower().RemoveDiacritics())
query = query.Where(x => x.Prenom.ToLower().RemoveDiacritics().Contains(textFilter));
}
}
This is the basic query:
IQueryable<ListeUsagers> query = (from u in db.USAGERs
join e in db.ETABLISSEMENTs on u.USAGER_INST equals e.ETAB_CODE
where u.USAGER_INST == instId && u.USAGER_NIVEAU > 3 && u.USAGER_NIVEAU < 5 //&& u.USAGER_ACTIF == 1
select new ListeUsagers()
{
UsagerId = u.USAGER_id,
Nom = u.USAGER_NOM,
Prenom = u.USAGER_PRENOM,
EtabCode = e.ETAB_CODE,
EtabNom = e.ETAB_NOM_COURT,
EtabType = e.ETAB_TYPE,
Niveau = u.USAGER_NIVEAU,
Username = u.USAGER_USERNAME,
UserPassword = u.USAGER_MP,
DateCreation = u.USAGER_DATE_INSC,
Sexe = u.USAGER_SEXE,
Lang = u.USAGER_LANGUE,
Telephone = u.USAGER_TELEPHONE,
Email = u.USAGER_EMAIL,
FonctionTravail = u.USAGER_FONCTION,
LieuTravail = u.USAGER_LIEUTRAVAIL,
Note = u.USAGER_NOTE,
Actif = u.USAGER_ACTIF,
ActifStatut = u.USAGER_ACTIF == 0 ? "Inactif" : "Actif"
});
This is the error:
LINQ to Entities does not recognize the method 'System.String RemoveDiacritics(System.String)' method, and this method cannot be translated into a store expression.
There's built-in functionality to do this in entityframework: https://learn.microsoft.com/en-us/ef/core/miscellaneous/collations-and-case-sensitivity if you're using EF 5+
You'll want an accent insensitive collation ("AI", not "AS" in the examples on that page.)

Learning Flutter

I am Ali from Senegal. I am 60 years old(maybe this is my real problem - smiley!!!).
I am learning Flutter and Dart. Today I wanted to use a list of given data model (it's name is Mortalite, please see in the code below).
I try to use dartpad. I am very sad because I do not understand why the following snipet does not run:
//https://www.dartpad.dev/
void main(){
print('Beginning');
List<Mortalite>pertes;
var causes = ['Bla0', 'Bla1', 'Bla2', 'Bla3'];
var lstDate = ['21/09/2020', '22/09/2020', '23/09/2020', '24/09/2020'];
var perteInst = [2, 4, 3, 1];
var total=0;
for (int i = 0; i < 4; i++) {
total += perteInst[i];
print(i); // prints only '0'
pertes[i] = new Mortalite(
causesPerte: causes[i],
datePerte: lstDate[i],
perteInstant:perteInst[i],
totalPertes: total);
};
print(pertes.length); // nothing printed
print('Why?'); // nothing printed
}
class Mortalite {
String datePerte;
String causesPerte;
int perteInstant; // pertes du jour
int totalPertes; // Total des pertes.
Mortalite(
{this.datePerte,
this.causesPerte,
this.perteInstant,
this.totalPertes}
);
}
Thank you well for help.
A.KOTE
The reason above code doesn't work is because you have List pertes initialized and you are passing to the elements of pertes. When you try to pass to 0 index of pertes, it cannot find it and it throws range error because pertes doesn't have an index 0 yet. Please take a look at fix below and let me know if you need help with anything.
void main(){
print('Beginning');
List<Mortalite>pertes=[];
var causes = ['Bla0', 'Bla1', 'Bla2', 'Bla3'];
var lstDate = ['21/09/2020', '22/09/2020', '23/09/2020', '24/09/2020'];
var perteInst = [2, 4, 3, 1];
var total=0;
for (int i = 0; i < causes.length; i++) {
total += perteInst[i]; // prints only '0'
pertes.add (new Mortalite(
causesPerte: causes[i],
datePerte: lstDate[i],
perteInstant:perteInst[i],
totalPertes: total));
};
print(pertes.length); // nothing printed // nothing printed
}
class Mortalite {
String datePerte;
String causesPerte;
int perteInstant; // pertes du jour
int totalPertes; // Total des pertes.
Mortalite(
{this.datePerte,
this.causesPerte,
this.perteInstant,
this.totalPertes}
);
}

Studio won't print array elements when I test. (Java)

I'm working with eclipse and when I want the program to print either all the array elements or just a single array element via indexing. It doesn't return an error or even an address when I run the program.
import java.util.Scanner;
public class FavoriteListMaker {
public static void main(String[] args)
{
Scanner inputDevice = new Scanner(System.in);
System.out.println("This program is to help you order a favorites list. Please enter the amount of items on the list.");
int ListSize = inputDevice.nextInt();
String [] TopXA = new String [ListSize + 1];
int [] TopXB = new int[ListSize + 1];
for (int x = 0; x < TopXA.length; ++ x)
{
System.out.println("Please enter an item to be organized on the list");
String item = inputDevice.nextLine();
TopXA[x] = item;
System.out.println("You have " + (ListSize - x) + " items left to fill on the list.");
}
System.out.println("Now we will compare each item on the list with every item on the list one at a time.");
System.out.println("We will ask you a series a question on whether you like item A better then item B and tally the score.");
System.out.println("At the end the item with the most points wins.");
for (int y = 0; y < TopXA.length; ++ y)
for (int z = 0; z < TopXA.length; ++ z)
{
String compareA = TopXA[y];
String compareB = TopXA[z];
System.out.println("Do you prefer " + compareA + " or " + compareB + " .");
System.out.println("If you prefer " + compareA + "Please press 1. If you prefer " + compareB + " please press 2.");
int choice = inputDevice.nextInt();
switch(choice)
{
case 1:
TopXB[y] =+ 1;
break;
case 2:
TopXB[z] =+ 2;
break;
default:
System.out.print("I'm sorry but that is not a valid input.");
}
}
When I run the code it will print out. "Do you prefer or ." instead of the array element.
Disclaimer - I don't have eclipse, I dont program in Java
Please check two things
1) The for loop for y doesn't have curly brackets
2) Please check array 'TopXA' to see if 'item' is being populated in array once you input it.
3) Are CompareA and CompareB being assigned values?
EDIT - found problem
Add scanner.nextLine(); after the nextINt line.
Your problem is that the nextInt() method is not consuming the newline character (see other question). This leads to the fact that your first element in the list is empty. In the first iteration (y=0, z=0) of the nested for-loops this empty element gets printed two times.
Try adding inputDevice.nextLine() after each nextInt() call and you will see that it is working ;)
And PLEASE: Don't name variables uppercase!
import java.util.Scanner;
public class FavoriteListMaker {
public static void main(String[] args)
{
Scanner inputDevice = new Scanner(System.in);
System.out.println("This program is to help you order a favorites list. Please enter the amount of items on the list.");
int listSize = inputDevice.nextInt();
inputDevice.nextLine();
String [] topXA = new String [listSize];
int [] topXB = new int[listSize];
for (int x = 0; x < topXA.length; ++x)
{
System.out.println("Please enter an item to be organized on the list");
String item = inputDevice.nextLine();
topXA[x] = item;
System.out.println("You have " + (listSize - x) + " items left to fill on the list.");
}
System.out.println("Now we will compare each item on the list with every item on the list one at a time.");
System.out.println("We will ask you a series a question on whether you like item A better then item B and tally the score.");
System.out.println("At the end the item with the most points wins.");
for (int y = 0; y < topXA.length; ++y)
{
for (int z = 0; z < topXA.length; ++z)
{
String compareA = topXA[y];
String compareB = topXA[z];
System.out.println("Do you prefer " + compareA + " or " + compareB + " .");
System.out.println("If you prefer " + compareA + "Please press 1. If you prefer " + compareB + " please press 2.");
int choice = inputDevice.nextInt();
inputDevice.nextLine();
switch(choice)
{
case 1:
topXB[y] =+ 1;
break;
case 2:
topXB[z] =+ 2;
break;
default:
System.out.print("I'm sorry but that is not a valid input.");
}
}
}
}
}

How to make 'ő' and 'ű' work in Java?

String word = inputField.getText();
int wordLength = word.length();
boolean backWord = false;
boolean longWord = false;
String backArray[]=new String[6];
backArray[0] = "a";
backArray[1] = "á";
backArray[2] = "ö";
backArray[3] = "ő";
backArray[4] = "ü";
backArray[5] = "ű";
for (int i = 0;i < wordLength ;i ++ ) {
String character = word.substring(i, i + 1);
for (int j = 0;j < 5;j ++) {
if (character.equals(backArray[j])) {
backWord = true;
}
}
}
if (backWord) {
outputField.setText(word+"ban");
}
else {
outputField.setText(word+"ben");
}
This is the code I wrote for an applet for conjugating Hungarian nouns while taking vowel harmony into consideration. For the unaware, the TL;DR of vowel harmony is that Hungarian has lots of suffixes and you can determine which suffix to use based on the vowels in a word.
This code works fine for all the vowels, except for ő and ű. So if my input is 'szálloda', the output will be 'szállodaban'. However, if my input is 'idő' (weather) the output will be 'időben', though it should be 'időban' according to the code.
I assumed this is because java somehow doesn't recognize these two letters because the code works fine for the other ones. Is that the problem? And if so, how do I solve it?