Help needed: glob pattern generator

  • 19 Replies
  • 1726 Views
*

frankinstien

  • Replicant
  • ********
  • 618
    • Knowledgeable Machines
Re: Help needed: glob pattern generator
« Reply #15 on: October 03, 2021, 08:18:29 pm »
I made modifications to force the code to only count a word once when checking within the same sentence.

*

frankinstien

  • Replicant
  • ********
  • 618
    • Knowledgeable Machines
Re: Help needed: glob pattern generator
« Reply #16 on: October 03, 2021, 11:03:12 pm »
I placed a sequence check so if not all sentences are in a somewhat similar sequence it will print no matches.

Code
using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
public static void Main()
{
List<string> alreadyCounted = new List<string>();
var counts = new Dictionary<string, Tally>();
string[] sets = new string[] { "so I have a car, but this car is used", "so I have a bicycle", "so you have a car", "so, I have a knife too" };
foreach (String aSet in sets)
{
var sentenceSplit = aSet.Split(new char[] { ' ', ',' });
alreadyCounted.Clear();
int pos = 0;
foreach (String aWord in sentenceSplit)
{
pos++;
var trimmed = aWord.Trim();
if (counts.ContainsKey(trimmed) && !alreadyCounted.Contains(trimmed))
{
counts[trimmed].Count = counts[trimmed].Count + 1;
counts[trimmed].Positions.Add(pos);
}
else if(!alreadyCounted.Contains(trimmed))
counts.Add(trimmed, new Tally(1, pos));
alreadyCounted.Add(trimmed);
}
}

var results = counts.Where(x => x.Value.Count == sets.Length);
if (results != null && CheckSequence(results.ToList()))
{
foreach (KeyValuePair<string, Tally> entry in results)
{
Console.Write(entry.Key + ": ");
foreach(int pos in entry.Value.Positions)
{
Console.Write(pos.ToString() + ",");
}
Console.WriteLine("");
}
}
else
Console.WriteLine("No matches that are sequentially similar.");
}

private static bool CheckSequence(List<KeyValuePair<string, Tally>> results)
        {
foreach (KeyValuePair<string, Tally> anEntry in results)
{
int temp = anEntry.Value.Positions[0];
for (int idx = 1; idx <anEntry.Value.Positions.Count; idx++)
                {
if (temp > anEntry.Value.Positions[idx])
{
return false;
}

temp = anEntry.Value.Positions[idx];

}
}

return true;
        }
}

sealed class Tally
{
public int Count {set; get;}
public List<int> Positions {set; get;}

public Tally(int count, int pos)
{
Count = count;
Positions = new List<int>();
Positions.Add(pos);
}
}

*

8pla.net

  • Trusty Member
  • ***********
  • Eve
  • *
  • 1278
  • TV News. Pub. UAL (PhD). Robitron Mod. LPC Judge.
    • 8pla.net
Re: Help needed: glob pattern generator
« Reply #17 on: October 05, 2021, 07:15:49 am »
I was thinking word frequency analysis...

Code
<?php
$inputs = <<<EOD
so I have a car
so I have a bicycle
so you have a car
EOD;
$table=array_count_values(str_word_count($inputs, 1));
foreach($table as $word=>$freq){
if($freq==3){
   $words[]=$word;
}
}
echo var_export($words,TRUE);
?>

array (
  0 => 'so',
  1 => 'have',
  2 => 'a',
)
My Very Enormous Monster Just Stopped Using Nine

*

Zero

  • Eve
  • ***********
  • 1287
Re: Help needed: glob pattern generator
« Reply #18 on: October 05, 2021, 10:17:08 pm »
Check this one:
Code: text
    "car I have a car, but this car is used",
    "so I have a car",
    "I so car have you"
Mine is wrong...
Getting:
Code: text
"* I* have *"
Expected:
Code: text
"*I * have *"

*

frankinstien

  • Replicant
  • ********
  • 618
    • Knowledgeable Machines
Re: Help needed: glob pattern generator
« Reply #19 on: October 06, 2021, 08:22:19 pm »
Check this one:
Code: text
    "car I have a car, but this car is used",
    "so I have a car",
    "I so car have you"
Mine is wrong...
Getting:
Code: text
"* I* have *"
Expected:
Code: text
"*I * have *"

Trim the text for each word parsed, that would remove the space. If you look at my code it does that and you could also modify it, easily, to sense the number of sentences with a pattern and list those with it and without it.

 


Users Online

45 Guests, 2 Users
Users active in past 15 minutes:
8pla.net, squarebear
[Trusty Member]

Most Online Today: 87. Most Online Ever: 2369 (November 21, 2020, 04:08:13 pm)

Articles