Skip to content

Commit

Permalink
Add KiwiGui
Browse files Browse the repository at this point in the history
  • Loading branch information
bab2min committed Oct 30, 2017
1 parent 3b19b21 commit 82a9745
Show file tree
Hide file tree
Showing 9 changed files with 317 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,6 @@ ModelManifest.xml
/TestSets/11s.txt
/TestSets/10s.txt
/TestSets/t.txt
/KiwiGui/KiwiGui.exe
/KiwiGui/bin_x86
/KiwiGui/bin_x64
55 changes: 55 additions & 0 deletions KiwiGui/BatchDlg.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="KiwiGui.BatchDlg"
xmlns:local="clr-namespace:KiwiGui"
Title="일괄 처리 대화상자"
Height="360"
Width="500"
MinHeight="250"
MinWidth="350"
ResizeMode="CanResizeWithGrip"
ShowInTaskbar="False"
WindowStartupLocation="CenterOwner" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="170"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="25"/>
</Grid.RowDefinitions>
<ListBox Name="FileList" SelectionChanged="FileList_SelectionChanged" SelectionMode="Multiple">

</ListBox>
<StackPanel Grid.Column="1">
<Button Name="AddBtn" Content="파일 추가..." Padding="3" Click="AddBtn_Click" />
<Button Name="AddFolderBtn" Content="폴더 추가..." Padding="3" Click="AddFolderBtn_Click" />
<Button Name="DelBtn" Content="삭제" Padding="3" Click="DelBtn_Click" IsEnabled="False" />
<ComboBox SelectedIndex="0" Name="TypeCmb" ToolTip="분석 단위를 선택합니다.">
<ComboBoxItem>줄별 분석</ComboBoxItem>
<ComboBoxItem>전체 분석</ComboBoxItem>
</ComboBox>
<ComboBox SelectedIndex="0" Name="TopNCmb" ToolTip="분석 결과를 몇 개까지 출력할 지 선택합니다.">
<ComboBoxItem>결과 1개</ComboBoxItem>
<ComboBoxItem>결과 2개</ComboBoxItem>
<ComboBoxItem>결과 3개</ComboBoxItem>
<ComboBoxItem>결과 4개</ComboBoxItem>
<ComboBoxItem>결과 5개</ComboBoxItem>
</ComboBox>
<ComboBox SelectedIndex="1" Name="FormatCmb" ToolTip="분석 결과를 어떤 형태로 저장할지 선택합니다.">
<ComboBoxItem>형태소 (탭 구분자)</ComboBoxItem>
<ComboBoxItem>형태소/NNG (탭 구분자)</ComboBoxItem>
<ComboBoxItem>형태소 ( + 구분자)</ComboBoxItem>
<ComboBoxItem>형태소/NNG ( + 구분자)</ComboBoxItem>
</ComboBox>
<Button Name="StartBtn" Content="분석 시작" Padding="3" Click="StartBtn_Click" IsEnabled="False"/>
<Button Name="StopBtn" Content="중단" Padding="3" Click="StopBtn_Click" Visibility="Collapsed"/>
<TextBlock TextWrapping="Wrap">분석 결과는 <LineBreak/>"파일이름.tagged"에 저장됩니다.</TextBlock>
</StackPanel>
<ProgressBar Grid.ColumnSpan="2" Grid.Row="1" Name="Prg"></ProgressBar>
</Grid >

</Window>
180 changes: 180 additions & 0 deletions KiwiGui/BatchDlg.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
using System.Windows;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using System.IO;

namespace KiwiGui
{
/// <summary>
/// Interaction logic for BatchDlg.xaml
/// </summary>
public partial class BatchDlg : Window
{
public KiwiCS instKiwi;
private BackgroundWorker worker = new BackgroundWorker();

protected struct WorkerArgs
{
public List<string> fileList;
public bool byline;
public int topN;
public bool formatTag;
public string formatSep;
}

public BatchDlg()
{
InitializeComponent();
worker.DoWork += Worker_DoWork;
worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
worker.ProgressChanged += Worker_ProgressChanged;
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
}

private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Prg.Value = e.ProgressPercentage;
}

private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// First, handle the case where an exception was thrown.
if (e.Error != null)
{
System.Windows.MessageBox.Show(e.Error.Message);
}
else if (e.Cancelled)
{

}
else
{
System.Windows.MessageBox.Show("일괄 처리 완료");
}
StopBtn.Visibility = Visibility.Collapsed;
StartBtn.Visibility = Visibility.Visible;
Prg.Value = 0;
}

private void analyzeWriteResult(string input, WorkerArgs args, string outputPath)
{
string[] lines = args.byline ? input.Trim().Split('\n') : new string[] { input.Trim() };
using (StreamWriter output = new StreamWriter(outputPath))
{
foreach (string line in lines)
{
if (line.Length == 0) continue;
var res = instKiwi.analyze(line.Trim(), args.topN);
string ret = "";
foreach(var r in res)
{
foreach(var m in r.morphs)
{
if (ret.Length > 0) ret += args.formatSep;
ret += m.Item1;
if (args.formatTag) ret += "/" + m.Item2;
}
}
output.WriteLine(line.Trim());
output.WriteLine(ret);
}
}
}

private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
WorkerArgs args = (WorkerArgs)e.Argument;
worker.ReportProgress(1);
int n = 0;
foreach(string path in args.fileList)
{
n++;
if(worker.CancellationPending)
{
e.Cancel = true;
return;
}
analyzeWriteResult(MainWindow.GetFileText(path), args, path + ".tagged");
worker.ReportProgress((int)(n * 100.0 / args.fileList.Count));
}
}

