@@ -1688,10 +1688,13 @@ public SemanticGraphEdge addEdge(SemanticGraphEdge edge) {
1688
1688
* dumb, could be made more sophisticated.
1689
1689
* <br>
1690
1690
*
1691
- * Example: " [ate subj>Bill dobj>[muffins compound>blueberry]]"
1691
+ * Example: {@code [ate subj>Bill dobj>[muffins compound>blueberry]]}
1692
1692
* <br>
1693
1693
*
1694
1694
* This is the same format generated by toCompactString().
1695
+ * <br>
1696
+ * Indices are represented by a dash separated number after the word:
1697
+ * {@code [ate-1 subj>Bill-2 ...}
1695
1698
*/
1696
1699
public static SemanticGraph valueOf (String s , Language language , Integer sentIndex ) {
1697
1700
return (new SemanticGraphParsingTask (s , language , sentIndex )).parse ();
@@ -1841,7 +1844,7 @@ public SemanticGraph makeSoftCopy() {
1841
1844
1842
1845
// ============================================================================
1843
1846
1844
- private static final Pattern WORD_AND_INDEX_PATTERN = Pattern .compile ("([^-]+ )-([0-9]+)" );
1847
+ private static final Pattern WORD_AND_INDEX_PATTERN = Pattern .compile ("([^-]* )-([0-9]+)" );
1845
1848
1846
1849
/**
1847
1850
* This nested class is a helper for valueOf(). It represents the task of
@@ -1850,7 +1853,7 @@ public SemanticGraph makeSoftCopy() {
1850
1853
private static class SemanticGraphParsingTask extends StringParsingTask <SemanticGraph > {
1851
1854
1852
1855
private SemanticGraph sg ;
1853
- private Set <Integer > indexesUsed = Generics .newHashSet ();
1856
+ private Map <Integer , IndexedWord > indexesUsed = Generics .newHashMap ();
1854
1857
private final Language language ;
1855
1858
private final Integer sentIndex ;
1856
1859
@@ -1922,21 +1925,19 @@ private IndexedWord makeVertex(String word) {
1922
1925
} else {
1923
1926
index = getNextFreeIndex ();
1924
1927
}
1925
- indexesUsed .add (index );
1926
- // Note that, despite the use of indexesUsed and getNextFreeIndex(),
1927
- // nothing is actually enforcing that no indexes are used twice. This
1928
- // could occur if some words in the string representation being parsed
1929
- // come with index markers and some do not.
1928
+ if (indexesUsed .containsKey (index )) {
1929
+ return indexesUsed .get (index );
1930
+ }
1930
1931
IndexedWord ifl = new IndexedWord (null , sentIndex != null ? sentIndex : 0 , index );
1931
1932
// log.info("SemanticGraphParsingTask>>> word = " + word);
1932
1933
// log.info("SemanticGraphParsingTask>>> index = " + index);
1933
- // log.info("SemanticGraphParsingTask>>> indexesUsed = " +
1934
- // indexesUsed);
1934
+ // log.info("SemanticGraphParsingTask>>> indexesUsed = " + indexesUsed);
1935
1935
String [] wordAndTag = word .split ("/" );
1936
1936
ifl .set (CoreAnnotations .TextAnnotation .class , wordAndTag [0 ]);
1937
1937
ifl .set (CoreAnnotations .ValueAnnotation .class , wordAndTag [0 ]);
1938
1938
if (wordAndTag .length > 1 )
1939
1939
ifl .set (CoreAnnotations .PartOfSpeechAnnotation .class , wordAndTag [1 ]);
1940
+ indexesUsed .put (index , ifl );
1940
1941
return ifl ;
1941
1942
}
1942
1943
@@ -1953,7 +1954,7 @@ private static Pair<String, Integer> readWordAndIndex(String word) {
1953
1954
1954
1955
private Integer getNextFreeIndex () {
1955
1956
int i = 0 ;
1956
- while (indexesUsed .contains (i ))
1957
+ while (indexesUsed .containsKey (i ))
1957
1958
i ++;
1958
1959
return i ;
1959
1960
}
0 commit comments