using System;
using System. Collections. Generic;
using System. Linq;
using System. Text;
namespace JSM_VS
{
class JsmProcessor
{
/// <summary>
/// Функция разрешения конфликтов гипотез (количество гипотез)
/// </summary>
/// <param name="coincidedHyp"></param>
/// <param name="k"></param>
/// <returns></returns>
static char ConflictResolution(List<Hypothesis> coincidedHyp, double k)
{
int countPlusHyp = 0, countMinusHyp = 0;
for (int i = 0; i < coincidedHyp. Count; i++)
if (coincidedHyp[i].type == '+')
countPlusHyp++;
else
countMinusHyp++;
double res = (double)countPlusHyp - k * (double)countMinusHyp;
if (res > 0)
return '+';
else if (res < 0)
return '-';
else
return 'n';
}
/// <summary>
/// Функция разрешения конфликтов гипотез (суммарное количество характеристик во всех гипотезах)
/// </summary>
/// <param name="coincidedHyp"></param>
/// <param name="k"></param>
/// <returns></returns>
static char ConflictResolution2(List<Hypothesis> coincidedHyp, double k)
{
int countPlusValue = 0, countMinusValue = 0;
for (int i = 0; i < coincidedHyp. Count; i++)
if (coincidedHyp[i].type == '+')
countPlusValue += coincidedHyp[i].setValues. Count;
else
countMinusValue += coincidedHyp[i].setValues. Count;
double res = countPlusValue - k * countMinusValue;
if (res > 0)
return '+';
else if (res < 0)
return '-';
else
return 'n';
}
/// <summary>
/// Функция разрешения конфликтов гипотез (суммарное количество родителей всех гипотез)
/// </summary>
/// <param name="coincidedHyp"></param>
/// <param name="k"></param>
/// <returns></returns>
static char ConflictResolution3(List<Hypothesis> coincidedHyp, double k)
{
int countPlusParent = 0, countMinusParent = 0;
for (int i = 0; i < coincidedHyp. Count; i++)
if (coincidedHyp[i].type == '+')
countPlusParent += coincidedHyp[i].setParents. Count;
else
countMinusParent += coincidedHyp[i].setParents. Count;
double res = countPlusParent - k * countMinusParent;
if (res > 0)
return '+';
else if (res < 0)
return '-';
else
return 'n';
}
/// <summary>
/// Функция разрешения конфликтов гипотез (произведение количества характеристик на количество родителей)
/// </summary>
/// <param name="coincidedHyp"></param>
/// <param name="k"></param>
/// <returns></returns>
static char ConflictResolution4(List<Hypothesis> coincidedHyp, double k)
{
int countPlusHyp = 0, countMinusHyp = 0;
for (int i = 0; i < coincidedHyp. Count; i++)
if (coincidedHyp[i].type == '+')
countPlusHyp += coincidedHyp[i].setValues. Count * coincidedHyp[i].setParents. Count;
else
countMinusHyp += coincidedHyp[i].setValues. Count * coincidedHyp[i].setParents. Count;
double res = countPlusHyp - k * countMinusHyp;
if (res > 0)
return '+';
else if (res < 0)
return '-';
else
return 'n';
}
/// <summary>
/// Функция разрешения конфликтов гипотез (отношение произведения количества характеристик на количество родителей
/// к общему количеству характеристик в совпавших с текстом гипотезах одного класса)
/// </summary>
/// <param name="coincidedHyp"></param>
/// <param name="k"></param>
/// <returns></returns>
static char ConflictResolution5(List<Hypothesis> coincidedHyp, double k)
{
int commonNumValuesPlusHyp = 0, commonNumValuesMinusHyp = 0;
for (int i = 0; i < coincidedHyp. Count; i++)
if (coincidedHyp[i].type == '+')
commonNumValuesPlusHyp += coincidedHyp[i].setValues. Count;
else
commonNumValuesMinusHyp += coincidedHyp[i].setValues. Count;
int countPlusHyp = 0, countMinusHyp = 0;
for (int i = 0; i < coincidedHyp. Count; i++)
if (coincidedHyp[i].type == '+')
countPlusHyp += coincidedHyp[i].setValues. Count * coincidedHyp[i].setParents. Count;
else
countMinusHyp += coincidedHyp[i].setValues. Count * coincidedHyp[i].setParents. Count;
double res = (double)countPlusHyp / (double)commonNumValuesPlusHyp -
k * (double)countMinusHyp / (double)commonNumValuesMinusHyp;
if (res > 0)
return '+';
else if (res < 0)
return '-';
else
return 'n';
}
/// <summary>
/// Функция разрешения конфликтов гипотез (отношение произведения количества характеристик на количество родителей
/// к общему количеству родителей в совпавших с текстом гипотезах одного класса)
/// </summary>
/// <param name="coincidedHyp"></param>
/// <param name="k"></param>
/// <returns></returns>
static char ConflictResolution6(List<Hypothesis> coincidedHyp, double k)
{
int commonNumParentsPlusHyp = 0, commonNumParentsMinusHyp = 0;
for (int i = 0; i < coincidedHyp. Count; i++)
if (coincidedHyp[i].type == '+')
commonNumParentsPlusHyp += coincidedHyp[i].setParents. Count;
else
commonNumParentsMinusHyp += coincidedHyp[i].setParents. Count;
int countPlusHyp = 0, countMinusHyp = 0;
for (int i = 0; i < coincidedHyp. Count; i++)
if (coincidedHyp[i].type == '+')
countPlusHyp += coincidedHyp[i].setValues. Count * coincidedHyp[i].setParents. Count;
else
countMinusHyp += coincidedHyp[i].setValues. Count * coincidedHyp[i].setParents. Count;
double res = (double)countPlusHyp / (double)commonNumParentsPlusHyp -
k * (double)countMinusHyp / (double)commonNumParentsMinusHyp;
if (res > 0)
return '+';
else if (res < 0)
return '-';
else
return 'n';
}
/// <summary>
/// Функция классификации текстов
/// </summary>
/// <param name="res"></param>
/// <param name="coincidedHyp"></param>
/// <param name="k"></param>
/// <param name="typeFuncResolution"></param>
public void Classification(char[] res, List<Hypothesis>[] coincidedHyp, double k, int typeFuncResolution)
{
for (int i = 0; i < coincidedHyp. Length; i++)
switch (typeFuncResolution)
{
case 1:
ConflictResolution(coincidedHyp[i], k);
res[i] = ConflictResolution(coincidedHyp[i], k);
break;
case 2:
ConflictResolution2(coincidedHyp[i], k);
res[i] = ConflictResolution(coincidedHyp[i], k);
break;
case 3:
ConflictResolution3(coincidedHyp[i], k);
res[i] = ConflictResolution(coincidedHyp[i], k);
break;
case 4:
ConflictResolution(coincidedHyp[i], k);
res[i] = ConflictResolution(coincidedHyp[i], k);
break;
case 5:
ConflictResolution(coincidedHyp[i], k);
res[i] = ConflictResolution(coincidedHyp[i], k);
break;
case 6:
ConflictResolution(coincidedHyp[i], k);
res[i] = ConflictResolution(coincidedHyp[i], k);
break;
}
}
/// <summary>
/// Процедура индукции
/// </summary>
/// <param name="hypotheses"></param>
/// <param name="setSamples"></param>
public void Induction(List<Hypothesis> hypotheses, TextInfo[] setSamples)
{
for (int i = 0; i < setSamples. Length; i++)
{
int hypCount1 = hypotheses. Count;
for (int j = 0; j < hypCount1; j++)
{
// Находим пересечение очередного объекта с текущей гипотезой
HashSet<int> newIntersection = new HashSet<int>();
newIntersection. UnionWith(setSamples[i].setValues);
newIntersection. IntersectWith(hypotheses[j].setValues);
// Если пересечение совпадает с текущей гипотезой,
// то добавляем объект в список родителей гипотезы
if (hypotheses[j].setValues. Count == newIntersection. Count &&
hypotheses[j].setValues. SetEquals(newIntersection))
{
hypotheses[j].setParents. Add(i);
}
// иначе проверяем в какие ещё гипотезы входит найденное пересечение
// для определения всех родителей новой гипотезы
else
{
Hypothesis newHyp = new Hypothesis();
newHyp. setValues = new HashSet<int>();
newHyp. setValues. UnionWith(newIntersection);
newHyp. setParents = new HashSet<int>();
newHyp. setParents. UnionWith(hypotheses[j].setParents);
newHyp. setParents. Add(i);
newHyp. type = hypotheses[j].type;
int hypCount2 = hypotheses. Count;
int k = 0;
while (k < hypCount2)
{
if (hypotheses[k].setValues. Count == newHyp. setValues. Count &&
hypotheses[k].setValues. SetEquals(newHyp. setValues))
{
hypotheses[k].setParents. UnionWith(newHyp. setParents);
break;
}
else
{
if (hypotheses[k].setValues. SetEquals(newHyp. setValues))
newHyp. setParents. UnionWith(hypotheses[k].setParents);
}
k++;
}
if (k == hypCount2)
hypotheses. Add(newHyp);
}
}
int q = 0;
while (q < i)
{
HashSet<int> newIntersection = new HashSet<int>();
newIntersection. UnionWith(setSamples[i].setValues);
newIntersection. IntersectWith(setSamples[q].setValues);
if (newIntersection. Count == setSamples[i].setValues. Count &&
setSamples[i].setValues. SetEquals(newIntersection))
{
break;
}
q++;
}
if (q == i)
{
Hypothesis newHyp = new Hypothesis();
newHyp. setValues = new HashSet<int>();
newHyp. setValues. UnionWith(setSamples[i].setValues);
newHyp. setParents = new HashSet<int>();
newHyp. setParents. Add(i);
newHyp. type = setSamples[i].type;
hypotheses. Add(newHyp);
}
}
}
/// <summary>
/// Процедура аналогии
/// </summary>
/// <param name="plusHypotheses"></param>
/// <param name="minusHypotheses"></param>
/// <param name="setSamples"></param>
/// <param name="coincidedHyp"></param>
public void Analogy(List<Hypothesis> plusHypotheses, List<Hypothesis> minusHypotheses,
TextInfo[] setSamples, out List<Hypothesis>[] coincidedHyp)
{
coincidedHyp = new List<Hypothesis>[setSamples. Length];
for (int i = 0; i < coincidedHyp. Length; i++)
coincidedHyp[i] = new List<Hypothesis>();
for (int i = 0; i < setSamples. Length; i++)
{
// Поиск совпадений с положительными гипотезами
for (int j = 0; j < plusHypotheses. Count; j++)
{
HashSet<int> intersection = new HashSet<int>();
intersection. UnionWith(setSamples[i].setValues);
intersection. IntersectWith(plusHypotheses[j].setValues);
if (intersection. SetEquals(plusHypotheses[j].setValues))
coincidedHyp[i].Add(plusHypotheses[j]);
}
// Поиск совпадений с отрицательными гипотезами
for (int j = 0; j < minusHypotheses. Count; j++)
{
HashSet<int> intersection = new HashSet<int>();
intersection. UnionWith(setSamples[i].setValues);
intersection. IntersectWith(minusHypotheses[j].setValues);
if (intersection. SetEquals(minusHypotheses[j].setValues))
coincidedHyp[i].Add(minusHypotheses[j]);
}
}
}
}
}
|
Из за большого объема этот материал размещен на нескольких страницах:
1 2 3 4 5 6 7 8 9 |


