拡張cabocha tagに#DOCATTRを追加(DOCIDとATTRの中間)
@@ -113,6 +113,9 @@ | ||
113 | 113 | <!-- 標準Reader定義"CONLL" --> |
114 | 114 | <ReaderDef Name="CONLL" LineFormat="CONLL"/> |
115 | 115 | |
116 | + <!-- 標準Reader定義"CONLLU" --> | |
117 | + <ReaderDef Name="CONLLU" LineFormat="CONLLU"/> | |
118 | + | |
116 | 119 | <!-- 標準Reader定義"PlainText" --> |
117 | 120 | <ReaderDef Name="PlainText" LineFormat="TextLine"/> |
118 | 121 |
@@ -289,6 +289,31 @@ | ||
289 | 289 | } |
290 | 290 | m_Corpus.DocumentSet.AddDocument(newdoc); |
291 | 291 | } |
292 | + // 2021.1.5 add: DOCIDと似ているが、IDの定義の役割がなく、直前のDocumentに書誌情報(Attribute)を付与するだけのタグ | |
293 | + else if (s.StartsWith("#! DOCATTR")) | |
294 | + { | |
295 | + if (doc == null) | |
296 | + { | |
297 | + Console.WriteLine($"No current doc found: {s}"); | |
298 | + } | |
299 | + try | |
300 | + { | |
301 | + string[] fields = s.Split(new char[] { '\t' }); | |
302 | + if (fields.Length != 2) | |
303 | + { | |
304 | + throw new Exception(); | |
305 | + } | |
306 | + AddDocumentAttribute(doc, fields[1]); | |
307 | + } | |
308 | + catch (Exception ex) | |
309 | + { | |
310 | + if (s.Length > 25) | |
311 | + { | |
312 | + s = s.Substring(0, 25); | |
313 | + } | |
314 | + Console.WriteLine("Ignoring an invalid extdata line: {0}...", s); | |
315 | + } | |
316 | + } | |
292 | 317 | else if (s.StartsWith("#! DOC")) |
293 | 318 | { |
294 | 319 | // Cabocha拡張タグ - Document開始 |
@@ -297,10 +322,21 @@ | ||
297 | 322 | string[] fields = s.Split(new char[] { ' ' }); |
298 | 323 | if (fields.Length != 3) |
299 | 324 | { |
300 | - throw new Exception(); | |
325 | + fields = s.Split(new char[] { ' ', '\t' }); | |
326 | + if (fields.Length != 3) | |
327 | + { | |
328 | + throw new Exception(); | |
329 | + } | |
301 | 330 | } |
302 | 331 | int docid = Int32.Parse(fields[2]); |
303 | - docid = m_DocIdMapping[docid]; | |
332 | + // 存在しないDocumentなら新規作成してDocumentSetに追加する. | |
333 | + if (m_Corpus.DocumentSet.FindDocument(docid) == null) | |
334 | + { | |
335 | + var newdoc = new Document() { ID = docid }; | |
336 | + m_Corpus.DocumentSet.AddDocument(newdoc); | |
337 | + m_DocIdMapping.Add(docid, docid); | |
338 | + } | |
339 | + m_DocIdMapping.TryGetValue(docid, out docid); // 見つからなければdocidは不変 | |
304 | 340 | if (docid != doc.ID) |
305 | 341 | { |
306 | 342 | doc.Text = sb.ToString(); |
@@ -672,6 +708,48 @@ | ||
672 | 708 | } |
673 | 709 | } |
674 | 710 | |
711 | + /// <summary> | |
712 | + /// 拡張CabochaFormatに埋め込まれたDOCATTRタグからDocument Attributeを追加する. | |
713 | + /// </summary> | |
714 | + /// <param name="id"></param> | |
715 | + /// <param name="xmlstr"></param> | |
716 | + /// <returns></returns> | |
717 | + private void AddDocumentAttribute(Document targetdoc, string xmlstr) | |
718 | + { | |
719 | + string s; | |
720 | + int sp = xmlstr.IndexOf('<'); | |
721 | + if (sp >= 0) | |
722 | + { | |
723 | + if (sp > 0) | |
724 | + { | |
725 | + // XMLパートの前にファイルパスがある場合 | |
726 | + s = string.Format("<Root><FilePath>{0}</FilePath>{1}</Root>", xmlstr.Substring(0, sp), xmlstr.Substring(sp)); | |
727 | + } | |
728 | + else | |
729 | + { | |
730 | + // XMLパートのみの場合 | |
731 | + s = string.Format("<Root>{0}</Root>", xmlstr); | |
732 | + } | |
733 | + } | |
734 | + else | |
735 | + { | |
736 | + // ファイルパスのみの場合 | |
737 | + s = string.Format("<Root><FilePath>{0}</FilePath></Root>", xmlstr); | |
738 | + } | |
739 | + using (TextReader trdr = new StringReader(s)) | |
740 | + { | |
741 | + XmlReader xrdr = XmlReader.Create(trdr); | |
742 | + while (xrdr.Read()) | |
743 | + { | |
744 | + if (xrdr.Name.Equals("Root")) continue; | |
745 | + DocumentAttribute dt = new DocumentAttribute(); | |
746 | + dt.ID = DocumentAttribute.UniqueID++; | |
747 | + dt.Key = xrdr.Name; | |
748 | + dt.Value = xrdr.ReadString(); | |
749 | + targetdoc.Attributes.Add(dt); | |
750 | + } | |
751 | + } | |
752 | + } | |
675 | 753 | |
676 | 754 | /// <summary> |
677 | 755 | /// Lexiconデータをストリームから一時Lexiconに読み込む。 |