Skip to content

Commit

Permalink
Merge pull request #2 from pavel-belenko/feature/netflix-glyph-check
Browse files Browse the repository at this point in the history
Feature / Netflix glyph check
  • Loading branch information
pavel-belenko authored Dec 27, 2016
2 parents de4a82a + 7b87bcf commit f517315
Show file tree
Hide file tree
Showing 21 changed files with 720 additions and 33 deletions.
10 changes: 10 additions & 0 deletions LanguageMaster.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@ Note: Do check free disk space.</WaveFileMalformed>
<RemoveTextForHi>Remove text for hearing impaired</RemoveTextForHi>
<VisualSync>Visual sync</VisualSync>
<SpellCheck>Spell check</SpellCheck>
<NetflixQualityCheck>Netflix quality check</NetflixQualityCheck>
<Settings>Settings</Settings>
<Help>Help</Help>
<ShowHideWaveform>Show/hide waveform</ShowHideWaveform>
Expand Down Expand Up @@ -1675,6 +1676,7 @@ can edit in same subtitle file (collaboration)</Information>
<Replace>Replace</Replace>
<VisualSync>Visual sync</VisualSync>
<SpellCheck>Spell check</SpellCheck>
<NetflixQualityCheck>Netflix quality check</NetflixQualityCheck>
<SettingsName>Settings</SettingsName>
<Help>Help</Help>
<ShowFrameRate>Show frame rate in toolbar</ShowFrameRate>
Expand Down Expand Up @@ -1975,6 +1977,14 @@ can edit in same subtitle file (collaboration)</Information>
<SpellCheckAborted>Spell check aborted</SpellCheckAborted>
<UndoX>Undo: {0}</UndoX>
</SpellCheck>
<NetflixQualityCheck>
<GlyphCheckSuccessfull>Character validation successful.</GlyphCheckSuccessfull>
<GlyphCheckFailed>Character validation failed. Refer to the Netflix Glyph List for valid characters. Please see the full report here: {0}.</GlyphCheckFailed>
<GlyphCheckReport>Invalid character {0} found at column {1}.</GlyphCheckReport>
<WhiteSpaceCheckSuccessfull>White space validation successful.</WhiteSpaceCheckSuccessfull>
<WhiteSpaceCheckFailed>White space validation failed. Please see the full report here: {0}.</WhiteSpaceCheckFailed>
<WhiteSpaceCheckReport>Invalid white space found at column {0}.</WhiteSpaceCheckReport>
</NetflixQualityCheck>
<Split>
<Title>Split</Title>
<SplitOptions>Split options</SplitOptions>
Expand Down
14 changes: 14 additions & 0 deletions libse/Language.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public class Language
public LanguageStructure.ShowEarlierLater ShowEarlierLater;
public LanguageStructure.ShowHistory ShowHistory;
public LanguageStructure.SpellCheck SpellCheck;
public LanguageStructure.NetflixQualityCheck NetflixQualityCheck;
public LanguageStructure.Split Split;
public LanguageStructure.SplitLongLines SplitLongLines;
public LanguageStructure.SplitSubtitle SplitSubtitle;
Expand Down Expand Up @@ -1549,6 +1550,7 @@ public Language()
RemoveTextForHi = "Remove text for hearing impaired",
VisualSync = "Visual sync",
SpellCheck = "Spell check",
NetflixQualityCheck = "Netflix quality check",
Settings = "Settings",
Help = "Help",
ShowHideWaveform = "Show/hide waveform",
Expand Down Expand Up @@ -1959,6 +1961,7 @@ can edit in same subtitle file (collaboration)",
Replace = "Replace",
VisualSync = "Visual sync",
SpellCheck = "Spell check",
NetflixQualityCheck = "Netflix quality check",
SettingsName = "Settings",
Help = "Help",
ShowFrameRate = "Show frame rate in toolbar",
Expand Down Expand Up @@ -2268,6 +2271,17 @@ can edit in same subtitle file (collaboration)",
UndoX = "Undo: {0}",
};

NetflixQualityCheck = new LanguageStructure.NetflixQualityCheck
{
GlyphCheckSuccessfull = "Character validation successful.",
GlyphCheckFailed = "Character validation failed. Refer to the Netflix Glyph List for valid characters. Please see the full report here: {0}.",
GlyphCheckReport = "Invalid character {0} found at column {1}.",

WhiteSpaceCheckSuccessfull = "White space validation successful.",
WhiteSpaceCheckFailed = "White space validation failed. Please see the full report here: {0}.",
WhiteSpaceCheckReport = "Invalid white space found at column {0}."
};

