Help needed: glob pattern generator

  • 19 Replies
  • 4700 Views
*

frankinstien

  • Replicant
  • ********
  • 642
    • 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
  • ********
  • 642
    • 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
  • *
  • 1302
  • 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
  • ********
  • 642
    • 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.

 


OpenAI Speech-to-Speech Reasoning Demo
by ivan.moony (AI News )
March 28, 2024, 01:31:53 pm
Say good-bye to GPUs...
by MikeB (AI News )
March 23, 2024, 09:23:52 am
Google Bard report
by ivan.moony (AI News )
February 14, 2024, 04:42:23 pm
Elon Musk's xAI Grok Chatbot
by MikeB (AI News )
December 11, 2023, 06:26:33 am
Nvidia Hype
by 8pla.net (AI News )
December 06, 2023, 10:04:52 pm
How will the OpenAI CEO being Fired affect ChatGPT?
by 8pla.net (AI News )
December 06, 2023, 09:54:25 pm
Independent AI sovereignties
by WriterOfMinds (AI News )
November 08, 2023, 04:51:21 am
LLaMA2 Meta's chatbot released
by 8pla.net (AI News )
October 18, 2023, 11:41:21 pm

Users Online

363 Guests, 0 Users

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

Articles