Add a sample project for PROSE

This commit is contained in:
Alex Polozov 2015-12-10 16:52:44 -08:00
Родитель b15bc921ed
Коммит b79816503f
26 изменённых файлов: 1303 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,40 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ProseSample.Substrings")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ProseSample.Substrings")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("41e9a9b1-b013-4a87-9759-e3004fef8a81")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

Просмотреть файл

@ -0,0 +1,119 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{41E9A9B1-B013-4A87-9759-E3004FEF8A81}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ProseSample.Substrings</RootNamespace>
<AssemblyName>ProseSample.Substrings</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Antlr4.Runtime.net45, Version=4.3.0.0, Culture=neutral, PublicKeyToken=eb42632606e9261f, processorArchitecture=MSIL">
<HintPath>..\packages\Antlr4.Runtime.4.3.0\lib\net45\Antlr4.Runtime.net45.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="CsQuery, Version=1.3.5.124, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\CsQuery.1.3.5-beta5\lib\net40\CsQuery.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="dslc, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\dslc.exe</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Experimental.Collections, Version=1.0.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Experimental.Collections.1.0.3-alpha\lib\portable-net45+win8+wp8\Microsoft.Experimental.Collections.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProgramSynthesis, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\Microsoft.ProgramSynthesis.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProgramSynthesis.Extraction, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\Microsoft.ProgramSynthesis.Extraction.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProgramSynthesis.Extraction.Text, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\Microsoft.ProgramSynthesis.Extraction.Text.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProgramSynthesis.Extraction.Text.Learning, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\Microsoft.ProgramSynthesis.Extraction.Text.Learning.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProgramSynthesis.Extraction.Text.Semantics, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\Microsoft.ProgramSynthesis.Extraction.Text.Semantics.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProgramSynthesis.Learning, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\Microsoft.ProgramSynthesis.Learning.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProgramSynthesis.Utils, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\Microsoft.ProgramSynthesis.Utils.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Collections.Immutable, Version=1.1.36.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Collections.Immutable.1.1.36\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Interactive, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Ix-Main.1.2.5\lib\net45\System.Interactive.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Reflection.Metadata, Version=1.0.21.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Reflection.Metadata.1.0.21\lib\portable-net45+win8\System.Reflection.Metadata.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RankingScore.cs" />
<Compile Include="RegexUtils.cs" />
<Compile Include="Semantics.cs" />
<Compile Include="WitnessFunctions.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="ProseSample.Substrings.grammar">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

Просмотреть файл

@ -0,0 +1,24 @@
reference 'Microsoft.ProgramSynthesis.Extraction.Text.Semantics.dll';
reference 'ProseSample.Substrings.dll';
using System.Text.RegularExpressions;
using Microsoft.ProgramSynthesis.Extraction.Text.Semantics;
using ProseSample.Substrings;
using semantics ProseSample.Substrings.Semantics;
using learners ProseSample.Substrings.WitnessFunctions;
language ProseSample_Substrings;
@vardefault[VariableScore]
feature double Score = RankingScore;
@start StringRegion sub := SubStr(v, posPair);
Tuple<uint?, uint?> posPair := PosPair(pos, pos) = Pair(pos, pos);
uint? pos := AbsPos(v, k) | RegPos(v, rr, k);
Tuple<RegularExpression, RegularExpression> rr := BoundaryPair(r, r) = Pair(r, r);
@feature[Score=RegexScore] RegularExpression r;
@input StringRegion v;
@feature[Score=KScore] int k;

Просмотреть файл

@ -0,0 +1,40 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
using Microsoft.ProgramSynthesis;
using Microsoft.ProgramSynthesis.Extraction.Text.Semantics;
namespace ProseSample.Substrings
{
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public static class RankingScore
{
public const double VariableScore = 0;
[FeatureCalculator("SubStr")]
public static double Score_SubStr(double x, double pp) => Math.Log(pp);
[FeatureCalculator("PosPair")]
public static double Score_PosPair(double pp1, double pp2) => pp1 * pp2;
[FeatureCalculator("AbsPos")]
public static double Score_AbsPos(double x, double k)
{
k = 1 / k - 1;
// Prefer absolute positions to regex positions if k is small
return Math.Max(10 * Token.MinScore - (k - 1) * 3 * Token.MinScore, 1 / k);
}
[FeatureCalculator(Method = CalculationMethod.FromLiteral)]
public static double KScore(int k) => k >= 0 ? 1.0 / (k + 1.0) : 1.0 / (-k + 1.1);
[FeatureCalculator("BoundaryPair")]
public static double Score_BoundaryPair(double r1, double r2) => r1 + r2;
[FeatureCalculator("RegPos")]
public static double Score_RegPos(double x, double rr, double k) => rr * k;
[FeatureCalculator(Method = CalculationMethod.FromLiteral)]
public static double RegexScore(RegularExpression r) => r.Score;
}
}

Просмотреть файл

