-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFreqWord.cs
187 lines (150 loc) · 4.83 KB
/
FreqWord.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (C) 2012-2014 Christopher Brochtrup
//
// This file is part of cb's Japanese Text Analysis Tool.
//
// cb's Japanese Text Analysis Tool is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// cb's Japanese Text Analysis Tool is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with cb's Japanese Text Analysis Tool. If not, see <http://www.gnu.org/licenses/>.
//
//////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace JapaneseTextAnalysisTool
{
public class FreqWord : Freq
{
public enum WordFreqMethod
{
MECAB,
JPARSER,
}
private List<string> lastWordList = new List<string>();
/// <summary>
/// The list of words from the last parse.
/// </summary>
public List<string> LastWordList
{
get
{
return lastWordList;
}
}
/// <summary>
/// The method/tool used to extract words from the text.
/// </summary>
public WordFreqMethod ParseMethod { get; set; }
/// <summary>
/// If true, use kanji form of word even if it is normally written in kana
/// (as determined by presence of a (uk) tag in EDICT).
///
/// If false, use the kana form of word if it is normally written in kana.
///
/// For example:
/// If true, ともに will be converted to 共に.
/// If false, 共に will be converted to ともに.
/// </summary>
public bool Kanjify { get; set; }
public FreqWord(string outFilename)
: base(outFilename)
{
this.ParseMethod = WordFreqMethod.MECAB;
this.Kanjify = false;
}
public override void addFileText(string text)
{
this.lastWordList.Clear();
if (ParseMethod == WordFreqMethod.MECAB)
{
addWithMecab(text);
}
else
{
addWithJParser(text);
}
}
/// <summary>
/// Analyze the text with Mecab.
/// </summary>
private void addWithMecab(string text)
{
List<InfoMecab> mecabList = Mecab.parseFields(text, true);
foreach (InfoMecab mecab in mecabList)
{
string word = mecab.Root.Trim();
if ((word != "*")
&& (UtilsLang.containsIdeograph(word)
|| UtilsLang.containsHiragana(word)
|| UtilsLang.containsKatakana(word)))
{
addItemToFreqTable(word, mecab.Pos);
this.lastWordList.Add(word);
}
}
}
/// <summary>
/// Analyze the text with JParser.
/// </summary>
private void addWithJParser(string text)
{
// Split text into small chunks, otherwise JParser takes FOREVER!!! (minutes rather than seconds).
const int linesPerParse = 100;
string lineEnding = UtilsCommon.getLineEnding(text);
string[] lines = text.Split(new string[] { lineEnding }, StringSplitOptions.RemoveEmptyEntries);
string curText = "";
for (int i = 0; i < lines.Length; i++)
{
curText += lines[i];
// Parse text every linesPerParse lines or if the end is reached.
if ((i % linesPerParse) == (linesPerParse - 1)
|| (i == lines.Length - 1))
{
List<InfoJParser> jparseList = JParser.parseFields(curText);
foreach (InfoJParser info in jparseList)
{
string word = info.Deinflected.Trim();
// If the word is normally written with kana, use the reading instead (if desired)
if (!Kanjify
&& info.Definition.Contains("(uk)"))
{
if (info.Reading != "")
{
word = info.Reading;
}
else
{
word = info.Word;
}
}
string partOfSpeech = "";
Match match = Regex.Match(info.Definition, @"^\((?<POS>.*?)\)");
if (match.Success)
{
partOfSpeech = match.Groups["POS"].ToString().Trim();
}
if ((UtilsLang.containsIdeograph(word)
|| UtilsLang.containsHiragana(word)
|| UtilsLang.containsKatakana(word)))
{
addItemToFreqTable(word, partOfSpeech);
this.lastWordList.Add(word);
}
}
// Reset
curText = "";
}
}
}
}
}