Matchlists/tables in power query - match

I'm thinking this has to have a simple answer, but I can't find any examples.
I have to compare every member of a list to a list of substrings to see if that member contains a substring, and if it does - return the substring to a third list at the same position as the member of the first list.
Example:
ListA = {"help me rhonda", "in my room", "good vibrations", "god only knows"}
ListB = {"room", "me", "only"}
ListC should then should = {"me", "room", null, "only"}
I'm an advanced programmer who has been writing M for about 4 days now. It's driving me crazy. I have been trying several different functions, but so far i'm not even close, so I'm not going to list my code. List.Transform seems the most likely option, but I can't quite work it out.
Thanks for the help,
-J

Words intersections
let
ListA = {"help me rhonda", "in my room", "good vibrations", "god only knows"},
ListB = {"room", "me", "only"},
intersect=List.Transform(ListA, (lineA)=>Text.Combine(List.Intersect({Text.Split(lineA, " "), ListB}), "|"))
in
intersect
Flags only
let
ListA = {"help me rhonda", "in my room", "good vibrations", "god only knows"},
ListB = {"room", "me", "only"},
contains_word=List.Transform(ListA, (lineA)=>List.MatchesAny(ListB, (wordB)=>Text.Contains(lineA, wordB)))
in
contains_word

Related

Eliminate repetition in updating strings selected randomly within a state property

First, I'm very new to Swift, and coding in general. I'm trying to use the correct terminology, so I apologize if it makes no sense. Doing my best!
Ok, so there appears to be a lot of information/answers on ways to random select strings (or integers, etc) from an array without repetition. However, in my current application, I am not using an array (I kind of am but not directly related to the question). But rather a #State designation, in which the variable has an initial value (a string with a number and letter) and is later updated when a button is pushed. The string changes to a randomly selected string from my assets folder based on the file name, which will be the same word but with a different number and letter. What is the simplest way to keep from updating to strings that have already appeared?
Example of what the code looks like:
#State var relevantWord = "bird"
Button {
let randoNum = Int.random(in: 1...5)
let x = ["a", "b", "c", "d"]
let randoLet = x.randomElement()
relevantWord = "bird" + String(randoNum) + String(randoLet)
}
So, the relevantWord variable starts as "bird2c" for example, and each time the button is pushed, it will change to "bird3b" then "bird4a" etc. I just want to keep it from repeating and return nothing when the assets are depleted. Thanks!

Restore String Interpolation AFTER value replace

Given a str: "My name is Gustavo", that was created with "My name is \(foo.name)"
Is it possible to get the string "My name is \(foo.name)" after the value was replaced?
In other words, is it possible to find out "My name is \(foo.name)", having "My name is Gustavo"?
Edit: If I can get to "My name is ", a.k.a. the hard coded string from all strings that were build dynamically, that would be a solution.
No, this is not possible. In fact, "My name is \(foo.name)" is not even included in the compiled code. Instead, Swift-5 compiler generates code equivalent to this:
let str = String(stringInterpolation: {
var temp = String.StringInterpolation(literalCapacity: 11, interpolationCount: 1)
temp.appendLiteral("My name is ")
temp.appendInterpolation(foo.name)
return temp
}())
This article contains details on how string interpolation is implemented in Swift-4 and Swift-5.

Swift: String Concatenation for Outlet names

I'm looking to reduce the number of lines in my code, I have quite a lot of label names that correspond to their values. I was wondering if there is a way of setting these values in a for-in loop or something similar. Right now I'm struggling to find a way of concatenating the label names to make them dynamic. I'm not sure if I'm able to do this in Swift, any thoughts? Thanks.
This code
LabelGiraffe1.text = "This is Giraffe 1"
LabelGiraffe2.text = "This is Giraffe 2"
LabelGiraffe3.text = "This is Giraffe 3"
LabelGiraffe4.text = "This is Giraffe 4"
LabelGiraffe5.text = "This is Giraffe 5"
To something cleaner like...
for number in 1...5 {
LabelGiraffe + /(number).text = "This is Giraffe /(number)"
}
Make an array of labels and over them:
let giraffeLabels = [LabelGiraffe1, LabelGiraffe2, LabelGiraffe3, LabelGiraffe4, LabelGiraffe5]
for (index, label) in enumerate(giraffeLabels) {
label.text = "This is Giraffe \(index + 1)"
}
You can also make giraffeLabels as stored or calculated property of your view controller.
Use an IBOutletCollection. It's effectively like putting all your outlets into an array (which is what it does in the background, I believe). You can then iterate over them, or access them by index, as you're trying to do.
More information here: http://nshipster.com/ibaction-iboutlet-iboutletcollection/