@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.ProgramSynthesis.Extraction.Text.Semantics;
namespace ProseSample.Substrings
{
static class RegexUtils
{
public static readonly Regex[] Tokens =
{
new Regex(@"", RegexOptions.Compiled), // Epsilon
new Regex(@"\p{Lu}(\p{Ll})+", RegexOptions.Compiled), // Camel Case
new Regex(@"\p{Ll}+", RegexOptions.Compiled), // Lowercase word
new Regex(@"\p{Lu}(\p{Lu})+", RegexOptions.Compiled), // ALL CAPS
new Regex(@"[0-9]+(\,[0-9]{3})*(\.[0-9]+)?", RegexOptions.Compiled), // Number
new Regex(@"[-.\p{Lu}\p{Ll}]+", RegexOptions.Compiled), // Words/dots/hyphens
new Regex(@"[-.\p{Lu}\p{Ll}0-9]+", RegexOptions.Compiled), // Alphanumeric
new Regex(@"\p{Zs}+", RegexOptions.Compiled), // WhiteSpace
new Regex(@"\t", RegexOptions.Compiled), // Tab
new Regex(@",", RegexOptions.Compiled), // Comma
new Regex(@"\.", RegexOptions.Compiled), // Dot
new Regex(@":", RegexOptions.Compiled), // Colon
new Regex(@";", RegexOptions.Compiled), // Semicolon
new Regex(@"!", RegexOptions.Compiled), // Exclamation
new Regex(@"\)", RegexOptions.Compiled), // Right Parenthesis
new Regex(@"\(", RegexOptions.Compiled), // Left Parenthesis
new Regex(@"""", RegexOptions.Compiled), // Double Quote
new Regex(@"'", RegexOptions.Compiled), // Single Quote
new Regex(@"/", RegexOptions.Compiled), // Forward Slash
new Regex(@"\\", RegexOptions.Compiled), // Backward Slash
new Regex(@"-", RegexOptions.Compiled), // Hyphen
new Regex(@"\*", RegexOptions.Compiled), // Star
new Regex(@"\+", RegexOptions.Compiled), // Plus
new Regex(@"_", RegexOptions.Compiled), // Underscore
new Regex(@"=", RegexOptions.Compiled), // Equal
new Regex(@">", RegexOptions.Compiled), // Greater-than
new Regex(@"<", RegexOptions.Compiled), // Left-than
new Regex(@"\]", RegexOptions.Compiled), // Right Bracket
new Regex(@"\[", RegexOptions.Compiled), // Left Bracket
new Regex(@"}", RegexOptions.Compiled), // Right Brace
new Regex(@"{", RegexOptions.Compiled), // Left Brace
new Regex(@"\|", RegexOptions.Compiled), // Bar
new Regex(@"&", RegexOptions.Compiled), // Ampersand
new Regex(@"#", RegexOptions.Compiled), // Hash
new Regex(@"\$", RegexOptions.Compiled), // Dollar
new Regex(@"\^", RegexOptions.Compiled), // Hat
new Regex(@"@", RegexOptions.Compiled), // At
new Regex(@"%", RegexOptions.Compiled), // Percentage
new Regex(@"\?", RegexOptions.Compiled), // Question Mark
new Regex(@"~", RegexOptions.Compiled), // Tilde
new Regex(@"`", RegexOptions.Compiled), // Back Prime
new Regex(@"\u2192", RegexOptions.Compiled), // RightArrow
new Regex(@"\u2190", RegexOptions.Compiled), // LeftArrow
new Regex(@"(?<=\n)[\p{Zs}\t]*(\r)?\n", RegexOptions.Compiled), // Empty Line
new Regex(@"[\p{Zs}\t]*((\r)?\n|^|$)", RegexOptions.Compiled), // Line separator
};
private static Regex[] _leftTokens;
public static readonly RegularExpression Epsilon = RegularExpression.Create();
public static Regex[] LeftTokens =>
_leftTokens ?? (_leftTokens = Tokens.Select(t => new Regex($"(?<={t})", RegexOptions.Compiled)).ToArray());
public static List<PositionMatch> Run(this RegularExpression r, StringRegion v)
=> r.Run(v.Cache, v.Start, v.End);
public static bool MatchesAt(this RegularExpression r, StringRegion v, uint pos)
=> r.MatchesAt(v.Cache, pos, v.Start, v.End);
public static int BinarySearchBy<T>(this IList<T> list, Func<T, int> comparer)
{
int min = 0;
int max = list.Count - 1;
while (min <= max)
{
int mid = min + (max - min) / 2;
int comparison = comparer(list[mid]);
if (comparison == 0)
{
return mid;
}
if (comparison < 0)
{
min = mid + 1;
}
else
{
max = mid - 1;
}
}
return ~min;
}
}
}

Просмотреть файл

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FlashMeta.Core;
using FlashMeta.Core.Rules;
using FlashMeta.Core.Specifications;
using FlashMeta.Core.Synthesis;
namespace MOD2015.Substrings.RelativePositioning {
public static class LeftWitnessFunction {
[WitnessFunction(0)]
public static DisjunctiveExamplesSpecification WitnessLeft(LetRule rule, int parameter,
DisjunctiveExamplesSpecification spec) {
var examples = new Dictionary<State, IEnumerable<object>>();
foreach (State input in spec.ProvidedInputs) {
var leftPositions = new List<object>();
foreach (Tuple<uint?, uint?> example in spec.DisjunctiveExamples[input]) {
leftPositions.Add(example.Item1);
}
examples[input] = leftPositions;
}
return DisjunctiveExamplesSpecification.From(examples);
}
}
}

Просмотреть файл

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using FlashMeta.Core;
using FlashMeta.Core.Rules;
using FlashMeta.Core.Specifications;
using FlashMeta.Core.Synthesis;
namespace MOD2015.Substrings.RelativePositioning {
public static class RightWitnessFunction {
[WitnessFunction(0)]
public static DisjunctiveExamplesSpecification WitnessRight(LetRule rule, int parameter,
DisjunctiveExamplesSpecification spec) {
var examples = new Dictionary<State, IEnumerable<object>>();
foreach (State input in spec.ProvidedInputs) {
var rightPositions = new List<object>();
foreach (Tuple<uint?, uint?> example in spec.DisjunctiveExamples[input]) {
rightPositions.Add(example.Item2);
}
examples[input] = rightPositions;
}
return DisjunctiveExamplesSpecification.From(examples);
}
}
}

Просмотреть файл

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.ProgramSynthesis.Extraction.Text.Semantics;
namespace ProseSample.Substrings
{
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public static class Semantics
{
public static StringRegion SubStr(StringRegion v, Tuple<uint?, uint?> posPair)
{
uint? start = posPair.Item1, end = posPair.Item2;
if (start == null || end == null || start < v.Start || start > v.End || end < v.Start || end > v.End)
return null;
return v.Slice((uint) start, (uint) end);
}
public static uint? AbsPos(StringRegion v, int k)
{
if (Math.Abs(k) > v.Length + 1) return null;
return (uint) (k > 0 ? (v.Start + k - 1) : (v.End + k + 1));
}
public static uint? RegPos(StringRegion v, Tuple<RegularExpression, RegularExpression> rr, int k)
{
List<PositionMatch> ms = rr.Item1.Run(v).Where(m => rr.Item2.MatchesAt(v, m.Right)).ToList();
int index = k > 0 ? (k - 1) : (ms.Count + k);
return index < 0 || index >= ms.Count ? null : (uint?) ms[index].Right;
}
}
}

Просмотреть файл

@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.ProgramSynthesis;
using Microsoft.ProgramSynthesis.Extraction.Text.Semantics;
using Microsoft.ProgramSynthesis.Learning;
using Microsoft.ProgramSynthesis.Rules;
using Microsoft.ProgramSynthesis.Specifications;
using Microsoft.ProgramSynthesis.Utils;
using static ProseSample.Substrings.RegexUtils;
namespace ProseSample.Substrings
{
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public static class WitnessFunctions
{
[WitnessFunction("SubStr", 1)]
public static DisjunctiveExamplesSpec WitnessPositionPair(GrammarRule rule, int parameter,
ExampleSpec spec)
{
var ppExamples = new Dictionary<State, IEnumerable<object>>();
foreach (State input in spec.ProvidedInputs)
{
var v = (StringRegion) input[rule.Body[0]];
var desiredOutput = (StringRegion) spec.Examples[input];
var occurrences = new List<object>();
for (int i = v.Value.IndexOf(desiredOutput.Value, StringComparison.Ordinal);
i >= 0;
i = v.Value.IndexOf(desiredOutput.Value, i + 1, StringComparison.Ordinal))
{
occurrences.Add(Tuple.Create(v.Start + (uint?) i, v.Start + (uint?) i + desiredOutput.Length));
}
ppExamples[input] = occurrences;
}
return DisjunctiveExamplesSpec.From(ppExamples);
}
[WitnessFunction("AbsPos", 1)]
public static DisjunctiveExamplesSpec WitnessK(GrammarRule rule, int parameter,
DisjunctiveExamplesSpec spec)
{
var kExamples = new Dictionary<State, IEnumerable<object>>();
foreach (State input in spec.ProvidedInputs)
{
var v = (StringRegion) input[rule.Body[0]];
var positions = new List<object>();
foreach (uint pos in spec.DisjunctiveExamples[input])
{
positions.Add((int) pos + 1 - (int) v.Start);
positions.Add((int) pos - (int) v.End - 1);
}
kExamples[input] = positions;
}
return DisjunctiveExamplesSpec.From(kExamples);
}
[WitnessFunction("RegPos", 1)]
public static DisjunctiveExamplesSpec WitnessRegexPair(GrammarRule rule, int parameter,
DisjunctiveExamplesSpec spec)
{
var rrExamples = new Dictionary<State, IEnumerable<object>>();
foreach (State input in spec.ProvidedInputs)
{
var v = (StringRegion) input[rule.Body[0]];
var regexes = new List<Tuple<RegularExpression, RegularExpression>>();
foreach (uint pos in spec.DisjunctiveExamples[input])
{
Dictionary<Token, TokenMatch> rightMatches;
if (!v.Cache.TryGetAllMatchesStartingAt(pos, out rightMatches)) continue;
Dictionary<Token, TokenMatch> leftMatches;
if (!v.Cache.TryGetAllMatchesEndingAt(pos, out leftMatches)) continue;
var leftRegexes = leftMatches.Keys.Select(RegularExpression.Create).Append(Epsilon);
var rightRegexes = rightMatches.Keys.Select(RegularExpression.Create).Append(Epsilon);
var regexPairs = from l in leftRegexes from r in rightRegexes select Tuple.Create(l, r);
regexes.AddRange(regexPairs);
}
rrExamples[input] = regexes;
}
return DisjunctiveExamplesSpec.From(rrExamples);
}
[WitnessFunction("RegPos", 2, DependsOnParameters = new[] { 1 })]
public static DisjunctiveExamplesSpec WitnessRegexCount(GrammarRule rule, int parameter,
DisjunctiveExamplesSpec spec,
ExampleSpec regexBinding)
{
var kExamples = new Dictionary<State, IEnumerable<object>>();
foreach (State input in spec.ProvidedInputs)
{
var v = (StringRegion) input[rule.Body[0]];
var rr = (Tuple<RegularExpression, RegularExpression>) regexBinding.Examples[input];
var ks = new List<object>();
foreach (uint pos in spec.DisjunctiveExamples[input])
{
var ms = rr.Item1.Run(v).Where(m => rr.Item2.MatchesAt(v, m.Right)).ToArray();
int index = ms.BinarySearchBy(m => m.Right.CompareTo(pos));
if (index < 0) return null;
ks.Add(index + 1);
ks.Add(index - ms.Length);
}
kExamples[input] = ks;
}
return DisjunctiveExamplesSpec.From(kExamples);
}
}
}

Просмотреть файл

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Antlr4.Runtime" version="4.3.0" targetFramework="net452" />
<package id="CsQuery" version="1.3.5-beta5" targetFramework="net452" />
<package id="Ix-Main" version="1.2.5" targetFramework="net452" />
<package id="Microsoft.Experimental.Collections" version="1.0.3-alpha" targetFramework="net452" />
<package id="Microsoft.ProgramSynthesis" version="0.1.1-preview-1" targetFramework="net452" />
<package id="System.Collections.Immutable" version="1.1.36" targetFramework="net452" />
<package id="System.Reflection.Metadata" version="1.0.21" targetFramework="net452" />
</packages>

Просмотреть файл

@ -0,0 +1,40 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ProseSample.TextExtraction")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ProseSample.TextExtraction")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("5aa217f9-deab-46be-81be-afd00948ac50")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

Просмотреть файл

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{5AA217F9-DEAB-46BE-81BE-AFD00948AC50}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ProseSample.TextExtraction</RootNamespace>
<AssemblyName>ProseSample.TextExtraction</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Antlr4.Runtime.net45, Version=4.3.0.0, Culture=neutral, PublicKeyToken=eb42632606e9261f, processorArchitecture=MSIL">
<HintPath>..\packages\Antlr4.Runtime.4.3.0\lib\net45\Antlr4.Runtime.net45.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="CsQuery, Version=1.3.5.124, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\CsQuery.1.3.5-beta5\lib\net40\CsQuery.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="dslc, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\dslc.exe</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Experimental.Collections, Version=1.0.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Experimental.Collections.1.0.3-alpha\lib\portable-net45+win8+wp8\Microsoft.Experimental.Collections.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProgramSynthesis, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\Microsoft.ProgramSynthesis.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProgramSynthesis.Extraction, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\Microsoft.ProgramSynthesis.Extraction.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProgramSynthesis.Extraction.Text, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\Microsoft.ProgramSynthesis.Extraction.Text.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProgramSynthesis.Extraction.Text.Learning, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\Microsoft.ProgramSynthesis.Extraction.Text.Learning.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProgramSynthesis.Extraction.Text.Semantics, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\Microsoft.ProgramSynthesis.Extraction.Text.Semantics.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProgramSynthesis.Learning, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\Microsoft.ProgramSynthesis.Learning.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProgramSynthesis.Utils, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\Microsoft.ProgramSynthesis.Utils.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Collections.Immutable, Version=1.1.36.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Collections.Immutable.1.1.36\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Interactive, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Ix-Main.1.2.5\lib\net45\System.Interactive.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Reflection.Metadata, Version=1.0.21.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Reflection.Metadata.1.0.21\lib\portable-net45+win8\System.Reflection.Metadata.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RankingScore.cs" />
<Compile Include="Semantics.cs" />
<Compile Include="WitnessFunctions.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="ProseSample.TextExtraction.grammar">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ProseSample.Substrings\ProseSample.Substrings.csproj">
<Project>{41e9a9b1-b013-4a87-9759-e3004fef8a81}</Project>
<Name>ProseSample.Substrings</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

Просмотреть файл

@ -0,0 +1,22 @@
reference 'ProseSample.Substrings.grammar.xml';
reference 'ProseSample.TextExtraction.dll';
reference 'Microsoft.ProgramSynthesis.Utils.dll';
reference 'Microsoft.ProgramSynthesis.Extraction.Text.Semantics.dll';
using System.Text.RegularExpressions;
using Microsoft.ProgramSynthesis.Extraction.Text.Semantics;
using ProseSample.TextExtraction;
using semantics ProseSample.TextExtraction.Semantics;
using learners ProseSample.TextExtraction.WitnessFunctions;
language ProseSample_TextExtraction;
@vardefault[VariableScore]
feature double Score = RankingScore;
@start IEnumerable<StringRegion> spans := LinesMap(selection, lines) = Map(\line: StringRegion => selection, lines);
StringRegion selection := @featuremap[Score=Score] @subst[v := line] ProseSample_Substrings.sub;
IEnumerable<StringRegion> lines := SplitLines(document);
@input StringRegion document;

Просмотреть файл

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ProgramSynthesis;
using Microsoft.ProgramSynthesis.Extraction.Text.Semantics;
namespace ProseSample.TextExtraction
{
public static class RankingScore
{
public const double VariableScore = 0;
[FeatureCalculator("SplitLines")]
public static double Score_SplitLines(double document) => document;
[FeatureCalculator("LinesMap")]
public static double Score_LinesMap(double selection, double lines) => selection + lines + Token.MinScore * 20;
}
}

Просмотреть файл

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ProgramSynthesis.Extraction.Text.Semantics;
namespace ProseSample.TextExtraction
{
public static class Semantics
{
public static IEnumerable<StringRegion> SplitLines(StringRegion document)
{
Token lineBreak = StringLearningCache.GetStaticTokenByName(Token.LineSeparatorName);
CachedList lineBreakPositions;
if (!document.Cache.TryGetMatchPositionsFor(lineBreak, out lineBreakPositions))
return new[] { document };
var lines = new List<StringRegion>();
for (int i = 0; i < lineBreakPositions.Count - 1; i++)
{
if (lineBreakPositions[i + 1].Length == 0) continue;
lines.Add(document.Slice(lineBreakPositions[i].Right, lineBreakPositions[i + 1].Position));
}
return lines;
}
}
}

Просмотреть файл

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ProgramSynthesis;
using Microsoft.ProgramSynthesis.Extraction.Text.Semantics;
using Microsoft.ProgramSynthesis.Learning;
using Microsoft.ProgramSynthesis.Rules;
using Microsoft.ProgramSynthesis.Specifications;
using static Microsoft.ProgramSynthesis.Extraction.Text.Semantics.Semantics;
namespace ProseSample.TextExtraction
{
public static class WitnessFunctions
{
[WitnessFunction("LinesMap", 1)]
public static PrefixSpec WitnessLinesMap(GrammarRule rule, int parameter,
PrefixSpec spec)
{
var linesExamples = new Dictionary<State, IEnumerable<object>>();
foreach (State input in spec.ProvidedInputs)
{
var document = ((StringRegion) input[rule.Grammar.InputSymbol]);
var selectionPrefix = spec.Examples[input].Cast<StringRegion>();
var linesContainingSelection = new List<StringRegion>();
foreach (StringRegion example in selectionPrefix)
{
var startLine = GetLine(document, example.Start);
var endLine = GetLine(document, example.End);
if (startLine == null || endLine == null || startLine != endLine)
return null;
linesContainingSelection.Add(startLine);
}
linesExamples[input] = linesContainingSelection;
}
return new PrefixSpec(linesExamples);
}
}
}

Просмотреть файл

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Antlr4.Runtime" version="4.3.0" targetFramework="net452" />
<package id="CsQuery" version="1.3.5-beta5" targetFramework="net452" />
<package id="Ix-Main" version="1.2.5" targetFramework="net452" />
<package id="Microsoft.Experimental.Collections" version="1.0.3-alpha" targetFramework="net452" />
<package id="Microsoft.ProgramSynthesis" version="0.1.1-preview-1" targetFramework="net452" />
<package id="System.Collections.Immutable" version="1.1.36" targetFramework="net452" />
<package id="System.Reflection.Metadata" version="1.0.21" targetFramework="net452" />
</packages>

Просмотреть файл

@ -0,0 +1,48 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProseSample", "ProseSample\ProseSample.csproj", "{C7FC4D3C-3CAE-42B9-AFF9-83CB07A7872F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProseSample.Substrings", "ProseSample.Substrings\ProseSample.Substrings.csproj", "{41E9A9B1-B013-4A87-9759-E3004FEF8A81}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProseSample.TextExtraction", "ProseSample.TextExtraction\ProseSample.TextExtraction.csproj", "{5AA217F9-DEAB-46BE-81BE-AFD00948AC50}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
RecompileAndRelease|Any CPU = RecompileAndRelease|Any CPU
Release|Any CPU = Release|Any CPU
ReleaseNoHumanReadableAsts|Any CPU = ReleaseNoHumanReadableAsts|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C7FC4D3C-3CAE-42B9-AFF9-83CB07A7872F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C7FC4D3C-3CAE-42B9-AFF9-83CB07A7872F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C7FC4D3C-3CAE-42B9-AFF9-83CB07A7872F}.RecompileAndRelease|Any CPU.ActiveCfg = Release|Any CPU
{C7FC4D3C-3CAE-42B9-AFF9-83CB07A7872F}.RecompileAndRelease|Any CPU.Build.0 = Release|Any CPU
{C7FC4D3C-3CAE-42B9-AFF9-83CB07A7872F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C7FC4D3C-3CAE-42B9-AFF9-83CB07A7872F}.Release|Any CPU.Build.0 = Release|Any CPU
{C7FC4D3C-3CAE-42B9-AFF9-83CB07A7872F}.ReleaseNoHumanReadableAsts|Any CPU.ActiveCfg = Release|Any CPU
{C7FC4D3C-3CAE-42B9-AFF9-83CB07A7872F}.ReleaseNoHumanReadableAsts|Any CPU.Build.0 = Release|Any CPU
{41E9A9B1-B013-4A87-9759-E3004FEF8A81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{41E9A9B1-B013-4A87-9759-E3004FEF8A81}.Debug|Any CPU.Build.0 = Debug|Any CPU
{41E9A9B1-B013-4A87-9759-E3004FEF8A81}.RecompileAndRelease|Any CPU.ActiveCfg = Release|Any CPU
{41E9A9B1-B013-4A87-9759-E3004FEF8A81}.RecompileAndRelease|Any CPU.Build.0 = Release|Any CPU
{41E9A9B1-B013-4A87-9759-E3004FEF8A81}.Release|Any CPU.ActiveCfg = Release|Any CPU
{41E9A9B1-B013-4A87-9759-E3004FEF8A81}.Release|Any CPU.Build.0 = Release|Any CPU
{41E9A9B1-B013-4A87-9759-E3004FEF8A81}.ReleaseNoHumanReadableAsts|Any CPU.ActiveCfg = Release|Any CPU
{41E9A9B1-B013-4A87-9759-E3004FEF8A81}.ReleaseNoHumanReadableAsts|Any CPU.Build.0 = Release|Any CPU
{5AA217F9-DEAB-46BE-81BE-AFD00948AC50}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5AA217F9-DEAB-46BE-81BE-AFD00948AC50}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5AA217F9-DEAB-46BE-81BE-AFD00948AC50}.RecompileAndRelease|Any CPU.ActiveCfg = Release|Any CPU
{5AA217F9-DEAB-46BE-81BE-AFD00948AC50}.RecompileAndRelease|Any CPU.Build.0 = Release|Any CPU
{5AA217F9-DEAB-46BE-81BE-AFD00948AC50}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5AA217F9-DEAB-46BE-81BE-AFD00948AC50}.Release|Any CPU.Build.0 = Release|Any CPU
{5AA217F9-DEAB-46BE-81BE-AFD00948AC50}.ReleaseNoHumanReadableAsts|Any CPU.ActiveCfg = Release|Any CPU
{5AA217F9-DEAB-46BE-81BE-AFD00948AC50}.ReleaseNoHumanReadableAsts|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

Просмотреть файл

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add name="consoleListener" type="System.Diagnostics.ConsoleTraceListener" />
</listeners>
</trace>
</system.diagnostics>
</configuration>

Просмотреть файл

@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.ProgramSynthesis;
using Microsoft.ProgramSynthesis.AST;
using Microsoft.ProgramSynthesis.Extraction.Text.Semantics;
using Microsoft.ProgramSynthesis.Specifications;
using Microsoft.ProgramSynthesis.Specifications.Extensions;
using Microsoft.ProgramSynthesis.Utils;
using static ProseSample.Utils;
namespace ProseSample
{
internal static class Program
{
private static void Main(string[] args)
{
LoadAndTestSubstrings();
LoadAndTestTextExtraction();
}
private static void LoadAndTestSubstrings()
{
var grammar = LoadGrammar("ProseSample.Substrings.grammar");
if (grammar == null) return;
ProgramNode p = grammar.ParseAST(@"SubStr(v, PosPair(AbsPos(v, -4), AbsPos(v, -1)))",
ASTSerializationFormat.HumanReadable);
StringRegion prose = StringRegion.Create("Microsoft Program Synthesis using Examples SDK");
State input = State.Create(grammar.InputSymbol, prose);
Console.WriteLine(p.Invoke(input));
StringRegion sdk = prose.Slice(prose.End - 3, prose.End);
Spec spec = ShouldConvert.Given(grammar).To(prose, sdk);
Learn(grammar, spec);
TestFlashFillBenchmark(grammar, "emails");
}
private static void TestFlashFillBenchmark(Grammar grammar, string benchmark, int exampleCount = 2)
{
string[] lines = File.ReadAllLines($"benchmarks/{benchmark}.tsv");
Tuple<string, string>[] data = lines.Select(l =>
{
var parts = l.Split(new[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
return Tuple.Create(parts[0], parts[1]);
}).ToArray();
var examples = data.Take(exampleCount)
.ToDictionary(t => State.Create(grammar.InputSymbol, StringRegion.Create(t.Item1)),
t => (object) StringRegion.Create(t.Item2));
var spec = new ExampleSpec(examples);
ProgramNode program = Learn(grammar, spec);
foreach (Tuple<string, string> row in data.Skip(exampleCount))
{
State input = State.Create(grammar.InputSymbol, StringRegion.Create(row.Item1));
var output = (StringRegion) program.Invoke(input);
WriteColored(ConsoleColor.DarkCyan, $"{row.Item1} => {output.Value}");
}
}
private static void LoadAndTestTextExtraction()
{
var grammar = LoadGrammar("ProseSample.TextExtraction.grammar", "ProseSample.Substrings.grammar");
if (grammar == null) return;
TestExtractionBenchmark(grammar, "countries");
}
private static void TestExtractionBenchmark(Grammar grammar, string benchmark)
{
StringRegion document;
List<StringRegion> examples = LoadBenchmark($"benchmarks/{benchmark}.txt", out document);
var input = State.Create(grammar.InputSymbol, document);
var spec = new PrefixSpec(input, examples);
ProgramNode program = Learn(grammar, spec);
string[] output = program.Invoke(input).ToEnumerable().Select(s => ((StringRegion) s).Value).ToArray();
WriteColored(ConsoleColor.DarkCyan, output.DumpCollection(openDelim: "", closeDelim: "", separator: "\n"));
}
}
}

Просмотреть файл

@ -0,0 +1,40 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ProseSample")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ProseSample")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("c7fc4d3c-3cae-42b9-aff9-83cb07a7872f")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

Просмотреть файл

@ -0,0 +1,126 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(SolutionDir)packages\Antlr4.4.3.0\build\Antlr4.props" Condition="Exists('$(SolutionDir)packages\Antlr4.4.3.0\build\Antlr4.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C7FC4D3C-3CAE-42B9-AFF9-83CB07A7872F}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ProseSample</RootNamespace>
<AssemblyName>ProseSample</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utils.cs" />
</ItemGroup>
<ItemGroup>
<None Include="..\benchmarks\emails.tsv">
<Link>benchmarks\emails.tsv</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Analyzer Include="$(SolutionDir)packages\Microsoft.CodeAnalysis.Analyzers.1.0.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.Analyzers.dll" />
<Analyzer Include="$(SolutionDir)packages\Microsoft.CodeAnalysis.Analyzers.1.0.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.CSharp.Analyzers.dll" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ProseSample.Substrings\ProseSample.Substrings.csproj">
<Project>{41e9a9b1-b013-4a87-9759-e3004fef8a81}</Project>
<Name>ProseSample.Substrings</Name>
</ProjectReference>
<ProjectReference Include="..\ProseSample.TextExtraction\ProseSample.TextExtraction.csproj">
<Project>{5aa217f9-deab-46be-81be-afd00948ac50}</Project>
<Name>ProseSample.TextExtraction</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="..\benchmarks\countries.txt">
<Link>benchmarks\countries.txt</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Reference Include="CsQuery, Version=1.3.5.124, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\CsQuery.1.3.5-beta5\lib\net40\CsQuery.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="dslc, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\dslc.exe</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProgramSynthesis, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\Microsoft.ProgramSynthesis.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProgramSynthesis.Extraction, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\Microsoft.ProgramSynthesis.Extraction.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProgramSynthesis.Extraction.Text, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\Microsoft.ProgramSynthesis.Extraction.Text.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProgramSynthesis.Extraction.Text.Learning, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\Microsoft.ProgramSynthesis.Extraction.Text.Learning.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProgramSynthesis.Extraction.Text.Semantics, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\Microsoft.ProgramSynthesis.Extraction.Text.Semantics.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProgramSynthesis.Learning, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\Microsoft.ProgramSynthesis.Learning.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProgramSynthesis.Utils, Version=0.1.1.2395, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ProgramSynthesis.0.1.1-preview-1\lib\net45\Microsoft.ProgramSynthesis.Utils.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)packages\Antlr4.4.3.0\build\Antlr4.props')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)packages\Antlr4.4.3.0\build\Antlr4.props'))" />
<Error Condition="!Exists('$(SolutionDir)packages\Antlr4.4.3.0\build\Antlr4.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)packages\Antlr4.4.3.0\build\Antlr4.targets'))" />
</Target>
<Import Project="$(SolutionDir)packages\Antlr4.4.3.0\build\Antlr4.targets" Condition="Exists('$(SolutionDir)packages\Antlr4.4.3.0\build\Antlr4.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

Просмотреть файл

@ -0,0 +1,104 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.ProgramSynthesis;
using Microsoft.ProgramSynthesis.AST;
using Microsoft.ProgramSynthesis.Compiler;
using Microsoft.ProgramSynthesis.Extraction.Text.Semantics;
using Microsoft.ProgramSynthesis.Learning;
using Microsoft.ProgramSynthesis.Learning.Logging;
using Microsoft.ProgramSynthesis.Specifications;
using Microsoft.ProgramSynthesis.VersionSpace;
namespace ProseSample
{
internal static class Utils
{
public static Grammar LoadGrammar(string grammarFile, params string[] prerequisiteGrammars)
{
foreach (string prerequisite in prerequisiteGrammars)
{
var buildResult = DSLCompiler.Compile(prerequisite, $"{prerequisite}.xml");
if (buildResult.HasErrors)
{
WriteColored(ConsoleColor.Magenta, buildResult.TraceDiagnostics);
return null;
}
}
var compilationResult = DSLCompiler.LoadGrammarFromFile(grammarFile);
if (compilationResult.HasErrors)
{
WriteColored(ConsoleColor.Magenta, compilationResult.TraceDiagnostics);
return null;
}
if (compilationResult.Diagnostics.Count > 0)
{
WriteColored(ConsoleColor.Yellow, compilationResult.TraceDiagnostics);
}
return compilationResult.Value;
}
public static ProgramNode Learn(Grammar grammar, Spec spec)
{
var engine = new SynthesisEngine(grammar, new SynthesisEngine.Config
{
UseThreads = false,
LogListener = new LogListener(),
});
ProgramSet consistentPrograms = engine.LearnGrammar(spec);
engine.Configuration.LogListener.SaveLogToXML("learning.log.xml");
//foreach (ProgramNode p in consistentPrograms.RealizedPrograms) {
// Console.WriteLine(p);
//}
ProgramNode bestProgram = consistentPrograms.TopK("Score").FirstOrDefault();
if (bestProgram == null)
{
WriteColored(ConsoleColor.Red, "No program :(");
return null;
}
var score = bestProgram["Score"];
WriteColored(ConsoleColor.Cyan, $"[score = {score:F3}] {bestProgram}");
return bestProgram;
}
#region Auxiliary methods
public static void WriteColored(ConsoleColor color, object obj)
=> WriteColored(color, () => Console.WriteLine(obj));
public static void WriteColored(ConsoleColor color, Action write)
{
ConsoleColor currentColor = Console.ForegroundColor;
Console.ForegroundColor = color;
write();
Console.ForegroundColor = currentColor;
}
private static readonly Regex ExampleRegex = new Regex(@"(?<=\{)[^}]+(?=\})", RegexOptions.Compiled);
public static List<StringRegion> LoadBenchmark(string filename, out StringRegion document)
{
string content = File.ReadAllText(filename);
Match[] examples = ExampleRegex.Matches(content).Cast<Match>().ToArray();
document = StringRegion.Create(content.Replace("}", "").Replace("{", ""));
var result = new List<StringRegion>();
for (int i = 0, shift = -1; i < examples.Length; i++, shift -= 2)
{
int start = shift + examples[i].Index;
int end = start + examples[i].Length;
result.Add(document.Slice((uint) start, (uint) end));
}
return result;
}
#endregion Auxiliary methods
}
}

Просмотреть файл

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Antlr4" version="4.3.0" targetFramework="net452" />
<package id="Antlr4.Runtime" version="4.3.0" targetFramework="net452" />
<package id="CsQuery" version="1.3.5-beta5" targetFramework="net452" />
<package id="Ix-Main" version="1.2.5" targetFramework="net452" />
<package id="Microsoft.CodeAnalysis" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.CodeAnalysis.Analyzers" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.CodeAnalysis.Common" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.CodeAnalysis.CSharp" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.CodeAnalysis.CSharp.Workspaces" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.CodeAnalysis.VisualBasic" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.CodeAnalysis.VisualBasic.Workspaces" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.CodeAnalysis.Workspaces.Common" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Composition" version="1.0.27" targetFramework="net452" />
<package id="Microsoft.Experimental.Collections" version="1.0.3-alpha" targetFramework="net452" />
<package id="Microsoft.ProgramSynthesis" version="0.1.1-preview-1" targetFramework="net452" />
<package id="System.Collections.Immutable" version="1.1.36" targetFramework="net452" />
<package id="System.Reflection.Metadata" version="1.0.21" targetFramework="net452" />
</packages>

Просмотреть файл

@ -0,0 +1,43 @@
{Albania} 23 20 1
{Argentina} 35 35 2
{Belgium} 33.99 33.99 8
Aruba 35 35 3
Australia 30 30 4
Austria 25 25 5
Bangladesh 30 30 6
Barbados 30 25 7
Bolivia 25 25 9
Botswana 25 25 10
Brazil 34 34 11
Bulgaria 15 15 12
Canada 36.1 36.1 13
Cayman Islands 0 0 14
Chile 17 17 15
China 33 33 16
Colombia 35 35 17
Costa Rica 30 30 18
Croatia 20.32 20.32 19
Cyprus 10 10 20
Czech Republic 26 24 21
Denmark 28 28 22
Dominican Republic 25 30 23
Ecuador 25/15 25/15 24
Estonia 0/24 0/23 25
Fiji 31 31 26
Finland 26 26 27
France 33.83 33.33 28
Germany 38.31 38.34 29
Greece 24/32 22/29 30
Honduras 30 30 31
Hong Kong 17.5 17.5 32
Hungary 16 16 33
Iceland 18 18 34
India 36.5925 33.66 35
Indonesia 30 30 36
Ireland 12.5 12.5 37
Israel 34 31 38
Italy 37.25 37.25 39
Japan 40.69 40.69 40
Jamaica 33.3 33.3 41
Kazakhstan 30 30 42
Korea, Republic of 27.5 27.5 43

Просмотреть файл

@ -0,0 +1,16 @@
Nancy.FreeHafer@fourthcoffee.com FreeHafer
Andrew.Cencici@northwindtraders.com Cencici
Jan.Kotas@litwareinc.com Kotas
Mariya.Sergienko@gradicdesigninstitute.com Sergienko
Steven.Thorpe@northwindtraders.com Thorpe
Michael.Neipper@northwindtraders.com Neipper
Robert.Zare@northwindtraders.com Zare
Laura.Giussani@adventure-works.com Giussani
Anne.Hill@northwindtraders.com Hill
Alexander.David@contoso.com David
Kim.Shane@northwindtraders.com Shane
Manish.Chopra@northwindtraders.com Chopra
Gerwald.Oberleitner@northwindtraders.com Oberleitner
Amr.Zaki@northwindtraders.com Zaki
Yvonne.McKay@northwindtraders.com McKay
Amanda.Pinto@northwindtraders.com Pinto
1 Nancy.FreeHafer@fourthcoffee.com FreeHafer
2 Andrew.Cencici@northwindtraders.com Cencici
3 Jan.Kotas@litwareinc.com Kotas
4 Mariya.Sergienko@gradicdesigninstitute.com Sergienko
5 Steven.Thorpe@northwindtraders.com Thorpe
6 Michael.Neipper@northwindtraders.com Neipper
7 Robert.Zare@northwindtraders.com Zare
8 Laura.Giussani@adventure-works.com Giussani
9 Anne.Hill@northwindtraders.com Hill
10 Alexander.David@contoso.com David
11 Kim.Shane@northwindtraders.com Shane
12 Manish.Chopra@northwindtraders.com Chopra
13 Gerwald.Oberleitner@northwindtraders.com Oberleitner
14 Amr.Zaki@northwindtraders.com Zaki
15 Yvonne.McKay@northwindtraders.com McKay
16 Amanda.Pinto@northwindtraders.com Pinto