Blame view

morfeusz/InflexionGraph.hpp 2.28 KB
Michał Lenart authored
1
2
3
4
5
6
7
8
9
10
11
/* 
 * File:   FlexionGraph.hpp
 * Author: mlenart
 *
 * Created on 18 listopad 2013, 15:03
 */

#ifndef FLEXIONGRAPH_HPP
#define	FLEXIONGRAPH_HPP

#include <vector>
Michał Lenart authored
12
13
#include <set>
#include <utility>
Michał Lenart authored
14
15
#include "InterpretedChunk.hpp"
Michał Lenart authored
16
17
namespace morfeusz {
Michał Lenart authored
18
19
20
21
/**
 * This class build inflection graph (indexes the nodes, takes into account segments marked as "weak").
 * Takes care to make the number of nodes as little as possible.
 */
Michał Lenart authored
22
class InflexionGraph {
Michał Lenart authored
23
public:
Michał Lenart authored
24
Michał Lenart authored
25
    InflexionGraph(): graph(), node2ChunkStartPtr(), onlyWeakPaths(true) {
Michał Lenart authored
26
27

    }
Michał Lenart authored
28
29
30

    struct Edge {
        InterpretedChunk chunk;
Michał Lenart authored
31
        unsigned int nextNode;
Michał Lenart authored
32
    };
Michał Lenart authored
33
34
35
36
37
38
39

    /**
     * Adds new path to the graph.
     * 
     * @param path
     * @param weak
     */
Michał Lenart authored
40
    void addPath(const std::vector<InterpretedChunk>& path, bool weak);
Michał Lenart authored
41
42
43

    //    void getResults(const Tagset& tagset, const CharsetConverter& charsetConverter, std::vector<MorphInterpretation>& results);
Michał Lenart authored
44
45
46
47
48
    /**
     * Return current graph.
     * 
     * @return 
     */
Michał Lenart authored
49
    const std::vector< std::vector<InflexionGraph::Edge> >& getTheGraph();
Michał Lenart authored
50
Michał Lenart authored
51
52
53
54
55
    /**
     * True iff the graph is empty.
     * 
     * @return 
     */
Michał Lenart authored
56
    bool empty() const;
Michał Lenart authored
57
Michał Lenart authored
58
59
60
    /**
     * Clears the graph.
     */
Michał Lenart authored
61
    void clear();
Michał Lenart authored
62
Michał Lenart authored
63
private:
Michał Lenart authored
64
65
66
67

    typedef std::pair<const char*, int> PathElement;
    typedef std::set<PathElement> Path;
Michał Lenart authored
68
69
70
71
72
    /**
     * Adds an edge that starts a chunk.
     * 
     * @param e
     */
Michał Lenart authored
73
    void addStartEdge(const Edge& e);
Michał Lenart authored
74
75
76
77
78
79

    /**
     * Adds non-starting edge.
     * @param startNode
     * @param e
     */
Michał Lenart authored
80
    void addMiddleEdge(unsigned int startNode, const Edge& e);
Michał Lenart authored
81
Michał Lenart authored
82
83
84
    /**
     * Minimizes the graph so it contains as little number of nodes as possible.
     */
Michał Lenart authored
85
    void minimizeGraph();
Michał Lenart authored
86
Michał Lenart authored
87
88
89
90
91
92
93
94
95
96
97
    bool canMergeNodes(unsigned int node1, unsigned int node2);

    void doMergeNodes(unsigned int node1, unsigned int node2);

    bool tryToMergeTwoNodes();

    std::set<Path> getPossiblePaths(unsigned int node);

    void redirectEdges(unsigned int fromNode, unsigned int toNode);

    void doRemoveNode(unsigned int node);
Michał Lenart authored
98
Michał Lenart authored
99
    void repairLastNodeNumbers();
Michał Lenart authored
100
101

    void repairOrthShifts();
Michał Lenart authored
102
Michał Lenart authored
103
    std::vector< std::vector<Edge> > graph;
Michał Lenart authored
104
    std::vector< const char* > node2ChunkStartPtr;
Michał Lenart authored
105
    bool onlyWeakPaths;
Michał Lenart authored
106
107
};
Michał Lenart authored
108
109
}
Michał Lenart authored
110
111
#endif	/* FLEXIONGRAPH_HPP */