about summary refs log tree commit diff stats
path: root/kgramstats.h
blob: a97d7bf50293dc71da2cd3a86aee994bbdbd19d4 (plain) (blame)
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
#include <string>
#include <map>
#include <list>
#include <vector>
#include "histogram.h"

#ifndef KGRAMSTATS_H
#define KGRAMSTATS_H

struct word {
  std::string canon;
  histogram<std::string> forms;
  histogram<std::string> terms;
  
  word(std::string canon) : canon(canon) {}
  
  bool operator<(const word& other) const
  {
    return canon < other.canon;
  }
};

extern word blank_word;

enum class suffixtype {
  none,
  terminating,
  comma
};

enum class parentype {
  paren,
  square_bracket,
  asterisk,
  quote
};

enum class doublestatus {
  opening,
  closing,
  both
};

struct delimiter {
  parentype type;
  doublestatus status;
  
  delimiter(parentype type, doublestatus status) : type(type), status(status) {}
  
  bool operator<(const delimiter& other) const
  {
    return std::tie(type, status) < std::tie(other.type, other.status);
  }
};

struct token {
  const word& w;
  std::map<delimiter, int> delimiters;
  suffixtype suffix;
  std::string raw;
    
  token(const word& w) : w(w), suffix(suffixtype::none) {}
  
  bool operator<(const token& other) const
  {
    return std::tie(w, delimiters, suffix) < std::tie(other.w, other.delimiters, other.suffix);
  }
};

enum class querytype {
  literal,
  sentence
};

struct query {
  querytype type;
  token tok;
  
  query(token tok) : tok(tok), type(querytype::literal) {}
  
  query(querytype type) : tok(blank_word), type(type) {}
  
  bool operator<(const query& other) const
  {
    if (type == other.type)
    {
      return tok < other.tok;
    } else {
      return type < other.type;
    }
  }
};

typedef std::list<query> kgram;

class kgramstats
{
public:
	kgramstats(std::string corpus, int maxK);
	std::string randomSentence(int n);
	
private:
	struct token_data
	{
		int all;
		int titlecase;
		int uppercase;
    token tok;
    
    token_data(token tok) : tok(tok), all(0), titlecase(0), uppercase(0) {}
	};
  
	int maxK;
	std::map<kgram, std::map<int, token_data> > stats;
  word hashtags {"#hashtag"};
  std::map<std::string, word> words;
};

void printKgram(kgram k);

#endif