What is the equivalent of apply(this, arguments) in coffeescript?

In javascript you would write something like:
method.apply(this,arguments);
However, how do you translate it to coffeescript?:
method.apply(#, arguments)
Is there a different name for the arguments variable?
Using splats you can use the cleaner coffeescript syntax:
caller: ->
#method arguments...
The above compiles to the following Javascript:
caller: function() {
return this.method.apply(this, arguments);
}
argumentsis available in coffee-script, too. So you can do:
method.apply #, arguments
If you want it to work exactly like javascript, you probably could, but coffeescript has "splats" for what you are probably trying to accomplish. Here's the explanation from coffeescript.org:
gold = silver = rest = "unknown"
awardMedals = (first, second, others...) ->
gold = first
silver = second
rest = others
contenders = [
"Michael Phelps"
"Liu Xiang"
"Yao Ming"
"Allyson Felix"
"Shawn Johnson"
"Roman Sebrle"
"Guo Jingjing"
"Tyson Gay"
"Asafa Powell"
"Usain Bolt"
]
awardMedals contenders...
alert "Gold: " + gold
alert "Silver: " + silver
alert "The Field: " + rest

Lucene.NET stemming problem

I'm running into a problem using the SnowBallAnalyzer in Lucene.NET. It works great for some words, but others it doesn't find any results on at all, and I'm not sure how to dig into this further to find out what is happening. I am testing the search on the USDA Food Description file which can be found here (http://www.ars.usda.gov/SP2UserFiles/Place/12354500/Data/SR23/asc/FOOD_DES.txt). I'm using the English stemming algorithm. I get the following results when searching for "eggs":
Bagels, egg
Bread, egg
Egg, whole, raw, fresh
Egg, white, raw, fresh
Egg, yolk, raw, fresh
Egg, yolk, raw, frozen
Egg, whole, cooked, fried
...
Those results are great. However I get no results at all when searching for "apple". When I use the StandardAnalyzer, and search for "apple" I get the following results.
Croissants, apple
Strudel, apple,
Babyfood, juice, apple
Babyfood, apple-banana juice
...
Not the best results, but at least it's showing something. Anyone know why the stemming analyzer would be filtering in such a way that I would not get any results?
Edit: Here is my prototype code that I'm working with.
static string[] Search(string searchTerm)
{
//Lucene.Net.Analysis.Analyzer analyzer = new Lucene.Net.Analysis.Snowball.SnowballAnalyzer("English");
Lucene.Net.Analysis.Analyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer();
Lucene.Net.QueryParsers.QueryParser parser = new Lucene.Net.QueryParsers.QueryParser(Lucene.Net.Util.Version.LUCENE_29, "text", analyzer);
Lucene.Net.Search.Query query = parser.Parse(searchTerm);
Lucene.Net.Search.Searcher searcher = new Lucene.Net.Search.IndexSearcher(Lucene.Net.Store.FSDirectory.Open(new DirectoryInfo("./index/")), true);
var topDocs = searcher.Search(query, null, 10);
List<string> results = new List<string>();
foreach(var scoreDoc in topDocs.scoreDocs)
{
results.Add(searcher.Doc(scoreDoc.doc).Get("raw"));
}
return results.ToArray();
}
Are you sure you used Lucene.Net.Analysis.Snowball.SnowballAnalyzer("English") to write your index ? You have to use the same analyzer to write and query the index.