Help needed: glob pattern generator

  • 19 Replies
  • 689 Views
*

frankinstien

  • Starship Trooper
  • *******
  • 445
    • 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

  • Starship Trooper
  • *******
  • 445
    • 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
  • *
  • 1262
  • 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

  • Trusty Member
  • **********
  • Millennium Man
  • *
  • 1163
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 *"
Google is a plague, a disease. It is the metastatic cancer of the human species.

*

frankinstien

  • Starship Trooper
  • *******
  • 445
    • 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.

 


New challenge: Online Turing test
by chattable (AI News )
October 09, 2021, 11:47:42 am
Spooky?
by MagnusWootton (AI News )
September 24, 2021, 11:19:09 am
OpenAI Shuts Down Chatbot Project To Prevent 'Possible Misuse'
by LOCKSUIT (AI News )
September 23, 2021, 04:07:25 pm
Beautiful new work robot.
by MagnusWootton (Robotics News)
September 15, 2021, 01:00:31 pm

Users Online

129 Guests, 0 Users

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

Articles