private void AddBtn_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "텍스트 파일|*.txt|모든 파일|*.*";
ofd.Title = "분석할 텍스트 파일 열기";
ofd.Multiselect = true;
if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
foreach(string name in ofd.FileNames)
{
FileList.Items.Add(name);
}
StartBtn.IsEnabled = true;
}

private void AddFolderBtn_Click(object sender, RoutedEventArgs e)
{
FolderBrowserDialog fd = new FolderBrowserDialog();
if (fd.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
FileList.Items.Add(fd.SelectedPath);
}

private void DelBtn_Click(object sender, RoutedEventArgs e)
{
List<string> sel = new List<string>();
foreach(string i in FileList.SelectedItems)
{
sel.Add(i);
}

for (int n = FileList.Items.Count - 1; n >= 0; --n)
{
string v = FileList.Items[n].ToString();
if (sel.IndexOf(v) >= 0)
{
FileList.Items.RemoveAt(n);
}
}
StartBtn.IsEnabled = FileList.Items.Count > 0;
}

private void StartBtn_Click(object sender, RoutedEventArgs e)
{
StopBtn.Visibility = Visibility.Visible;
StartBtn.Visibility = Visibility.Collapsed;
WorkerArgs args;
args.fileList = new List<string>();
foreach (string i in FileList.Items)
{
var attr = File.GetAttributes(i);
if(Directory.Exists(i))
{
foreach (string j in Directory.GetFiles(i)) args.fileList.Add(j);
}
else if(File.Exists(i)) args.fileList.Add(i);
}
args.byline = TypeCmb.SelectedIndex == 0;
args.topN = TopNCmb.SelectedIndex + 1;
args.formatTag = FormatCmb.SelectedIndex % 2 == 1;
args.formatSep = FormatCmb.SelectedIndex / 2 == 1 ? " + " : "\t";
worker.RunWorkerAsync(args);
}

private void StopBtn_Click(object sender, RoutedEventArgs e)
{
StopBtn.Visibility = Visibility.Collapsed;
StartBtn.Visibility = Visibility.Visible;
worker.CancelAsync();
Prg.Value = 0;
}

private void FileList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
DelBtn.IsEnabled = FileList.SelectedIndex >= 0;
}
}
}
Binary file removed KiwiGui/KiwiC.dll
Binary file not shown.
23 changes: 20 additions & 3 deletions KiwiGui/KiwiCS.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;

namespace KiwiGui
{
Expand Down Expand Up @@ -90,9 +89,27 @@ public void Dispose()
}
}

class KiwiCS
public class KiwiCS
{

private const string DLLPATH = "KiwiC.dll";
static KiwiCS()
{
var myPath = new Uri(typeof(KiwiCS).Assembly.CodeBase).LocalPath;
var myFolder = Path.GetDirectoryName(myPath);

var is64 = IntPtr.Size == 8;
var subfolder = is64 ? "\\bin_x64\\" : "\\bin_x86\\";
#if DEBUG
subfolder = "\\..\\.." + subfolder;
#endif

LoadLibrary(myFolder + subfolder + DLLPATH);
}

[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibrary(string dllToLoad);

[DllImport(DLLPATH, CallingConvention = CallingConvention.Cdecl)]
extern public static int kiwi_version();

Expand Down
14 changes: 14 additions & 0 deletions KiwiGui/KiwiGui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>kiwi_icon.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
Expand All @@ -55,6 +59,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Page Include="BatchDlg.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand All @@ -63,6 +71,9 @@
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="BatchDlg.xaml.cs">
<DependentUpon>BatchDlg.xaml</DependentUpon>
</Compile>
<Compile Include="KiwiCS.cs" />
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
Expand Down Expand Up @@ -95,5 +106,8 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Resource Include="kiwi_icon.ico" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
6 changes: 4 additions & 2 deletions KiwiGui/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:KiwiGui"
mc:Ignorable="d"
Title="키위 형태소 분석기 GUI" Height="350" Width="525" WindowStartupLocation="CenterScreen" Closed="Window_Closed">
Title="키위 형태소 분석기 GUI" Height="450" Width="625" WindowStartupLocation="CenterScreen" Closed="Window_Closed">
<DockPanel Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
<Menu DockPanel.Dock="Top">
<MenuItem Header="파일(_F)">
<MenuItem Header="열기(_O)" Click="MenuItem_Open" />
<MenuItem Header="일괄 분석(_B)" Click="MenuItem_Batch" />
<Separator />
<MenuItem Header="결과 저장(_V)" Click="MenuItem_Save" Name="MenuSave" IsEnabled="False" />
<Separator />
<MenuItem Header="종료(_X)" Click="MenuItem_Close"/>
</MenuItem>
<MenuItem Header="정보(_H)">
<MenuItem Header="홈페이지(_H)" Click="MenuItem_Homepage"/>
<MenuItem Header="GitHub(_G)" Click="MenuItem_GitHub"/>
<MenuItem Header="블로그(_B)" Click="MenuItem_Blog"/>
<Separator />
<MenuItem Name="VersionInfo" IsEnabled="False" />
<MenuItem Name="VersionInfo" />
</MenuItem>
</Menu>
<Grid Margin="3,0">
Expand Down
Loading

0 comments on commit 82a9745

Please sign in to comment.