How to display array of split elements using LINQ - linq

I have this simple code
string[] sequences = {"red,green,blue","orange","white,pink"};
var allWords = sequences.Select(s => s.Split(','));
foreach (var letter in allWords)
{
Console.WriteLine(letter);
}
The problem is that in output I get System.String[] insted of splitted array.
How to display result at console?

Use SelectMany if you want an array of strings and not an array of arrays of strings.
See https://dotnetfiddle.net/0vsjfN
SelectMany concatenates the lists, which are generated by using .Split(','), into a single list.

Related

How to use StartsWith with an array of string?

Suppose I have an array of strings:
var array = new string[] {"A", "B"}.
Then I want check if the following string: "boca" starts with the letter included in the array.
What I did is:
var result = "boca".StartsWith(array);
but the method doesn't not accept an arra as argument but a single string
You have to loop the array and check if the word starts with anything in the array. Something like this:
var result = array.Any(s => "boca".StartsWith(s));
Assuming your array is {"A", "B"}, then result will be false, because StartsWith is case-sensitive by default.
If you want it case-insensitive, then this will work:
var result = array.Any(s => "boca".StartsWith(s, StringComparison.CurrentCultureIgnoreCase));
In this case, result will be true.

How to reverse tokenization after running tokens through name finder?

After using NameFinderME to find the names in a series of tokens, I would like to reverse the tokenization and reconstruct the original text with the names that have been modified. Is there a way I can reverse the tokenization operation in the exact way in which it was performed, so that the output is the exact structure as the input?
Example
Hello my name is John. This is another sentence.
Find sentences
Hello my name is John.
This is another sentence.
Tokenize sentences.
> Hello
> my
> name
> is
> John.
>
> This
> is
> another
> sentence.
My code that analyzes the tokens above looks something like this so far.
TokenNameFinderModel model3 = new TokenNameFinderModel(modelIn3);
NameFinderME nameFinder = new NameFinderME(model3);
List<Span[]> spans = new List<Span[]>();
foreach (string sentence in sentences)
{
String[] tokens = tokenizer.tokenize(sentence);
Span[] nameSpans = nameFinder.find(tokens);
string[] namedEntities = Span.spansToStrings(nameSpans, tokens);
//I want to modify each of the named entities found
//foreach(string s in namedEntities) { modifystring(s) };
spans.Add(nameSpans);
}
Desired output, perhaps masking the names that were found.
Hello my name is XXXX. This is another sentence.
In the documentation, there is a link to this post describing how to use the detokenizer. I don't understand how the operations array relates to the original tokenization (if at all)
https://issues.apache.org/jira/browse/OPENNLP-216
Create instance of SimpleTokenizer.
String sentence = "He said \"This is a test\".";
SimpleTokenizer instance = SimpleTokenizer.INSTANCE;
Tokenize the sentence using tokenize(String str) method from SimpleTokenizer
String tokens[] = instance.tokenize(sentence);
The operations array must have the same number of operation name as tokens array. Basically array length should be equal.
Store the operation name N-times (tokens.length times) into operation array.
Operation operations[] = new Operation[tokens.length];
String oper = "MOVE_RIGHT"; // please refer above list for the list of operations
for (int i = 0; i < tokens.length; i++)
{ operations[i] = Operation.parse(oper); }
System.out.println(operations.length);
Here the operation array length will be equal to the tokens array length.
Now create an instance of DetokenizationDictionary by passing tokens and operations arrays to the constructor.
DetokenizationDictionary detokenizeDict = new DetokenizationDictionary(tokens, operations);
Pass DetokenizationDictionary instance to the DictionaryDetokenizer class to detokenize the tokens.
DictionaryDetokenizer dictDetokenize = new DictionaryDetokenizer(detokenizeDict);
DictionaryDetokenizer.detokenize requires two parameters. a). tokens array and b). split marker
String st = dictDetokenize.detokenize(tokens, " ");
Output:
Use the Detokenizer.
String text = detokenize(myTokens, null);

Compare arrays together with different array types

I want to compare if 2 arrays are equal, here is my code:
var letteronloc = [String]();
letteronloc.append("test")
let characters = Array("test")
if(letteronloc == characters) {
}
but i have an error: could not find an overload for == that accepts the supplied arguments
I think its because the arrays are not equal, because the second array is not an string array. But how can i fix this?
let characters = Array("test") treats the string as a sequence
(of characters) and creates an array by enumerating the elements of the sequence.
Therefore characters is an array of four Characters,
the same that you would get with
let characters : [Character] = ["t", "e", "s", "t"]
So you have two arrays of different element types and that's why
you cannot compare them with ==.
If you want an array with a single string "test" then write it as
let characters = ["test"]
and you can compare both arrays without problem.
You just need to specify the type of the second array:
var letteronloc = [String]();
letteronloc.append("test")
let characters: [String] = Array(arrayLiteral: "test")
if (letteronloc == characters) {
}

Building a D3 table with json and comma delimited array

I'm trying to build an html table from a 2D array using a row for each array element and a cell for each string in the row. I get a cell for each character instead. I've tried some combinations for splitting the strings by comma, but haven't one that works. How do I get
onetwothree
http://jsfiddle.net/johnpoole/BfTWP/
var json_data = ["one,two,three","red,green,blue"];
var table = d3.select("body").append("table");
var rows = table.selectAll("tr").data(json_data).enter().append("tr");
rows.selectAll("td").data(function(d){return d;}).enter().append("td").text(function(d) {return d;});
The last line needs to return d.split(",") to break the string into an array. Otherwise, JS iterates through the characters in the string.
Alternatively, you could keep the code as is and change the data to:
var json_data = [["one","two","three"],["red","green","blue"]];
jsFiddle updated both ways.

sorting text lines Google Apps Script

Sorry for this extreme beginner question. I have a string variable originaltext containing some multiline text. I can convert it into an array of lines like so:
lines = originaltext.split("\n");
But I need help sorting this array. This DOES NOT work:
lines.sort;
The array remains unsorted.
And an associated question. Assuming I can sort my array somehow, how do I then convert it back to a single variable with no separators?
Your only issue is a small one - sort is actually a method, so you need to call lines.sort(). In order to join the elements together, you can use the join() method:
var originaltext = "This\n\is\na\nline";
lines = originaltext.split("\n");
lines.sort();
joined = lines.join("");

Resources