Reputation: 43
I have a List of words and a List of sentences. I want to know which words can be found in which sentences.
Here is my code:
List<string> sentences = new List<string>();
List<string> words = new List<string>();
sentences.Add("Gallia est omnis divisa in partes tres, quarum unam incolunt Belgae, aliam Aquitani, tertiam qui ipsorum lingua Celtae, nostra Galli appellantur.");
sentences.Add("Alea iacta est.");
sentences.Add("Libenter homines id, quod volunt, credunt.");
words.Add("est");
words.Add("homines");
List<string> myResults = sentences
.Where(sentence => words
.Any(word => sentence.Contains(word)))
.ToList();
What i need is a list of Tuples. With the sentence and the word, that was found in the sentence.
Upvotes: 4
Views: 1560
Reputation: 186668
First, we have to define what is word. Let it be any combination of letters and apostrophes.
Regex regex = new Regex(@"[\p{L}']+");
Second, we should think over on what shall we do with case. Let's implement case insensitive routine:
HashSet<string> wordsToFind = new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
"est",
"homines"
};
Then we can use Regex
to match words in the sentences, and Linq to query the sentences:
Code:
var actualWords = sentences
.Select((text, index) => new {
text = text,
index = index,
words = regex
.Matches(text)
.Cast<Match>()
.Select(match => match.Value)
.ToArray()
})
.SelectMany(item => item.words
.Where(word => wordsToFind.Contains(word))
.Select(word => Tuple.Create(word, item.index + 1)));
string report = string.Join(Environment.NewLine, actualWords);
Console.Write(report);
Outcome:
(est, 1) // est appears in the 1st sentence
(est, 2) // est appears in the 2nd sentence as well
(homines, 3) // homines appears in the 3d sentence
If you want Tuple<string, string>
for word, sentence, just change Tuple.Create(word, item.index + 1)
for Tuple.Create(word, item.text)
in the last Select
Upvotes: 7
Reputation: 943
You can try this way,
var result = from sentence in sentences
from word in words
where sentence.Contains(word)
select Tuple.Create(sentence, word);
Upvotes: 3
Reputation: 117037
Do you just mean this:
IEnumerable<(string, string)> query =
from sentence in sentences
from word in words
where sentence.Contains(word)
select (sentence, word);
That gives:
Upvotes: 4