Split = new LanguageStructure.Split
{
Title = "Split",
Expand Down
27 changes: 27 additions & 0 deletions libse/LanguageDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3346,6 +3346,9 @@ private static void SetValue(Language language, XmlReader reader, string name)
case "Main/Menu/Tools/SortBy":
language.Main.Menu.Tools.SortBy = reader.Value;
break;
case "Main/Menu/Tools/NetflixQualityCheck":
language.Main.Menu.Tools.NetflixQualityCheck = reader.Value;
break;
case "Main/Menu/Tools/Number":
language.Main.Menu.Tools.Number = reader.Value;
break;
Expand Down Expand Up @@ -3565,6 +3568,9 @@ private static void SetValue(Language language, XmlReader reader, string name)
case "Main/Menu/ToolBar/SpellCheck":
language.Main.Menu.ToolBar.SpellCheck = reader.Value;
break;
case "Main/Menu/ToolBar/NetflixQualityCheck":
language.Main.Menu.ToolBar.NetflixQualityCheck = reader.Value;
break;
case "Main/Menu/ToolBar/Settings":
language.Main.Menu.ToolBar.Settings = reader.Value;
break;
Expand Down Expand Up @@ -4474,6 +4480,9 @@ private static void SetValue(Language language, XmlReader reader, string name)
case "Settings/SpellCheck":
language.Settings.SpellCheck = reader.Value;
break;
case "Settings/NetflixQualityCheck":
language.Settings.NetflixQualityCheck = reader.Value;
break;
case "Settings/SettingsName":
language.Settings.SettingsName = reader.Value;
break;
Expand Down Expand Up @@ -5341,6 +5350,24 @@ private static void SetValue(Language language, XmlReader reader, string name)
case "SpellCheck/UndoX":
language.SpellCheck.UndoX = reader.Value;
break;
case "NetflixQualityCheck/GlyphCheckSuccessfull":
language.NetflixQualityCheck.GlyphCheckSuccessfull = reader.Value;
break;
case "NetflixQualityCheck/GlyphCheckFailed":
language.NetflixQualityCheck.GlyphCheckFailed = reader.Value;
break;
case "NetflixQualityCheck/GlyphCheckReport":
language.NetflixQualityCheck.GlyphCheckReport = reader.Value;
break;
case "NetflixQualityCheck/WhiteSpaceCheckSuccessfull":
language.NetflixQualityCheck.WhiteSpaceCheckSuccessfull = reader.Value;
break;
case "NetflixQualityCheck/WhiteSpaceCheckFailed":
language.NetflixQualityCheck.WhiteSpaceCheckFailed = reader.Value;
break;
case "NetflixQualityCheck/WhiteSpaceCheckReport":
language.NetflixQualityCheck.WhiteSpaceCheckReport = reader.Value;
break;
case "Split/Title":
language.Split.Title = reader.Value;
break;
Expand Down
14 changes: 14 additions & 0 deletions libse/LanguageStructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,7 @@ public class ToolsMenu
public string SplitLongLines { get; set; }
public string MinimumDisplayTimeBetweenParagraphs { get; set; }
public string SortBy { get; set; }
public string NetflixQualityCheck { get; set; }
public string Number { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
Expand Down Expand Up @@ -1413,6 +1414,7 @@ public class ToolBarMenu
public string RemoveTextForHi { get; set; }
public string VisualSync { get; set; }
public string SpellCheck { get; set; }
public string NetflixQualityCheck { get; set; }
public string Settings { get; set; }
public string Help { get; set; }
public string ShowHideWaveform { get; set; }
Expand Down Expand Up @@ -1836,6 +1838,7 @@ public class Settings
public string Replace { get; set; }
public string VisualSync { get; set; }
public string SpellCheck { get; set; }
public string NetflixQualityCheck { get; set; }
public string SettingsName { get; set; }
public string Help { get; set; }
public string ShowFrameRate { get; set; }
Expand Down Expand Up @@ -2148,6 +2151,17 @@ public class SpellCheck
public string UndoX { get; set; }
}

public class NetflixQualityCheck
{
public string GlyphCheckSuccessfull { get; set; }
public string GlyphCheckFailed { get; set; }
public string GlyphCheckReport { get; set; }

public string WhiteSpaceCheckSuccessfull { get; set; }
public string WhiteSpaceCheckFailed { get; set; }
public string WhiteSpaceCheckReport { get; set; }
}

public class Split
{
public string Title { get; set; }
Expand Down
19 changes: 19 additions & 0 deletions libse/LibSE.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,19 @@
<Compile Include="ManagedBitmap.cs" />
<Compile Include="MurMurHash3.cs" />
<Compile Include="NativeMethods.cs" />
<Compile Include="NetflixQualityCheck\NetflixGlyphChecker.cs" />
<Compile Include="NetflixQualityCheck\INetflixQualityChecker.cs" />
<Compile Include="NetflixQualityCheck\NetflixQualityReportBuilder.cs" />
<Compile Include="NetflixQualityCheck\NetflixWhiteSpaceChecker.cs" />
<Compile Include="NikseBitmap.cs" />
<Compile Include="NoBreakAfterItem.cs" />
<Compile Include="Paragraph.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="RealFFT.cs" />
<Compile Include="RegistryUtil.cs" />
<Compile Include="RichTextToPlainText.cs" />
Expand Down Expand Up @@ -502,6 +511,16 @@
<Compile Include="XSub.cs" />
<Compile Include="ZipExtractor.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Include="Resources\codes.dat" />
<None Include="Resources\NetflixAllowedGlyphs.bin" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="BeforeCompile">
<Exec Command="%22$(SolutionDir)..\build_helpers.bat%22 rev %22$(ConfigurationName)%22" />
Expand Down
12 changes: 12 additions & 0 deletions libse/NetflixQualityCheck/INetflixQualityChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nikse.SubtitleEdit.Core.NetflixQualityCheck
{
public interface INetflixQualityChecker
{
void Check(Subtitle subtitle, NetflixQualityReportBuilder report);
}
}
60 changes: 60 additions & 0 deletions libse/NetflixQualityCheck/NetflixGlyphChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Nikse.SubtitleEdit.Core.NetflixQualityCheck
{
public class NetflixGlyphChecker : INetflixQualityChecker
{
private static int[] LoadNetflixGlyphs()
{
int[] glyphs;

using (MemoryStream ms = new MemoryStream(Properties.Resources.NetflixAllowedGlyphs))
{
using (BinaryReader br = new BinaryReader(ms))
{
const int codepointSize = 4;
long n = ms.Length / codepointSize;
glyphs = new int[n];

for (int i = 0; i < n; i++)
{
glyphs[i] = br.ReadInt32();
}
}
}

return glyphs;
}

public void Check(Subtitle subtitle, NetflixQualityReportBuilder report)
{
// Load allowed glyphs
int[] allowedGlyphsArr = LoadNetflixGlyphs();
HashSet<int> allowedGlyphsSet = new HashSet<int>(allowedGlyphsArr);

foreach (Paragraph paragraph in subtitle.Paragraphs)
{
for (int pos = 0, actualPos = 0; pos < paragraph.Text.Length;
pos += char.IsSurrogatePair(paragraph.Text, pos) ? 2 : 1, actualPos++)
{
int curCodepoint = char.ConvertToUtf32(paragraph.Text, pos);

if (!allowedGlyphsSet.Contains(curCodepoint))
{
string timecode = paragraph.StartTime.ToHHMMSSFF();
string context = NetflixQualityReportBuilder.StringContext(paragraph.Text, pos, 6);
string comment = string.Format(Configuration.Settings.Language.NetflixQualityCheck.GlyphCheckReport,
string.Format("U+{0:X}", curCodepoint), actualPos);

report.AddRecord(timecode, context, comment);
}
}
}
}

}
}
75 changes: 75 additions & 0 deletions libse/NetflixQualityCheck/NetflixQualityReportBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nikse.SubtitleEdit.Core.NetflixQualityCheck
{
public class NetflixQualityReportBuilder
{
public class Record
{
public string Timecode { get; set; }
public string Context { get; set; }
public string Comment { get; set; }

public Record(string timecode, string context, string comment)
{
Timecode = timecode;
Context = context;
Comment = comment;
}

public string ToCSVRow()
{
string safeContext = Context;
safeContext = safeContext.Replace("\"", "\"\"");
safeContext = safeContext.Replace("\r\n", "\n");
safeContext = safeContext.Replace("\n", "\\n");
safeContext = string.Format("\"{0}\"", safeContext);

return string.Format("{0},{1},{2}", Timecode, safeContext, Comment);
}
}

public List<Record> Records { get; private set; }

public NetflixQualityReportBuilder()
{
Records = new List<Record>();
}

public void AddRecord(string timecod, string context, string comment)
{
Records.Add(new Record(timecod, context, comment));
}

public string ExportCSV()
{
StringBuilder csvBuilder = new StringBuilder();

// Header
csvBuilder.AppendLine("Timecode,Context,Comment");

// Rows
Records.ForEach(r => csvBuilder.AppendLine(r.ToCSVRow()));

return csvBuilder.ToString();
}

public bool IsEmpty {
get
{
return Records.Count == 0;
}
}

public static string StringContext(string str, int pos, int radius)
{
int beginPos = Math.Max(0, pos - radius);
int endPos = Math.Min(str.Length, pos + radius);
int length = endPos - beginPos;
return str.Substring(beginPos, length);
}
}
}
26 changes: 26 additions & 0 deletions libse/NetflixQualityCheck/NetflixWhiteSpaceChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Nikse.SubtitleEdit.Core.NetflixQualityCheck
{
public class NetflixWhiteSpaceChecker : INetflixQualityChecker
{
public void Check(Subtitle subtitle, NetflixQualityReportBuilder report)
{
foreach (Paragraph p in subtitle.Paragraphs)
{
foreach (Match m in Regex.Matches(p.Text, "([ ]{2,}|(\n|\r\n){2,})"))
{
string timecode = p.StartTime.ToHHMMSSFF();
string context = NetflixQualityReportBuilder.StringContext(p.Text, m.Index, 6);
string comment = string.Format(Configuration.Settings.Language.NetflixQualityCheck.WhiteSpaceCheckReport, m.Index);

report.AddRecord(timecode, context, comment);
}
}
}
}
}
Loading

0 comments on commit f517315

Please sign in to comment.