Find another word between two lines

I have a large list of phrases such as

"Nola jumped off the cliff"
"Loroy jumped off the cliff"
"Nola jumped off the couch"
"Leroy lept off the couch"

I need to find every point of the phrase, which is a different word, and add this word to the node, which is a list of words that can be used at this position in the phrase. So we are done.

"Node1(1) Node2(1) off the Node3(1)"
"Node1(2) Node2(1) off the Node3(1)"
...etc

Where node 1 represents a list of names (Nola, Leroy), node2 represents a list of actions (jumped, lept), and node3 ends with a list of locations (cliff, couch)

The idea is to take a list of phrases and automatically create nodes and fill it with words that can be used in this node phrase.

So 1st, how would I generate a list of phrase nodes? I was not able to figure out how to compare the two sentences and see if they exactly match the minus of one word.

2- , , ? (, )

+5
3

, . #, #.

:

string phrase1 = "Nola jumped off the cliff";
string phrase2 = "Juri jumped off the coach";

//Split phrases into word arrays
var phrase1Words = phrase1.Split(' ');
var phrase2Words = phrase2.Split(' ');

//Find the intersection of the two arrays (find the matching words)
var wordsInPhrase1and2 = phrase1Words.Intersect(phrase2Words);

//The number of words that differ 
int wordDelta = phrase1Words.Count() - wordsInPhrase1and2.Count();

//Find the differing words
var wordsOnlyInPhrase1 = phrase1Words.Except(wordsInPhrase1and2);
var wordsOnlyInPhrase2 = phrase2Words.Except(wordsInPhrase1and2);

, , , LINQ Intersect, Except ..

NominSim.

+5

Linq, :

var phrases = new List<string> {
           "Nola jumped off the cliff",
           "Loroy jumped off the cliff",
           "Nola jumped off the couch",
           "Leroy lept off the couch"
                           };

var sets = (from p in phrases
            from indexedWord in p.Split(' ').Select((word,idx) => new {idx,word})
            group indexedWord by indexedWord.idx into g
            select g.Select(e => e.word).Distinct()).ToArray();


var allCombos = from w1 in sets[0]
                from w2 in sets[1]
                from w3 in sets[2]
                from w4 in sets[3]
                from w5 in sets[4]
                select String.Format("{0} {1} {2} {3} {4}.", w1, w2, w3, w4, w5);

, . =)

+1

- :

        HashSet<String>[] NodeList = new HashSet<String>[phraseLength];
        for (int i = 0; i < phraseLength; i++)
        {
            NodeList[i] = new HashSet<string>();
        }

        foreach (String phrase in PhraseList)
        {
            string[] phraseStrings = phrase.Split(' ');
            for (int i = 0; i < phraseLength; i++)
            {
                if(!NodeList[i].Contains(phraseStrings[i]))
                {
                    NodeList[i].Add(phraseStrings[i]);
                }
            }
        }

Then, when you create your sentences, you can just cross the NodeList and select a line from each node, if you want to do this randomly, maybe something like this:

        String sentence = "";
        foreach (HashSet<String> Node in NodeList)
        {
            Random rand = new Random();
            sentence += Node.ToArray()[rand.Next(0, Node.Count)];
        }

Keep in mind that a HashSet is probably not the best idea if you need to access it randomly.

0
source

All Articles